【问题标题】:IDAPython dump hex pres of opcode to fileIDAPython 将操作码的 hex pres 转储到文件
【发布时间】:2019-02-15 09:33:19
【问题描述】:

我尝试将操作码旁边的操作码的十六进制表示转储到文本文件中,但我还没有真正成功。这就是现在的样子:

.init_proc   start: 6a0  end: 6b0
6a0  å-à   PUSH  {LR}; _init
6a4  ëO    BL    frame_dummy
6a8  ë¨    BL    __do_global_ctors_aux
6ac  äð    POP   {PC}

strerror     start: 6c4  end: 6d0
6c4  âÆ    ADR   R12, 0x6CC
6c8  âŒÊ   ADD   R12, R12, #0x8000
6cc  å¼þ`  LDR   PC, [R12,#(strerror_ptr - 0x86CC)]!; __imp_strerror

不幸的是,get_bytes 函数只返回一个字符串而不是一个整数,所以 我无法将其转换为十六进制。有没有其他方法可以做到这一点? 这是我的 idapython 脚本:

cur_addr = 0

with open("F:/Ida_scripts/ida_output.txt", "w") as f:
    for func in Functions():
        start = get_func_attr(func, FUNCATTR_START)
        end = get_func_attr(func, FUNCATTR_END)
        f.write("%s\t start: %x\t end: %x" % (get_func_name(func), start, end))
        cur_addr = start
        while cur_addr <= end:
            f.write("\n%x\t%s\t%s" % (curr_addr, get_bytes(cur_addr, get_item_size(curr_addr)), generate_disasm_line(cur_addr, 0)))
            cur_addr = next_head(cur_addr, end)
        f.write("\n\n")

【问题讨论】:

    标签: python hex ida opcode


    【解决方案1】:

    如果get_bytes() 返回一个字符串,那么我假设您想将此字符串中的每个字节转换为十六进制并打印出来。试试这个:

    print(' '.join('%02x' % ord(c) for c in get_bytes(…))
    

    这将打印如下内容:

    61 62 63 64
    

    (对于 'abcd' 作为输入。)

    或者作为一个函数:

    def str_to_hex(s):
      return ' '.join('%02x' % ord(c) for c in s)
    

    请注意,在 Python3 中,str 类型是 unicode 数据类型,因此它不仅仅是每个字符一个字节;那里你有字节数组的bytes 类型(也许你的get_bytes() 应该返回这个而不是字符串)。在 Python2 中,str 类型是字节数组,unicode 类型用于 unicode 字符串。我不知道你在开发哪个 Python 版本。

    【讨论】:

    • 谢谢一百万!这正是我所需要的。不幸的是,get_bytes() 函数是 IDA 的 idc 库的一部分。
    猜你喜欢
    • 2014-01-04
    • 2014-11-14
    • 2018-03-11
    • 1970-01-01
    • 2010-09-22
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    相关资源
    最近更新 更多