← 都来码 / Dolaima← 例程索引python/school/_unsupported/043_pedasm.py 原文

python/school/_unsupported/043_pedasm.py · 826 字节 · 下载原文

import pefile  
import pydasm  
import sys

pe = pefile.PE(r"c:\windows\notepad.exe")  

console = sys.stdout 
f = open('pe_dasm.txt', 'w')
sys.stdout = f

ep = pe.OPTIONAL_HEADER.AddressOfEntryPoint 
ep_ava = ep + pe.OPTIONAL_HEADER.ImageBase 
data = pe.get_memory_mapped_image()[ep:] 
offset = 0 
while offset < len(data): 
    i = pydasm.get_instruction(data[offset:], pydasm.MODE_32)
    instruction = pydasm.get_instruction_string(i, pydasm.FORMAT_INTEL, ep_ava+offset)
    if instruction != None:
        print hex(ep + offset), '\t', instruction
    else:
        break
    try:
        offset += i.length
    # 语料修正(2026-11-06):原文是 Python 2 的 `except BaseException,e:`,CPython 3 这一行直接 SyntaxError;改成 `as`
    except BaseException as e:
        break

f.close()
sys.stdout = console