python - IDAPython 将操作码的十六进制 pres 转储到文件

标签 python hex ida opcode

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

.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")

最佳答案

如果 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版本。

关于python - IDAPython 将操作码的十六进制 pres 转储到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54706327/

相关文章:

python - 如何在cygwin中使用conda

python - 带有单个子图的 Matplotlib

将十六进制 C 数组转换为 Matlab vector

c - main 和 __libc_start_main 的区别

c - HexRays - 输出读取了从未写入的值?

assembly - 修补时 IDA "Invalid Operand"错误

使用 utf-8 编码的主题进行 Python IMAP 搜索

python - 绘制多个时间序列,单个日期序列和 py_date()

c++ - 我怎样才能阻止我的整数显示为十六进制?

Codevision 生成 `.ASM` 输出