[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import os
import sys
import ctypes
class MasterPDFPatcher:
def __init__(self, root):
self.root = root
self.root.title("Master PDF Editor 补丁工具")
self.root.geometry("600x400")
self.root.resizable(False, False)
# 设置中文字体
self.font = ('SimHei', 10)
# 文件路径变量
self.file_path = tk.StringVar()
self.target_address = tk.StringVar(value="00007FF7EC0C255C")
self.target_instruction = tk.StringVar(value="mov bl,1")
self.machine_code = tk.StringVar(value="B301") # mov bl,1的机器码
self.create_widgets()
def create_widgets(self):
# 创建主框架
main_frame = ttk.Frame(self.root, padding="20")
main_frame.pack(fill=tk.BOTH, expand=True)
# 文件选择部分
ttk.Label(main_frame, text="Master PDF Editor 可执行文件:", font=self.font).grid(row=0, column=0, sticky=tk.W,
pady=5)
file_frame = ttk.Frame(main_frame)
file_frame.grid(row=1, column=0, columnspan=2, sticky=tk.EW, pady=5)
ttk.Entry(file_frame, textvariable=self.file_path, width=50, font=self.font).pack(side=tk.LEFT, fill=tk.X,
expand=True, padx=(0, 10))
ttk.Button(file_frame, text="浏览...", command=self.browse_file, width=10).pack(side=tk.RIGHT)
# 地址设置部分
ttk.Label(main_frame, text="目标内存地址:", font=self.font).grid(row=2, column=0, sticky=tk.W, pady=5)
ttk.Entry(main_frame, textvariable=self.target_address, width=30, font=self.font).grid(row=2, column=1,
sticky=tk.W, pady=5)
# 指令设置部分
ttk.Label(main_frame, text="要替换的指令:", font=self.font).grid(row=3, column=0, sticky=tk.W, pady=5)
ttk.Entry(main_frame, textvariable=self.target_instruction, width=30, font=self.font).grid(row=3, column=1,
sticky=tk.W, pady=5)
# 机器码设置部分
ttk.Label(main_frame, text="对应的机器码 (十六进制):", font=self.font).grid(row=4, column=0, sticky=tk.W,
pady=5)
ttk.Entry(main_frame, textvariable=self.machine_code, width=30, font=self.font).grid(row=4, column=1,
sticky=tk.W, pady=5)
# 状态显示
self.status_var = tk.StringVar(value="就绪")
ttk.Label(main_frame, textvariable=self.status_var, font=self.font, foreground="blue").grid(row=5, column=0,
columnspan=2,
pady=10)
# 操作按钮
button_frame = ttk.Frame(main_frame)
button_frame.grid(row=6, column=0, columnspan=2, pady=20)
ttk.Button(button_frame, text="备份文件", command=self.backup_file, width=15).pack(side=tk.LEFT, padx=10)
ttk.Button(button_frame, text="执行修改", command=self.patch_file, width=15).pack(side=tk.LEFT, padx=10)
ttk.Button(button_frame, text="退出", command=self.root.quit, width=15).pack(side=tk.LEFT, padx=10)
# 添加说明文本(修复变量名和作用域问题)
instruction_text = """
注意事项:
1. 请先备份原始文件,以防操作失误
2. 修改前请关闭Master PDF Editor程序
3. 可能需要以管理员权限运行此工具
4. 本工具仅用于学习和研究目的
"""
# 确保在main_frame的作用域内使用它
ttk.Label(main_frame, text=instruction_text, font=self.font, justify=tk.LEFT).grid(
row=7, column=0, columnspan=2, pady=10, sticky=tk.W)
# 配置列权重
main_frame.columnconfigure(1, weight=1)
def browse_file(self):
file_path = filedialog.askopenfilename(
title="选择masterpdfeditor.exe",
filetypes=[("可执行文件", "*.exe"), ("所有文件", "*.*")]
)
if file_path:
self.file_path.set(file_path)
def backup_file(self):
exe_path = self.file_path.get()
if not exe_path or not os.path.exists(exe_path):
messagebox.showerror("错误", "请先选择有效的可执行文件")
return
backup_path = f"{exe_path}.backup"
try:
# 复制文件作为备份
with open(exe_path, 'rb') as src, open(backup_path, 'wb') as dst:
dst.write(src.read())
self.status_var.set(f"已创建备份: {backup_path}")
messagebox.showinfo("成功", f"备份文件已创建: {backup_path}")
except Exception as e:
self.status_var.set(f"备份失败: {str(e)}")
messagebox.showerror("错误", f"备份失败: {str(e)}")
def patch_file(self):
exe_path = self.file_path.get()
if not exe_path or not os.path.exists(exe_path):
messagebox.showerror("错误", "请先选择有效的可执行文件")
return
# 检查是否以管理员权限运行
if not self.is_admin():
messagebox.showwarning("警告", "建议以管理员权限运行此程序,否则可能无法修改文件")
try:
# 转换地址为整数 (去掉0x前缀)
address_str = self.target_address.get().replace("0x", "")
target_address = int(address_str, 16)
# 获取机器码
machine_code = self.machine_code.get()
# 转换为字节
patch_bytes = bytes.fromhex(machine_code)
# 打开文件并修改
with open(exe_path, 'r+b') as f:
# 移动到目标地址
f.seek(target_address)
# 写入新的机器码
f.write(patch_bytes)
self.status_var.set("修改成功!")
messagebox.showinfo("成功", "文件修改成功!")
except Exception as e:
self.status_var.set(f"修改失败: {str(e)}")
messagebox.showerror("错误", f"修改失败: {str(e)}")
def is_admin(self):
"""检查程序是否以管理员权限运行"""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if __name__ == "__main__":
root = tk.Tk()
app = MasterPDFPatcher(root)
root.mainloop()