添加新的节到PE文件中,然后在其中添加函数。
示例代码(使用win32 API):
#include
int main(int argc, char** argv) { char* filename = argv[1]; HANDLE hFile = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("Error: cannot open file.\n"); return 1; }
HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMapping == NULL)
{
printf("Error: cannot create file mapping.\n");
CloseHandle(hFile);
return 1;
}
LPVOID lpBase = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
if (lpBase == NULL)
{
printf("Error: cannot map file.\n");
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)lpBase;
if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
{
printf("Error: not a valid DOS image.\n");
UnmapViewOfFile(lpBase);
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)((DWORD)lpBase + pDosHeader->e_lfanew);
if (pNtHeaders->Signature != IMAGE_NT_SIGNATURE)
{
printf("Error: not a valid NT image.\n");
UnmapViewOfFile(lpBase);
CloseHandle(hMapping);
CloseHandle(hFile);
return 1;
}
DWORD dwSizeOfImage = pNtHeaders->OptionalHeader.SizeOfImage;
LPVOID lpBase2 = VirtualAlloc(NULL, dwSizeOfImage, MEM_COMMIT, PAGE_READWRITE);
if (lpBase2 == NULL)
{
printf