【问题标题】:Replace specific string (after) in file in Python在Python中替换文件中的特定字符串(之后)
【发布时间】:2020-03-06 05:11:33
【问题描述】:

在 Python 中替换文件中的特定字符串(如下)

我有一个类似于以下内容的文本文件:

test   al,al
jne    0x514 <asm4+23>
mov    DWORD PTR [ebp-0x8],0x1
jmp    0x587 <asm4+138>
mov    edx,DWORD PTR [ebp-0x8]

我需要这样的结果

test   al,al
jne    asm4+23
mov    DWORD PTR [ebp-0x8],0x1
jmp    asm4+138
mov    edx,DWORD PTR [ebp-0x8]

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 在问题中以格式正确的文本形式展示您自己解决问题的努力和代码。
  • 两者到底有什么区别?你试过什么没用?

标签: python file assembly replace


【解决方案1】:

这可能适用于您的示例。我用过python正则表达式

import re

regex = r"(0.+<)(.+)>"
input_string="""
test   al,al
jne    0x514 <asm4+23>
mov    DWORD PTR [ebp-0x8],0x1
jmp    0x587 <asm4+138>
mov    edx,DWORD PTR [ebp-0x8]

"""
output=re.sub(r"(0.+<)(.+)>", r"\2", input_string)
print(output)

希望对你有帮助

【讨论】:

    【解决方案2】:

    您可以只使用“.startswith”方法:

    test = """
        test   al,al
        jne    0x514 <asm4+23>
        mov    DWORD PTR [ebp-0x8],0x1
        jmp    0x587 <asm4+138>
        mov    edx,DWORD PTR [ebp-0x8]
        """.splitlines()
    
    i = 0
    while i < len(test):
        if test[i].startswith("jne"):
            test[i] = "jne    0x514 <asm4+23>"
        elif test[i].startswith("jmp"):
            test[i] = "jmp    0x587 <asm4+138>"
        i += 1
    
    print("\n".join(test))
    

    【讨论】:

      【解决方案3】:

      您正在将反汇编转换为可以重新组装的东西。

      不要对 GDB 或objdump -drwC -Mintel 输出进行后处理,而是首先使用以实际 GAS 或 NASM 语法输出的反汇编程序,并在 cmets 中提供额外信息。专门针对 x86,Agner Fog's objconv。

      例如输出见How to generate assembly code with gcc that can be compiled with nasm。

      (我认为它不能针对 GAS .intel_syntax,只能针对 GAS、NASM/YASM 或 MASM。但 GAS .intel_syntax 除了指令之外类似于 MASM,因此这可能有用。或者如果你不'不介意AT&T语法,不用安装NASM就可以直接组装。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-07
        • 2017-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-02
        • 2023-04-05
        • 1970-01-01
        相关资源
        最近更新 更多