-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (129 loc) · 4.4 KB
/
main.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
import struct
import ir
import graphs
import macho
import elf
import ssa
import annotations
class Binparser:
def __init__(self, filename):
self.filename = filename
self.binformat = None
self.data = ""
self.memory = ir.memory()
self.parse()
self.entry_points = self.find_entry_points()
def parse(self):
data = open(self.filename).read()
if "ELF" in data[:4]:
self.data = data
kipler = elf.Elf(data)
codesegments = []
for s in kipler.Phdrs:
if s.type == elf.PT_LOAD:
seg = ir.segment(s.vaddr, s.vaddr+s.memsz,\
data[s.offset:s.offset+s.filesz] + "\x00"*(s.memsz-s.filesz),\
s.flags, s.flags)
if s.flags & elf.PF_X:
seg.code = 1
self.memory.add(seg)
if kipler.e_machine == elf.EM_MIPS:
self.architecture = "MIPS"
elif kipler.e_machine == elf.EM_386:
self.architecture = "386"
self.binformat = kipler
elif data[:4] in ["\xca\xfe\xba\xbe", "\xce\xfa\xed\xfe"]:
macho_object = macho.macho(self.filename)
self.data = macho_object.read()
macho_header = macho_object.macho_header
#only 386 for now
self.architecture = "386"
for cmd in macho_header.commands:
if type(cmd[1]) == macho.SEGMENT_COMMAND:
seg = ir.segment(cmd[1].vmaddr, cmd[1].vmaddr+cmd[1].vmsize,
self.data[cmd[1].fileoff : cmd[1].fileoff + cmd[1].filesize],
cmd[1].initprot)
if seg.prot & macho.EXEC:
seg.code = 1
self.memory.add( seg )
self.binformat = macho_header
self.binformat.name = "macho"
else:
raise Exception("- unknown binary format"+`data[:4]`)
def find_entry_points(self):
if self.binformat.name == "ELF":
entries = [self.binformat.e_entry]
#add .ctors and .dtors
for shdr in self.binformat.Shdrs:
if shdr.strname in ['.ctors','.dtors']:
ptr_table = self.memory.getrange(shdr.addr, shdr.addr+shdr.size)
if len(ptr_table)%4:
print "[-] UHOH, invalid ctor/dtor (unaligned size)"
break
okay = False
for i in range(0, len(ptr_table), 4):
addr = struct.unpack(self.binformat.endianness+"L", ptr_table[i:i+4])[0]
if addr == 0xffffffff:
okay = True
elif addr == 0:
break
else:
if okay:
entries.append(addr)
else:
print "Invalid CTOR/DTOR table? Missing 0xffffffff"
entries.sort()
return entries
elif self.binformat.name == "macho":
eip = None
for cmd in self.binformat.commands:
if type(cmd[1]) == macho.THREAD_COMMAND:
regs = struct.unpack("<LLLLLLLLLLLLLLLLLL",cmd[2])
flavor, count = regs[:2]
regs = regs[2:]
eip = regs[10]
if eip:
return [eip]
return []
else:
return []
if __name__ == "__main__":
import sys
fn = sys.argv[1]
bin = Binparser(fn)
arch = None
if bin.architecture == "MIPS":
from mips_translator import MIPS_Translator
arch = mips = MIPS_Translator()
mips.external_functions = elf.mips_resolve_external_funcs(bin)
IR_rep = mips.translate(bin)
elif bin.architecture == "386":
from x86_translator import X86_Translator
arch = x86 = X86_Translator()
if bin.binformat.name == "ELF":
x86.external_functions = elf.nix_resolve_external_funcs(bin)
elif bin.binformat.name == "macho":
x86.external_functions = macho.macho_resolve_external_funcs(bin)
else:
#unsupported file format
x86.external_functions = {}
if not x86.external_functions:
print "[-] No dynamic functions found, static binary?"
IR_rep = x86.translate(bin)
else:
print "UNKNOWN ARCHITECTURE", bin.architecture
callgraph = {}
f = graphs.linear_sweep_split_functions(IR_rep)
for func in f:
callgraph[func] = graphs.make_blocks(f[func])
ssa.propagate_intra_block_values(arch, callgraph, bin)
import play
#play.stack_explore(arch, bin, callgraph)
play.prop_blocks(arch, bin, callgraph)
#annotations.transform(arch, callgraph, bin)
#k = callgraph.keys()
#k.sort()
#for func in k:
# print "====== func %x ====="%func
# graphs.graph_function(f[func])
# print "===== \n\n\n"