6 if (buf == NULL)
return false;
9 WORD *magic = (WORD*) buf->
getContentAt(dosOffset,
sizeof(WORD));
10 if (magic == NULL)
return false;
12 if ((*magic) != pe::S_DOS) {
15 offset_t newOffset = dosOffset + (
sizeof(IMAGE_DOS_HEADER) -
sizeof(LONG));
16 LONG* lfnew = (LONG*) buf->
getContentAt(newOffset,
sizeof(LONG));
21 DWORD *peMagic = (DWORD*) buf->
getContentAt(peOffset,
sizeof(DWORD));
22 if (peMagic == NULL) {
25 if (*peMagic == pe::S_NT) {
47 if (!buffer || !bufferSize)
return 0;
49 WORD *wordBuff = (WORD*)buffer;
50 size_t wordSize = bufferSize /
sizeof(WORD);
52 size_t checksumBgn = 0;
53 size_t checksumEnd = 0;
55 checksumBgn = size_t(checksumOffset);
56 checksumEnd = checksumBgn +
sizeof(DWORD);
59 const long long maxVal = ((
long long)1) << 32;
60 long long checksum = 0;
62 for (
int i = 0; i < wordSize; i++) {
63 WORD chunk = wordBuff[i];
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;
72 checksum = (checksum & 0xffffffff) + chunk + (checksum >> 32);
73 if (checksum > maxVal) {
74 checksum = (checksum & 0xffffffff) + (checksum >> 32);
77 checksum = (checksum & 0xffff) + (checksum >> 16);
78 checksum = (checksum)+(checksum >> 16);
79 checksum = checksum & 0xffff;
80 checksum += bufferSize;
87 :
MappedExe(v_buf,
Executable::BITS_32), dosHdrWrapper(NULL), fHdr(NULL), optHdr(NULL), sects(NULL),
108 for (
size_t i = 0 ; i < pe::DIR_ENTRIES_COUNT; i++) {
165 for (
int i = 0; i < pe::DIR_ENTRIES_COUNT; i++) {
176 if (!richSign)
return NULL;
178 DWORD xorkey = richSign->checksum;
181 pe::RICH_DANS_HEADER* dansHdr = NULL;
183 offset_t offset = richOffset -
sizeof(pe::RICH_DANS_HEADER);
185 dansHdr = (pe::RICH_DANS_HEADER*) this->
getContentAt(offset,
sizeof(pe::RICH_DANS_HEADER));
189 if (dansHdr->dansId == (pe::DANS_HDR_MAGIC ^ xorkey)) {
193 offset -=
sizeof(DWORD);
195 if (!dansHdr || dansHdr->dansId != (pe::DANS_HDR_MAGIC ^ xorkey)) {
203 size_t dosStubOffset = this->
core.
dos->e_lfarlc;
204 size_t dosStubEnd = this->
core.
dos->e_lfanew;
205 const size_t maxSize = dosStubEnd - dosStubOffset;
206 BYTE *dosPtr = this->
getContentAt(dosStubOffset, maxSize);
211 pe::RICH_SIGNATURE* richSign = NULL;
212 size_t toSearchSize = maxSize;
213 const offset_t startOffset = dosStubOffset;
214 const size_t step =
sizeof(DWORD);
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;
221 toSearchSize -= step;
223 if (!richSign)
return NULL;
224 if (richSign->richId != pe::RICH_HDR_MAGIC) {
262 const size_t PAGE_SIZE = 0x1000;
267 if (vSize < PAGE_SIZE) {
282 if (addrType != epType) {
283 entryPoint = this->
convertAddr(entryPoint, epType, addrType);
290 if (
optHdr == NULL)
return false;
301 if (isOk ==
false)
return 0;
303 return static_cast<size_t> (secNum);
308 uint64_t count = newNum;
310 if (canSet ==
false) {
319 uint64_t size = newSize;
321 if (canSet ==
false) {
330 if (useMapped ==
false) {
375 if (curr >= rawSize) {
379 return bgnRaw + curr;
396 if (eType >= pe::DIR_ENTRIES_COUNT)
return NULL;
412 bool allowExceptions =
true;
416 if (allowExceptions)
throw ExeException(
"No such Data Directory");
421 if (ddirWrapper == NULL || ddir == NULL) {
422 if (allowExceptions)
throw ExeException(
"Cannot fetch DataDirTable");
428 if (allowExceptions)
throw ExeException(
"Invalid new offset");
433 if (allowExceptions)
throw ExeException(
"Cannot copy: no space at such offset");
437 if (allowExceptions)
throw ExeException(
"Cannot copy: error occured");
441 ddir[id].VirtualAddress =
static_cast<DWORD
> (dataDirAddr);
464 if (!v_size) v_size = size;
468 bufsize_t newSize = roundedRawEnd + size;
469 bufsize_t newVirtualSize = roundedVirtualEnd + v_size;
476 if (
resize(newSize) ==
false) {
486 IMAGE_SECTION_HEADER secHdr;
487 ::memset(&secHdr, 0,
sizeof(IMAGE_SECTION_HEADER));
490 const size_t nameLen = name.length();
491 const size_t bufSize =
sizeof(secHdr.Name);
492 const size_t copySize = (nameLen < bufSize) ? nameLen : bufSize;
494 ::memcpy(secHdr.Name, name.toStdString().c_str(), copySize);
497 secHdr.PointerToRawData =
static_cast<DWORD
>(roundedRawEnd);
498 secHdr.VirtualAddress =
static_cast<DWORD
>(roundedVirtualEnd);
499 secHdr.SizeOfRawData = size;
500 secHdr.Misc.VirtualSize = v_size;
510 if (secCount == 0)
return NULL;
524 for (
size_t i = 0; i < secCounter; i++) {
532 if (size == 0)
continue;
534 secLastMapped += size;
535 if (secLastMapped > lastMapped) {
536 lastMapped = secLastMapped;
547 if (lastMapped < peHdrsEnd) {
548 lastMapped = peHdrsEnd;
552 if (lastMapped < ntHeadersEndOffset) {
553 lastMapped = ntHeadersEndOffset;
561 if (secHdr == NULL)
return NULL;
565 bufsize_t newSize = fullSize + addedSize;
571 const bufsize_t secNewRSize = newSize - secROffset;
579 if (secVSize < secNewRSize) {
584 bufsize_t newVSize = secVOffset + secNewRSize;
597 if (ddir[pe::DIR_BOUND_IMPORT].VirtualAddress == 0 && ddir[pe::DIR_BOUND_IMPORT].Size == 0) {
601 ddir[pe::DIR_BOUND_IMPORT].VirtualAddress = 0;
602 ddir[pe::DIR_BOUND_IMPORT].Size = 0;
608 bool isOk = bImp->
wrap();
619 if (!secView)
return false;
624 return dumpedSize ? true :
false;
642 size_t initialSize = entrypoints.size();
645 if (!exports)
return 0;
648 if (entriesCnt == 0)
return 0;
650 for (
int i = 0; i < entriesCnt; i++) {
652 if (!entry)
continue;
655 if (forwarder.length()) {
663 entrypoints.insert(offset, entry->
getName());
665 return entrypoints.size() - initialSize;
const offset_t INVALID_ADDR
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)
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 ExeNodeWrapper * getEntryAt(size_t fieldId)
virtual bool canAddEntry()
virtual size_t getEntriesCount()
virtual ExeNodeWrapper * addEntry(ExeNodeWrapper *entry)
std::map< size_t, ExeElementWrapper * > wrappers
virtual ExeElementWrapper * getWrapper(size_t wrapperId)
virtual offset_t toRaw(offset_t offset, addr_type addrType, bool allowExceptions=false)
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
virtual offset_t convertAddr(offset_t inAddr, Executable::addr_type inType, Executable::addr_type outType)
virtual bufsize_t getContentSize()
QString getForwarderStr()
virtual QString getName()
virtual bool resize(bufsize_t newSize)
bufsize_t hdrsSize() const
bool wrap(AbstractByteBuffer *v_buf)
virtual bufsize_t getImageSize()
offset_t peSignatureOffset() const
bufsize_t peNtHeadersSize() const
virtual Executable * build(AbstractByteBuffer *buf)
virtual bool signatureMatches(AbstractByteBuffer *buf)
DataDirEntryWrapper * dataDirEntries[pe::DIR_ENTRIES_COUNT]
size_t getSecIndex(SectionHdrWrapper *sec) const
virtual bufsize_t getMappedSize(Executable::addr_type aType)
BufferView * _createSectionView(SectionHdrWrapper *sec)
size_t getExportsMap(QMap< offset_t, QString > &entrypoints, Executable::addr_type aType=Executable::RVA)
SectionHdrWrapper * getSecHdr(size_t index) const
offset_t peDataDirOffset()
virtual offset_t getEntryPoint(Executable::addr_type addrType=Executable::RVA)
friend class SectHdrsWrapper
offset_t getLastMapped(Executable::addr_type aType)
bool setEntryPoint(offset_t entry, Executable::addr_type aType)
size_t getSectionsCount(bool useMapped=true) const
pe::RICH_DANS_HEADER * getRichHeaderBgn(pe::RICH_SIGNATURE *sign)
SectionHdrWrapper * extendLastSection(bufsize_t addedSize)
SectionHdrWrapper * getLastSection()
bool dumpSection(SectionHdrWrapper *sec, QString fileName)
virtual offset_t rvaToRaw(offset_t rva)
BufferView * createSectionView(size_t secNum)
bool setHdrSectionsNum(size_t newNum)
bool setVirtualSize(bufsize_t newSize)
DataDirEntryWrapper * getDataDirEntry(pe::dir_entry eType)
bool moveDataDirEntry(pe::dir_entry id, offset_t newOffset, Executable::addr_type addType=Executable::RAW)
virtual void clearWrappers()
IMAGE_DATA_DIRECTORY * getDataDirectory()
virtual offset_t rawToRva(offset_t raw)
offset_t secHdrsEndOffset() const
size_t hdrSectionsNum() const
PEFile(AbstractByteBuffer *v_buf)
virtual bufsize_t getAlignment(Executable::addr_type aType) const
pe::RICH_SIGNATURE * getRichHeaderSign()
static long computeChecksum(BYTE *buffer, size_t bufferSize, offset_t checksumOffset)
DosHdrWrapper * dosHdrWrapper
SectionHdrWrapper * getSecHdrAtOffset(offset_t offset, Executable::addr_type aType, bool recalculate=false, bool verbose=false)
SectionHdrWrapper * addNewSection(QString name, bufsize_t size, bufsize_t v_size=0)
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,...)
bufsize_t roundupToUnit(bufsize_t size, bufsize_t unit)