Example #1
0
def test_class():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 16)
        print("Platform: %s" %comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)

            if syntax != 0:
                md.syntax = syntax

            md.skipdata = True

            # Default "data" instruction's name is ".byte". To rename it to "db", just uncomment
            # the code below.
            # md.skipdata_setup = ("db", None, None)
            # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None)

            # To customize the SKIPDATA callback, uncomment the line below.
            # md.skipdata_setup = (".db", CS_SKIPDATA_CALLBACK(testcb), None)

            for insn in md.disasm(code, 0x1000):
                #bytes = binascii.hexlify(insn.bytes)
                #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes))
                print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))

            print("0x%x:" % (insn.address + insn.size))
            print
        except CsError as e:
            print("ERROR: %s" % e)
Example #2
0
def test_class():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)

            if syntax != 0:
                md.syntax = syntax

            md.skipdata = True

            # Default "data" instruction's name is ".byte". To rename it to "db", just uncomment
            # the code below.
            # md.skipdata_setup = ("db", None, None)
            # NOTE: This example ignores SKIPDATA's callback (first None) & user_data (second None)

            # To customize the SKIPDATA callback, uncomment the line below.
            # md.skipdata_setup = (".db", CS_SKIPDATA_CALLBACK(testcb), None)

            for insn in md.disasm(code, 0x1000):
                #bytes = binascii.hexlify(insn.bytes)
                #print("0x%x:\t%s\t%s\t// hex-code: %s" %(insn.address, insn.mnemonic, insn.op_str, bytes))
                print("0x%x:\t%s\t%s" %
                      (insn.address, insn.mnemonic, insn.op_str))

            print("0x%x:" % (insn.address + insn.size))
            print
        except CsError as e:
            print("ERROR: %s" % e)
Example #3
0
def test_cs_disasm_quick():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 40)
        print("Platform: %s" % comment)
        print("Disasm:"),
        print(to_hex(code))
        for insn in cs_disasm_quick(arch, mode, code, 0x1000):
            print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
        print
Example #4
0
def test_cs_disasm_quick():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 40)
        print("Platform: %s" % comment)
        print("Disasm:"),
        print(to_hex(code))
        for (addr, size, mnemonic, op_str) in cs_disasm_lite(arch, mode, code, 0x1000):
            print("0x%x:\t%s\t%s" % (addr, mnemonic, op_str))
        print()
Example #5
0
def test_cs_disasm_quick():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 40)
        print("Platform: %s" % comment)
        print("Disasm:"),
        print(to_hex(code))
        for insn in cs_disasm_quick(arch, mode, code, 0x1000):
            print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
        print
Example #6
0
def test_cs_disasm_quick():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 40)
        print("Platform: %s" % comment)
        print("Disasm:"),
        print(to_hex(code))
        for (addr, size, mnemonic,
             op_str) in cs_disasm_lite(arch, mode, code, 0x1000):
            print("0x%x:\t%s\t%s" % (addr, mnemonic, op_str))
        print()
