Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions parser/include/bearparser/pe/ResourceDirWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "rsrc/ResourceContentFactory.h"

#include <map>
#include <set>
#include <vector>


Expand All @@ -25,9 +26,9 @@ class ResourceDirWrapper : public DataDirEntryWrapper
FIELD_COUNTER
};

ResourceDirWrapper(PEFile* pe, ResourcesAlbum *resAlbum = NULL, offset_t rawOffset = 0, long depth = 0, long topEntryId = TOP_ENTRY_ROOT)
ResourceDirWrapper(PEFile* pe, ResourcesAlbum *resAlbum = NULL, offset_t rawOffset = 0, long depth = 0, long topEntryId = TOP_ENTRY_ROOT, ResourceDirWrapper *rootDir = NULL)
: DataDirEntryWrapper(pe, pe::DIR_RESOURCE),
rawOff(rawOffset), dirDepth(depth), album(resAlbum), topEntryID(topEntryId)
rawOff(rawOffset), dirDepth(depth), album(resAlbum), topEntryID(topEntryId), root(rootDir ? rootDir : this), entriesLeft(0)
{
wrap();
}
Expand Down Expand Up @@ -55,12 +56,33 @@ class ResourceDirWrapper : public DataDirEntryWrapper
IMAGE_RESOURCE_DIRECTORY* mainResourceDir();
ResourcesAlbum* getAlbumPtr() { return album; }

/* Registers the directory at the given raw offset as expanded in the tree
* this directory belongs to. Returns false if it was already expanded:
* a directory is parsed at most once, so entries that form cycles or share
* children do not make the tree grow exponentially. */
bool markExpanded(offset_t dirRawOffset) { return root->expandedDirs.insert(dirRawOffset).second; }

/* Takes one entry from the budget of the tree. Returns false when the budget is spent.
* The budget is the number of entries that fit in the file: in a well-formed tree
* every entry occupies its own bytes, but directories of a malformed file may overlap
* and reuse the same bytes as entries of many other directories. */
bool takeEntry()
{
if (root->entriesLeft == 0) return false;
root->entriesLeft--;
return true;
}
ResourceDirWrapper* getRoot() { return root; }

private:
IMAGE_RESOURCE_DIRECTORY* resourceDir();
offset_t rawOff;
long dirDepth;
ResourcesAlbum *album;
long topEntryID;
ResourceDirWrapper *root; // the top directory of this tree (this, for the top directory itself)
std::set<offset_t> expandedDirs; // used only in the root
size_t entriesLeft; // used only in the root
};

class ResourceEntryWrapper : public PENodeWrapper
Expand Down
13 changes: 12 additions & 1 deletion parser/pe/ResourceDirWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,18 @@ bool ResourceDirWrapper::wrap()
}
IMAGE_RESOURCE_DIRECTORY* dir = resourceDir();
if (dir == NULL) return false;
if (this->root == this) {
// the top directory is the first one to be expanded (it is addressed by rawOff == 0)
this->expandedDirs.clear();
this->entriesLeft = static_cast<size_t>(m_Exe->getRawSize()) / sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY);
markExpanded(getOffset(mainResourceDir()));
}
size_t namesNum = dir->NumberOfNamedEntries;
size_t idsNum = dir->NumberOfIdEntries;

size_t totalEntries = namesNum + idsNum;
for (size_t i = 0; i < totalEntries && i < MAX_ENTRIES; i++ ) {
if (!takeEntry()) break;

long topDirId = (this->topEntryID != TOP_ENTRY_ROOT) ? this->topEntryID : long(i) ;

Expand Down Expand Up @@ -183,7 +190,11 @@ bool ResourceEntryWrapper::wrap()
if (this->isDir()) {
long depth = this->parentDir->getDepth() + 1;
if (depth >= MAX_DEPTH) return false;
this->childDir = new ResourceDirWrapper(this->m_PE, this->album, childRaw, depth, topEntryID);
// A malformed file may make directories point back at themselves (or at the top directory),
// or make many entries point at the same child. Expand every directory only once;
// otherwise the tree grows exponentially with the depth.
if (!this->parentDir->markExpanded(childRaw)) return false;
this->childDir = new ResourceDirWrapper(this->m_PE, this->album, childRaw, depth, topEntryID, this->parentDir->getRoot());
} else {
this->childLeaf = new ResourceLeafWrapper(m_Exe, childRaw, topEntryID);
if (this->album != NULL) {
Expand Down
46 changes: 46 additions & 0 deletions tests/gen_rsrc_poc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""Generates synthetic PE32 files with malformed resource directories (no malware).
usage: gen_rsrc_poc.py {selfloop|chain|overlap} OUT [ENTRIES]"""
import struct, sys
mode, out = sys.argv[1], sys.argv[2]
N = int(sys.argv[3]) if len(sys.argv) > 3 else 100
DIR = 0x80000000
def rdir(entries):
b = struct.pack('<IIHHHH', 0, 0, 0, 0, 0, len(entries))
for i, off in enumerate(entries):
b += struct.pack('<II', i, off)
return b
dsz = 16 + 8 * N
if mode == 'selfloop':
rs = rdir([DIR | 0] * N)
elif mode == 'chain':
D = 5
rs = b''
for k in range(D):
last = (k == D - 1)
tgt = (D * dsz) if last else (DIR | ((k + 1) * dsz))
rs += rdir([tgt] * N)
rs += struct.pack('<IIII', 0x1000 + len(rs) + 16, 4, 0, 0) + b'\0' * 4 # data entry + payload
elif mode == 'overlap':
# entry j points at a directory whose header starts on entry j of the root:
# its entry counts are the words of the next entry's OffsetToData (high word >= 0x8000)
rs = rdir([DIR | (16 + 8 * j) for j in range(N)])
rs += b'\0' * 16
else:
raise SystemExit('unknown mode')
rs = rs.ljust((len(rs) + 0x1ff) & ~0x1ff, b'\0')
hdr = bytearray(0x200)
hdr[0:2] = b'MZ'; struct.pack_into('<I', hdr, 0x3c, 0x40)
p = 0x40
hdr[p:p+4] = b'PE\0\0'
struct.pack_into('<HHIIIHH', hdr, p+4, 0x14c, 1, 0, 0, 0, 0xE0, 0x102)
o = p + 24
struct.pack_into('<HBBIIIIIIIIIHHHHHHIIIIHHIIIIII', hdr, o,
0x10b, 0, 0, 0, 0, 0, 0, 0, 0x1000, 0x400000, 0x1000, 0x200,
4, 0, 0, 0, 4, 0, 0, 0x2000, 0x200, 0, 3, 0, 0x100000, 0x1000, 0x100000, 0x1000, 0, 16)
dd = o + 96
struct.pack_into('<II', hdr, dd + 2 * 8, 0x1000, len(rs))
s = o + 0xE0
hdr[s:s+8] = b'.rsrc\0\0\0'
struct.pack_into('<IIIIIIHHI', hdr, s + 8, len(rs), 0x1000, len(rs), 0x200, 0, 0, 0, 0, 0x40000040)
open(out, 'wb').write(bytes(hdr) + rs)