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
| #include<stdio.h> #include<malloc.h> #include<windows.h>
#define ADD_SIZE 0x1000 #define ImgaeBase 0x10000 #define MessageBox_Addr 0x75C93D90 BYTE Shell[] = {0x6A,0x00,0x6A,0x00,0x6A,0x00,0x6A,0x00, 0xE8,0x00,0x00,0x00,0x00, 0xe9,0x00,0x00,0x00,0x00};
DWORD Size(FILE* fp){ DWORD size = 0; fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); return size; }
DWORD FOA_TO_RVA(DWORD FOA,char* Buffer){ PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FileHeader= NULL; PIMAGE_OPTIONAL_HEADER OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL;
DosHeader = (PIMAGE_DOS_HEADER)Buffer; FileHeader = (PIMAGE_FILE_HEADER)(Buffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER)(Buffer + DosHeader->e_lfanew + 4 +IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(Buffer + DosHeader->e_lfanew + 4 +IMAGE_SIZEOF_FILE_HEADER + FileHeader->SizeOfOptionalHeader);
if (FOA < OptionalHeader->SizeOfHeaders) { return FOA; }
for(int i =0;i<FileHeader->NumberOfSections-1;i++){ if(FOA >= SectionHeader->PointerToRawData&& FOA < (SectionHeader +1)->PointerToRawData){ DWORD offset = FOA - SectionHeader->PointerToRawData; return SectionHeader->VirtualAddress + offset; } else{ SectionHeader += 1; } } DWORD offset = FOA - SectionHeader->PointerToRawData; return SectionHeader->VirtualAddress + offset; }
char* Section_add(FILE* fp,char* buffer){ DWORD size = Size(fp); DWORD new_size = size + ADD_SIZE + 0x100;
char* NewBuffer = (char*)malloc(new_size); if (!NewBuffer) { printf("failed to creat buffer!\n"); return 0; }
PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FILEHeader = NULL; PIMAGE_OPTIONAL_HEADER OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL; memset(NewBuffer,0,new_size); memcpy(NewBuffer,buffer,size);
DosHeader = (PIMAGE_DOS_HEADER)NewBuffer; NTHeader = (PIMAGE_NT_HEADERS)(NewBuffer + DosHeader->e_lfanew); FILEHeader = (PIMAGE_FILE_HEADER)(NewBuffer+DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER)(NewBuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(NewBuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FILEHeader->SizeOfOptionalHeader);
PIMAGE_SECTION_HEADER LastSection = (PIMAGE_SECTION_HEADER)(NewBuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FILEHeader->SizeOfOptionalHeader+IMAGE_SIZEOF_SECTION_HEADER*(FILEHeader->NumberOfSections-1)); PIMAGE_SECTION_HEADER NewSection = (PIMAGE_SECTION_HEADER)(NewBuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FILEHeader->SizeOfOptionalHeader + IMAGE_SIZEOF_SECTION_HEADER * (FILEHeader->NumberOfSections));
DWORD Remained_size = (DWORD)(OptionalHeader->SizeOfHeaders - DosHeader->e_lfanew - 4 - IMAGE_SIZEOF_FILE_HEADER - IMAGE_SIZEOF_SECTION_HEADER * FILEHeader->NumberOfSections); if (Remained_size < 2 * IMAGE_SIZEOF_SECTION_HEADER) { printf("do not have enough space!\n"); free(NewBuffer); return 0; }
FILEHeader->NumberOfSections += 1; OptionalHeader->SizeOfImage += ADD_SIZE; memcpy(NewSection->Name,".NewSec",8); NewSection->Misc.VirtualSize = ADD_SIZE; NewSection->SizeOfRawData = ADD_SIZE; NewSection->PointerToRawData = LastSection->PointerToRawData + LastSection->SizeOfRawData;
DWORD add_size = LastSection->Misc.VirtualSize > LastSection->SizeOfRawData ? LastSection->Misc.VirtualSize : LastSection->SizeOfRawData;
NewSection->VirtualAddress = LastSection->VirtualAddress + add_size;
if (NewSection->VirtualAddress % OptionalHeader->SectionAlignment) { NewSection->VirtualAddress = NewSection->VirtualAddress / OptionalHeader->SectionAlignment * OptionalHeader->SectionAlignment +OptionalHeader->SectionAlignment; } NewSection->Characteristics = 0x60000020;
char* shell_begin = NewBuffer + NewSection->PointerToRawData;
DWORD begin_FOA = NewSection->PointerToRawData;
DWORD e8_FOA = begin_FOA + 8; DWORD e9_FOA = e8_FOA + 5;
DWORD begin_RVA = FOA_TO_RVA(begin_FOA,NewBuffer); DWORD e8_x_addr = MessageBox_Addr - (FOA_TO_RVA(e9_FOA,NewBuffer)+ImgaeBase); DWORD e9_x_addr = OptionalHeader->AddressOfEntryPoint - (FOA_TO_RVA(e9_FOA,NewBuffer)+5);
OptionalHeader->AddressOfEntryPoint = NewSection->VirtualAddress; printf("%X",FOA_TO_RVA(begin_FOA,NewBuffer));
memcpy(shell_begin,Shell,18);
DWORD* e8 = (DWORD*)(shell_begin + 9); DWORD* e9 = (DWORD*)(shell_begin + 14); memcpy(e8,&e8_x_addr,4); memcpy(e9,&e9_x_addr,4);
return NewBuffer; }
int main(){ char* buffer; char* new_buffer;
FILE* fp1 = NULL; FILE* fp2 = NULL; errno_t err_1 = fopen_s(&fp1, "C:\\Users\\yongrin\\Desktop\\PE_info.exe", "rb"); errno_t err_2 = fopen_s(&fp2, "C:\\Users\\yongrin\\Desktop\\PE_info_shell.exe", "wb");
DWORD size = Size(fp1); buffer = (char*)malloc(size); fread(buffer,size,1,fp1);
new_buffer = Section_add(fp1,buffer); fwrite(new_buffer,size+0x1000+0x100,1,fp2); return 0; }
|