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
|
#! /usr/bin/env python
import os
from elfesteem import *
from miasm.tools.pe_helper import *
from miasm.tools import seh_helper
from miasm.core import bin_stream
import inspect
from miasm.core import asmbloc
from miasm.core import parse_asm
from elfesteem import pe
from miasm.arch import ia32_arch
try:
from miasm.arch.java_arch import java_mn
except ImportError:
pass
import pickle
import sys
print sys.argv
fname = sys.argv[1]
ad_to_dis = None
if len(sys.argv) >2:
ad_to_dis = sys.argv[2]
dll_dyn_funcs = {}
data = open(fname, 'rb').read()
if data.startswith("MZ"):
e = pe_init.PE(open(fname, 'rb').read())
if len(sys.argv) <=2:
ad_to_dis = e.rva2virt(e.Opthdr.AddressOfEntryPoint)
else:
ad_to_dis = int(sys.argv[2], 16)
in_str = bin_stream.bin_stream(e.virt)
try:
dll_dyn_funcs = get_import_address(e)
except:
print 'bug in import parsing'
mnemo = ia32_arch.x86_mn
elif data.startswith("\x7fELF") :
e = elf_init.ELF(open(fname, 'rb').read())
if len(sys.argv) <=2:
ad_to_dis = e.Ehdr.entry
else:
ad_to_dis = int(sys.argv[2], 16)
in_str = bin_stream.bin_stream(e.virt)
try:
dll_dyn_funcs = get_import_address_elf(e)
except:
print 'bug in import parsing'
mnemo = ia32_arch.x86_mn
elif data.startswith("\xca\xfe\xba\xbe"):
def java_usage():
print 'usage:'
print '%s methodname methodtype'%sys.argv[0]
print 'possible methods:'
for i, (c_name, c_type) in enumerate(methods):
print i, str(c_name), str(c_type)
sys.exit(-1)
e = jclass_init.JCLASS(data)
methods = {}
for m in e.description.methods:
name = m.name
descr = m.descriptor
code = filter(lambda x: type(x) is jclass_init.CAttribute_code, m.attributes)[0].code
methods[(name, descr)] = code
if len(sys.argv) != 4:
java_usage()
if not (sys.argv[2], sys.argv[3]) in methods:
java_usage()
in_str = bin_stream.bin_stream(methods[(sys.argv[2], sys.argv[3])])
ad_to_dis = 0
mnemo = java_mn
try:
constants_pool = get_java_constant_pool(e)
except:
print 'bug in constant pool parsing'
constants_pool = {}
else:
in_str = bin_stream.bin_stream(data)
ad_to_dis = 0
mnemo = ia32_arch.x86_mn
print 'dis', fname, 'at', "0x%.8X"%ad_to_dis
symbol_pool = asmbloc.asm_symbol_pool()
# test qt
from miasm.graph.graph_qt import graph_blocs
#test symbols from ida
for (n,f), ad in dll_dyn_funcs.items():
l = asmbloc.asm_label("%s_%s"%(n, f), ad)
print l
symbol_pool.add(l)
def my_disasm_callback(ad):
all_bloc = asmbloc.dis_bloc_all(mnemo, in_str, ad, set(),
symbol_pool=symbol_pool,
dont_dis_nulstart_bloc=True)
if mnemo == ia32_arch.x86_mn:
for b in all_bloc:
for l in b.lines:
for i, a in enumerate(l.arg):
if not ia32_arch.is_ad_lookup(a):
continue
x = a[ia32_arch.x86_afs.imm]
if x in symbol_pool.s_offset:
l.arg[i][x86_afs.symb] = symbol_pool.s_offset[x]
del(l.arg[i][ia32_arch.x86_afs.imm])
elif mnemo == java_mn:
o = {}
for k, v in constants_pool.items():
if hasattr(v, "pp"):
o[k] = v.pp()
else:
o[k] = repr(v)
for b in all_bloc:
for l in b.lines:
l.set_args_symbols(o)
return all_bloc
graph_blocs(ad_to_dis, all_bloc = [], dis_callback = my_disasm_callback)
|