-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathannotate_and_export.py
More file actions
138 lines (110 loc) · 4.72 KB
/
Copy pathannotate_and_export.py
File metadata and controls
138 lines (110 loc) · 4.72 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# Copyright (c) 2024-2026 Elias Bachaalany
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# annotate_and_export: Create types, rename functions, add comments, then
# decompile all functions and write the output to a file.
#
# Demonstrates mutation operations: CreateType, CreateTypeEnum, RenameFunction,
# SetComment, SetFunctionSignature, plus batch decompilation.
#
# Usage: python annotate_and_export.py [host_url] [output_file]
import sys
import libghidra as ghidra
def main() -> None:
host_url = sys.argv[1] if len(sys.argv) >= 2 else "http://127.0.0.1:18080"
output_file = sys.argv[2] if len(sys.argv) >= 3 else "decompiled.c"
client = ghidra.connect(host_url)
try:
status = client.get_status()
except ghidra.GhidraError as e:
print(f"Cannot reach host at {host_url}: {e}", file=sys.stderr)
sys.exit(1)
print(f"Connected: {status.service_name} v{status.service_version}")
# -- Create types ----------------------------------------------------------
print("\nCreating types...")
try:
client.create_type("context_t", "struct", 64)
print(" Created struct context_t (64 bytes)")
except ghidra.GhidraError as e:
print(f" CreateType(context_t): {e}")
try:
client.create_type_enum("error_code_t", width=4, is_signed=False)
print(" Created enum error_code_t (4 bytes)")
except ghidra.GhidraError as e:
print(f" CreateTypeEnum(error_code_t): {e}")
try:
client.add_type_enum_member("error_code_t", "ERR_NONE", 0)
client.add_type_enum_member("error_code_t", "ERR_INVALID", 1)
client.add_type_enum_member("error_code_t", "ERR_TIMEOUT", 2)
print(" Added 3 enum members to error_code_t")
except ghidra.GhidraError as e:
print(f" AddTypeEnumMember: {e}")
try:
client.add_type_member("context_t", "flags", "int", 4)
client.add_type_member("context_t", "status", "error_code_t", 4)
print(" Added 2 struct members to context_t")
except ghidra.GhidraError as e:
print(f" AddTypeMember: {e}")
# -- Rename functions and add comments -------------------------------------
print("\nAnnotating functions...")
funcs = client.list_functions(limit=5)
renames = [
("entry", "program_entry"),
("init", "initialize_subsystems"),
("main", "application_main"),
]
for f in funcs.functions:
# Try renaming well-known function names
for old, new in renames:
if f.name.lower().startswith(old):
try:
resp = client.rename_function(f.entry_address, new)
if resp.renamed:
print(f" Renamed {f.name} -> {new}")
except ghidra.GhidraError:
pass
# Add a plate comment to every function
try:
client.set_comment(
f.entry_address,
ghidra.CommentKind.PLATE,
f"Auto-analyzed by libghidra Python client\n"
f"Original name: {f.name}\n"
f"Size: {f.size} bytes, Params: {f.parameter_count}",
)
except ghidra.GhidraError:
pass
# -- Batch decompile -------------------------------------------------------
print(f"\nDecompiling all functions...")
all_funcs = client.list_functions()
total = len(all_funcs.functions)
results = client.list_decompilations(limit=total, timeout_ms=60000)
succeeded = sum(1 for d in results.decompilations if d.completed)
failed = total - succeeded
print(f" {succeeded}/{total} succeeded, {failed} failed")
# -- Export to file --------------------------------------------------------
print(f"\nWriting to {output_file}...")
with open(output_file, "w") as out:
out.write(f"// Decompiled output from {status.service_name}\n")
out.write(f"// {succeeded} functions\n\n")
for d in results.decompilations:
if not d.completed or not d.pseudocode:
continue
out.write(f"// ========== {d.function_name} @ 0x{d.function_entry_address:x} ==========\n")
out.write(d.pseudocode)
out.write("\n\n")
print(f" Wrote {succeeded} functions to {output_file}")
# -- Save ------------------------------------------------------------------
try:
client.save_program()
print("\nProgram saved.")
except ghidra.GhidraError as e:
print(f"\nSave failed: {e}")
print("Done.")
if __name__ == "__main__":
main()