File: sample_x86.rb

package info (click to toggle)
unicorn-engine 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,812 kB
  • sloc: ansic: 379,830; python: 9,213; sh: 9,011; java: 8,609; ruby: 4,241; pascal: 1,805; haskell: 1,379; xml: 490; cs: 424; makefile: 343; cpp: 298; asm: 64
file content (552 lines) | stat: -rw-r--r-- 17,389 bytes parent folder | download | duplicates (3)
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/usr/bin/env ruby
require 'unicorn_engine'
require 'unicorn_engine/x86_const'

include UnicornEngine

X86_CODE32 = "\x41\x4a" # INC ecx; DEC edx
X86_CODE32_LOOP = "\x41\x4a\xeb\xfe" # INC ecx; DEC edx; JMP self-loop
X86_CODE32_MEM_READ = "\x8B\x0D\xAA\xAA\xAA\xAA\x41\x4a" # mov ecx,[0xaaaaaaaa]; INC ecx; DEC edx
X86_CODE32_MEM_WRITE = "\x89\x0D\xAA\xAA\xAA\xAA\x41\x4a" # mov [0xaaaaaaaa], ecx; INC ecx; DEC edx
X86_CODE64 = "\x41\xBC\x3B\xB0\x28\x2A\x49\x0F\xC9\x90\x4D\x0F\xAD\xCF\x49\x87\xFD\x90\x48\x81\xD2\x8A\xCE\x77\x35\x48\xF7\xD9\x4D\x29\xF4\x49\x81\xC9\xF6\x8A\xC6\x53\x4D\x87\xED\x48\x0F\xAD\xD2\x49\xF7\xD4\x48\xF7\xE1\x4D\x19\xC5\x4D\x89\xC5\x48\xF7\xD6\x41\xB8\x4F\x8D\x6B\x59\x4D\x87\xD0\x68\x6A\x1E\x09\x3C\x59"
X86_CODE32_INOUT = "\x41\xE4\x3F\x4a\xE6\x46\x43" # INC ecx; IN AL, 0x3f; DEC edx; OUT 0x46, AL; INC ebx
X86_CODE64_SYSCALL = "\x0f\x05" # SYSCALL
X86_CODE16 = "\x00\x00" # add   byte ptr [bx + si], al

# memory address where emulation starts
ADDRESS = 0x1000000


# callback for tracing basic blocks
HOOK_BLOCK = Proc.new do |uc, address, size, user_data |
    puts(">>> Tracing basic block at 0x%x, block size = 0x%x" % [address, size])
end

# callback for tracing instructions
HOOK_CODE = Proc.new do |uc, address, size, user_data|
    puts(">>> Tracing instruction at 0x%x, instruction size = %u" % [address, size])
end


# callback for tracing invalid memory access (READ or WRITE)
HOOK_MEM_INVALID = lambda do |uc, access, address, size, value, user_data|
    if access == UC_MEM_WRITE_UNMAPPED
        puts(">>> Missing memory is being WRITE at 0x%x, data size = %u, data value = 0x%x" % [address, size, value])
        # map this memory in with 2MB in size
        uc.mem_map(0xaaaa0000, 2 * 1024*1024)
        # return True to indicate we want to continue emulation
        return true
    else
        puts(">>> Missing memory is being READ at 0x%x" % address)
        # return False to indicate we want to stop emulation
        return false
    end
end


# callback for tracing memory access (READ or WRITE)
HOOK_MEM_ACCESS = Proc.new do |uc, access, address, size, value, user_data|
    if access == UC_MEM_WRITE
        puts(">>> Memory is being WRITE at 0x%x, data size = %u, data value = 0x%x" % [address, size, value])
    else   # READ
        puts(">>> Memory is being READ at 0x%x, data size = %u" % [address, size])
    end
end

# callback for IN instruction
HOOK_IN = lambda do |uc, port, size, user_data|
    eip = uc.reg_read(UC_X86_REG_EIP)
    puts("--- reading from port 0x%x, size: %u, address: 0x%x" % [port, size, eip])
    if size == 1
        # read 1 byte to AL
        return 0xf1
    end
    if size == 2
        # read 2 byte to AX
        return 0xf2
    end
    if size == 4
        # read 4 byte to EAX
        return 0xf4
    end
    # we should never reach here
    return 0
end


