这个功能是先新增一个节区, 再把导出表移到新的节区
[C++] 纯文本查看 复制代码 PIMAGE_SECTION_HEADER pNewSectionHeader = pSectionHeader + pFileHeader->NumberOfSections - 1;
PDWORD pNewAddressOfFunctions = (PDWORD)((DWORD)pDosHeader + pNewSectionHeader->PointerToRawData);
PDWORD pNewAddressOfNames = pNewAddressOfFunctions + pExportDirectory->NumberOfFunctions;
PWORD pNewAddressOfNameOrdinals = (PWORD)(pNewAddressOfNames + pExportDirectory->NumberOfNames);
LPSTR pNewAddressOfDllName = (LPSTR)(pNewAddressOfNameOrdinals + pExportDirectory->NumberOfNames);
LPSTR pNewAddressOfNameString = NULL;
PIMAGE_EXPORT_DIRECTORY pNewExport = NULL;
PDWORD pOldAddressOfFunctions = (PDWORD)((DWORD)pDosHeader + RVA2FOA(pExportDirectory->AddressOfFunctions));
PDWORD pOldAddressOfNames = (PDWORD)((DWORD)pDosHeader + RVA2FOA(pExportDirectory->AddressOfNames));
PWORD pOldAddressOfNameOrdinals = (PWORD)((DWORD)pDosHeader + RVA2FOA(pExportDirectory->AddressOfNameOrdinals));
DWORD StrLen = 0;
memcpy(pNewAddressOfFunctions, pOldAddressOfFunctions, pExportDirectory->NumberOfFunctions * 4);
memcpy(pNewAddressOfNames, pOldAddressOfNames, pExportDirectory->NumberOfNames * 4);
memcpy(pNewAddressOfNameOrdinals, pOldAddressOfNameOrdinals, pExportDirectory->NumberOfNames * 2);
strcpy(pNewAddressOfDllName, "Dll2.dll");
pNewAddressOfNameString = (LPSTR)((DWORD)pNewAddressOfDllName + strlen(pNewAddressOfDllName) + 1);
for (DWORD i = 1; i <= pExportDirectory->NumberOfNames; i++) {
strcpy((char*)((DWORD)pNewAddressOfNameString + StrLen),(char*)((DWORD)pDosHeader+RVA2FOA(*(pOldAddressOfNames+(i-1)))));
*(pNewAddressOfNames+i-1) = FOA2RVA((DWORD)pNewAddressOfNameString - (DWORD)pDosHeader + StrLen);
StrLen += strlen((char*)((DWORD)pDosHeader + RVA2FOA(*(pOldAddressOfNames + (i - 1))))) + 1;
}
pNewExport = (PIMAGE_EXPORT_DIRECTORY)((DWORD)pNewAddressOfNameString + StrLen);
memcpy(pNewExport, pExportDirectory, sizeof(IMAGE_EXPORT_DIRECTORY));
pNewExport->AddressOfFunctions = FOA2RVA((DWORD)pNewAddressOfFunctions - (DWORD)pDosHeader);
pNewExport->AddressOfNames = FOA2RVA((DWORD)pNewAddressOfNames - (DWORD)pDosHeader);
pNewExport->AddressOfNameOrdinals = FOA2RVA((DWORD)pNewAddressOfNameOrdinals - (DWORD)pDosHeader);
pNewExport->Name = FOA2RVA((DWORD)pNewAddressOfDllName - (DWORD)pDosHeader);
pOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = FOA2RVA((DWORD)pNewExport - (DWORD)pDosHeader);
WriteFileBuffer2File(File,pFileBuffer,FileSize); |