forked from IIIImmmyyy/AntiOllvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathida_rebuild_cfg.py
56 lines (47 loc) · 1.75 KB
/
ida_rebuild_cfg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
import idc
import ida_bytes
import idaapi
import ida_kernwin
# 选择 JSON 文件
json_file_path = ida_kernwin.ask_file(0, "*.json", "please choose fix.json when gen_machine_code.py is executed")
if not json_file_path:
print("no json file selected")
else:
# 读取并解析 JSON 文件
with open(json_file_path, "r") as json_file:
data = json.load(json_file)
# 遍历每一个补丁项
for item in data:
addr_str = item.get("address")
fix_code_bytes_str = item.get("fix_machine_code_bytes")
if not addr_str or not fix_code_bytes_str:
print(f"jump : {item}")
continue
try:
# 将地址字符串转换为整数
addr = int(addr_str, 16)
except ValueError:
print(f"error address: {addr_str}")
continue
try:
# 将十六进制字符串转换为字节
machine_bytes = bytes.fromhex(fix_code_bytes_str)
print(f"机器码 '{fix_code_bytes_str}' 转换为字节。")
except ValueError as e:
print(f"转换机器码 '{fix_code_bytes_str}' 时出错: {e}")
continue
# 检查地址是否在可写范围内
if not ida_bytes.is_mapped(addr):
print(f"地址 {addr_str} 未在当前二进制文件中映射。")
continue
try:
# 补丁机器码到指定地址
ida_bytes.patch_bytes(addr, machine_bytes)
print(f"成功补丁地址 {addr_str} 以机器码 '{fix_code_bytes_str}'.")
except Exception as e:
print(f"在地址 {addr_str} 补丁时出错: {e}")
continue
# 刷新 IDA 的显示
idaapi.refresh_idaview_anyway()
print("patch success ")