-
Notifications
You must be signed in to change notification settings - Fork 10
/
elf.py
executable file
·182 lines (157 loc) · 8.97 KB
/
elf.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import capstone
from disassembler.formats.common.program import CommonProgramDisassemblyFormat
from disassembler.formats.common.section import CommonSectionFormat
from disassembler.formats.common.inst import CommonInstFormat
from disassembler.formats.helpers.exceptions import BadMagicHeaderException
from disassembler.formats.helpers.flags import Flags
from struct import unpack
BYTE = 1 # 8 bits
HWORD = 2 # 16 bits
WORD = 4 # 32 bits
DWORD = 8 # 64 bits
def disassemble(binary, filename=None):
return ELF(binary, filename=filename)
#except: return False
FILETYPE_NAME = 'ELF'
class ELF:
FILETYPE_NAME = 'ELF'
def __init__(self, binary, filename=None):
magic_offset = 0
if not binary[magic_offset : magic_offset + WORD] == '\x7FELF':
raise BadMagicHeaderException()
self.binary = binary
self.filename = filename
offset = 4
self.bin_class = capstone.CS_MODE_32 if unpack('B', self.binary[offset : offset + BYTE])[0] == 1 else capstone.CS_MODE_64
self.word = ('Q' if self.bin_class == capstone.CS_MODE_64 else 'I', DWORD if self.bin_class == capstone.CS_MODE_64 else WORD)
offset = 5
self.endian = capstone.CS_MODE_LITTLE_ENDIAN if unpack('B', self.binary[offset : offset + BYTE])[0] == 1 else capstone.CS_MODE_BIG_ENDIAN
self.end = '<' if self.endian == capstone.CS_MODE_LITTLE_ENDIAN else '>'
offset = 7
os_values = {
0x00 : 'System V',
0x01 : 'HP-UX',
0x02 : 'NetBSD',
0x03 : 'Linux',
0x06 : 'Solaris',
0x07 : 'AIX',
0x08 : 'IRIX',
0x09 : 'FreeBSD',
0x0C : 'OpenBSD'
}
self.os = os_values[unpack('B', self.binary[offset : offset + BYTE])[0]]
offset = 0x10
type_values = {
1 : 'relocatable',
2 : 'executable',
3 : 'shared',
4 : 'core'
}
self.type = type_values[unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]]
offset = 0x12
arch_values = {
# 0x02 : capstone.CS_ARCH_SPARC, Next version will have sparc
0x03 : capstone.CS_ARCH_X86,
0x08 : capstone.CS_ARCH_MIPS,
0x14 : capstone.CS_ARCH_PPC,
0x28 : capstone.CS_ARCH_ARM,
0x32 : False, # IA_64 not yet supported in capstone
0x3E : capstone.CS_ARCH_X86, # This is actually x86_64 which I think is taken care of by the CS_MODE_64
0xB7 : capstone.CS_ARCH_ARM64
}
self.arch = arch_values[unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]]
offset = 0x18
self.entry_point = unpack(self.end + self.word[0], self.binary[offset : offset + self.word[1]])[0]
offset += self.word[1]
self.program_header_offset = unpack(self.end + self.word[0], self.binary[offset : offset + self.word[1]])[0]
offset += self.word[1]
self.section_header_offset = unpack(self.end + self.word[0], self.binary[offset : offset + self.word[1]])[0]
offset += self.word[1]
self.flags = unpack(self.end + 'I', self.binary[offset : offset + WORD])[0]
offset += WORD
self.header_size = unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]
offset += HWORD
self.program_header_entry_size = unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]
offset += HWORD
self.program_header_entry_num = unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]
offset += HWORD
self.section_header_entry_size = unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]
offset += HWORD
self.section_header_entry_num = unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]
offset += HWORD
self.section_header_str_index = unpack(self.end + 'H', self.binary[offset : offset + HWORD])[0]
self.parseSections()
def parseSections(self):
self.sections = []
section_names = Section(self, self.section_header_str_index, self.section_header_entry_size, self.word)
section_names_strings = self.binary[section_names.sh_offset : section_names.sh_offset + section_names.sh_size]
for i in xrange(self.section_header_entry_num):
section = Section(self, i, self.section_header_entry_size, self.word)
section.sh_name_string = section_names_strings[section.sh_name:].split('\x00')[0]
self.sections.append(section)
self.sections.sort(key=lambda i: i.sh_offset)
def disassemble(self, settings_manager):
md = capstone.Cs(self.arch, self.bin_class)
disassembly = CommonProgramDisassemblyFormat(self.getProgramInfo(), settings_manager)
for s in self.sections:
section = None
sCODE = self.binary[s.sh_offset : s.sh_offset + s.sh_size]
if len(s.sh_name_string) == 0:
continue
elif s.sh_name_string in ELF.dont_disassemble:
section = CommonSectionFormat(disassembly, s.sh_name_string, self.arch, self.bin_class, s.sh_addr, Flags("r--"), bytes=sCODE) #TODO: make flags more accurate
else:
section = CommonSectionFormat(disassembly, s.sh_name_string, self.arch, self.bin_class, s.sh_addr, Flags("rwx")) #TODO: make flags more accurate
# linear sweep (for now)
for inst in md.disasm(sCODE, s.sh_addr):
section.addInst(CommonInstFormat(inst.address, inst.mnemonic, inst.op_str, inst.bytes))
section.searchForStrings()
section.searchForFunctions()
section.addStringLabels()
section.addFunctionLabels()
disassembly.addSection(section.sort())
return disassembly
def getArchName(self):
arc = None
if self.arch == capstone.CS_ARCH_X86:
arc = "x86"
elif self.arch == capstone.CS_ARCH_PPC:
arc = "PPC"
elif self.arch == capstone.CS_ARCH_ARM:
arc = "ARM"
elif self.arch == capstone.CS_ARCH_ARM64:
arc = "ARM64"
return arc
def getProgramInfo(self):
import hashlib
fields = {
"File MD5": hashlib.md5(self.binary).hexdigest(),
"File Format": self.FILETYPE_NAME,
"Architecture": self.getArchName(),
"Architecture Mode": "32-bit" if self.bin_class == capstone.CS_MODE_32 else "64-bit",
"Entry Point": "0x{:x}".format(self.entry_point),
}
if self.filename != None:
fields["Filename"] = self.filename
max_length = max(len(x) + len(fields[x]) for x in fields) + 2
program_info = "#"*(max_length+12) + "\n"
for x in fields:
program_info += "###"
program_info += (" {:<%d} " % max_length).format(x + ": " + fields[x])
program_info += "###\n"
program_info += "#"*(max_length+12) + "\n"
return program_info
dont_disassemble = ['.comment','.shstrtab','.symtab','.strtab','.note.ABI-tag','.note.gnu.build-id','.hash','.gnu.hash','.dynsym','.dynstr','.gnu.version','.gnu.version_r','.rodata','.eh_frame','.init_array','.jcr','.dynamic','.got','.got.plt','.data','.bss','.interp','.eh_frame_hdr','.plt','.init','.rel.plt','.rel.dyn','.rela.plt','.rela.dyn']
class Section:
def __init__(self, elf, index, entry_size, word):
entry_offset = elf.section_header_offset + index * entry_size
self.sh_name = unpack(elf.end + 'I', elf.binary[entry_offset : entry_offset + 4])[0] # Always 4 bytes
self.sh_type = unpack(elf.end + 'I', elf.binary[entry_offset + 4 : entry_offset + 8])[0] # Always 4 bytes
self.sh_flags = unpack(elf.end + word[0], elf.binary[entry_offset + 8 : entry_offset + 8 + word[1]])[0] # 32 or 64 bits
self.sh_addr = unpack(elf.end + word[0], elf.binary[entry_offset + 8 + word[1] : entry_offset + 8 + word[1] * 2])[0] # 32 or 64 bits
self.sh_offset = unpack(elf.end + word[0], elf.binary[entry_offset + 8 + word[1] * 2: entry_offset + 8 + word[1] * 3])[0] # 32 or 64 bits
self.sh_size = unpack(elf.end + word[0], elf.binary[entry_offset + 8 + word[1] * 3 : entry_offset + 8 + word[1] * 4])[0] # 32 or 64 bits
self.sh_link = unpack(elf.end + 'I', elf.binary[entry_offset + 8 + word[1] * 4 : entry_offset + 12 + word[1] * 4])[0] # Always 4 bytes
self.sh_info = unpack(elf.end + 'I', elf.binary[entry_offset + 12 + word[1] * 4 : entry_offset + 16 + word[1] * 4])[0] # Always 4 bytes
self.sh_addralign = unpack(elf.end + word[0], elf.binary[entry_offset + 16 + word[1] * 4 : entry_offset + 16 + word[1] * 5])[0] # 32 or 64 bits
self.sh_entrsize = unpack(elf.end + word[0], elf.binary[entry_offset + 16 + word[1] * 5 : entry_offset + 16 + word[1] * 6])[0] # 32 or 64 bits