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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
| #include<stdio.h> #include<malloc.h> #include<windows.h>
#define ADD_SIZE 0x3000
DWORD Size(FILE* fp){ DWORD size = 0; fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); return size; }
DWORD RVA_TO_FOA(DWORD RVA,char* Buffer){ PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FileHeader = NULL; PIMAGE_OPTIONAL_HEADER32 OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL;
DosHeader = (PIMAGE_DOS_HEADER)Buffer; FileHeader = (PIMAGE_FILE_HEADER)(Buffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER32)(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 (RVA < OptionalHeader->SizeOfHeaders) { return RVA; }
for(int i=0;i<FileHeader->NumberOfSections-1;i++){ if(RVA >= SectionHeader->VirtualAddress && RVA < (SectionHeader+1)->VirtualAddress){ DWORD offset = RVA - SectionHeader->VirtualAddress; return SectionHeader->PointerToRawData + offset; } else{ SectionHeader +=1; } }
DWORD offset = RVA - SectionHeader->VirtualAddress; return SectionHeader->PointerToRawData + offset; }
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 ;
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_HEADER32 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_HEADER32)(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 = 0xC0000040;
return NewBuffer; }
void DLL_INJECT(char* Buffer){ PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FILEHeader = NULL; PIMAGE_OPTIONAL_HEADER32 OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL;
DosHeader = (PIMAGE_DOS_HEADER)Buffer; NTHeader = (PIMAGE_NT_HEADERS)(Buffer + DosHeader->e_lfanew); FILEHeader = (PIMAGE_FILE_HEADER)(Buffer+DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER32)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FILEHeader->SizeOfOptionalHeader);
PIMAGE_SECTION_HEADER NewSec = SectionHeader + FILEHeader->NumberOfSections - 1; char* pNewSec = Buffer + NewSec->PointerToRawData; DWORD Import_Rva = OptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress; DWORD Import_Foa = RVA_TO_FOA(Import_Rva,Buffer); PIMAGE_IMPORT_DESCRIPTOR pImport_Table =(PIMAGE_IMPORT_DESCRIPTOR)(Buffer + Import_Foa);
int index = 0; while(pImport_Table->FirstThunk){ memcpy(pNewSec + 20*index,(pImport_Table),20); pImport_Table++; index++; }
PIMAGE_IMPORT_DESCRIPTOR New_Import = PIMAGE_IMPORT_DESCRIPTOR(pNewSec + index * 20);
DWORD* New_INT = (DWORD*)(pNewSec + index * 20+ 40); DWORD* New_IAT =(DWORD*)(pNewSec + index * 20 + 48);
PIMAGE_IMPORT_BY_NAME New_NAME = PIMAGE_IMPORT_BY_NAME(pNewSec + index * 20 + 56);
memcpy(New_NAME->Name,"_add",4);
*New_INT = FOA_TO_RVA((char*)New_NAME - Buffer,Buffer); *New_IAT = FOA_TO_RVA((char*)New_NAME - Buffer,Buffer);
New_Import->OriginalFirstThunk = FOA_TO_RVA(((char*)New_INT - Buffer),Buffer); New_Import->FirstThunk = FOA_TO_RVA((char*)New_IAT - Buffer,Buffer); char* New_DLL_NAME = pNewSec + index * 20 + 100; memcpy(New_DLL_NAME,"Func.dll",8);
New_Import->Name = FOA_TO_RVA(New_DLL_NAME - Buffer,Buffer);
OptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = NewSec->VirtualAddress; }
int main(){ char* buffer; char* new_buffer;
FILE* fp1 = NULL; FILE* fp2 = NULL; errno_t err_1 = fopen_s(&fp1, "C:\\Users\\yongrin\\Desktop\\T.exe", "rb"); errno_t err_2 = fopen_s(&fp2, "C:\\Users\\yongrin\\Desktop\\Test_Test.exe", "wb");
DWORD size = Size(fp1); buffer = (char*)malloc(size); fread(buffer,size,1,fp1);
new_buffer = Section_add(fp1,buffer); DLL_INJECT(new_buffer);
fwrite(new_buffer,size+ADD_SIZE,1,fp2); return 0; }
|