Example #7
0
def test_class():
    def print_insn_detail(insn):
        # print address, mnemonic and operands
        print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))

        # "data" instruction generated by SKIPDATA option has no detail
        if insn.id == 0:
            return

        if len(insn.operands) > 0:
            print("\top_count: %u" % len(insn.operands))
            c = 0
            for i in insn.operands:
                if i.type == SYSZ_OP_REG:
                    print("\t\toperands[%u].type: REG = %s" %
                          (c, insn.reg_name(i.reg)))
                if i.type == SYSZ_OP_ACREG:
                    print("\t\toperands[%u].type: ACREG = %u" % (c, i.reg))
                if i.type == SYSZ_OP_IMM:
                    print("\t\toperands[%u].type: IMM = 0x%s" %
                          (c, to_x(i.imm)))
                if i.type == SYSZ_OP_MEM:
                    print("\t\toperands[%u].type: MEM" % c)
                    if i.mem.base != 0:
                        print("\t\t\toperands[%u].mem.base: REG = %s" \
                            % (c, insn.reg_name(i.mem.base)))
                    if i.mem.index != 0:
                        print("\t\t\toperands[%u].mem.index: REG = %s" \
                            % (c, insn.reg_name(i.mem.index)))
                    if i.mem.length != 0:
                        print("\t\t\toperands[%u].mem.length: 0x%s" \
                            % (c, to_x(i.mem.length)))
                    if i.mem.disp != 0:
                        print("\t\t\toperands[%u].mem.disp: 0x%s" \
                            % (c, to_x(i.mem.disp)))
                c += 1

        if insn.cc:
            print("\tConditional code: %u" % insn.cc)

    for (arch, mode, code, comment) in all_tests:
        print("*" * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)
            md.detail = True
            for insn in md.disasm(code, 0x1000):
                print_insn_detail(insn)
                print
            print("0x%x:\n" % (insn.address + insn.size))
        except CsError as e:
            print("ERROR: %s" % e)
Example #8
0
def test_class():
    def print_insn_detail(insn):
        # print address, mnemonic and operands
        print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))

        # "data" instruction generated by SKIPDATA option has no detail
        if insn.id == 0:
            return

        if len(insn.operands) > 0:
            print("\top_count: %u" % len(insn.operands))
            c = 0
            for i in insn.operands:
                if i.type == SYSZ_OP_REG:
                    print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
                if i.type == SYSZ_OP_ACREG:
                    print("\t\toperands[%u].type: ACREG = %u" % (c, i.reg))
                if i.type == SYSZ_OP_IMM:
                    print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x(i.imm)))
                if i.type == SYSZ_OP_MEM:
                    print("\t\toperands[%u].type: MEM" % c)
                    if i.mem.base != 0:
                        print("\t\t\toperands[%u].mem.base: REG = %s" \
                            % (c, insn.reg_name(i.mem.base)))
                    if i.mem.index != 0:
                        print("\t\t\toperands[%u].mem.index: REG = %s" \
                            % (c, insn.reg_name(i.mem.index)))
                    if i.mem.length != 0:
                        print("\t\t\toperands[%u].mem.length: 0x%s" \
                            % (c, to_x(i.mem.length)))
                    if i.mem.disp != 0:
                        print("\t\t\toperands[%u].mem.disp: 0x%s" \
                            % (c, to_x(i.mem.disp)))
                c += 1

        if insn.cc:
            print("\tConditional code: %u" % insn.cc)

    for (arch, mode, code, comment) in all_tests:
        print("*" * 16)
        print("Platform: %s" %comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)
            md.detail = True
            for insn in md.disasm(code, 0x1000):
                print_insn_detail(insn)
                print
            print("0x%x:\n" % (insn.address + insn.size))
        except CsError as e:
            print("ERROR: %s" %e)
Example #9
0
def test_class():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)

            if syntax != 0:
                md.syntax = syntax

            for (addr, size, mnemonic, op_str) in md.disasm_lite(code, 0x1000):
                print("0x%x:\t%s\t%s" % (addr, mnemonic, op_str))

            print("0x%x:" % (addr + size))
            print()
        except CsError as e:
            print("ERROR: %s" % e)
Example #10
0
def test_class():
    for (arch, mode, code, comment, syntax) in all_tests:
        print('*' * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)

            if syntax != 0:
                md.syntax = syntax

            for (addr, size, mnemonic, op_str) in md.disasm_lite(code, 0x1000):
                print("0x%x:\t%s\t%s" % (addr, mnemonic, op_str))

            print("0x%x:" % (addr + size))
            print()
        except CsError as e:
            print("ERROR: %s" % e)
