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
| #include <iostream> #include <windows.h> #include "malloc.h" #include <malloc.h>
PIMAGE_DOS_HEADER dosHeader = nullptr; PIMAGE_FILE_HEADER fileHeader = nullptr; PIMAGE_OPTIONAL_HEADER32 optionalHeader = nullptr; PIMAGE_SECTION_HEADER* sectionArr = nullptr; LPVOID MemoryData = nullptr;
LPVOID ReadProgramData(LPCSTR programPath) { FILE* program = nullptr; size_t size = NULL; LPVOID data = nullptr;
fopen_s(&program, programPath, "rb"); if (program == nullptr) { printf("failed to open by program!\n"); goto END; }
if (fseek(program, 0, SEEK_END) == 0) { size = ftell(program); if (fseek(program, 0, SEEK_SET) != 0) { printf("failed to move the pointer to begin!\n"); goto END; } } else { printf("failed to move the pointer to end!\n"); goto END; }
data = malloc(size); if (data != nullptr) { memset(data, '\0', size); fread_s(data, size, 1, size, program); } else { printf("failed to apply by memory\n"); goto END; }
END: if (program) fclose(program);
return data; }
void AnalyzePeStruct(PCHAR fileData, PIMAGE_DOS_HEADER& dos, PIMAGE_FILE_HEADER& file, PIMAGE_OPTIONAL_HEADER32& optional, PIMAGE_SECTION_HEADER*& section) { dos = (PIMAGE_DOS_HEADER)fileData; fileData = &fileData[dos->e_lfanew + 4];
file = (PIMAGE_FILE_HEADER)fileData; fileData = &fileData[20];
optional = (PIMAGE_OPTIONAL_HEADER32)fileData; fileData = &fileData[file->SizeOfOptionalHeader];
section = (PIMAGE_SECTION_HEADER*)malloc(file->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)); if (section != nullptr) { for (size_t index = 0; index < file->NumberOfSections; index++) { section[index] = (PIMAGE_SECTION_HEADER)fileData; fileData = &fileData[40]; } } }
LPVOID ShrinkData(LPVOID memoryData) { LPVOID fileData = nullptr; LPVOID tempMemoryDataPointer = nullptr; LPVOID tempFileDataPointer = nullptr; int copyCharNumber = NULL;
size_t fileDataSize = optionalHeader->SizeOfHeaders; for (size_t index = 0; index < fileHeader->NumberOfSections; index++) { fileDataSize += sectionArr[index]->SizeOfRawData; }
fileData = malloc(fileDataSize); if (fileData != nullptr) { memset(fileData, '\0', fileDataSize); tempMemoryDataPointer = memoryData; tempFileDataPointer = fileData; copyCharNumber = optionalHeader->FileAlignment;
while (copyCharNumber < optionalHeader->SizeOfHeaders) { copyCharNumber *= 2; } memcpy_s(tempFileDataPointer, copyCharNumber, tempMemoryDataPointer, optionalHeader->SizeOfHeaders); for (size_t index = 0; index < fileHeader->NumberOfSections; index++) { copyCharNumber = optionalHeader->FileAlignment; tempFileDataPointer = (LPVOID)((UINT_PTR)fileData + sectionArr[index]->PointerToRawData); tempMemoryDataPointer = (LPVOID)((UINT_PTR)memoryData + sectionArr[index]->VirtualAddress); while (copyCharNumber < sectionArr[index]->SizeOfRawData) { copyCharNumber *= 2; } memcpy_s(tempFileDataPointer, copyCharNumber, tempMemoryDataPointer, sectionArr[index]->SizeOfRawData); } } return fileData; }
LPVOID StretchData(PCHAR fileData) { LPVOID tempMemoryDataPointer = nullptr; PCHAR tempFileDataPointer = nullptr; int copyCharNumber = NULL;
AnalyzePeStruct(fileData, dosHeader, fileHeader, optionalHeader, sectionArr); MemoryData = malloc(optionalHeader->SizeOfImage); if (MemoryData != nullptr) { memset(MemoryData, '\0', optionalHeader->SizeOfImage); tempMemoryDataPointer = MemoryData; tempFileDataPointer = fileData; copyCharNumber = optionalHeader->SectionAlignment;
while (copyCharNumber < optionalHeader->SizeOfHeaders) { copyCharNumber *= 0x2; } memcpy_s(tempMemoryDataPointer, copyCharNumber, tempFileDataPointer, optionalHeader->SizeOfHeaders);
for (size_t index = 0; index < fileHeader->NumberOfSections; index++) { copyCharNumber = optionalHeader->SectionAlignment; tempMemoryDataPointer = (LPVOID)((UINT_PTR)MemoryData + sectionArr[index]->VirtualAddress); tempFileDataPointer = (PCHAR)((UINT_PTR)fileData + sectionArr[index]->PointerToRawData); while (copyCharNumber < sectionArr[index]->SizeOfRawData) { copyCharNumber *= 2; } memcpy_s(tempMemoryDataPointer, copyCharNumber, tempFileDataPointer, sectionArr[index]->SizeOfRawData); } }
return MemoryData; }
bool WriteMyFilePeData(LPVOID fileData, LPCSTR newProgramPath) { FILE* newProgram = nullptr; bool result = true; size_t size = NULL;
fopen_s(&newProgram, newProgramPath, "wb"); if (newProgram == nullptr) { printf("failed to create by program!\n"); result = false; goto END; }
size = _msize(fileData); fwrite(fileData, 1, size, newProgram);
END: if (newProgram) fclose(newProgram);
return result; }
int main() { PCHAR fileData = (PCHAR)ReadProgramData("D:\\c\\0.exe"); LPVOID memoryData = StretchData(fileData); LPVOID myFileData = ShrinkData(memoryData); WriteMyFilePeData(myFileData, "D:\\c\\0.exe"); getchar(); free(memoryData); free(myFileData); free(fileData); return 0; }
|