(编辑:jimmy 日期: 2024/12/28 浏览:2)
VOID ExpansionSection(){ PVOID pFileBuffer = NULL; PVOID pImageBuffer = NULL; PVOID pNewBuffer = NULL; PVOID pNewImageBuffer = NULL;​ PIMAGE_DOS_HEADER pDosHeader = NULL; PIMAGE_FILE_HEADER pPEHeader = NULL; PIMAGE_OPTIONAL_HEADER32 pOptionHeader = NULL; PIMAGE_SECTION_HEADER pSectionHeader = NULL; PIMAGE_SECTION_HEADER pSectionHeaderUP = NULL; PBYTE codeBegin = NULL; BOOL isOk = FALSE; DWORD size = 0;​ ReadPEFile(file_path, &pFileBuffer); if (!pFileBuffer) { printf("文件->缓冲区失败"); return; } /*FileBuffer->ImageBuffer*/ CopyFileBufferToImageBuffer(pFileBuffer, &pImageBuffer); if (!pImageBuffer) { printf(" 复制文件到缓冲区失败"); free(pFileBuffer); return; } pDosHeader = (PIMAGE_DOS_HEADER)pImageBuffer; pPEHeader = (PIMAGE_FILE_HEADER)((DWORD)((DWORD)pImageBuffer + pDosHeader->e_lfanew) + 4); pOptionHeader = (PIMAGE_OPTIONAL_HEADER32)(((DWORD)pImageBuffer + pDosHeader->e_lfanew) + 4 + IMAGE_SIZEOF_FILE_HEADER); pSectionHeader = (PIMAGE_SECTION_HEADER)(((DWORD)pImageBuffer + pDosHeader->e_lfanew) + 4 + IMAGE_SIZEOF_FILE_HEADER + pPEHeader->SizeOfOptionalHeader);​ pOptionHeader->SizeOfImage += 0x1000; pSectionHeader[pPEHeader->NumberOfSections - 1].Misc.VirtualSize += 0x1000; pSectionHeader[pPEHeader->NumberOfSections - 1].SizeOfRawData += 0x1000; size = CopyImageBufferToNewFileBuffer(pImageBuffer, &pNewBuffer);​ if (size == 0 || !pNewBuffer) { printf("存盘失败"); free(pFileBuffer); free(pImageBuffer); free(pNewBuffer); return; }​ isOk = MemoryToFile(pNewBuffer, size, write_file_path); printf("MemoryToFile"); if (isOk) { printf("存盘成功"); return; } free(pFileBuffer); free(pImageBuffer); free(pNewBuffer); return;}
VOID MergeSection(){ PIMAGE_DOS_HEADER pDos = NULL; PIMAGE_FILE_HEADER pFile = NULL; PIMAGE_NT_HEADERS32 pNt = NULL; PIMAGE_OPTIONAL_HEADER pOpt = NULL; PIMAGE_SECTION_HEADER pSec = NULL;​ PVOID pFileBuffer = NULL; PVOID pImageBuffer = NULL; PVOID pNewBuffer = NULL; BOOL isOk = FALSE; DWORD size = 0; ReadPEFile(file_path,&pFileBuffer); if (!pFileBuffer) { printf("读取文件失败"); return; } CopyFileBufferToImageBuffer(pFileBuffer, &pImageBuffer); if (!pImageBuffer) { printf("复制文件到imagebuffer失败"); free(pFileBuffer); return; } pDos = (PIMAGE_DOS_HEADER)pImageBuffer; pNt = (PIMAGE_NT_HEADERS32)((DWORD)pDos + pDos->e_lfanew); pFile = (PIMAGE_FILE_HEADER)((DWORD)pNt + 4); pOpt = (PIMAGE_OPTIONAL_HEADER)((DWORD)pFile + IMAGE_SIZEOF_FILE_HEADER); pSec = (PIMAGE_SECTION_HEADER)((DWORD)pOpt + pFile->SizeOfOptionalHeader);​ //修改节的属性 DWORD dwMax = pSec[pFile->NumberOfSections - 1].Misc.VirtualSize > pSec[pFile->NumberOfSections - 1].SizeOfRawData ? pSec[pFile->NumberOfSections - 1].Misc.VirtualSize : pSec[pFile->NumberOfSections - 1].SizeOfRawData; pSec->Misc.VirtualSize = pSec->SizeOfRawData = pSec[pFile->NumberOfSections - 1].VirtualAddress + dwMax - pOpt->SizeOfHeaders; for (size_t i = 1; i < pFile->NumberOfSections; i++) { pSec->Characteristics |= pSec[i].Characteristics; } memset(pSec + 1, 0, IMAGE_SIZEOF_SECTION_HEADER * (pFile->NumberOfSections - 1)); pFile->NumberOfSections = 1; //TO NewBuffer size = CopyImageBufferToNewFileBuffer(pImageBuffer, &pNewBuffer); if (size == 0 || !pNewBuffer) { printf("存盘失败"); free(pFileBuffer); free(pImageBuffer); free(pNewBuffer); return; } isOk = MemoryToFile(pNewBuffer, size, write_file_path); if (isOk) { printf("存盘成功"); return; } free(pFileBuffer); free(pImageBuffer); free(pNewBuffer); return;}