Example #11
0
def test_class():
    def print_string_hex(comment, str):
        print(comment, end=' '),
        for c in str:
            print("0x%02x" % c, end=''),
        print()

    def print_insn_detail(mode, insn):
        # print address, mnemonic and operands
        print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))

        # "data" instruction generated by SKIPDATA option has no detail
        if insn.id == 0:
            return

        # print instruction prefix
        print_string_hex("\tPrefix:", insn.prefix)

        # print segment override (if applicable)
        if insn.segment != X86_REG_INVALID:
            print("\tSegment override: %s" % insn.reg_name(insn.segment))

        # print instruction's opcode
        print_string_hex("\tOpcode:", insn.opcode)

        # print operand's size, address size, displacement size & immediate size
        print("\top_size: %u, addr_size: %u, disp_size: %u, imm_size: %u" \
            % (insn.op_size, insn.addr_size, insn.disp_size, insn.imm_size))

        # print modRM byte
        print("\tmodrm: 0x%x" % (insn.modrm))

        # print displacement value
        print("\tdisp: 0x%s" % to_x_32(insn.disp))

        # SIB is not available in 16-bit mode
        if (mode & CS_MODE_16 == 0):
            # print SIB byte
            print("\tsib: 0x%x" % (insn.sib))
            if (insn.sib):
                print("\tsib_index: %s, sib_scale: %d, sib_base: %s" % (insn.reg_name(insn.sib_index), insn.sib_scale, insn.reg_name(insn.sib_base)))

        count = insn.op_count(X86_OP_IMM)
        if count > 0:
            print("\timm_count: %u" % count)
            for i in range(count):
                op = insn.op_find(X86_OP_IMM, i + 1)
                print("\t\timms[%u]: 0x%s" % (i + 1, to_x(op.imm)))

        if len(insn.operands) > 0:
            print("\top_count: %u" % len(insn.operands))
            c = -1
            for i in insn.operands:
                c += 1
                if i.type == X86_OP_REG:
                    print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
                if i.type == X86_OP_IMM:
                    print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x(i.imm)))
                if i.type == X86_OP_FP:
                    print("\t\toperands[%u].type: FP = %f" % (c, i.fp))
                if i.type == X86_OP_MEM:
                    print("\t\toperands[%u].type: MEM" % c)
                    if i.mem.base != 0:
                        print("\t\t\toperands[%u].mem.base: REG = %s" % (c, insn.reg_name(i.mem.base)))
                    if i.mem.index != 0:
                        print("\t\t\toperands[%u].mem.index: REG = %s" % (c, insn.reg_name(i.mem.index)))
                    if i.mem.scale != 1:
                        print("\t\t\toperands[%u].mem.scale: %u" % (c, i.mem.scale))
                    if i.mem.disp != 0:
                        print("\t\t\toperands[%u].mem.disp: 0x%s" % (c, to_x(i.mem.disp)))


    for (arch, mode, code, comment, syntax) in all_tests:
        print("*" * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)
            md.detail = True

            if syntax != 0:
                md.syntax = syntax

            for insn in md.disasm(code, 0x1000):
                print_insn_detail(mode, insn)
                print
            print ("0x%x:\n" % (insn.address + insn.size))
        except CsError as e:
            print("ERROR: %s" % e)
