#include <stdio.h> #include <Windows.h> #include <ImageHlp.h> #pragma comment(lib,"Imagehlp.lib")
void EncrySection(LPSTR szFileName, DWORD Key) { HANDLE hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, 0); HANDLE lpBase = MapViewOfFile(hMap, FILE_MAP_READ | FILE_SHARE_WRITE, 0, 0, 0);
PIMAGE_DOS_HEADER DosHdr = (PIMAGE_DOS_HEADER)lpBase; PIMAGE_NT_HEADERS NtHdr = (PIMAGE_NT_HEADERS)((DWORD)lpBase + DosHdr->e_lfanew); PIMAGE_FILE_HEADER FileHdr = &NtHdr->FileHeader; PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(NtHdr); printf("[-] 节虚拟地址: 0x%08X 虚拟大小: 0x%08X\n", pSection->VirtualAddress, pSection->Misc.VirtualSize); printf("[-] 读入FOA基地址: 0x%08X 节表长度: 0x%08X \n", pSection->PointerToRawData, pSection->SizeOfRawData); printf("[*] 已对 %s 节 --> XOR加密/解密 --> XOR密钥: %d \n\n", pSection->Name, Key);
DWORD dwRead = 0; PBYTE pByte = (PBYTE)malloc(pSection->SizeOfRawData);
SetFilePointer(hFile, pSection->PointerToRawData, 0, FILE_BEGIN); memset(pByte, 0, pSection->SizeOfRawData); ReadFile(hFile, pByte, pSection->SizeOfRawData, &dwRead, NULL);
for (int x = 0; x < pSection->SizeOfRawData; x++) { pByte[x] ^= Key; }
SetFilePointer(hFile, pSection->PointerToRawData, 0, FILE_BEGIN); WriteFile(hFile, pByte, pSection->SizeOfRawData, 0, FILE_BEGIN); pSection->Characteristics = 0xE0000020;
free(pByte); FlushViewOfFile(lpBase, 0); UnmapViewOfFile(lpBase); }
void DecodeCode(LPSTR szFileName) { HANDLE hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, 0); HANDLE lpBase = MapViewOfFile(hMap, FILE_MAP_READ | FILE_SHARE_WRITE, 0, 0, 0);
PIMAGE_DOS_HEADER DosHdr = (PIMAGE_DOS_HEADER)lpBase; PIMAGE_NT_HEADERS NtHdr = (PIMAGE_NT_HEADERS)((DWORD)lpBase + DosHdr->e_lfanew);
DWORD ImageBase = NtHdr->OptionalHeader.ImageBase; DWORD BaseRVA = NtHdr->OptionalHeader.AddressOfEntryPoint; printf("Base RVA %x \n", BaseRVA);
PIMAGE_FILE_HEADER FileHdr = &NtHdr->FileHeader; PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(NtHdr);
DWORD SectionNum = FileHdr->NumberOfSections; char Code[] = { "\x60" "\xb8\x00\x00\x00\x00" "\x80\x30\x88" "\x40" "\x3d\xff\x4f\x40\x00" "\x75\xf5" "\x61" "\xb8\x00\x00\x00\x00" "\xff\xe0" }; DWORD dwWrite = 0; printf("%x \n", ImageBase + pSection->VirtualAddress); *(DWORD *)&Code[2] = ImageBase + pSection->VirtualAddress; *(DWORD *)&Code[11] = ImageBase + pSection->VirtualAddress + pSection->Misc.VirtualSize; *(DWORD *)&Code[19] = ImageBase + BaseRVA; pSection = pSection + (SectionNum - 1); printf("得到最后一个节的实际地址: %x \n", pSection->PointerToRawData);
SetFilePointer(hFile, pSection->PointerToRawData, 0, FILE_BEGIN); WriteFile(hFile, (LPVOID)Code, sizeof(Code), &dwWrite, NULL); FlushViewOfFile(lpBase, 0); UnmapViewOfFile(lpBase); }
|