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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<Windows.h> #include<malloc.h> #include<stdlib.h>
#define MessageBoxA_ADDR 0x76C20F40 #define shellcode_len 0x12
char file_path[] = "D:\\new\\Notepadx32\\Notepad++.7.6.1.bin.x32\\Notepad++.7.6.1.bin.x32\\notepad++.exe"; char final_path[] = "D:\\new\\Notepadx32\\Notepad++.7.6.1.bin.x32\\Notepad++.7.6.1.bin.x32\\5.exe";
BYTE shellcode[] = { 0x6A,0x00,0x6A,0x00,0x6A,0x00,0x6A,0x00, 0xE8,0x00,0x00,0x00,0x00, 0xE9,0x00,0x00,0x00,0x00 }; int LoadPE(LPVOID*pFileBuffer) { FILE* pfile; DWORD FileSize = 0; LPVOID pTempBuffer; pfile = fopen(file_path, "rb"); fseek(pfile, 0, SEEK_END); FileSize = ftell(pfile);
fseek(pfile, 0, SEEK_SET); pTempBuffer = malloc(FileSize);
fread(pTempBuffer, FileSize, 1, pfile);
*pFileBuffer = pTempBuffer; pTempBuffer = NULL; fclose(pfile); return FileSize; }
int CopyFileBufferToImageBuffer(LPVOID pFileBuffer,LPVOID* pImageBuffer) { PIMAGE_DOS_HEADER pDOSHeader = NULL; PIMAGE_NT_HEADERS pNTHeader = NULL; PIMAGE_FILE_HEADER pFileHeader = NULL; PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = NULL; PIMAGE_SECTION_HEADER pSectionHeader = NULL; LPVOID pTempBuffer = NULL; pDOSHeader = PIMAGE_DOS_HEADER(pFileBuffer); pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pFileBuffer + pDOSHeader->e_lfanew); pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNTHeader + 4); pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader + IMAGE_SIZEOF_FILE_HEADER); pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionalHeader + pFileHeader->SizeOfOptionalHeader); pTempBuffer = malloc(pOptionalHeader->SizeOfImage); memset(pTempBuffer, 0, pOptionalHeader->SizeOfImage); memcpy(pTempBuffer, pDOSHeader, pOptionalHeader->SizeOfHeaders); PIMAGE_SECTION_HEADER pTempSectionHeader = pSectionHeader; for (int i = 0; i < pFileHeader->NumberOfSections; i++,pTempSectionHeader++) { memcpy((void*)((DWORD)pTempBuffer + pTempSectionHeader->VirtualAddress), (void*)((DWORD)pFileBuffer + pTempSectionHeader->PointerToRawData), pTempSectionHeader->SizeOfRawData); } *pImageBuffer = pTempBuffer; pTempBuffer = NULL; return pOptionalHeader->SizeOfImage; }
int CopyImageBufferToNewBuffer(LPVOID pImageBuffer,LPVOID* pNewBuffer) { PIMAGE_DOS_HEADER pDOSHeader = NULL; PIMAGE_NT_HEADERS pNTHeader = NULL; PIMAGE_FILE_HEADER pFileHeader = NULL; PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = NULL; PIMAGE_SECTION_HEADER pSectionHeader = NULL; LPVOID pTempBuffer = NULL;
pDOSHeader = PIMAGE_DOS_HEADER(pImageBuffer); pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pImageBuffer + pDOSHeader->e_lfanew); pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNTHeader + 4); pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader + IMAGE_SIZEOF_FILE_HEADER); pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionalHeader + pFileHeader->SizeOfOptionalHeader);
DWORD new_buffer_size = pOptionalHeader->SizeOfHeaders; PIMAGE_SECTION_HEADER pTempSectionHeader = pSectionHeader; for (int i = 0; i < pFileHeader->NumberOfSections; i++, pTempSectionHeader++) { new_buffer_size += pTempSectionHeader->SizeOfRawData; } printf("%X\n", new_buffer_size); pTempBuffer = malloc(new_buffer_size); memset(pTempBuffer, 0, new_buffer_size); memcpy(pTempBuffer, pDOSHeader, pOptionalHeader->SizeOfHeaders); pTempSectionHeader = pSectionHeader; for (int i = 0; i < pFileHeader->NumberOfSections; i++, ++pTempSectionHeader) { memcpy((PWORD)((DWORD)pTempBuffer + pTempSectionHeader->PointerToRawData), (PWORD)((DWORD)pImageBuffer + pTempSectionHeader->VirtualAddress), pTempSectionHeader->SizeOfRawData); } *pNewBuffer = pTempBuffer; pTempBuffer = NULL; return new_buffer_size; } void newbuffer_write2_exe(PVOID NewFileBuffer, DWORD FileSize, char* FilePath) { FILE* fp1 = fopen(FilePath, "wb"); if (fp1 != NULL) { fwrite(NewFileBuffer, FileSize, 1, fp1); printf("成功存盘"); } fclose(fp1); return ;
} void ADD_Opcode(LPVOID pImageBuffer, LPVOID* pNewBuffer) { PIMAGE_DOS_HEADER pDOSHeader = NULL; PIMAGE_NT_HEADERS pNTHeader = NULL; PIMAGE_FILE_HEADER pFileHeader = NULL; PIMAGE_OPTIONAL_HEADER32 pOptionalHeader = NULL; PIMAGE_SECTION_HEADER pSectionHeader = NULL; pDOSHeader = PIMAGE_DOS_HEADER(pImageBuffer); pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pImageBuffer + pDOSHeader->e_lfanew); pFileHeader = (PIMAGE_FILE_HEADER)((DWORD)pNTHeader + 4); pOptionalHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pFileHeader + IMAGE_SIZEOF_FILE_HEADER); pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionalHeader + pFileHeader->SizeOfOptionalHeader); if (shellcode_len > pSectionHeader->SizeOfRawData - pSectionHeader->Misc.VirtualSize) { printf("长度不足,无法插入\n"); free(pImageBuffer); }
PBYTE code_begin = (PBYTE)((DWORD)pImageBuffer + pSectionHeader->VirtualAddress+ pSectionHeader->Misc.VirtualSize); memcpy(code_begin, shellcode, shellcode_len); DWORD callADDr = (MessageBoxA_ADDR - (pOptionalHeader->ImageBase + pSectionHeader->VirtualAddress + pSectionHeader->Misc.VirtualSize + 0xD)); *(PDWORD)((DWORD)code_begin + 0x09) = callADDr;
DWORD jmpADDr = pOptionalHeader->ImageBase + pOptionalHeader->AddressOfEntryPoint - ((DWORD)code_begin + pOptionalHeader->ImageBase + shellcode_len-(DWORD)pImageBuffer); *(PDWORD)((code_begin + 14)) = jmpADDr;
pOptionalHeader->AddressOfEntryPoint = ((DWORD)code_begin - (DWORD)pImageBuffer);
DWORD ret2=CopyImageBufferToNewBuffer(pImageBuffer, pNewBuffer); newbuffer_write2_exe(*pNewBuffer,ret2, final_path); return ; } void operate_pe() { LPVOID pFileBuffer = NULL; LPVOID pNewBuffer = NULL; LPVOID pImageBuffer = NULL; DWORD file_size = LoadPE(&pFileBuffer); printf("文件长度为: %08X\n", file_size);
DWORD Image_Size = CopyFileBufferToImageBuffer(pFileBuffer,&pImageBuffer); printf("拉伸后的大小为: %08X\n",Image_Size);
ADD_Opcode(pImageBuffer, &pNewBuffer);
free(pFileBuffer); free(pNewBuffer); free(pImageBuffer); return; } int main() { operate_pe(); return 0; }
|