diff options
Diffstat (limited to 'example/loader')
| -rw-r--r-- | example/loader/build_pe.py | 33 | ||||
| -rw-r--r-- | example/loader/minidump_to_pe.py | 48 |
2 files changed, 81 insertions, 0 deletions
diff --git a/example/loader/build_pe.py b/example/loader/build_pe.py new file mode 100644 index 00000000..6baeb645 --- /dev/null +++ b/example/loader/build_pe.py @@ -0,0 +1,33 @@ +#! /usr/bin/env python + +from miasm.loader.pe_init import PE + +# Build an empty PE object +pe_object = PE() + +# Add a section with a just a "RET" +payload = b"\xc3" +s_text = pe_object.SHList.add_section( + name="text", addr=0x1000, rawsize=0x1000, data=payload +) + +# Set the entry point on this instruction +pe_object.Opthdr.AddressOfEntryPoint = s_text.addr + +# Add some imports +new_dll = [ + ({"name": "kernel32.dll", + "firstthunk": s_text.addr + 0x100}, + ["CreateFileA", "SetFilePointer", "WriteFile", "CloseHandle"] + ), + ({"name": "USER32.dll", + "firstthunk": None}, + ["SetDlgItemInt", "GetMenu", "HideCaret"] + ) +] +pe_object.DirImport.add_dlldesc(new_dll) +s_myimp = pe_object.SHList.add_section(name="myimp", rawsize=0x1000) +pe_object.DirImport.set_rva(s_myimp.addr) + +# Rebuild the PE and dump it to a file +open('fresh_pe.exe', 'wb').write(bytes(pe_object)) diff --git a/example/loader/minidump_to_pe.py b/example/loader/minidump_to_pe.py new file mode 100644 index 00000000..30a95325 --- /dev/null +++ b/example/loader/minidump_to_pe.py @@ -0,0 +1,48 @@ +#! /usr/bin/env python +"""Minidump to PE example""" + +import sys + +from future.utils import viewvalues + +from miasm.loader.minidump_init import Minidump +from miasm.loader.pe_init import PE + +minidump = Minidump(open(sys.argv[1], 'rb').read()) + +pe = PE() +for i, memory in enumerate(sorted(viewvalues(minidump.memory), + key=lambda x:x.address)): + # Get section name + name = str(memory.name) + if not name: + name = "s_%02d" % i + else: + name = name.split('\\')[-1] + + # Get section protection + protect = memory.pretty_protect + protect_mask = 0x20 + if protect == "UNKNOWN": + protect_mask |= 0xe0000000 + else: + if "EXECUTE" in protect: + protect_mask |= 1 << 29 + if "READ" in protect: + protect_mask |= 1 << 30 + if "WRITE" in protect: + protect_mask |= 1 << 31 + + # Add the section + pe.SHList.add_section(name=name, addr=memory.address, rawsize=memory.size, + data=memory.content, flags=protect_mask) + +# Find entry point +try: + entry_point = minidump.threads.Threads[0].ThreadContext.Eip[0] +except AttributeError: + entry_point = minidump.threads.Threads[0].ThreadContext.Rip[0] + +pe.Opthdr.AddressOfEntryPoint = entry_point + +open("out_pe.bin", "wb").write(bytes(pe)) |