Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
malwarefrank committed Mar 23, 2024
1 parent 096de1b commit 0d6bd0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
7 changes: 7 additions & 0 deletions examples/dndump.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ def render_pe(ostream: Formatter, dn):
value = v
elif isinstance(v, int):
value = "0x%x" % (v)
elif isinstance(v, dnfile.stream.HeapItemString):
if v.value is None:
value = "(invalid){!r}".format(v.value_bytes)
else:
value = v.value
elif isinstance(v, dnfile.stream.HeapItemBinary):
value = v.value_bytes()
else:
value = str(v)
rows.append(("%s:" % (field), value))
Expand Down
22 changes: 12 additions & 10 deletions examples/dnstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ def show_strings(fname):
offset = 1
# while there is still data in the stream
while offset < size:
# check if we are at padding bytes near end of stream
if offset + 4 >= size:
if b"\x00" == dn.get_data(us.rva + offset, 1):
break
# read the raw string bytes, and provide number of bytes read (includes encoded length)
ret = us.get_with_size(offset)
if ret is None:
item = us.get(offset)
if item is None:
print(f"Bad string: offset=0x{offset:08x}")
break

buf, readlen = ret
try:
# convert to a UserString object
s = dnfile.stream.UserString(buf)
if item.value is None:
print(f"Bad string: {item.raw_data}")
else:
# display the decoded string
print(s.value)
except UnicodeDecodeError:
print(f"Bad string: {buf}")
print(item.value)
# continue to next entry
offset += readlen
offset += item.raw_size


# for each filepath provided on command-line
Expand Down

0 comments on commit 0d6bd0d

Please sign in to comment.