5 if (buf == NULL)
return false;
8 WORD *magic = (WORD*) buf->
getContentAt(dosOffset,
sizeof(WORD));
9 if (magic == NULL)
return false;
11 if ((*magic) != pe::S_DOS) {
14 offset_t newOffset = dosOffset + (
sizeof(IMAGE_DOS_HEADER) -
sizeof(LONG));
15 LONG* lfnew = (LONG*) buf->
getContentAt(newOffset,
sizeof(LONG));
20 DWORD *peMagic = (DWORD*) buf->
getContentAt(peOffset,
sizeof(DWORD));
21 if (peMagic == NULL) {
24 if (*peMagic == pe::S_NT) {
46 WORD *wordBuff = (WORD*)buffer;
47 size_t wordSize = bufferSize /
sizeof(WORD);
49 size_t checksumBgn = checksumOffset;
50 size_t checksumEnd = checksumOffset +
sizeof(DWORD);
52 const long long maxVal = ((
long long)1) << 32;
53 long long checksum = 0;
55 for (
int i = 0; i < wordSize; i++) {
56 WORD chunk = wordBuff[i];
58 size_t bI = i *
sizeof(WORD);
59 if (bI >= checksumBgn && bI < checksumEnd) {
60 size_t mask = (checksumEnd - bI) %
sizeof(WORD);
61 size_t shift = (
sizeof(WORD) - mask) * 8;
62 chunk = (chunk >> shift) << shift;
65 checksum = (checksum & 0xffffffff) + chunk + (checksum >> 32);
66 if (checksum > maxVal) {
67 checksum = (checksum & 0xffffffff) + (checksum >> 32);
70 checksum = (checksum & 0xffff) + (checksum >> 16);
71 checksum = (checksum)+(checksum >> 16);
72 checksum = checksum & 0xffff;
73 checksum += bufferSize;
80 :
MappedExe(v_buf,
Executable::BITS_32), dosHdrWrapper(NULL), fHdr(NULL), optHdr(NULL), sects(NULL),
96 for (
size_t i = 0 ; i < pe::DIR_ENTRIES_COUNT; i++) {
145 for (
int i = 0; i < pe::DIR_ENTRIES_COUNT; i++) {
156 if (!richSign)
return NULL;
158 DWORD xorkey = richSign->checksum;
161 pe::RICH_DANS_HEADER* dansHdr = NULL;
163 offset_t offset = richOffset -
sizeof(pe::RICH_DANS_HEADER);
165 dansHdr = (pe::RICH_DANS_HEADER*) this->
getContentAt(offset,
sizeof(pe::RICH_DANS_HEADER));
169 if (dansHdr->dansId == (pe::DANS_HDR_MAGIC ^ xorkey)) {
173 offset -=
sizeof(DWORD);
175 if (!dansHdr || dansHdr->dansId != (pe::DANS_HDR_MAGIC ^ xorkey)) {
183 size_t dosStubOffset = this->
core.
dos->e_lfarlc;
184 size_t dosStubEnd = this->
core.
dos->e_lfanew;
185 const size_t maxSize = dosStubEnd - dosStubOffset;
186 BYTE *dosPtr = this->
getContentAt(dosStubOffset, maxSize);
191 pe::RICH_SIGNATURE* richSign = NULL;
192 size_t toSearchSize = maxSize;
193 const offset_t startOffset = dosStubOffset;
194 const size_t step =
sizeof(DWORD);
196 while (toSearchSize > 0) {
197 richSign = (pe::RICH_SIGNATURE*) this->
getContentAt(startOffset + toSearchSize,
sizeof(pe::RICH_SIGNATURE));
198 if (!richSign)
break;
199 if (richSign->richId == pe::RICH_HDR_MAGIC)
break;
201 toSearchSize -= step;
203 if (!richSign)
return NULL;
204 if (richSign->richId != pe::RICH_HDR_MAGIC) {
240 const size_t PAGE_SIZE = 0x1000;
245 if (vSize < PAGE_SIZE) {
260 if (addrType != epType) {
261 entryPoint = this->
convertAddr(entryPoint, epType, addrType);
268 if (
optHdr == NULL)
return false;
279 if (isOk ==
false)
return 0;
281 return static_cast<size_t> (secNum);
286 uint64_t count = newNum;
288 if (canSet ==
false) {
297 uint64_t size = newSize;
299 if (canSet ==
false) {
308 if (useMapped ==
false) {
353 if (curr >= rawSize) {
357 return bgnRaw + curr;
372 if (eType >= pe::DIR_ENTRIES_COUNT)
return NULL;
394 bool allowExceptions =
true;
398 if (allowExceptions)
throw ExeException(
"No such Data Directory");
403 if (ddirWrapper == NULL || ddir == NULL) {
404 if (allowExceptions)
throw ExeException(
"Cannot fetch DataDirTable");
410 if (allowExceptions)
throw ExeException(
"Invalid new offset");
415 if (allowExceptions)
throw ExeException(
"Cannot copy: no space at such offset");
419 if (allowExceptions)
throw ExeException(
"Cannot copy: error occured");
423 ddir[id].VirtualAddress =
static_cast<DWORD
> (dataDirAddr);
449 bufsize_t newSize = roundedRawEnd + size;
450 bufsize_t newVirtualSize = roundedVirtualEnd + size;
457 if (
resize(newSize) ==
false) {
467 IMAGE_SECTION_HEADER secHdr;
468 memset(&secHdr, 0,
sizeof(IMAGE_SECTION_HEADER));
471 std::string nameStr = name.toStdString();
472 const char *nameChar = nameStr.c_str();
473 size_t copySize =
sizeof(secHdr.Name);
474 size_t nameLen = strlen(nameChar);
475 if (nameLen < copySize) copySize = nameLen;
476 memcpy(secHdr.Name, nameChar, copySize);
478 secHdr.PointerToRawData =
static_cast<DWORD
>(roundedRawEnd);
479 secHdr.VirtualAddress =
static_cast<DWORD
>(roundedVirtualEnd);
480 secHdr.SizeOfRawData = size;
481 secHdr.Misc.VirtualSize = size;
491 if (secCount == 0)
return NULL;
501 for (
size_t i = 0; i < counter; i++) {
506 if (secLastRaw > lastRaw) lastRaw = secLastRaw;
515 if (lastRaw < ntHeadersEndOffset) lastRaw = ntHeadersEndOffset;
522 if (secHdr == NULL)
return NULL;
526 bufsize_t newSize = fullSize + addedSize;
530 bufsize_t secNewRSize = newSize - secROffset;
538 if (secVSize < secNewRSize) {
539 secNewVSize = secNewRSize;
544 bufsize_t newVSize = secVOffset + secNewVSize;
557 if (ddir[pe::DIR_BOUND_IMPORT].VirtualAddress == 0 && ddir[pe::DIR_BOUND_IMPORT].Size == 0) {
561 ddir[pe::DIR_BOUND_IMPORT].VirtualAddress = 0;
562 ddir[pe::DIR_BOUND_IMPORT].Size = 0;
568 bool isOk = bImp->
wrap();
576 size_t initialSize = entrypoints.size();
579 if (!exports)
return 0;
582 if (entriesCnt == 0)
return 0;
584 for(
int i = 0; i < entriesCnt; i++) {
586 if (!entry)
continue;
589 if (forwarder.length()) {
598 entrypoints.insert(offset, entry->
getName());
600 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)
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]
virtual bufsize_t getMappedSize(Executable::addr_type aType)
size_t getExportsMap(QMap< offset_t, QString > &entrypoints, Executable::addr_type aType=Executable::RVA)
SectionHdrWrapper * getSecHdr(size_t index) const
SectionHdrWrapper * addNewSection(QString name, bufsize_t size)
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()
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
virtual bufsize_t getAlignment(Executable::addr_type aType)
PEFile(AbstractByteBuffer *v_buf)
SectionHdrWrapper * getSecHdrAtOffset(offset_t offset, Executable::addr_type aType, bool roundup, bool verbose=false)
static long computeChecksum(BYTE *buffer, size_t bufferSize, size_t checksumOffset)
pe::RICH_SIGNATURE * getRichHeaderSign()
DosHdrWrapper * dosHdrWrapper
static size_t SECT_COUNT_MAX
bufsize_t getContentSize(Executable::addr_type aType, bool roundup)
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)