PE-sieve
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
Loading...
Searching...
No Matches
pe_reconstructor.cpp
Go to the documentation of this file.
1#include "pe_reconstructor.h"
2
4
5#include <fstream>
6
7namespace pesieve {
8 inline bool shift_artefacts(PeArtefacts& artefacts, size_t shift_size)
9 {
10 artefacts.ntFileHdrsOffset += shift_size;
11 artefacts.secHdrsOffset += shift_size;
12 return true;
13 }
14}; //namespace pesieve
15
16
17//WARNING: this function shifts also offsets saved in the artefacts
19{
20 BYTE *vBuf = this->peBuffer.vBuf;
21 const size_t vBufSize = this->peBuffer.vBufSize;
22 if (vBuf == nullptr) return 0;
23
24 if (!this->artefacts.hasNtHdrs()) return 0;
25
26 const size_t dos_pe_size = sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_SIGNATURE);
27 const size_t nt_offset = this->artefacts.dropPeBase(this->artefacts.ntFileHdrsOffset);
28 if (nt_offset == INVALID_OFFSET || nt_offset >= dos_pe_size) {
29 return 0;
30 }
31 //TODO: shift the header
32 if (!this->artefacts.hasSectionHdrs()) return 0; //cannot proceed
33
34 size_t shift_size = dos_pe_size - nt_offset;
35 size_t hdrs_end = this->artefacts.secHdrsOffset + (this->artefacts.secCount + 1)* sizeof(IMAGE_SECTION_HEADER);
36 if (!peconv::is_padding(vBuf + hdrs_end, shift_size, 0)) {
37 return 0; // no empty space, cannot proceed
38 }
39 size_t hdrs_size = this->artefacts.dropPeBase(hdrs_end);
41 if (!peconv::validate_ptr(vBuf, vBufSize, new_nt_ptr, hdrs_size)) {
42 return 0;
43 }
44 if (nt_offset < sizeof(IMAGE_NT_SIGNATURE)) {
45 return 0;
46 }
47 const size_t pe_offset = nt_offset - sizeof(IMAGE_NT_SIGNATURE);
51
52 //check mz signature:
53 BYTE *mz_ptr = vBuf + this->artefacts.peBaseOffset;
54 if (!peconv::validate_ptr(vBuf, vBufSize, mz_ptr, sizeof(IMAGE_DOS_HEADER))) {
55 return 0;
56 }
57 //check PE signature:
58 DWORD* pe_ptr = (DWORD*)(vBuf + this->artefacts.peBaseOffset + dos_template.e_lfanew);
59 if (!peconv::validate_ptr(vBuf, vBufSize, pe_ptr, sizeof(DWORD))) {
60 return 0;
61 }
62 //all checks passed, do the actual headers shift:
63 memmove(new_nt_ptr, (vBuf + this->artefacts.peBaseOffset), hdrs_size);
64
65 //write the DOS header:
67
68 //write the PE signature:
70
71 shift_artefacts(this->artefacts, shift_size);
72 return shift_size;
73}
74
76{
77 this->artefacts = origArtefacts;
78
79 ULONGLONG moduleBase = artefacts.regionStart + artefacts.peBaseOffset;
80 if (!peBuffer.readRemote(moduleBase, artefacts.calculatedImgSize)) {
81 return false;
82 }
83 size_t shift_size = shiftPeHeader();
84 if (shift_size) {
85 std::cout << "[!] The PE header was shifted by: " << std::hex << shift_size << std::endl;
86 }
87 bool is_pe_hdr = false;
88 if (this->artefacts.hasNtHdrs() && reconstructFileHdr()) {
89 is_pe_hdr = reconstructPeHdr();
90 }
91 if (!is_pe_hdr) {
92 return false;
93 }
94 //do not modify section headers if the PE is in raw format, or no unmapping requested
95 if (!peconv::is_pe_raw(peBuffer.vBuf, peBuffer.vBufSize)) {
96 if (!fixSectionsVirtualSize(peBuffer.processHndl) || !fixSectionsCharacteristics(peBuffer.processHndl)) {
97 return false;
98 }
99 }
100 return peBuffer.isValidPe();
101}
102
104{
105 BYTE *vBuf = this->peBuffer.vBuf;
106 const size_t vBufSize = this->peBuffer.vBufSize;
107 if (!vBuf) return false;
108
109 if (!this->artefacts.hasSectionHdrs()) {
110 return false;
111 }
112 ULONGLONG sec_offset = this->artefacts.dropPeBase(this->artefacts.secHdrsOffset);
113 BYTE *hdr_ptr = (sec_offset + vBuf);
114
115 size_t max_sec_size = 0;
116
119
120 const ULONGLONG pe_img_base = (ULONGLONG)artefacts.peImageBase();
121
122 const size_t hdr_sec_count = peconv::get_sections_count(vBuf, vBufSize);
123 const size_t sec_count = artefacts.secCount > hdr_sec_count ? artefacts.secCount : hdr_sec_count;
124
125 size_t i = 0;
126 for (i = 0; i < sec_count; i++, curr_sec++) {
127 if (!peconv::validate_ptr(vBuf, vBufSize, curr_sec, sizeof(IMAGE_SECTION_HEADER))) {
128 // buffer finished
129 break;
130 }
131 const DWORD sec_rva = curr_sec->VirtualAddress;
132 const DWORD sec_size = curr_sec->Misc.VirtualSize;
133
135 size_t real_sec_size = peconv::fetch_region_size(processHandle, (PBYTE)sec_va);
136
137 // if the RVA is out of scope, and the calculated size is 0:
138 if (!peconv::validate_ptr(vBuf, vBufSize, vBuf + sec_rva, sizeof(BYTE)) && !real_sec_size) {
139#ifdef _DEBUG
140 std::cout << i << "# Invalid section found: " << std::hex
141 << sec_rva << " of size: " << sec_size << std::endl;
142#endif
143 break;
144 }
145
146 if (sec_size > real_sec_size) {
147 curr_sec->Misc.VirtualSize = DWORD(real_sec_size);
148#ifdef _DEBUG
149 std::cout << i << "# Fixed section " << std::hex << sec_rva << " size: " << std::hex
150 << sec_size << " vs real: " << real_sec_size << std::endl;
151#endif
152 }
153
155
156 if (prev_sec && curr_sec->Misc.VirtualSize > 0) {
157 ULONGLONG prev_sec_end = prev_sec->VirtualAddress + prev_sec->Misc.VirtualSize;
158 if (prev_sec_end > curr_sec->VirtualAddress) {
159 if (curr_sec->VirtualAddress > prev_sec->VirtualAddress) {
160 DWORD diff = curr_sec->VirtualAddress - prev_sec->VirtualAddress;
161 prev_sec->Misc.VirtualSize = diff;
162#ifdef _DEBUG
163 std::cout << "Trimmed section: " << std::dec << i << std::endl;
164#endif
165 }
166 }
167 }
168 if (curr_sec->Misc.VirtualSize > 0) {
170 }
171 }
172
173 IMAGE_FILE_HEADER* file_hdr = const_cast<IMAGE_FILE_HEADER*>(peconv::get_file_hdr(vBuf, vBufSize));
174 if (file_hdr && (i > 0)) {
175 // set the actual number of valid sections:
176 file_hdr->NumberOfSections = MASK_TO_WORD(i);
177 }
178
179 if (max_sec_size == 0) {
180 return false;
181 }
182 return true;
183}
184
186{
187 BYTE *vBuf = this->peBuffer.vBuf;
188 const size_t vBufSize = this->peBuffer.vBufSize;
189 if (!vBuf) return false;
190
191 if (!this->artefacts.hasSectionHdrs()) {
192 return false;
193 }
194
195 ULONGLONG sec_offset = this->artefacts.dropPeBase(this->artefacts.secHdrsOffset);
196 const BYTE *hdr_ptr = (sec_offset + vBuf);
198
205
206 for (size_t i = 0; i < artefacts.secCount; i++, curr_sec++) {
207 if (!is_valid_section(vBuf, vBufSize, (BYTE*)curr_sec, 0)) {
208 break;
209 }
210 //leave only the flags that are valid
211 const DWORD charact = curr_sec->Characteristics;
212 curr_sec->Characteristics = charact & sec_all_flags;
213#ifdef DEBUG
214 if (charact != curr_sec->Characteristics) {
215 std::cout << "Section characteristics overwriten\n";
216 }
217#endif
218 }
219 return true;
220}
221
223{
224 BYTE *vBuf = this->peBuffer.vBuf;
225 const size_t vBufSize = this->peBuffer.vBufSize;
226 if (!vBuf) return false;
227
228 if (!this->artefacts.hasNtHdrs()) {
229 return false;
230 }
231 size_t nt_offset = this->artefacts.dropPeBase(this->artefacts.ntFileHdrsOffset);
232 BYTE* nt_ptr = (BYTE*)((ULONGLONG)vBuf + nt_offset);
233 if (is_valid_file_hdr(vBuf, vBufSize, nt_ptr, 0)) {
234 return true;
235 }
237 if (!peconv::validate_ptr(vBuf, vBufSize, hdr_candidate, sizeof(IMAGE_FILE_HEADER))) {
238 // probably buffer finished
239 return false;
240 }
241
242 size_t opt_hdr_size = 0;
243 if (artefacts.is64bit) {
246 }
247 else {
250 }
251 if (this->artefacts.secHdrsOffset) {
252 const size_t sec_offset = this->artefacts.dropPeBase(this->artefacts.secHdrsOffset);
253 size_t calc_offset = sec_offset - (nt_offset + sizeof(IMAGE_FILE_HEADER));
254
255 if (calc_offset != opt_hdr_size) {
256 std::cout << "[WARNING] Calculated sections header offset is different than the saved one!\n";
257 }
258 hdr_candidate->NumberOfSections = WORD(this->artefacts.secCount);
259 hdr_candidate->SizeOfOptionalHeader = WORD(calc_offset);
260 }
261 hdr_candidate->NumberOfSymbols = 0;
262 hdr_candidate->PointerToSymbolTable = 0;
263 return true;
264}
265
267{
268 BYTE *vBuf = this->peBuffer.vBuf;
269 const size_t vBufSize = this->peBuffer.vBufSize;
270 if (!vBuf) return false;
271
272 if (!this->artefacts.hasNtHdrs()) {
273 return false;
274 }
275 ULONGLONG nt_offset = this->artefacts.dropPeBase(this->artefacts.ntFileHdrsOffset);
276 BYTE* nt_ptr = (BYTE*)((ULONGLONG)vBuf + nt_offset);
277 BYTE *pe_ptr = nt_ptr - sizeof(DWORD);
278
279 if (!peconv::validate_ptr(vBuf, vBufSize, pe_ptr, sizeof(DWORD))) {
280 return false;
281 }
283 //write signature:
284 nt32->Signature = IMAGE_NT_SIGNATURE;
285 IMAGE_FILE_HEADER *file_hdr = &nt32->FileHeader;
286
287 bool is64bit = (file_hdr->Machine == IMAGE_FILE_MACHINE_AMD64) ? true : false;
288
289 if (nt32->FileHeader.SizeOfOptionalHeader == 0) {
290 nt32->FileHeader.SizeOfOptionalHeader = is64bit ? sizeof(IMAGE_OPTIONAL_HEADER64) : sizeof(IMAGE_OPTIONAL_HEADER32);
291 }
294 dosHdr->e_magic = IMAGE_DOS_SIGNATURE;
295 dosHdr->e_lfanew = pe_offset;
296
297 bool is_fixed = false;
298 if (is64bit) {
299 is_fixed = overwrite_opt_hdr<IMAGE_OPTIONAL_HEADER64>(vBuf, vBufSize, (IMAGE_OPTIONAL_HEADER64*)&nt32->OptionalHeader, this->artefacts);
300 }
301 else {
302 is_fixed = overwrite_opt_hdr<IMAGE_OPTIONAL_HEADER32>(vBuf, vBufSize, &nt32->OptionalHeader, this->artefacts);
303 }
304 if (!is_fixed) {
305 return false;
306 }
307 if (!peconv::get_nt_hdrs(vBuf, vBufSize)) {
308 return false;
309 }
310 return true;
311}
312
#define INVALID_OFFSET
A report about the PE artefact detected in the workingset.
ULONGLONG dropPeBase(const ULONGLONG offset_with_pe_base) const
bool fixSectionsVirtualSize(HANDLE processHandle)
bool fixSectionsCharacteristics(HANDLE processHandle)
bool is_valid_file_hdr(BYTE *loadedData, size_t loadedSize, BYTE *hdr_ptr, DWORD charact)
size_t fill_iat(BYTE *vBuf, size_t vBufSize, IN const peconv::ExportsMapper *exportsMap, IN OUT IATBlock &iat, IN ThunkFoundCallback *callback)
Definition iat_finder.h:31
bool shift_artefacts(PeArtefacts &artefacts, size_t shift_size)
bool is_valid_section(BYTE *loadedData, size_t loadedSize, BYTE *hdr_ptr, DWORD charact)