Example #12
0
def test_class():
    def print_string_hex(comment, str):
        print(comment, end=' '),
        for c in str:
            print("0x%02x" % c, end=''),
        print()

    def print_insn_detail(mode, insn):
        # print address, mnemonic and operands
        print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))

        # "data" instruction generated by SKIPDATA option has no detail
        if insn.id == 0:
            return

        # print instruction prefix
        print_string_hex("\tPrefix:", insn.prefix)

        # print segment override (if applicable)
        if insn.segment != X86_REG_INVALID:
            print("\tSegment override: %s" % insn.reg_name(insn.segment))

        # print instruction's opcode
        print_string_hex("\tOpcode:", insn.opcode)

        # print operand's size, address size, displacement size & immediate size
        print("\top_size: %u, addr_size: %u, disp_size: %u, imm_size: %u" \
            % (insn.op_size, insn.addr_size, insn.disp_size, insn.imm_size))

        # print modRM byte
        print("\tmodrm: 0x%x" % (insn.modrm))

        # print displacement value
        print("\tdisp: 0x%s" % to_x_32(insn.disp))

        # SIB is not available in 16-bit mode
        if (mode & CS_MODE_16 == 0):
            # print SIB byte
            print("\tsib: 0x%x" % (insn.sib))
            if (insn.sib):
                print("\tsib_index: %s, sib_scale: %d, sib_base: %s" %
                      (insn.reg_name(insn.sib_index), insn.sib_scale,
                       insn.reg_name(insn.sib_base)))

        count = insn.op_count(X86_OP_IMM)
        if count > 0:
            print("\timm_count: %u" % count)
            for i in range(count):
                op = insn.op_find(X86_OP_IMM, i + 1)
                print("\t\timms[%u]: 0x%s" % (i + 1, to_x(op.imm)))

        if len(insn.operands) > 0:
            print("\top_count: %u" % len(insn.operands))
            c = -1
            for i in insn.operands:
                c += 1
                if i.type == X86_OP_REG:
                    print("\t\toperands[%u].type: REG = %s" %
                          (c, insn.reg_name(i.reg)))
                if i.type == X86_OP_IMM:
                    print("\t\toperands[%u].type: IMM = 0x%s" %
                          (c, to_x(i.imm)))
                if i.type == X86_OP_FP:
                    print("\t\toperands[%u].type: FP = %f" % (c, i.fp))
                if i.type == X86_OP_MEM:
                    print("\t\toperands[%u].type: MEM" % c)
                    if i.mem.base != 0:
                        print("\t\t\toperands[%u].mem.base: REG = %s" %
                              (c, insn.reg_name(i.mem.base)))
                    if i.mem.index != 0:
                        print("\t\t\toperands[%u].mem.index: REG = %s" %
                              (c, insn.reg_name(i.mem.index)))
                    if i.mem.scale != 1:
                        print("\t\t\toperands[%u].mem.scale: %u" %
                              (c, i.mem.scale))
                    if i.mem.disp != 0:
                        print("\t\t\toperands[%u].mem.disp: 0x%s" %
                              (c, to_x(i.mem.disp)))

    for (arch, mode, code, comment, syntax) in all_tests:
        print("*" * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)
            md.detail = True

            if syntax != 0:
                md.syntax = syntax

            for insn in md.disasm(code, 0x1000):
                print_insn_detail(mode, insn)
                print
            print("0x%x:\n" % (insn.address + insn.size))
        except CsError as e:
            print("ERROR: %s" % e)
