1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
PDWORD Move_Export_Table(LPVOID pFileBuffer) { PIMAGE_DOS_HEADER pDosHeader = NULL; PIMAGE_NT_HEADERS32 pNTHeader = NULL; PIMAGE_FILE_HEADER pFileHeader = NULL; PIMAGE_OPTIONAL_HEADER32 pOptionHeader = NULL; PIMAGE_SECTION_HEADER pSectionHeader = NULL; PIMAGE_DATA_DIRECTORY pDataDirectory = NULL; PIMAGE_EXPORT_DIRECTORY pExportDirectory = NULL; LPSTR New_File_Name = "C:/Move_Export_Table.dll";
pDosHeader = (PIMAGE_DOS_HEADER)pFileBuffer; pNTHeader = (PIMAGE_NT_HEADERS32)((DWORD)pFileBuffer + pDosHeader->e_lfanew); pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNTHeader + sizeof(DWORD)); pOptionHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader + IMAGE_SIZEOF_FILE_HEADER); pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionHeader + pFileHeader->SizeOfOptionalHeader); pDataDirectory = pOptionHeader->DataDirectory; cout << "DataDirectory[0].VirtualAddress:" << hex << pNTHeader->OptionalHeader.DataDirectory[0].VirtualAddress << endl; printf("文件偏移为:%x\n",RVAToFOA(pNTHeader->OptionalHeader.DataDirectory[0].VirtualAddress,pFileBuffer)); if(pDataDirectory[0].VirtualAddress == 0) { cout << "不存在导出表..." << endl; return ERROR; }
DWORD Add_Section_Length = 0x1000; LPVOID pTempFileBuffer = NULL; LPSTR Section_Name = ".In7ect"; AddNewSection(Add_Section_Length,&pTempFileBuffer,Section_Name);
PIMAGE_DOS_HEADER pDosHeader_Temp = NULL; PIMAGE_NT_HEADERS pNTHeader_Temp = NULL; PIMAGE_FILE_HEADER pPEHeader_Temp = NULL; PIMAGE_OPTIONAL_HEADER32 pOptionHeader_Temp = NULL; PIMAGE_SECTION_HEADER pSectionHeader_Temp = NULL; PIMAGE_DATA_DIRECTORY pDataDirectory_Temp = NULL; PIMAGE_EXPORT_DIRECTORY pExportDirectory_Temp = NULL; if (!pTempFileBuffer) { printf("(MoveExportTable)Can't open file!\n"); return ERROR; } if (*((PWORD)pTempFileBuffer) != IMAGE_DOS_SIGNATURE) { printf("(MoveExportTable)No MZ flag, not exe file!\n"); free(pTempFileBuffer); return ERROR; } pDosHeader_Temp = (PIMAGE_DOS_HEADER)pTempFileBuffer; if (*((PDWORD)((DWORD)pTempFileBuffer + pDosHeader->e_lfanew)) != IMAGE_NT_SIGNATURE) { printf("(MoveExportTable)Not a valid PE flag!\n"); free(pTempFileBuffer); return ERROR; }
pNTHeader_Temp = (PIMAGE_NT_HEADERS32)((DWORD)pTempFileBuffer + pDosHeader_Temp->e_lfanew); pPEHeader_Temp = (PIMAGE_FILE_HEADER)(((DWORD)pNTHeader_Temp) + sizeof(DWORD)); pOptionHeader_Temp = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pPEHeader_Temp + IMAGE_SIZEOF_FILE_HEADER); pSectionHeader_Temp = (PIMAGE_SECTION_HEADER)((DWORD)pOptionHeader_Temp + pPEHeader_Temp->SizeOfOptionalHeader); pDataDirectory_Temp = (PIMAGE_DATA_DIRECTORY)pOptionHeader_Temp->DataDirectory; pExportDirectory_Temp = (PIMAGE_EXPORT_DIRECTORY)(RVAToFOA(pDataDirectory_Temp[0].VirtualAddress, pTempFileBuffer) + (DWORD)pTempFileBuffer); cout << "DataDirectory[0].VirtualAddresscccccTTTT:" << hex << pExportDirectory_Temp->AddressOfFunctions << endl; DWORD *AddrFunctions = (DWORD *)((DWORD)pTempFileBuffer + RVAToFOA(pExportDirectory_Temp->AddressOfFunctions, pTempFileBuffer)); DWORD *AddrName = (DWORD *)((DWORD)pTempFileBuffer + RVAToFOA(pExportDirectory_Temp->AddressOfNames, pTempFileBuffer)); WORD *AddrOrdinals = (WORD *)((DWORD)pTempFileBuffer + RVAToFOA(pExportDirectory_Temp->AddressOfNameOrdinals, pTempFileBuffer));
DWORD FileSize_Source = ReadPESize(FILEPATH); pSectionHeader_Temp = pSectionHeader_Temp + pPEHeader_Temp->NumberOfSections - 1; PDWORD StartAddressofFunctions_Temp = (PDWORD)((DWORD)pTempFileBuffer+pSectionHeader_Temp->PointerToRawData); memcpy(StartAddressofFunctions_Temp, AddrFunctions, pExportDirectory_Temp->NumberOfFunctions * 4);
PWORD StartAddressofOrdianls_Temp = (PWORD)((DWORD)StartAddressofFunctions_Temp + pExportDirectory_Temp->NumberOfFunctions * 4); memcpy(StartAddressofOrdianls_Temp, AddrOrdinals, pExportDirectory_Temp->NumberOfNames * 2);
PDWORD StartAddressofNames_Temp = (PDWORD)((DWORD)StartAddressofOrdianls_Temp + pExportDirectory_Temp->NumberOfNames * 2); memcpy(StartAddressofNames_Temp, AddrName, pExportDirectory_Temp->NumberOfNames * 4);
PDWORD Start_Name_Address_Temp = (PDWORD)((DWORD)StartAddressofNames_Temp + pExportDirectory_Temp->NumberOfNames * 4); PDWORD NameAddress_Temp = Start_Name_Address_Temp; for (DWORD i = 0; i < pExportDirectory_Temp->NumberOfNames; i++) { DWORD NameofFOA = RVAToFOA(*(AddrName + i), pTempFileBuffer); memcpy( NameAddress_Temp, (PDWORD)((DWORD)pTempFileBuffer + NameofFOA), strlen((LPSTR)((DWORD)pTempFileBuffer + NameofFOA)) + 1); *(StartAddressofNames_Temp + i) = FOAToRVA((DWORD)NameAddress_Temp - (DWORD)pTempFileBuffer,pTempFileBuffer); printf("%x: %s\n", strlen((LPSTR)((DWORD)pTempFileBuffer + NameofFOA)), (PDWORD)((DWORD)pTempFileBuffer + NameofFOA)); NameAddress_Temp = (PDWORD)((DWORD)NameAddress_Temp + strlen((LPSTR)((DWORD)pTempFileBuffer + NameofFOA))+1); } Start_Name_Address_Temp = NameAddress_Temp;
pExportDirectory_Temp->AddressOfFunctions = FOAToRVA((DWORD)StartAddressofFunctions_Temp - (DWORD)pTempFileBuffer,pTempFileBuffer); pExportDirectory_Temp->AddressOfNames = FOAToRVA((DWORD)StartAddressofNames_Temp - (DWORD)pTempFileBuffer,pTempFileBuffer); pExportDirectory_Temp->AddressOfNameOrdinals = FOAToRVA((DWORD)StartAddressofOrdianls_Temp - (DWORD)pTempFileBuffer,pTempFileBuffer);
memcpy(Start_Name_Address_Temp, pExportDirectory_Temp, sizeof(IMAGE_EXPORT_DIRECTORY));
pDataDirectory_Temp[0].VirtualAddress = FOAToRVA((DWORD)Start_Name_Address_Temp - (DWORD)pTempFileBuffer,pTempFileBuffer);
NewFileBufferToFile(pTempFileBuffer, FileSize_Source + Add_Section_Length, New_File_Name); return NULL; }
|