-
Notifications
You must be signed in to change notification settings - Fork 10
/
pil-splitter.py
144 lines (115 loc) · 3.52 KB
/
pil-splitter.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/bin/env python
"""
Splits elf files into segments.
If the elf is signed, the elf headers and the hash segment are output to
the *.mdt file, and then the segments are output to *.b<n> files.
If the elf isn't signed each segment is output to a *.b<n> file and the
elf headers are output to the *.mdt file.
"""
import sys
import struct
def die(message):
print message
exit(1)
def usage():
print "Usage: %s <elf> <prefix>" % sys.argv[0]
exit(1)
def dump_data(input, output, start, size):
"""Dump 'size' bytes from 'input' at 'start' into newfile 'output'"""
if size == 0:
return
input.seek(start)
outFile = open(output, 'wb')
outFile.write(input.read(size))
outFile.close()
print 'BIN %s' % output
def append_data(input, output, start, size):
"""Append 'size' bytes from 'input' at 'start' to 'output' file"""
if size == 0:
return
input.seek(start)
outFile = open(output, 'ab')
outFile.write(input.read(size))
outFile.close()
def gen_struct(format, image):
"""Generates a dictionary from the format tuple by reading image"""
str = "<%s" % "".join([x[1] for x in format])
elems = struct.unpack(str, image.read(struct.calcsize(str)))
keys = [x[0] for x in format]
return dict(zip(keys, elems))
def parse_metadata(image):
"""Parses elf header metadata"""
metadata = {}
elf32_hdr = [
("ident", "16s"),
("type", "H"),
("machine", "H"),
("version", "I"),
("entry", "I"),
("phoff", "I"),
("shoff", "I"),
("flags", "I"),
("ehsize", "H"),
("phentsize", "H"),
("phnum", "H"),
("shentsize", "H"),
("shnum", "H"),
("shstrndx", "H"),
]
elf32_hdr = gen_struct(elf32_hdr, image)
metadata['num_segments'] = elf32_hdr['phnum']
metadata['pg_start'] = elf32_hdr['phoff']
elf32_phdr = [
("type", "I"),
("offset", "I"),
("vaddr", "I"),
("paddr", "I"),
("filesz", "I"),
("memsz", "I"),
("flags", "I"),
("align", "I"),
]
print "pg_start = 0x%08x" % metadata['pg_start']
metadata['segments'] = []
for i in xrange(metadata['num_segments']):
poff = metadata['pg_start'] + (i * elf32_hdr['phentsize'])
image.seek(poff)
phdr = gen_struct(elf32_phdr, image)
metadata['segments'].append(phdr)
phdr['hash'] = (phdr['flags'] & (0x7 << 24)) == (0x2 << 24)
print "["+str(i)+"] %08x" % poff," offset =",phdr['offset']," size =",phdr['filesz']
return metadata
def dump_metadata(metadata, image, name):
"""Creates <name>.mdt file from elf metadata"""
name = "%s.mdt" % name
# Dump out the elf header
dump_data(image, name, 0, 52)
# Append the program headers
append_data(image, name, metadata['pg_start'], 32 * metadata['num_segments'])
for seg in metadata['segments']:
if seg['hash']:
append_data(image, name, seg['offset'], seg['filesz'])
break
def dump_segments(metadata, image, name):
"""Creates <name>.bXX files for each segment"""
for i, seg in enumerate(metadata['segments']):
start = seg['offset']
size = seg['filesz']
output = "%s.b%02d" % (name, i)
dump_data(image, output, start, size)
def is_elf(file):
"""Verifies a file as being an ELF file"""
file.seek(0)
magic = file.read(4)
image.seek(0)
return magic == '\x7fELF'
if __name__ == "__main__":
if len(sys.argv) != 3:
usage()
image = open(sys.argv[1], 'rb')
if not is_elf(image):
usage()
prefix = sys.argv[2]
metadata = parse_metadata(image)
dump_metadata(metadata, image, prefix)
dump_segments(metadata, image, prefix)