1#!/usr/bin/env python 2 3# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com> 4 5from __future__ import print_function 6from capstone import * 7import binascii 8from xprint import to_x, to_hex, to_x_32 9 10 11X86_CODE32 = b"\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x00\x91\x92" 12RANDOM_CODE = b"\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" 13 14all_tests = ( 15 (CS_ARCH_X86, CS_MODE_32, X86_CODE32, "X86 32 (Intel syntax)", 0), 16 (CS_ARCH_ARM, CS_MODE_ARM, RANDOM_CODE, "Arm", 0), 17) 18 19 20# ## Test cs_disasm_quick() 21def test_cs_disasm_quick(): 22 for (arch, mode, code, comment, syntax) in all_tests: 23 print('*' * 40) 24 print("Platform: %s" % comment) 25 print("Disasm:"), 26 print(to_hex(code)) 27 for insn in cs_disasm_quick(arch, mode, code, 0x1000): 28 print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) 29 print 30 31 32# Sample callback for SKIPDATA option 33def testcb(buffer, size, offset, userdata): 34 # always skip 2 bytes of data 35 return 2 36 37 38# ## Test class Cs 39def test_class(): 40 for (arch, mode, code, comment, syntax) in all_tests: 41 print('*' * 16) 42 print("Platform: %s" %comment) 43 print("Code: %s" % to_hex(code)) 44 print("Disasm:") 45 46 try: 47 md = Cs(arch, mode) 48 49 if syntax != 0: 50 md.syntax = syntax 51 52 md.skipdata = True 53 54 # Default "data" instruction's name is ".byte". To rename it to "db", just uncomment 55 # the code below. 56 # md.skipdata_setup = ("db", None, None) 57 # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None) 58 59 # To customize the SKIPDATA callback, uncomment the line below. 60 # md.skipdata_setup = (".db", testcb, None) 61 62 for insn in md.disasm(code, 0x1000): 63 #bytes = binascii.hexlify(insn.bytes) 64 #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes)) 65 print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) 66 67 print("0x%x:" % (insn.address + insn.size)) 68 print 69 except CsError as e: 70 print("ERROR: %s" % e) 71 72 73if __name__ == '__main__': 74 test_class() 75