about summary refs log tree commit diff stats
path: root/example/unpack_upx.py
blob: 890f7fc2600e16327ea75d9ee99ab64b0d5a3129 (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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import sys
import struct
from elfesteem import *
from miasm.tools.pe_helper import *
from elfesteem.strpatchwork import StrPatchwork
from miasm.tools.to_c_helper import *
from miasm.tools.codenat import *
from pdb import pm


# example for scrambled upx unpacking

fname = sys.argv[1]

e = pe_init.PE(open(fname, 'rb').read())
in_str = bin_stream(e.virt)

ep =  e.rva2virt(e.Opthdr.AddressOfEntryPoint)
decomp_func = ep




vm_init_regs()
init_memory_page_pool_py()
init_code_bloc_pool_py()

codenat_tcc_init()



job_done = set()
symbol_pool = asmbloc.asm_symbol_pool()
if e.Coffhdr.characteristics & (1<<13):
    # dll
    all_bloc = asmbloc.dis_bloc_all(x86_mn, in_str, decomp_func, job_done, symbol_pool, bloc_wd=2)
    b = all_bloc[1]
else:
    # binary
    all_bloc = asmbloc.dis_bloc_all(x86_mn, in_str, decomp_func, job_done, symbol_pool, bloc_wd=1)
    b = all_bloc[0]
    
print b


print "start emul..."
machine = x86_machine()
f_eip = emul_bloc(machine, b)

decomp_buf_ad_in =  int(machine.pool[esi].arg)
decomp_buf_ad_out = int( machine.pool[edi].arg)


decomp_buf_len_in = decomp_func - decomp_buf_ad_in
decomp_buf_len_out = decomp_buf_ad_in - decomp_buf_ad_out
print "in l", hex(decomp_buf_len_in), "out l", hex(decomp_buf_len_out)

dont_dis = [(decomp_buf_ad_out, decomp_buf_ad_in)]

g = asmbloc.bloc2graph(all_bloc)
open("graph.txt" , "w").write(g)


job_done = set()
symbol_pool = asmbloc.asm_symbol_pool()
all_bloc = asmbloc.dis_bloc_all(x86_mn, in_str, decomp_func, job_done, symbol_pool, dont_dis)


end_label = None
for b in all_bloc:
    if len(b.bto) == 1 and asmbloc.getblocby_label(all_bloc, b.bto[0].label)== None:
        end_label = b.bto[0].label.offset
        break
if not end_label:
    raise ValueError('cannot find final bloc')

print 'final label'
print hex(end_label)


    
base_imp = 0
offset_imp = 0
libbase_ad = 0x77700000
def myloadlibexa():
    global base_imp, offset_imp, libbase_ad, runtime_dll
    ret_ad = vm_pop_uint32_t()
    pname = vm_pop_uint32_t()
    print 'loadlib', hex(pname), hex(ret_ad)

    libname = vm_get_str(pname, 0x100)
    libname = libname[:libname.find('\x00')]

    print repr(libname)

    ad = runtime_dll.lib_get_add_base(libname)
        
    regs = vm_get_gpreg()
    
    if not base_imp:
        base_imp = regs["edi"]
    if not offset_imp:
        offset_imp = regs['eax']

    print hex(base_imp), hex(offset_imp)
    regs['eip'] = ret_ad
    regs['eax'] = ad
    vm_set_gpreg(regs)


    




def mygetproc():
    global runtime_dll
    ret_ad = vm_pop_uint32_t()
    libbase = vm_pop_uint32_t()
    fname = vm_pop_uint32_t()
    print 'getproc', hex(fname), hex(libbase), hex(ret_ad)
    
    regs = vm_get_gpreg()
    dst_ad = regs['ebx']
    print 'ebx', hex(dst_ad)

    if fname < 0x10000:
        fname = fname
    else:
        fname = vm_get_str(fname, 0x100)
        fname = fname[:fname.find('\x00')]
        print fname


    ad = runtime_dll.lib_get_add_func(libbase, fname, dst_ad)

    
    
    regs['eip'] = ret_ad
    regs['eax'] = ad
    vm_set_gpreg(regs)


stack_size = 0x10000
vm_add_memory_page(0x40000000, PAGE_READ|PAGE_WRITE, "\x00"*stack_size)

vm_load_pe(e)

runtime_dll, dll_dyn_funcs = preload_lib(e)
dll_dyn_ad2name = dict([(x[1], x[0]) for x in dll_dyn_funcs.items()])

from miasm.tools import win_api


dyn_func = {}
dyn_func[dll_dyn_funcs['kernel32_LoadLibraryA']] = myloadlibexa
dyn_func[dll_dyn_funcs['kernel32_GetProcAddress']] = mygetproc
dyn_func[dll_dyn_funcs['kernel32_VirtualProtect']] = win_api.kernel32_VirtualProtect




    
dump_memory_page_pool_py()


regs = vm_get_gpreg()
regs['eip'] = decomp_func
regs['esp'] = 0x40000000+stack_size

vm_set_gpreg(regs)

vm_push_uint32_t(1) #reason code if dll
vm_push_uint32_t(1) #reason code if dll
vm_push_uint32_t(0x1337beef)

known_blocs = {}
cpt =0
code_blocs_mem_range = []

def my_run():
    global cpt, my_eip, known_blocs, code_blocs_mem_range
    trace_on = {'log_mn':False, 'log_regs':False}
    
    print 'start'
    while True:
        cpt+=1
        #print 'eip', hex(my_eip)
        if my_eip in [ end_label]:
            e.Opthdr.AddressOfEntryPoint = e.virt2rva(my_eip)
            print 'updating binary', cpt
            for s in e.SHList:
                sdata = vm_get_str(e.rva2virt(s.addr), s.rawsize)
                e.virt[e.rva2virt(s.addr)] = sdata
            in_str = bin_stream(e.virt)
    
            open('uu.bin', 'wb').write(str(e))
            g = asmbloc.bloc2graph([x.b for x in known_blocs.values()], lines = False)
            open("graph.txt" , "w").write(g)
    
            break
        if my_eip in dyn_func:
            dyn_func[my_eip]()
            print 'call dyn func', hex(my_eip)
            regs = vm_get_gpreg()
            my_eip = regs['eip']
            continue
        if not my_eip in known_blocs:
            in_str = updt_pe_from_emul(e)
            updt_bloc_emul(known_blocs, in_str, my_eip, symbol_pool, code_blocs_mem_range, **trace_on)

        my_eip = known_blocs[my_eip].module_c.func()
        py_exception = vm_get_exception()
        if py_exception:
            if py_exception & EXCEPT_CODE_AUTOMOD:
                print 'automod code'
                dump_gpregs_py()
                known_blocs, code_blocs_mem_range = updt_automod_code(known_blocs)
            else:
                raise ValueError("zarb exception", hex(py_exception))


my_eip = decomp_func

my_run()

print "decomp end", hex(base_imp), hex(offset_imp)
regs = vm_get_gpreg()

for r, v in regs.items():
    print r, hex(v&0xFFFFFFFF)

oo = vm_get_str(decomp_buf_ad_out, decomp_func-decomp_buf_ad_out)

open('uu', 'w').write("A"*0x1000 + oo)
print repr(oo[:0x10])
print repr(oo[-0x10:])


print hex(len(oo))

###rebuild import table##########
print 'assing'
e.virt[decomp_buf_ad_out] = oo
e.SHList.align_sections(0x1000, 0x1000)
print repr(e.SHList)

ad_base = regs['esi']

ad_tmp = base_imp -8
print "imp addr", hex(ad_tmp)
print 'ad base:', hex(ad_base)
print "base imp", hex(offset_imp)
print 'decomp_buf_ad_out', hex(decomp_buf_ad_out)
new_dll = []

offset_imp = offset_imp - decomp_buf_ad_out - struct.unpack('L', e.virt[ad_tmp:ad_tmp+4])[0]
print "read ofset imp", hex(offset_imp)

#XXXXX 
ad_base = decomp_buf_ad_out

print repr(e.SHList)
st = StrPatchwork()
st[0] = e.content

# get back data from emulator
for s in e.SHList:
    ad1 = e.rva2virt(s.addr)
    ad2 =ad1 + len(s.data)
    st[s.offset] = e.virt[ad1:ad2]
e.content = str(st)

e.DirRes = pe_init.DirRes(e)
#e.DirImport.impdesc = None
print repr(e.DirImport.impdesc)
new_dll = runtime_dll.gen_new_lib(e)
print new_dll
e.DirImport.add_dlldesc(new_dll)
s_myimp = e.SHList.add_section(name = "myimp", rawsize = len(e.DirImport))
print repr(e.SHList)
e.DirImport.set_rva(s_myimp.addr)

e.Opthdr.AddressOfEntryPoint = e.virt2rva(end_label)
open('out.bin','w').write(str(e))