# callback for OUT instruction
HOOK_OUT = Proc.new do |uc, port, size, value, user_data|
    eip = uc.reg_read(UC_X86_REG_EIP)
    puts("--- writing to port 0x%x, size: %u, value: 0x%x, address: 0x%x" % [port, size, value, eip])

    # confirm that value is indeed the value of AL/AX/EAX
    v = 0
    if size == 1
        # read 1 byte in AL
        v = uc.reg_read(UC_X86_REG_AL)
    end
    if size == 2
        # read 2 bytes in AX
        v = uc.reg_read(UC_X86_REG_AX)
    end
    if size == 4
        # read 4 bytes in EAX
        v = uc.reg_read(UC_X86_REG_EAX)
    end

    puts("--- register value = 0x%x" %v)
end


# Test X86 32 bit
def test_i386()
    puts("Emulate i386 code")
   begin
    # Initialize emulator in X86-32bit mode
    mu = Uc.new UC_ARCH_X86, UC_MODE_32
    # map 2MB memory for this emulation
    mu.mem_map(ADDRESS, 2 * 1024 * 1024)

    # write machine code to be emulated to memory
    mu.mem_write(ADDRESS, X86_CODE32)

    # initialize machine registers
    mu.reg_write(UC_X86_REG_ECX, 0x1234)
    mu.reg_write(UC_X86_REG_EDX, 0x7890)

    # tracing all basic blocks with customized callback
    mu.hook_add(UC_HOOK_BLOCK, HOOK_BLOCK)

    # tracing all instructions with customized callback
    mu.hook_add(UC_HOOK_CODE, HOOK_CODE)
    mu.hook_add(UC_HOOK_MEM_READ_UNMAPPED, HOOK_MEM_INVALID)

    # emulate machine code in infinite time
    mu.emu_start(ADDRESS, ADDRESS + X86_CODE32.bytesize)

    # now print out some registers
    puts(">>> Emulation done. Below is the CPU context")

    r_ecx = mu.reg_read(UC_X86_REG_ECX)
    r_edx = mu.reg_read(UC_X86_REG_EDX)
    puts(">>> ECX = 0x%x" % r_ecx)
    puts(">>> EDX = 0x%x" % r_edx)

    # read from memory
    tmp = mu.mem_read(ADDRESS, 2)
    print(">>> Read 2 bytes from [0x%x] =" % (ADDRESS))
    tmp.each_byte { |i| print(" 0x%x" % i) }

    puts

    rescue UcError => e
        puts("ERROR: %s" % e)
    end
end


