about summary refs log tree commit diff stats
path: root/example/loader/minidump_to_pe.py
blob: 30a953254d4a2e7af365d4a6bad800f4fd7e62a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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))