about summary refs log tree commit diff stats
path: root/example/loader/minidump_to_pe.py
diff options
context:
space:
mode:
authorserpilliere <serpilliere@users.noreply.github.com>2019-05-10 12:55:21 +0200
committerGitHub <noreply@github.com>2019-05-10 12:55:21 +0200
commitc922e7067018a8ec13082fa9a268f70b026b0ad7 (patch)
tree32e07d366d08451db96c15b5d5627e3abad92531 /example/loader/minidump_to_pe.py
parent82ec0ab9b24553c894540bb1f560df2cf062679b (diff)
parent178b202120a22b7ca92c137c363ebb15d843a726 (diff)
downloadmiasm-c922e7067018a8ec13082fa9a268f70b026b0ad7.tar.gz
miasm-c922e7067018a8ec13082fa9a268f70b026b0ad7.zip
Merge pull request #1036 from commial/refactor/example-loader
Refactor/example loader
Diffstat (limited to 'example/loader/minidump_to_pe.py')
-rw-r--r--example/loader/minidump_to_pe.py48
1 files changed, 48 insertions, 0 deletions
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))