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
| LPVOID lpSecMemory = new BYTE[0x1000]; if(lpSecMemory == NULL) { printf("申请内存失败\n"); return 0; }
PIMAGE_DATA_DIRECTORY pDir = (PIMAGE_DATA_DIRECTORY)(pNt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT); PIMAGE_EXPORT_DIRECTORY pExport = (PIMAGE_EXPORT_DIRECTORY)(RVA2FOA(pDir->VirtualAddress, lpData) + (DWORD)lpData); printf("NumberOfNames: %d\n", pExport->NumberOfNames);
LPVOID lpCopyDest = lpSecMemory; LPDWORD lpNewFuncAddr = (LPDWORD)lpCopyDest; LPVOID lpFuncAddr = (LPVOID)(RVA2FOA(pExport->AddressOfFunctions, lpData) + (DWORD)lpData); memcpy(lpCopyDest, lpFuncAddr, pExport->NumberOfFunctions * 4); printf("lpSecMemory => AddressOfFunctions: %x\n", *(int*)lpSecMemory); printf("pExport->AddressOfFuntions: %x\n", *(int*)(RVA2FOA(pExport->AddressOfFunctions, lpData) + (DWORD)lpData));
lpCopyDest = (LPVOID)((DWORD)lpSecMemory + pExport->AddressOfFunctions * 4); LPDWORD lpNewOrdAddr = (LPDWORD)lpCopyDest; LPVOID lpOrdAddr = (LPVOID)(RVA2FOA(pExport->AddressOfNameOrdinals, lpData) + (DWORD)lpData); memcpy(lpCopyDest, lpOrdAddr, pExport->NumberOfNames * 2); printf("lpSecMemory => AddressOfNameOrdinals: %x\n", *(char*)((DWORD)lpSecMemory + pExport->NumberOfFunctions * 4)); printf("pExport->AddressOfNameOrdinals: %x\n", *(char*)(RVA2FOA(pExport->AddressOfNameOrdinals, lpData) + (DWORD)lpData));
lpCopyDest = (LPVOID)((DWORD)lpSecMemory + pExport->NumberOfNames * 2); LPDWORD lpNewNameAddr = (LPDWORD)lpCopyDest; LPDWORD lpNameAddr = (LPDWORD)(RVA2FOA(pExport->AddressOfNames, lpData) + (DWORD)lpData); memcpy(lpCopyDest, 0, pExport->NumberOfNames * 4);
lpCopyDest = (LPVOID)((DWORD)lpSecMemory + pExport->NumberOfNames * 4); for(size_t i=0; i<pExoprt->NumberOfNames; i++) { LPSTR lpFuncName = (LPSTR)(RVA2FOA(lpNameAddr[i], lpData) + (DWORD)lpData); printf("lpFuncName: %s\n", lpFuncName); DWORD dwFuncLen = strlen(lpFuncName) + 1; memcpy(lpCopyDest, lpFuncName, dwFuncLen); *(lpNewFuncAddr + i) = FOA2RVA((dwFileSize + (DWORD)lpCopyDest - (DWORD)lpSecMemory), lpData); lpCopyDest = (LPVOID)((DWORD)lpCopyDest + dwFuncLen); } printf("lpSecMemory => AddressOfNames: %s\n", ((DWORD)lpSecMemory + pExport->NumberOfFunctions * 4 + pExport->NumberOfNames * 2 + pExport->NumberOfNames * 4)); printf("pExport->AddressName: %s\n", (RVA2FOA(lpNewFuncAddr[0], lpData) + (DWORD)lpData));
memcpy(lpCopyDest, pExport, pDir->Size); PIMAGE_EXPORT_DIRECTORY pNewExport = (PIMAGE_EXPORT_DIRECTORY)lpCopyDest; pNewExport->AddressOfFunctions = FOA2RVA((dwFileSize + (DWORD)lpNewFuncAddr - (DWORD)lpSecMemory), lpData); pNewExoprt->AddressOfNameOrdinals = FOA2RVA((dwFileSize + (DWORD)lpNewOrdAddr - (DWORD)lpSecMemory), lpData); pNewExport->AddressOfNames = FOA2RVA((dwFileSize + (DWORD)lpNewNameAddr - (DWORD)lpSecMemory), lpData);
pNt->OptionalHeader.DataDirectory[0].VirtualAddress = FOA2RVA((dwFileSize + (DWORD)pNewExport - (DWORD)lpSecMemory), lpData);
|