BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
PEFile.cpp
Go to the documentation of this file.
1#include "pe/PEFile.h"
2#include "FileBuffer.h"
3
5{
6 if (buf == NULL) return false;
7
8 offset_t dosOffset = 0;
9 WORD *magic = (WORD*) buf->getContentAt(dosOffset, sizeof(WORD));
10 if (magic == NULL) return false;
11
12 if ((*magic) != pe::S_DOS) {
13 return false;
14 }
15 offset_t newOffset = dosOffset + (sizeof(IMAGE_DOS_HEADER) - sizeof(LONG));
16 LONG* lfnew = (LONG*) buf->getContentAt(newOffset, sizeof(LONG));
17 if (lfnew == NULL) {
18 return false;
19 }
20 offset_t peOffset = static_cast<offset_t>(*lfnew);
21 DWORD *peMagic = (DWORD*) buf->getContentAt(peOffset, sizeof(DWORD));
22 if (peMagic == NULL) {
23 return false;
24 }
25 if (*peMagic == pe::S_NT) {
26 return true;
27 }
28 return false;
29}
30
32{
33 Executable *exe = NULL;
34 if (signatureMatches(buf) == false) return NULL;
35
36 try {
37 exe = new PEFile(buf);
38 } catch (ExeException &e) {
39 exe = NULL;
40 }
41 return exe;
42}
43
44//-------------------------------------------------------------
45long PEFile::computeChecksum(BYTE *buffer, size_t bufferSize, offset_t checksumOffset)
46{
47 if (!buffer || !bufferSize) return 0;
48
49 WORD *wordBuff = (WORD*)buffer;
50 size_t wordSize = bufferSize / sizeof(WORD);
51
52 size_t checksumBgn = 0;
53 size_t checksumEnd = 0;
54 if (checksumOffset != INVALID_ADDR) {
55 checksumBgn = size_t(checksumOffset);
56 checksumEnd = checksumBgn + sizeof(DWORD);
57 }
58
59 const long long maxVal = ((long long)1) << 32;
60 long long checksum = 0;
61
62 for (int i = 0; i < wordSize; i++) {
63 WORD chunk = wordBuff[i];
64
65 size_t bI = i * sizeof(WORD);
66 if (checksumBgn != checksumEnd && bI >= checksumBgn && bI < checksumEnd) {
67 size_t mask = (checksumEnd - bI) % sizeof(WORD);
68 size_t shift = (sizeof(WORD) - mask) * 8;
69 chunk = (chunk >> shift) << shift;
70 }
71
72 checksum = (checksum & 0xffffffff) + chunk + (checksum >> 32);
73 if (checksum > maxVal) {
74 checksum = (checksum & 0xffffffff) + (checksum >> 32);
75 }
76 }
77 checksum = (checksum & 0xffff) + (checksum >> 16);
78 checksum = (checksum)+(checksum >> 16);
79 checksum = checksum & 0xffff;
80 checksum += bufferSize;
81 return checksum;
82}
83
85
87 : MappedExe(v_buf, Executable::BITS_32), dosHdrWrapper(NULL), fHdr(NULL), optHdr(NULL), sects(NULL),
88 album(NULL)
89{
90 album = new ResourcesAlbum(this);
91 wrap(v_buf);
93}
94
96{
99
100 this->dosHdrWrapper = NULL;
101 this->fHdr = NULL;
102 this->optHdr = NULL;
103 this->sects = NULL;
104}
105
107{
108 for (size_t i = 0 ; i < pe::DIR_ENTRIES_COUNT; i++) {
109 dataDirEntries[i] = NULL;
110 }
111}
112
114{
115 PEFile::wrap(this->buf);
116}
117
119{
120 //erase all existing wrappers:
122
123 // rewrao the core:
124 core.wrap(v_buf);
125
126 //regenerate the wrappers:
127 this->dosHdrWrapper = new DosHdrWrapper(this);
128 this->wrappers[WR_DOS_HDR] = this->dosHdrWrapper;
129
130 this->fHdr = new FileHdrWrapper(this);
131 if (fHdr->getPtr() == NULL) throw ExeException("Cannot parse FileHdr: It is not PE File!");
132 this->wrappers[WR_FILE_HDR] = fHdr;
133 this->wrappers[WR_RICH_HDR] = new RichHdrWrapper(this);
134
135 this->optHdr = new OptHdrWrapper(this);
136 if (optHdr->getPtr() == NULL) throw ExeException("Cannot parse OptionalHeader: It is not PE File!");
138
139 this->wrappers[WR_DATADIR] = new DataDirWrapper(this);
140
141 bool isOk = false;
142 const size_t secNum = fHdr->getNumValue(FileHdrWrapper::SEC_NUM, &isOk);
143 if (isOk && secNum){
144 this->sects = new SectHdrsWrapper(this);
145 this->wrappers[WR_SECTIONS] = sects;
146 }
147 else {
148 this->sects = NULL;
149 }
150 // map Data Dirs
152 dataDirEntries[pe::DIR_IMPORT] = new ImportDirWrapper(this);
153 dataDirEntries[pe::DIR_DELAY_IMPORT] = new DelayImpDirWrapper(this);
154 dataDirEntries[pe::DIR_BOUND_IMPORT] = new BoundImpDirWrapper(this);
155 dataDirEntries[pe::DIR_DEBUG] = new DebugDirWrapper(this);
156 dataDirEntries[pe::DIR_EXPORT] = new ExportDirWrapper(this);
157 dataDirEntries[pe::DIR_SECURITY] = new SecurityDirWrapper(this);
158 dataDirEntries[pe::DIR_TLS] = new TlsDirWrapper(this);
159 dataDirEntries[pe::DIR_LOAD_CONFIG] = new LdConfigDirWrapper(this);
160 dataDirEntries[pe::DIR_BASERELOC] = new RelocDirWrapper(this);
161 dataDirEntries[pe::DIR_EXCEPTION] = new ExceptionDirWrapper(this);
162 dataDirEntries[pe::DIR_RESOURCE] = new ResourceDirWrapper(this, album);
163 dataDirEntries[pe::DIR_COM_DESCRIPTOR] = new ClrDirWrapper(this);
164
165 for (int i = 0; i < pe::DIR_ENTRIES_COUNT; i++) {
166 this->wrappers[WR_DIR_ENTRY + i] = dataDirEntries[i];
167 }
168
169 if (this->album) {
170 this->album->wrapLeafsContent();
171 }
172}
173
174pe::RICH_DANS_HEADER* PEFile::getRichHeaderBgn(pe::RICH_SIGNATURE* richSign)
175{
176 if (!richSign) return NULL;
177
178 DWORD xorkey = richSign->checksum;
179 const offset_t richOffset = this->getOffset(richSign);
180
181 pe::RICH_DANS_HEADER* dansHdr = NULL;
182
183 offset_t offset = richOffset - sizeof(pe::RICH_DANS_HEADER);
184 while (offset > 0) {
185 dansHdr = (pe::RICH_DANS_HEADER*) this->getContentAt(offset, sizeof(pe::RICH_DANS_HEADER));
186 if (!dansHdr) {
187 break;
188 }
189 if (dansHdr->dansId == (pe::DANS_HDR_MAGIC ^ xorkey)) {
190 break; //got it!
191 }
192 //walking back
193 offset -= sizeof(DWORD);
194 }
195 if (!dansHdr || dansHdr->dansId != (pe::DANS_HDR_MAGIC ^ xorkey)) {
196 return NULL; //not found
197 }
198 return dansHdr;
199}
200
201pe::RICH_SIGNATURE* PEFile::getRichHeaderSign()
202{
203 size_t dosStubOffset = this->core.dos->e_lfarlc;
204 size_t dosStubEnd = this->core.dos->e_lfanew; // PE header start
205 const size_t maxSize = dosStubEnd - dosStubOffset; // Rich Header is somewhere in the space between DOS and PE headers
206 BYTE *dosPtr = this->getContentAt(dosStubOffset, maxSize);
207 if (!dosPtr) {
208 return NULL;
209 }
210
211 pe::RICH_SIGNATURE* richSign = NULL;
212 size_t toSearchSize = maxSize;
213 const offset_t startOffset = dosStubOffset; //we are starting from the beginning of DOS stub
214 const size_t step = sizeof(DWORD); //RichHeader is padded by DWORDS
215
216 while (toSearchSize > 0) {
217 richSign = (pe::RICH_SIGNATURE*) this->getContentAt(startOffset + toSearchSize, sizeof(pe::RICH_SIGNATURE));
218 if (!richSign) break;
219 if (richSign->richId == pe::RICH_HDR_MAGIC) break; //got it!
220 // the search goes backward.
221 toSearchSize -= step;
222 }
223 if (!richSign) return NULL;
224 if (richSign->richId != pe::RICH_HDR_MAGIC) {
225 return NULL; //invalid
226 }
227 return richSign;
228}
229
230
232{
233 if (!this->getSectionsCount()) {
234 return INVALID_ADDR;
235 }
236 SectionHdrWrapper* sec = this->getSecHdr(0);
237 if (!sec) {
238 return INVALID_ADDR;
239 }
241}
242
244{
245 if (this->optHdr == NULL) return INVALID_ADDR;
247}
248
249IMAGE_DATA_DIRECTORY* PEFile::getDataDirectory()
250{
251 if (this->wrappers[WR_DATADIR] == NULL) return NULL;
252 return static_cast<IMAGE_DATA_DIRECTORY*>(this->wrappers[WR_DATADIR]->getPtr());
253}
254
256{
257 if (aType == Executable::NOT_ADDR) return 0;
258
259 if (aType == Executable::RAW) {
260 return this->getContentSize();
261 }
262 const size_t PAGE_SIZE = 0x1000;
263 bufsize_t vSize = 0;
264 if (aType == Executable::VA || aType == Executable::RVA) {
265 vSize = core.getImageSize();
266 }
267 if (vSize < PAGE_SIZE) {
268 return PAGE_SIZE;
269 }
270 return vSize;
271}
272
274{
275 if (optHdr == NULL) return INVALID_ADDR;
276
277 bool isOk = false;
278 offset_t entryPoint = static_cast<offset_t> (optHdr->getNumValue(OptHdrWrapper::EP, &isOk));
279 if (isOk == false) return INVALID_ADDR;
280
282 if (addrType != epType) {
283 entryPoint = this->convertAddr(entryPoint, epType, addrType);
284 }
285 return entryPoint;
286}
287
289{
290 if (optHdr == NULL) return false;
291
292 offset_t epRva = this->convertAddr(entry, aType, Executable::RVA);
293 bool isOk = optHdr->setNumValue(OptHdrWrapper::EP, epRva);
294 return isOk;
295}
296
298{
299 bool isOk = false;
300 uint64_t secNum = this->fHdr->getNumValue(FileHdrWrapper::SEC_NUM , &isOk);
301 if (isOk == false) return 0;
302
303 return static_cast<size_t> (secNum);
304}
305
306bool PEFile::setHdrSectionsNum(size_t newNum)
307{
308 uint64_t count = newNum;
309 bool canSet = fHdr->setNumValue(FileHdrWrapper::SEC_NUM , count);
310 if (canSet == false) {
311 Logger::append(Logger::D_ERROR,"Can not change FileHdr!");
312 return false;
313 }
314 return true;
315}
316
318{
319 uint64_t size = newSize;
320 bool canSet = optHdr->setNumValue(OptHdrWrapper::IMAGE_SIZE, size);
321 if (canSet == false) {
322 Logger::append(Logger::D_ERROR, "Can not change OptHdr!");
323 return false;
324 }
325 return true;
326}
327
328size_t PEFile::getSectionsCount(bool useMapped) const
329{
330 if (useMapped == false) {
331 return hdrSectionsNum();
332 }
333 return (this->sects) ? this->sects->getEntriesCount() : 0;
334}
335
337{
338 if (raw >= this->getMappedSize(Executable::RAW)) return INVALID_ADDR;
339
340 SectionHdrWrapper* sec = this->getSecHdrAtOffset(raw, Executable::RAW, true);
341 if (sec) {
344 if (bgnVA == INVALID_ADDR || bgnRaw == INVALID_ADDR) return INVALID_ADDR;
345
346 bufsize_t curr = (raw - bgnRaw);
347
348 bufsize_t vSize = sec->getContentSize(Executable::VA, true);
349 if (curr >= vSize) {
350 //address out of section
351 return INVALID_ADDR;
352 }
353 return bgnVA + curr;
354 }
355 //TODO: make more tests
356 if (this->getSectionsCount() == 0) return raw;
357 if (raw < this->hdrsSize()) {
358 return raw;
359 } //else: content that is between the end of sections headers and the first virtual section is not mapped
360 return INVALID_ADDR;
361}
362
364{
365 if (rva >= this->getMappedSize(Executable::RVA)) return INVALID_ADDR;
366
367 SectionHdrWrapper* sec = this->getSecHdrAtOffset(rva, Executable::RVA, true);
368 if (sec) {
371 if (bgnRVA == INVALID_ADDR || bgnRaw == INVALID_ADDR) return INVALID_ADDR;
372
373 bufsize_t curr = (rva - bgnRVA);
374 bufsize_t rawSize = sec->getContentSize(Executable::RAW, true);
375 if (curr >= rawSize) {
376 // the address might be in a virtual cave that is not related to any raw address
377 return INVALID_ADDR;
378 }
379 return bgnRaw + curr;
380 }
381 if (rva >= this->getMappedSize(Executable::RAW)) {
382 return INVALID_ADDR;
383 }
384 if (this->getSectionsCount()) { // do this check only if sections count is non-zero
385 if (rva >= this->hdrsSize()) {
386 // the address is in the cave between the headers and the first section: cannot be mapped
387 return INVALID_ADDR;
388 }
389 }
390 // at this point we are sure that the address is within the raw size:
391 return rva;
392}
393
395{
396 if (eType >= pe::DIR_ENTRIES_COUNT) return NULL;
397 return dataDirEntries[eType];
398}
399
401{
402 SectionHdrWrapper *sec = this->getSecHdr(secId);
403 if (sec == NULL) {
404 Logger::append(Logger::D_WARNING, "No such section");
405 return NULL;
406 }
407 return _createSectionView(sec);
408}
409
410bool PEFile::moveDataDirEntry(pe::dir_entry id, offset_t newOffset, Executable::addr_type addrType)
411{
412 bool allowExceptions = true; //TODO: configure exception mode outside...
413
415 if (entry == NULL) {
416 if (allowExceptions) throw ExeException("No such Data Directory");
417 return false;
418 }
419 DataDirWrapper* ddirWrapper = dynamic_cast<DataDirWrapper*> (this->wrappers[WR_DATADIR]);
420 IMAGE_DATA_DIRECTORY *ddir = this->getDataDirectory();
421 if (ddirWrapper == NULL || ddir == NULL) {
422 if (allowExceptions) throw ExeException("Cannot fetch DataDirTable");
423 return false;
424 }
425 Executable::addr_type dataDirAddrType = ddirWrapper->containsAddrType(id, DataDirWrapper::ADDRESS);
426 offset_t dataDirAddr = this->convertAddr(newOffset, addrType, dataDirAddrType);
427 if (dataDirAddr == INVALID_ADDR) {
428 if (allowExceptions) throw ExeException("Invalid new offset");
429 return false;
430 }
431 offset_t targetRaw = this->toRaw(newOffset, addrType);
432 if (entry->canCopyToOffset(targetRaw) == false) {
433 if (allowExceptions) throw ExeException("Cannot copy: no space at such offset");
434 return false;
435 }
436 if (entry->copyToOffset(targetRaw) == false) {
437 if (allowExceptions) throw ExeException("Cannot copy: error occured");
438 return false;
439 }
440 entry->fillContent(0);
441 ddir[id].VirtualAddress = static_cast<DWORD> (dataDirAddr);
442 return true;
443}
444
446{
448 if (sec == NULL || sec->canAddEntry() == false) {
449 return false;
450 }
451 const size_t secCount = hdrSectionsNum();
452 if (secCount == SectHdrsWrapper::SECT_COUNT_MAX) return false; //limit exceeded
453
454 //TODO: some more checks? overlay?
455 return true;
456}
457
458
460{
461 if (canAddNewSection() == false) return NULL;
462
464 if (!v_size) v_size = size;
465
468 bufsize_t newSize = roundedRawEnd + size;
469 bufsize_t newVirtualSize = roundedVirtualEnd + v_size;
470
471 if (setVirtualSize(newVirtualSize) == false) {
472 Logger::append(Logger::D_ERROR, "Failed to change virtual size");
473 return NULL;
474 }
475
476 if (resize(newSize) == false) {
477 Logger::append(Logger::D_ERROR, "Failed to resize");
478 return NULL;
479 }
480 // fetch again after resize:
481 sec = dynamic_cast<ExeNodeWrapper*>(getWrapper(PEFile::WR_SECTIONS));
482 if (sec == NULL) {
483 return NULL;
484 }
485
486 IMAGE_SECTION_HEADER secHdr;
487 ::memset(&secHdr, 0, sizeof(IMAGE_SECTION_HEADER));
488
489 //name copy:
490 const size_t nameLen = name.length();
491 const size_t bufSize = sizeof(secHdr.Name);
492 const size_t copySize = (nameLen < bufSize) ? nameLen : bufSize;
493 if (copySize) {
494 ::memcpy(secHdr.Name, name.toStdString().c_str(), copySize);
495 }
496
497 secHdr.PointerToRawData = static_cast<DWORD>(roundedRawEnd);
498 secHdr.VirtualAddress = static_cast<DWORD>(roundedVirtualEnd);
499 secHdr.SizeOfRawData = size;
500 secHdr.Misc.VirtualSize = v_size;
501
502 SectionHdrWrapper wr(this, &secHdr);
503 SectionHdrWrapper* secHdrWr = dynamic_cast<SectionHdrWrapper*>(sec->addEntry(&wr));
504 return secHdrWr;
505}
506
508{
509 size_t secCount = this->getSectionsCount(true);
510 if (secCount == 0) return NULL;
511 return this->getSecHdr(secCount - 1);
512}
513
515{
516 offset_t lastMapped = 0;
517
518 /* check sections bounds */
519 const size_t secCounter = this->getSectionsCount(true);
520 if (!secCounter) {
521 // if PE file has no sections, full file will be mapped
522 return getMappedSize(aType);
523 }
524 for (size_t i = 0; i < secCounter; i++) {
525 SectionHdrWrapper *sec = this->getSecHdr(i);
526 if (!sec) continue;
527
528 offset_t secLastMapped= sec->getContentOffset(aType, true);
529 if (secLastMapped == INVALID_ADDR) continue;
530
531 const size_t size = (aType == Executable::RAW) ? sec->getMappedRawSize() : sec->getMappedVirtualSize();
532 if (size == 0) continue; // exclude not mapped sections
533
534 secLastMapped += size;
535 if (secLastMapped > lastMapped) {
536 lastMapped = secLastMapped;
537 }
538 }
539
540 /* check header bounds */
541 /* section headers: */
542 if (lastMapped < this->secHdrsEndOffset()) {
543 lastMapped = this->secHdrsEndOffset();
544 }
545 // PE hdrs ending:
546 const offset_t peHdrsEnd = this->core.peSignatureOffset() + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + this->core.peNtHeadersSize();
547 if (lastMapped < peHdrsEnd) {
548 lastMapped = peHdrsEnd;
549 }
550 // OptionalHdr -> SizeOfHeaders:
551 const offset_t ntHeadersEndOffset = this->core.hdrsSize();
552 if (lastMapped < ntHeadersEndOffset) {
553 lastMapped = ntHeadersEndOffset;
554 }
555 return lastMapped;
556}
557
559{
561 if (secHdr == NULL) return NULL;
562
563 //TODO: check overlay...
564 bufsize_t fullSize = getContentSize();
565 bufsize_t newSize = fullSize + addedSize;
566
567 offset_t secROffset = secHdr->getContentOffset(Executable::RAW, false);
568 if (secROffset == INVALID_ADDR) {
569 return NULL;
570 }
571 const bufsize_t secNewRSize = newSize - secROffset; //include overlay in section
572
573 secHdr->setNumValue(SectionHdrWrapper::RSIZE, uint64_t(secNewRSize));
574
575 const offset_t secVOffset = secHdr->getContentOffset(Executable::RVA, false);
576 const bufsize_t secVSize = secHdr->getContentSize(Executable::RVA, false);
577
578 // if the previous virtual size is smaller than the new raw size, then update it:
579 if (secVSize < secNewRSize) {
580 secHdr->setNumValue(SectionHdrWrapper::VSIZE, uint64_t(secNewRSize));
581
582 // if the virtual size of section has changed,
583 // update the Size of Image (saved in the header):
584 bufsize_t newVSize = secVOffset + secNewRSize;
585 this->setVirtualSize(newVSize);
586 }
587
588 //update raw size:
589 this->resize(newSize);
590 //finally, retrieve the resized section:
591 return getLastSection();
592}
593
595{
596 IMAGE_DATA_DIRECTORY* ddir = this->getDataDirectory();
597 if (ddir[pe::DIR_BOUND_IMPORT].VirtualAddress == 0 && ddir[pe::DIR_BOUND_IMPORT].Size == 0) {
598 // No bound imports already, nothing to do here!
599 return true;
600 }
601 ddir[pe::DIR_BOUND_IMPORT].VirtualAddress = 0;
602 ddir[pe::DIR_BOUND_IMPORT].Size = 0;
603 DataDirEntryWrapper *bImp = this->getDataDirEntry(pe::DIR_BOUND_IMPORT);
604 if (bImp == NULL) {
605 //printf("No Bound imports wrapper!\n");
606 return false; // todo: throw error?
607 }
608 bool isOk = bImp->wrap();
609 //TODO: change timestamp for all library entries from (-1 : BOUND) to 0 : NOT BOUND
610 return isOk;
611}
612
613bool PEFile::dumpSection(SectionHdrWrapper *sec, QString fileName)
614{
616 return false; //not my section
617 }
618 BufferView *secView = this->_createSectionView(sec);
619 if (!secView) return false;
620
621 bufsize_t dumpedSize = FileBuffer::dump(fileName, *secView, false);
622 delete secView;
623
624 return dumpedSize ? true : false;
625}
626
627//protected:
628
630{
632 offset_t start = sec->getContentOffset(aType, true);
633 bufsize_t size = sec->getContentSize(aType, true);
634 if (start == INVALID_ADDR || size == 0) {
635 return NULL;
636 }
637 return new BufferView(this, start, size);
638}
639
640size_t PEFile::getExportsMap(QMap<offset_t,QString> &entrypoints, Executable::addr_type aType)
641{
642 size_t initialSize = entrypoints.size();
643
644 ExportDirWrapper* exports = dynamic_cast<ExportDirWrapper*>(this->getWrapper(PEFile::WR_DIR_ENTRY + pe::DIR_EXPORT));
645 if (!exports) return 0;
646
647 const size_t entriesCnt = exports->getEntriesCount();
648 if (entriesCnt == 0) return 0;
649
650 for (int i = 0; i < entriesCnt; i++) {
651 ExportEntryWrapper* entry = dynamic_cast<ExportEntryWrapper*>(exports->getEntryAt(i));
652 if (!entry) continue;
653
654 QString forwarder = entry->getForwarderStr();
655 if (forwarder.length()) {
656 continue;
657 }
658 offset_t rva = entry->getFuncRva();
659 offset_t offset = this->convertAddr(rva, Executable::RVA, aType);
660 if (offset == INVALID_ADDR) {
661 continue;
662 }
663 entrypoints.insert(offset, entry->getName());
664 }
665 return entrypoints.size() - initialSize;
666}
uint32_t bufsize_t
const offset_t INVALID_ADDR
uint64_t offset_t
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
virtual offset_t getOffset(void *ptr, bool allowExceptions=false)
bool fillContent(BYTE filling)
static bufsize_t dump(const QString &fileName, AbstractByteBuffer &buf, bool allowExceptions=false)
Definition: FileBuffer.cpp:114
virtual Executable::addr_type containsAddrType(size_t fieldId, size_t subField=FIELD_NONE)
virtual bool setNumValue(size_t fieldId, size_t subField, uint64_t val)
virtual bool canCopyToOffset(offset_t rawOffset)
virtual offset_t getFieldOffset(size_t fieldId, size_t subField=FIELD_NONE)
bool copyToOffset(offset_t rawOffset)
virtual uint64_t getNumValue(size_t fieldId, size_t subField, bool *isOk)
virtual bool wrap()
virtual ExeNodeWrapper * getEntryAt(size_t fieldId)
virtual bool canAddEntry()
virtual size_t getEntriesCount()
virtual ExeNodeWrapper * addEntry(ExeNodeWrapper *entry)
std::map< size_t, ExeElementWrapper * > wrappers
Definition: MappedExe.h:27
virtual ExeElementWrapper * getWrapper(size_t wrapperId)
Definition: MappedExe.cpp:13
AbstractByteBuffer * buf
Definition: Executable.h:125
virtual offset_t toRaw(offset_t offset, addr_type addrType, bool allowExceptions=false)
Definition: Executable.cpp:85
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition: Executable.h:57
virtual offset_t convertAddr(offset_t inAddr, Executable::addr_type inType, Executable::addr_type outType)
Definition: Executable.cpp:46
virtual bufsize_t getContentSize()
Definition: Executable.h:51
QString getForwarderStr()
offset_t getFuncRva()
virtual QString getName()
virtual void * getPtr()
virtual bool resize(bufsize_t newSize)
Definition: MappedExe.h:40
virtual void * getPtr()
bufsize_t hdrsSize() const
Definition: PECore.cpp:129
bool wrap(AbstractByteBuffer *v_buf)
Definition: PECore.cpp:13
virtual bufsize_t getImageSize()
Definition: PECore.cpp:117
offset_t peSignatureOffset() const
Definition: PECore.cpp:59
bufsize_t peNtHeadersSize() const
Definition: PECore.cpp:83
IMAGE_DOS_HEADER * dos
Definition: PECore.h:54
virtual Executable * build(AbstractByteBuffer *buf)
Definition: PEFile.cpp:31
virtual bool signatureMatches(AbstractByteBuffer *buf)
Definition: PEFile.cpp:4
Definition: PEFile.h:41
DataDirEntryWrapper * dataDirEntries[pe::DIR_ENTRIES_COUNT]
Definition: PEFile.h:272
size_t getSecIndex(SectionHdrWrapper *sec) const
Definition: PEFile.h:119
virtual bufsize_t getMappedSize(Executable::addr_type aType)
Definition: PEFile.cpp:255
BufferView * _createSectionView(SectionHdrWrapper *sec)
Definition: PEFile.cpp:629
size_t getExportsMap(QMap< offset_t, QString > &entrypoints, Executable::addr_type aType=Executable::RVA)
Definition: PEFile.cpp:640
SectHdrsWrapper * sects
Definition: PEFile.h:269
SectionHdrWrapper * getSecHdr(size_t index) const
Definition: PEFile.h:109
bool unbindImports()
Definition: PEFile.cpp:594
offset_t peDataDirOffset()
Definition: PEFile.cpp:243
virtual offset_t getEntryPoint(Executable::addr_type addrType=Executable::RVA)
Definition: PEFile.cpp:273
offset_t getMinSecRVA()
Definition: PEFile.cpp:231
friend class SectHdrsWrapper
Definition: PEFile.h:274
offset_t getLastMapped(Executable::addr_type aType)
Definition: PEFile.cpp:514
FileHdrWrapper * fHdr
Definition: PEFile.h:267
ResourcesAlbum * album
Definition: PEFile.h:271
bool setEntryPoint(offset_t entry, Executable::addr_type aType)
Definition: PEFile.cpp:288
size_t getSectionsCount(bool useMapped=true) const
Definition: PEFile.cpp:328
pe::RICH_DANS_HEADER * getRichHeaderBgn(pe::RICH_SIGNATURE *sign)
Definition: PEFile.cpp:174
SectionHdrWrapper * extendLastSection(bufsize_t addedSize)
Definition: PEFile.cpp:558
@ WR_DIR_ENTRY
Definition: PEFile.h:51
@ WR_DATADIR
Definition: PEFile.h:49
@ WR_OPTIONAL_HDR
Definition: PEFile.h:48
@ WR_FILE_HDR
Definition: PEFile.h:47
@ WR_DOS_HDR
Definition: PEFile.h:45
@ WR_SECTIONS
Definition: PEFile.h:50
@ WR_RICH_HDR
Definition: PEFile.h:46
PECore core
Definition: PEFile.h:259
void initDirEntries()
Definition: PEFile.cpp:106
virtual void wrap()
Definition: PEFile.cpp:113
SectionHdrWrapper * getLastSection()
Definition: PEFile.cpp:507
OptHdrWrapper * optHdr
Definition: PEFile.h:268
bool dumpSection(SectionHdrWrapper *sec, QString fileName)
Definition: PEFile.cpp:613
virtual offset_t rvaToRaw(offset_t rva)
Definition: PEFile.cpp:363
BufferView * createSectionView(size_t secNum)
Definition: PEFile.cpp:400
bool setHdrSectionsNum(size_t newNum)
Definition: PEFile.cpp:306
bool setVirtualSize(bufsize_t newSize)
Definition: PEFile.cpp:317
DataDirEntryWrapper * getDataDirEntry(pe::dir_entry eType)
Definition: PEFile.cpp:394
bool moveDataDirEntry(pe::dir_entry id, offset_t newOffset, Executable::addr_type addType=Executable::RAW)
Definition: PEFile.cpp:410
virtual void clearWrappers()
Definition: PEFile.cpp:95
IMAGE_DATA_DIRECTORY * getDataDirectory()
Definition: PEFile.cpp:249
virtual offset_t rawToRva(offset_t raw)
Definition: PEFile.cpp:336
bufsize_t hdrsSize()
Definition: PEFile.h:93
offset_t secHdrsEndOffset() const
Definition: PEFile.h:83
size_t hdrSectionsNum() const
Definition: PEFile.cpp:297
PEFile(AbstractByteBuffer *v_buf)
Definition: PEFile.cpp:86
virtual bufsize_t getAlignment(Executable::addr_type aType) const
Definition: PEFile.h:70
pe::RICH_SIGNATURE * getRichHeaderSign()
Definition: PEFile.cpp:201
bool canAddNewSection()
Definition: PEFile.cpp:445
static long computeChecksum(BYTE *buffer, size_t bufferSize, offset_t checksumOffset)
Definition: PEFile.cpp:45
DosHdrWrapper * dosHdrWrapper
Definition: PEFile.h:265
SectionHdrWrapper * getSecHdrAtOffset(offset_t offset, Executable::addr_type aType, bool recalculate=false, bool verbose=false)
Definition: PEFile.h:114
SectionHdrWrapper * addNewSection(QString name, bufsize_t size, bufsize_t v_size=0)
Definition: PEFile.cpp:459
static size_t SECT_COUNT_MAX
static size_t SECT_INVALID_INDEX
bufsize_t getMappedVirtualSize()
bufsize_t getContentSize(Executable::addr_type aType, bool recalculate)
offset_t getContentOffset(Executable::addr_type aType, bool useMapped=true)
bufsize_t getMappedRawSize()
bool append(dbg_level lvl, const char *format,...)
Definition: Util.cpp:8
@ D_ERROR
Definition: Util.h:26
@ D_WARNING
Definition: Util.h:26
@ D_INFO
Definition: Util.h:26
bufsize_t roundupToUnit(bufsize_t size, bufsize_t unit)