Nguyen Anh Quynh | f0c577f | 2014-04-10 16:09:15 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com> |
| 4 | |
| 5 | from capstone import * |
| 6 | import binascii |
| 7 | |
| 8 | X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92" |
| 9 | RANDOM_CODE = "\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78" |
| 10 | |
| 11 | all_tests = ( |
| 12 | (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", 0), |
Nguyen Anh Quynh | 1eccbab | 2014-04-10 17:48:19 +0800 | [diff] [blame] | 13 | (CS_ARCH_ARM, CS_MODE_ARM, RANDOM_CODE, "Arm", 0), |
Nguyen Anh Quynh | f0c577f | 2014-04-10 16:09:15 +0800 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | |
| 17 | def to_hex(s): |
| 18 | return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK |
| 19 | |
| 20 | |
| 21 | ### Test cs_disasm_quick() |
| 22 | def test_cs_disasm_quick(): |
| 23 | for (arch, mode, code, comment, syntax) in all_tests: |
| 24 | print('*' * 40) |
| 25 | print("Platform: %s" %comment) |
| 26 | print("Disasm:"), |
| 27 | print to_hex(code) |
| 28 | for insn in cs_disasm_quick(arch, mode, code, 0x1000): |
| 29 | print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str)) |
| 30 | print |
| 31 | |
| 32 | |
Nguyen Anh Quynh | 301d740 | 2014-04-10 22:34:27 +0800 | [diff] [blame] | 33 | # Sample callback for SKIPDATA option |
| 34 | def testcb(offset, userdata): |
| 35 | # always skip 2 bytes of data |
| 36 | return 2 |
| 37 | |
| 38 | |
Nguyen Anh Quynh | f0c577f | 2014-04-10 16:09:15 +0800 | [diff] [blame] | 39 | ### Test class Cs |
| 40 | def test_class(): |
| 41 | for (arch, mode, code, comment, syntax) in all_tests: |
| 42 | print('*' * 16) |
| 43 | print("Platform: %s" %comment) |
| 44 | print("Code: %s" % to_hex(code)) |
| 45 | print("Disasm:") |
| 46 | |
| 47 | try: |
| 48 | md = Cs(arch, mode) |
| 49 | |
| 50 | if syntax != 0: |
| 51 | md.syntax = syntax |
| 52 | |
| 53 | md.skipdata = True |
Nguyen Anh Quynh | 301d740 | 2014-04-10 22:34:27 +0800 | [diff] [blame] | 54 | |
Nguyen Anh Quynh | b64d1cf | 2014-04-10 23:05:28 +0800 | [diff] [blame^] | 55 | # Default "data" instruction's name is ".byte". To rename it to "db", just uncomment |
| 56 | # the code below. |
| 57 | # md.skipdata_setup = ("db", None, None) |
| 58 | # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None) |
| 59 | |
| 60 | # To customize the SKIPDATA callback, uncomment the line below. |
Nguyen Anh Quynh | 301d740 | 2014-04-10 22:34:27 +0800 | [diff] [blame] | 61 | # md.skipdata_setup = (".db", CS_SKIPDATA_CALLBACK(testcb), None) |
Nguyen Anh Quynh | f0c577f | 2014-04-10 16:09:15 +0800 | [diff] [blame] | 62 | |
| 63 | for insn in md.disasm(code, 0x1000): |
| 64 | #bytes = binascii.hexlify(insn.bytes) |
| 65 | #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes)) |
| 66 | print("0x%x:\t%s\t%s" %(insn.address, insn.mnemonic, insn.op_str)) |
| 67 | |
| 68 | print("0x%x:" % (insn.address + insn.size)) |
| 69 | print |
| 70 | except CsError as e: |
| 71 | print("ERROR: %s" %e) |
| 72 | |
| 73 | |
| 74 | test_class() |