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 WORD *wordBuff = (WORD*)buffer;
48 size_t wordSize = bufferSize /
sizeof(WORD);
50 size_t checksumBgn = checksumOffset;
51 size_t checksumEnd = checksumOffset +
sizeof(DWORD);
53 const long long maxVal = ((
long long)1) << 32;
54 long long checksum = 0;
56 for (
int i = 0; i < wordSize; i++) {
57 WORD chunk = wordBuff[i];
59 size_t bI = i *
sizeof(WORD);
60 if (bI >= checksumBgn && bI < checksumEnd) {
61 size_t mask = (checksumEnd - bI) %
sizeof(WORD);
62 size_t shift = (
sizeof(WORD) - mask) * 8;
63 chunk = (chunk >> shift) << shift;
66 checksum = (checksum & 0xffffffff) + chunk + (checksum >> 32);
67 if (checksum > maxVal) {
68 checksum = (checksum & 0xffffffff) + (checksum >> 32);
71 checksum = (checksum & 0xffff) + (checksum >> 16);
72 checksum = (checksum)+(checksum >> 16);
73 checksum = checksum & 0xffff;
74 checksum += bufferSize;
81 :
MappedExe(v_buf,
Executable::BITS_32), dosHdrWrapper(NULL), fHdr(NULL), optHdr(NULL), sects(NULL),
97 for (
size_t i = 0 ; i < pe::DIR_ENTRIES_COUNT; i++) {
151 for (
int i = 0; i < pe::DIR_ENTRIES_COUNT; i++) {
162 if (!richSign)
return NULL;
164 DWORD xorkey = richSign->checksum;
167 pe::RICH_DANS_HEADER* dansHdr = NULL;
169 offset_t offset = richOffset -
sizeof(pe::RICH_DANS_HEADER);
171 dansHdr = (pe::RICH_DANS_HEADER*) this->
getContentAt(offset,
sizeof(pe::RICH_DANS_HEADER));
175 if (dansHdr->dansId == (pe::DANS_HDR_MAGIC ^ xorkey)) {
179 offset -=
sizeof(DWORD);
181 if (!dansHdr || dansHdr->dansId != (pe::DANS_HDR_MAGIC ^ xorkey)) {
189 size_t dosStubOffset = this->
core.
dos->e_lfarlc;
190 size_t dosStubEnd = this->
core.
dos->e_lfanew;
191 const size_t maxSize = dosStubEnd - dosStubOffset;
192 BYTE *dosPtr = this->
getContentAt(dosStubOffset, maxSize);
197 pe::RICH_SIGNATURE* richSign = NULL;
198 size_t toSearchSize = maxSize;
199 const offset_t startOffset = dosStubOffset;
200 const size_t step =
sizeof(DWORD);
202 while (toSearchSize > 0) {
203 richSign = (pe::RICH_SIGNATURE*) this->
getContentAt(startOffset + toSearchSize,
sizeof(pe::RICH_SIGNATURE));
204 if (!richSign)
break;
205 if (richSign->richId == pe::RICH_HDR_MAGIC)
break;
207 toSearchSize -= step;
209 if (!richSign)
return NULL;
210 if (richSign->richId != pe::RICH_HDR_MAGIC) {
248 const size_t PAGE_SIZE = 0x1000;
253 if (vSize < PAGE_SIZE) {
268 if (addrType != epType) {
269 entryPoint = this->
convertAddr(entryPoint, epType, addrType);
276 if (
optHdr == NULL)
return false;
287 if (isOk ==
false)
return 0;
289 return static_cast<size_t> (secNum);
294 uint64_t count = newNum;
296 if (canSet ==
false) {
305 uint64_t size = newSize;
307 if (canSet ==
false) {
316 if (useMapped ==
false) {
361 if (curr >= rawSize) {
365 return bgnRaw + curr;
382 if (eType >= pe::DIR_ENTRIES_COUNT)
return NULL;
398 bool allowExceptions =
true;
402 if (allowExceptions)
throw ExeException(
"No such Data Directory");
407 if (ddirWrapper == NULL || ddir == NULL) {
408 if (allowExceptions)
throw ExeException(
"Cannot fetch DataDirTable");
414 if (allowExceptions)
throw ExeException(
"Invalid new offset");
419 if (allowExceptions)
throw ExeException(
"Cannot copy: no space at such offset");
423 if (allowExceptions)
throw ExeException(
"Cannot copy: error occured");
427 ddir[id].VirtualAddress =
static_cast<DWORD
> (dataDirAddr);
450 if (!v_size) v_size = size;
454 bufsize_t newSize = roundedRawEnd + size;
455 bufsize_t newVirtualSize = roundedVirtualEnd + v_size;
462 if (
resize(newSize) ==
false) {
472 IMAGE_SECTION_HEADER secHdr;
473 memset(&secHdr, 0,
sizeof(IMAGE_SECTION_HEADER));
476 std::string nameStr = name.toStdString();
477 const char *nameChar = nameStr.c_str();
478 size_t copySize =
sizeof(secHdr.Name);
479 size_t nameLen = strlen(nameChar);
480 if (nameLen < copySize) copySize = nameLen;
481 memcpy(secHdr.Name, nameChar, copySize);
483 secHdr.PointerToRawData =
static_cast<DWORD
>(roundedRawEnd);
484 secHdr.VirtualAddress =
static_cast<DWORD
>(roundedVirtualEnd);
485 secHdr.SizeOfRawData = size;
486 secHdr.Misc.VirtualSize = v_size;
496 if (secCount == 0)
return NULL;
510 for (
size_t i = 0; i < secCounter; i++) {
518 secLastMapped += size;
519 if (secLastMapped > lastMapped) {
520 lastMapped = secLastMapped;
531 if (lastMapped < ntHeadersEndOffset) {
532 lastMapped = ntHeadersEndOffset;
540 if (secHdr == NULL)
return NULL;
544 bufsize_t newSize = fullSize + addedSize;
548 bufsize_t secNewRSize = newSize - secROffset;
556 if (secVSize < secNewRSize) {
557 secNewVSize = secNewRSize;
562 bufsize_t newVSize = secVOffset + secNewVSize;
575 if (ddir[pe::DIR_BOUND_IMPORT].VirtualAddress == 0 && ddir[pe::DIR_BOUND_IMPORT].Size == 0) {
579 ddir[pe::DIR_BOUND_IMPORT].VirtualAddress = 0;
580 ddir[pe::DIR_BOUND_IMPORT].Size = 0;
586 bool isOk = bImp->
wrap();
597 if (!secView)
return false;
602 return dumpedSize ? true :
false;
620 size_t initialSize = entrypoints.size();
623 if (!exports)
return 0;
626 if (entriesCnt == 0)
return 0;
628 for (
int i = 0; i < entriesCnt; i++) {
630 if (!entry)
continue;
633 if (forwarder.length()) {
641 entrypoints.insert(offset, entry->
getName());
643 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
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
static long computeChecksum(BYTE *buffer, size_t bufferSize, size_t checksumOffset)
pe::RICH_SIGNATURE * getRichHeaderSign()
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)