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
| #include<stdio.h> #include<windows.h> #include<malloc.h>
# define EnlargeSize 0x1000 DWORD Size(FILE* fp){ DWORD size = 0; fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); return size; }
char* FileToImage(char* filebuffer){
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; char* TemBuffer = NULL;
if (!filebuffer) { printf("Invalid buffer!\n"); return 0; }
if (*(PWORD)filebuffer != IMAGE_DOS_SIGNATURE) { printf("not a PE file!\n"); return 0; }
DosHeader = (PIMAGE_DOS_HEADER)filebuffer; NTHeader = (PIMAGE_NT_HEADERS) (filebuffer + DosHeader->e_lfanew); FileHeader = (PIMAGE_FILE_HEADER)(filebuffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER)(filebuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(filebuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FileHeader->SizeOfOptionalHeader);
TemBuffer = (char*)malloc(OptionalHeader->SizeOfImage);
memset(TemBuffer,0,OptionalHeader->SizeOfImage); memcpy(TemBuffer,DosHeader,OptionalHeader->SizeOfHeaders);
for(int i=0;i<FileHeader->NumberOfSections;i++){ memcpy((TemBuffer + SectionHeader->VirtualAddress),(filebuffer + SectionHeader->PointerToRawData),SectionHeader->SizeOfRawData); SectionHeader +=1; }
return TemBuffer; }
char* Enlage(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; NTHeader = (PIMAGE_NT_HEADERS) (buffer + DosHeader->e_lfanew); 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);
char* NewBuffer = (char*)malloc(OptionalHeader->SizeOfImage + EnlargeSize); memset(NewBuffer,0,OptionalHeader->SizeOfImage + EnlargeSize); memcpy(NewBuffer,buffer,OptionalHeader->SizeOfImage);
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 LastSectionHeader = SectionHeader + FileHeader->NumberOfSections -1; DWORD LastSize = LastSectionHeader->Misc.VirtualSize > LastSectionHeader->SizeOfRawData?LastSectionHeader->Misc.VirtualSize : LastSectionHeader->SizeOfRawData;
LastSectionHeader->Misc.VirtualSize = LastSize + EnlargeSize; LastSectionHeader->SizeOfRawData = LastSize + EnlargeSize; OptionalHeader->SizeOfImage += EnlargeSize;
return NewBuffer;
}
char* Merge(char* buffer,DWORD &size){
char* ImageBuffer = FileToImage(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; PIMAGE_SECTION_HEADER NextSectionHeader =NULL; DosHeader = (PIMAGE_DOS_HEADER)ImageBuffer; NTHeader = (PIMAGE_NT_HEADERS) (ImageBuffer + DosHeader->e_lfanew); FileHeader = (PIMAGE_FILE_HEADER)(ImageBuffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER)(ImageBuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(ImageBuffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FileHeader->SizeOfOptionalHeader); SectionHeader->Misc.VirtualSize = OptionalHeader->SizeOfImage - SectionHeader->VirtualAddress; SectionHeader->SizeOfRawData = OptionalHeader->SizeOfImage - SectionHeader->VirtualAddress; SectionHeader->PointerToRawData = SectionHeader->VirtualAddress; NextSectionHeader = SectionHeader; for(int i=0;i<FileHeader->NumberOfSections;i++){ NextSectionHeader += 1; SectionHeader->Characteristics |= NextSectionHeader->Characteristics; }
FileHeader->NumberOfSections = 1; memset(NextSectionHeader,0,IMAGE_SIZEOF_SECTION_HEADER * (FileHeader->NumberOfSections -1)); size = OptionalHeader->SizeOfImage; return ImageBuffer; } int main(){ char* 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_changed.exe", "wb");
DWORD size = Size(fp1); buffer = (char*)malloc(size); fread(buffer,size,1,fp1); char* newbuffer; newbuffer = Merge(buffer,size);
fwrite(newbuffer,size,1,fp2);
return 0; }
|