13 if (!payload || !destAddress)
return false;
15 BYTE* payload_nt_hdr =
get_nt_hdrs(payload, payload_size);
16 if (payload_nt_hdr == NULL) {
17 std::cerr <<
"[-] Invalid payload: " << std::hex << (ULONGLONG) payload << std::endl;
21 const bool is64b =
is64bit(payload);
23 IMAGE_FILE_HEADER *fileHdr = NULL;
27 IMAGE_NT_HEADERS64* payload_nt_hdr64 = (IMAGE_NT_HEADERS64*) payload_nt_hdr;
28 fileHdr = &(payload_nt_hdr64->FileHeader);
29 hdrsSize = payload_nt_hdr64->OptionalHeader.SizeOfHeaders;
30 secptr = (LPVOID)((ULONGLONG)&(payload_nt_hdr64->OptionalHeader) + fileHdr->SizeOfOptionalHeader);
32 IMAGE_NT_HEADERS32* payload_nt_hdr32 = (IMAGE_NT_HEADERS32*) payload_nt_hdr;
33 fileHdr = &(payload_nt_hdr32->FileHeader);
34 hdrsSize = payload_nt_hdr32->OptionalHeader.SizeOfHeaders;
35 secptr = (LPVOID)((ULONGLONG)&(payload_nt_hdr32->OptionalHeader) + fileHdr->SizeOfOptionalHeader);
40 std::cout <<
"Coping sections:" << std::endl;
43 SIZE_T raw_end = hdrsSize;
44 for (WORD i = 0; i < fileHdr->NumberOfSections; i++) {
45 PIMAGE_SECTION_HEADER next_sec = (PIMAGE_SECTION_HEADER)((ULONGLONG)secptr + (IMAGE_SIZEOF_SECTION_HEADER * i));
46 if (!
validate_ptr(payload, payload_size, next_sec, IMAGE_SIZEOF_SECTION_HEADER)) {
50 LPVOID section_mapped = (BYTE*) payload + next_sec->VirtualAddress;
51 LPVOID section_raw_ptr = destAddress + next_sec->PointerToRawData;
52 SIZE_T sec_size = next_sec->SizeOfRawData;
54 size_t new_end = sec_size + next_sec->PointerToRawData;
55 if (new_end > raw_end) raw_end = new_end;
57 if ((next_sec->VirtualAddress + sec_size) > payload_size) {
59 std::cerr <<
"[!] Virtual section size is out ouf bounds: " << std::hex << sec_size << std::endl;
61 sec_size = (payload_size > next_sec->VirtualAddress) ? SIZE_T(payload_size - next_sec->VirtualAddress) : 0;
63 std::cerr <<
"[!] Truncated to maximal size: " << std::hex << sec_size <<
", buffer size: " << payload_size << std::endl;
66 if (next_sec->VirtualAddress > payload_size && sec_size != 0) {
68 std::cerr <<
"[-] VirtualAddress of section is out ouf bounds: " << std::hex << next_sec->VirtualAddress << std::endl;
72 if (next_sec->PointerToRawData + sec_size > payload_size) {
74 std::cerr <<
"[-] Raw section size is out ouf bounds: " << std::hex << sec_size << std::endl;
79 std::cout <<
"[+] " << next_sec->Name <<
" to: " << std::hex << section_raw_ptr << std::endl;
84 std::cerr <<
"[-] Section " << i <<
": out ouf bounds, skipping... " << std::endl;
91 std::cerr <<
"[-] Section " << i <<
": out ouf bounds, skipping... " << std::endl;
95 memcpy(section_raw_ptr, section_mapped, sec_size);
96 if (first_raw == 0 || (next_sec->PointerToRawData < first_raw)) {
97 first_raw = next_sec->PointerToRawData;
100 if (raw_end > payload_size) raw_end = payload_size;
101 if (raw_size_ptr != NULL) {
102 (*raw_size_ptr) = raw_end;
107 hdrsSize = first_raw;
109 std::cout <<
"[!] hdrsSize not filled, using calculated size: " << std::hex << hdrsSize <<
"\n";
112 if (!
validate_ptr(payload, payload_size, payload, hdrsSize)) {
115 memcpy(destAddress, payload, hdrsSize);
122 IN ULONGLONG loadBase,
123 OUT
size_t &out_size,
124 IN OPTIONAL
bool rebuffer
128 if (out_buf == NULL)
return NULL;
130 BYTE* in_buf = payload;
133 if (in_buf == NULL) {
137 memcpy(in_buf, payload, in_size);
146 std::cerr <<
"[-] Failed relocating the module!" << std::endl;
150 std::cerr <<
"[!] WARNING: The module could not be relocated, so the ImageBase has been changed instead!" << std::endl;
160 if (rebuffer && in_buf != NULL) {
174 IN
const BYTE* payload,
176 IN ULONGLONG loadBase,
186 memcpy(out_buf, payload, in_size);
194 std::cerr <<
"[-] Failed relocating the module!" << std::endl;
198 std::cerr <<
"[!] WARNING: The module could not be relocated, so the ImageBase has been changed instead!" << std::endl;
210 for (
size_t i = 0; i < sections_count; i++) {
215 sec->SizeOfRawData = sec->Misc.VirtualSize;
216 sec->PointerToRawData = sec->VirtualAddress;