def test_i386_loop()
    puts("Emulate i386 code with infinite loop - wait for 2 seconds then stop emulation")
    begin
        # Initialize emulator in X86-32bit mode
        mu = Uc.new UC_ARCH_X86, UC_MODE_32

        # map 2MB memory for this emulation
        mu.mem_map(ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(ADDRESS, X86_CODE32_LOOP)

        # initialize machine registers
        mu.reg_write(UC_X86_REG_ECX, 0x1234)
        mu.reg_write(UC_X86_REG_EDX, 0x7890)

        # emulate machine code in infinite time
        mu.emu_start(ADDRESS, ADDRESS + X86_CODE32_LOOP.bytesize, 2 * UC_SECOND_SCALE)

        # now print out some registers
        puts(">>> Emulation done. Below is the CPU context")

        r_ecx = mu.reg_read(UC_X86_REG_ECX)
        r_edx = mu.reg_read(UC_X86_REG_EDX)
        puts(">>> ECX = 0x%x" % r_ecx)
        puts(">>> EDX = 0x%x" % r_edx)

    rescue UcError => e
        puts("ERROR: %s" % e)
    end
end


def test_i386_invalid_mem_read()
    puts("Emulate i386 code that read from invalid memory")
    begin
        # Initialize emulator in X86-32bit mode
        mu = Uc.new UC_ARCH_X86, UC_MODE_32

        # map 2MB memory for this emulation
        mu.mem_map(ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(ADDRESS, X86_CODE32_MEM_READ)

        # initialize machine registers
        mu.reg_write(UC_X86_REG_ECX, 0x1234)
        mu.reg_write(UC_X86_REG_EDX, 0x7890)

        # tracing all basic blocks with customized callback
        mu.hook_add(UC_HOOK_BLOCK, HOOK_BLOCK)

        # tracing all instructions with customized callback
        mu.hook_add(UC_HOOK_CODE, HOOK_CODE)

        begin
            # emulate machine code in infinite time
            mu.emu_start(ADDRESS, ADDRESS + X86_CODE32_MEM_READ.bytesize)
        rescue UcError => e
            puts("ERROR: %s" % e)
        end

        # now print out some registers
        puts(">>> Emulation done. Below is the CPU context")

        r_ecx = mu.reg_read(UC_X86_REG_ECX)
        r_edx = mu.reg_read(UC_X86_REG_EDX)
        puts(">>> ECX = 0x%x" % r_ecx)
        puts(">>> EDX = 0x%x" % r_edx)

    rescue UcError => e
        print("ERROR: %s" % e)
    end
end


def test_i386_invalid_mem_write()
    puts("Emulate i386 code that write to invalid memory")
    begin
        # Initialize emulator in X86-32bit mode
        mu = Uc.new UC_ARCH_X86, UC_MODE_32

        # map 2MB memory for this emulation
        mu.mem_map(ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(ADDRESS, X86_CODE32_MEM_WRITE)

        # initialize machine registers
        mu.reg_write(UC_X86_REG_ECX, 0x1234)
        mu.reg_write(UC_X86_REG_EDX, 0x7890)

        # tracing all basic blocks with customized callback
        #mu.hook_add(UC_HOOK_BLOCK, HOOK_BLOCK)

        # tracing all instructions with customized callback
        #mu.hook_add(UC_HOOK_CODE, HOOK_CODE)

        # intercept invalid memory events
        mu.hook_add(UC_HOOK_MEM_READ_UNMAPPED | UC_HOOK_MEM_WRITE_UNMAPPED, HOOK_MEM_INVALID)

        begin
            # emulate machine code in infinite time
            mu.emu_start(ADDRESS, ADDRESS + X86_CODE32_MEM_WRITE.bytesize)
        rescue UcError => e
            puts "ERROR: %s" % e
        end

        # now print out some registers
        puts ">>> Emulation done. Below is the CPU context"

        r_ecx = mu.reg_read(UC_X86_REG_ECX)
        r_edx = mu.reg_read(UC_X86_REG_EDX)
        puts ">>> ECX = 0x%x" % r_ecx
        puts ">>> EDX = 0x%x" % r_edx

        begin
            # read from memory
            print ">>> Read 4 bytes from [0x%x] = " % (0xaaaaaaaa)
            tmp = mu.mem_read(0xaaaaaaaa, 4)
            tmp.each_byte { |i| print(" 0x%x" % i) }
            puts

            print ">>> Read 4 bytes from [0x%x] = " % 0xffffffaa
            tmp = mu.mem_read(0xffffffaa, 4)
            tmp.each_byte { |i| puts(" 0x%x" % i) }
            puts

        rescue UcError => e
            puts "ERROR: %s" % e
        end

    rescue UcError => e
        puts "ERROR: %s" % e
    end
end

def test_i386_context_save()

    puts("Save/restore CPU context in opaque blob")
    address = 0
    code = '\x40'  # inc eax
    begin
        # Initialize emulator
        mu = Uc.new UC_ARCH_X86, UC_MODE_32

        # map 8KB memory for this emulation
        mu.mem_map(address, 8 * 1024, UC_PROT_ALL)

        # write machine code to be emulated to memory
        mu.mem_write(address, code)

        # set eax to 1
        mu.reg_write(UC_X86_REG_EAX, 1)

        puts(">>> Running emulation for the first time")
        mu.emu_start(address, address+1)

        puts(">>> Emulation done. Below is the CPU context")
        puts(">>> EAX = 0x%x" %(mu.reg_read(UC_X86_REG_EAX)))
        puts(">>> Saving CPU context")
        saved_context = mu.context_save()

        puts(">>> Running emulation for the second time")
        mu.emu_start(address, address+1)
        puts(">>> Emulation done. Below is the CPU context")
        puts(">>> EAX = 0x%x" %(mu.reg_read(UC_X86_REG_EAX)))

        puts(">>> CPU context restored. Below is the CPU context")
        mu.context_restore(saved_context)
        puts(">>> EAX = 0x%x" %(mu.reg_read(UC_X86_REG_EAX)))

    rescue UcError => e
        puts("ERROR: %s" % e)
    end

end

# Test X86 32 bit with IN/OUT instruction
def test_i386_inout()
    puts("Emulate i386 code with IN/OUT instructions")
    begin
        # Initialize emulator in X86-32bit mode
        mu = Uc.new UC_ARCH_X86, UC_MODE_32

        # map 2MB memory for this emulation
        mu.mem_map(ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(ADDRESS, X86_CODE32_INOUT)

        # initialize machine registers
        mu.reg_write(UC_X86_REG_EAX, 0x1234)
        mu.reg_write(UC_X86_REG_ECX, 0x6789)

        # tracing all basic blocks with customized callback
        mu.hook_add(UC_HOOK_BLOCK, HOOK_BLOCK)

        # tracing all instructions with customized callback
        mu.hook_add(UC_HOOK_CODE, HOOK_CODE)

        # handle IN & OUT instruction
        mu.hook_add(UC_HOOK_INSN, HOOK_IN, nil, 1, 0, UC_X86_INS_IN)
        mu.hook_add(UC_HOOK_INSN, HOOK_OUT, nil, 1, 0, UC_X86_INS_OUT)

        # emulate machine code in infinite time
        mu.emu_start(ADDRESS, ADDRESS + X86_CODE32_INOUT.bytesize)

        # now print out some registers
        puts(">>> Emulation done. Below is the CPU context")

        r_ecx = mu.reg_read(UC_X86_REG_ECX)
        r_eax = mu.reg_read(UC_X86_REG_EAX)
        puts ">>> EAX = 0x%x" % r_eax
        puts ">>> ECX = 0x%x" % r_ecx
    rescue UcError => e
        puts("ERROR: %s" % e)
    end
end


def test_x86_64()
    puts("Emulate x86_64 code")
    begin
        # Initialize emulator in X86-64bit mode
        mu = Uc.new UC_ARCH_X86, UC_MODE_64

        # map 2MB memory for this emulation
        mu.mem_map(ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(ADDRESS, X86_CODE64)

        # initialize machine registers
        mu.reg_write(UC_X86_REG_RAX, 0x71f3029efd49d41d)
        mu.reg_write(UC_X86_REG_RBX, 0xd87b45277f133ddb)
        mu.reg_write(UC_X86_REG_RCX, 0xab40d1ffd8afc461)
        mu.reg_write(UC_X86_REG_RDX, 0x919317b4a733f01)
        mu.reg_write(UC_X86_REG_RSI, 0x4c24e753a17ea358)
        mu.reg_write(UC_X86_REG_RDI, 0xe509a57d2571ce96)
        mu.reg_write(UC_X86_REG_R8, 0xea5b108cc2b9ab1f)
        mu.reg_write(UC_X86_REG_R9, 0x19ec097c8eb618c1)
        mu.reg_write(UC_X86_REG_R10, 0xec45774f00c5f682)
        mu.reg_write(UC_X86_REG_R11, 0xe17e9dbec8c074aa)
        mu.reg_write(UC_X86_REG_R12, 0x80f86a8dc0f6d457)
        mu.reg_write(UC_X86_REG_R13, 0x48288ca5671c5492)
        mu.reg_write(UC_X86_REG_R14, 0x595f72f6e4017f6e)
        mu.reg_write(UC_X86_REG_R15, 0x1efd97aea331cccc)

        # setup stack
        mu.reg_write(UC_X86_REG_RSP, ADDRESS + 0x200000)

        # tracing all basic blocks with customized callback
        mu.hook_add(UC_HOOK_BLOCK, HOOK_BLOCK)

        # tracing all instructions in range [ADDRESS, ADDRESS+20]
        mu.hook_add(UC_HOOK_CODE, HOOK_CODE, 0, ADDRESS, ADDRESS+20)

        # tracing all memory READ & WRITE access
        mu.hook_add(UC_HOOK_MEM_WRITE, HOOK_MEM_ACCESS)
        mu.hook_add(UC_HOOK_MEM_READ, HOOK_MEM_ACCESS)
        # actually you can also use READ_WRITE to trace all memory access
        #mu.hook_add(UC_HOOK_MEM_READ | UC_HOOK_MEM_WRITE, hook_mem_access)

        begin
            # emulate machine code in infinite time
            mu.emu_start(ADDRESS, ADDRESS + X86_CODE64.bytesize)
        rescue UcError => e
            puts("ERROR: %s" % e)
        end
        # now print out some registers
        puts(">>> Emulation done. Below is the CPU context")
        rax = mu.reg_read(UC_X86_REG_RAX)
        rbx = mu.reg_read(UC_X86_REG_RBX)
        rcx = mu.reg_read(UC_X86_REG_RCX)
        rdx = mu.reg_read(UC_X86_REG_RDX)
        rsi = mu.reg_read(UC_X86_REG_RSI)
        rdi = mu.reg_read(UC_X86_REG_RDI)
        r8 = mu.reg_read(UC_X86_REG_R8)
        r9 = mu.reg_read(UC_X86_REG_R9)
        r10 = mu.reg_read(UC_X86_REG_R10)
        r11 = mu.reg_read(UC_X86_REG_R11)
        r12 = mu.reg_read(UC_X86_REG_R12)
        r13 = mu.reg_read(UC_X86_REG_R13)
        r14 = mu.reg_read(UC_X86_REG_R14)
        r15 = mu.reg_read(UC_X86_REG_R15)

        puts(">>> RAX = %d" % rax)
        puts(">>> RBX = %d" % rbx)
        puts(">>> RCX = %d" % rcx)
        puts(">>> RDX = %d" % rdx)
        puts(">>> RSI = %d" % rsi)
        puts(">>> RDI = %d" % rdi)
        puts(">>> R8 = %d" % r8)
        puts(">>> R9 = %d" % r9)
        puts(">>> R10 = %d" % r10)
        puts(">>> R11 = %d" % r11)
        puts(">>> R12 = %d" % r12)
        puts(">>> R13 = %d" % r13)
        puts(">>> R14 = %d" % r14)
        puts(">>> R15 = %d" % r15)
        #BUG
        mu.emu_start(ADDRESS, ADDRESS + X86_CODE64.bytesize)

    rescue UcError => e
        puts("ERROR: %s" % e)
    end
end


def test_x86_64_syscall()
    puts("Emulate x86_64 code with 'syscall' instruction")
    begin
        # Initialize emulator in X86-64bit mode
        mu = Uc.new UC_ARCH_X86, UC_MODE_64

        # map 2MB memory for this emulation
        mu.mem_map(ADDRESS, 2 * 1024 * 1024)

        # write machine code to be emulated to memory
        mu.mem_write(ADDRESS, X86_CODE64_SYSCALL)

        hook_syscall = Proc.new do |mu, user_data|
            rax = mu.reg_read(UC_X86_REG_RAX)
            if rax == 0x100
                mu.reg_write(UC_X86_REG_RAX, 0x200)
            else
                puts('ERROR: was not expecting rax=%d in syscall' % rax)
            end
        end

        # hook interrupts for syscall
        mu.hook_add(UC_HOOK_INSN, hook_syscall, nil, 1, 0, UC_X86_INS_SYSCALL)

        # syscall handler is expecting rax=0x100
        mu.reg_write(UC_X86_REG_RAX, 0x100)

        begin
            # emulate machine code in infinite time
            mu.emu_start(ADDRESS, ADDRESS + X86_CODE64_SYSCALL.bytesize)
        rescue UcError => e
            puts("ERROR: %s" % e)
        end

        # now print out some registers
        puts(">>> Emulation done. Below is the CPU context")

        rax = mu.reg_read(UC_X86_REG_RAX)
        puts(">>> RAX = 0x%x" % rax)

    rescue UcError => e
        puts("ERROR: %s" % e)
    end
end


def test_x86_16()
    puts("Emulate x86 16-bit code")
    begin
        # Initialize emulator in X86-16bit mode
        mu = Uc.new UC_ARCH_X86, UC_MODE_16

        # map 8KB memory for this emulation
        mu.mem_map(0, 8 * 1024)

        # set CPU registers
        mu.reg_write(UC_X86_REG_EAX, 7)
        mu.reg_write(UC_X86_REG_EBX, 5)
        mu.reg_write(UC_X86_REG_ESI, 6)

        # write machine code to be emulated to memory
        mu.mem_write(0, X86_CODE16)

        # emulate machine code in infinite time
        mu.emu_start(0, X86_CODE16.bytesize)

        # now print out some registers
        puts(">>> Emulation done. Below is the CPU context")

        tmp = mu.mem_read(11, 1)
        puts("[0x%x] = 0x%x" % [11, tmp[0].ord])

    rescue UcError => e
        puts("ERROR: %s" % e)
    end
end


test_i386()
puts("=" * 20)
test_i386_loop()
puts("=" * 20)
test_i386_invalid_mem_read()
puts("=" * 20)
test_i386_invalid_mem_write()
puts("=" * 20)
test_i386_context_save()
puts("=" * 20)
test_i386_inout()
puts("=" * 20)
test_x86_64()
puts("=" * 20)
test_x86_64_syscall()
puts("=" * 20)
test_x86_16()