Example #13
0
def test_class():
    def print_insn_detail(insn):
        # print address, mnemonic and operands
        print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))

        # "data" instruction generated by SKIPDATA option has no detail
        if insn.id == 0:
            return

        if len(insn.operands) > 0:
            print("\top_count: %u" % len(insn.operands))
            c = -1
            for i in insn.operands:
                c += 1
                if i.type == ARM64_OP_REG:
                    print("\t\toperands[%u].type: REG = %s" %
                          (c, insn.reg_name(i.reg)))
                if i.type == ARM64_OP_IMM:
                    print("\t\toperands[%u].type: IMM = 0x%s" %
                          (c, to_x(i.imm)))
                if i.type == ARM64_OP_CIMM:
                    print("\t\toperands[%u].type: C-IMM = %u" % (c, i.imm))
                if i.type == ARM64_OP_FP:
                    print("\t\toperands[%u].type: FP = %f" % (c, i.fp))
                if i.type == ARM64_OP_MEM:
                    print("\t\toperands[%u].type: MEM" % c)
                    if i.mem.base != 0:
                        print("\t\t\toperands[%u].mem.base: REG = %s" \
                            % (c, insn.reg_name(i.mem.base)))
                    if i.mem.index != 0:
                        print("\t\t\toperands[%u].mem.index: REG = %s" \
                            % (c, insn.reg_name(i.mem.index)))
                    if i.mem.disp != 0:
                        print("\t\t\toperands[%u].mem.disp: 0x%s" \
                            % (c, to_x(i.mem.disp)))

                if i.shift.type != ARM64_SFT_INVALID and i.shift.value:
                    print("\t\t\tShift: type = %u, value = %u" %
                          (i.shift.type, i.shift.value))

                if i.ext != ARM64_EXT_INVALID:
                    print("\t\t\tExt: %u" % i.ext)

        if insn.writeback:
            print("\tWrite-back: True")
        if not insn.cc in [ARM64_CC_AL, ARM64_CC_INVALID]:
            print("\tCode condition: %u" % insn.cc)
        if insn.update_flags:
            print("\tUpdate-flags: True")

    for (arch, mode, code, comment) in all_tests:
        print("*" * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)
            md.detail = True
            for insn in md.disasm(code, 0x2c):
                print_insn_detail(insn)
                print
            print("0x%x:\n" % (insn.address + insn.size))
        except CsError as e:
            print("ERROR: %s" % e)
Example #14
0
def test_class():
    def print_insn_detail(insn):
        # print address, mnemonic and operands
        print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))

        # "data" instruction generated by SKIPDATA option has no detail
        if insn.id == 0:
            return

        if len(insn.operands) > 0:
            print("\top_count: %u" % len(insn.operands))
            c = 0
            for i in insn.operands:
                if i.type == ARM_OP_REG:
                    print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
                if i.type == ARM_OP_IMM:
                    print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x_32(i.imm)))
                if i.type == ARM_OP_PIMM:
                    print("\t\toperands[%u].type: P-IMM = %u" % (c, i.imm))
                if i.type == ARM_OP_CIMM:
                    print("\t\toperands[%u].type: C-IMM = %u" % (c, i.imm))
                if i.type == ARM_OP_FP:
                    print("\t\toperands[%u].type: FP = %f" % (c, i.fp))
                if i.type == ARM_OP_MEM:
                    print("\t\toperands[%u].type: MEM" % c)
                    if i.mem.base != 0:
                        print("\t\t\toperands[%u].mem.base: REG = %s" \
                            % (c, insn.reg_name(i.mem.base)))
                    if i.mem.index != 0:
                        print("\t\t\toperands[%u].mem.index: REG = %s" \
                            % (c, insn.reg_name(i.mem.index)))
                    if i.mem.scale != 1:
                        print("\t\t\toperands[%u].mem.scale: %u" \
                            % (c, i.mem.scale))
                    if i.mem.disp != 0:
                        print("\t\t\toperands[%u].mem.disp: 0x%s" \
                            % (c, to_x_32(i.mem.disp)))

                if i.shift.type != ARM_SFT_INVALID and i.shift.value:
                    print("\t\t\tShift: type = %u, value = %u\n" \
                        % (i.shift.type, i.shift.value))
                c += 1

        if insn.update_flags:
            print("\tUpdate-flags: True")
        if insn.writeback:
            print("\tWrite-back: True")
        if not insn.cc in [ARM_CC_AL, ARM_CC_INVALID]:
            print("\tCode condition: %u" % insn.cc)

    for (arch, mode, code, comment) in all_tests:
        print("*" * 16)
        print("Platform: %s" % comment)
        print("Code: %s" % to_hex(code))
        print("Disasm:")

        try:
            md = Cs(arch, mode)
            md.detail = True
            for insn in md.disasm(code, 0x1000):
                print_insn_detail(insn)
                print
            print ("0x%x:\n" % (insn.address + insn.size))
        except CsError as e:
            print("ERROR: %s" % e)