-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtype_system.py
More file actions
154 lines (132 loc) · 5.25 KB
/
Copy pathtype_system.py
File metadata and controls
154 lines (132 loc) · 5.25 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/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/.
#
# type_system: Type system overview -- structs, aliases, enums, and full lifecycle.
#
# Usage: python type_system.py [host_url]
#
# Defaults: http://127.0.0.1:18080, expects a program already open in Ghidra.
import sys
import libghidra as ghidra
def main() -> None:
url = sys.argv[1] if len(sys.argv) >= 2 else "http://127.0.0.1:18080"
client = ghidra.connect(url)
# 1. List existing types
try:
types_resp = client.list_types(limit=10)
except ghidra.GhidraError as e:
print(f"list_types failed: {e}", file=sys.stderr)
sys.exit(1)
types = types_resp.types
print(f"Existing types ({len(types)} shown):")
for t in types:
defined = "" if not t.is_not_yet_defined else " [undefined]"
print(f" {t.name:<28} kind={t.kind:<10} size={t.length:>4}{defined}")
# 2. Get details on a specific type
if types:
sample = types[0]
print(f"\nDetails for '{sample.name}':")
try:
detail = client.get_type(sample.path_name)
if detail.type:
t = detail.type
print(f" id={t.type_id} path={t.path_name} category={t.category_path}")
print(f" display={t.display_name} kind={t.kind} length={t.length}")
if t.source_archive:
print(f" source_archive={t.source_archive}")
except ghidra.GhidraError as e:
print(f" get_type failed: {e}", file=sys.stderr)
# 3. Create a struct type
struct_name = "ExampleStruct"
print(f"\nCreating struct '{struct_name}' (size=16)...")
try:
create_resp = client.create_type(struct_name, "struct", 16)
print(f" updated={create_resp.updated}")
except ghidra.GhidraError as e:
print(f" create_type failed: {e}", file=sys.stderr)
sys.exit(1)
# Verify creation
verify = client.get_type(f"/{struct_name}")
if verify.type:
print(f" Verified: {verify.type.name} (kind={verify.type.kind}, size={verify.type.length})")
# 4. Create a type alias pointing to our struct
alias_name = "ExampleAlias"
print(f"\nCreating alias '{alias_name}' -> '{struct_name}'...")
try:
alias_resp = client.create_type_alias(alias_name, struct_name)
print(f" updated={alias_resp.updated}")
except ghidra.GhidraError as e:
print(f" create_type_alias failed: {e}", file=sys.stderr)
# 5. Create an enum type
enum_name = "ExampleEnum"
print(f"\nCreating enum '{enum_name}' (width=4)...")
try:
enum_resp = client.create_type_enum(enum_name, width=4, is_signed=False)
print(f" updated={enum_resp.updated}")
except ghidra.GhidraError as e:
print(f" create_type_enum failed: {e}", file=sys.stderr)
# 6. Rename the struct type
renamed = "ExampleStructRenamed"
print(f"\nRenaming '{struct_name}' -> '{renamed}'...")
try:
ren_resp = client.rename_type(f"/{struct_name}", renamed)
print(f" updated={ren_resp.updated} name={ren_resp.name}")
except ghidra.GhidraError as e:
print(f" rename_type failed: {e}", file=sys.stderr)
renamed = struct_name # fall back for cleanup
# 7. List aliases, unions, and enums
print("\nType aliases:")
try:
aliases = client.list_type_aliases(limit=10)
for a in aliases.aliases:
print(f" {a.name}")
if not aliases.aliases:
print(" (none)")
except ghidra.GhidraError as e:
print(f" list_type_aliases failed: {e}", file=sys.stderr)
print("\nType unions:")
try:
unions = client.list_type_unions(limit=10)
for u in unions.unions:
print(f" {u.name}")
if not unions.unions:
print(" (none)")
except ghidra.GhidraError as e:
print(f" list_type_unions failed: {e}", file=sys.stderr)
print("\nType enums:")
try:
enums = client.list_type_enums(limit=10)
for en in enums.enums:
print(f" {en.name}")
if not enums.enums:
print(" (none)")
except ghidra.GhidraError as e:
print(f" list_type_enums failed: {e}", file=sys.stderr)
# 8. Clean up -- delete all created types (reverse order of dependencies)
print("\nCleaning up...")
# Delete alias first (depends on struct)
try:
client.delete_type_alias(f"/{alias_name}")
print(f" Deleted alias '{alias_name}'")
except ghidra.GhidraError as e:
print(f" delete alias failed: {e}", file=sys.stderr)
# Delete enum
try:
client.delete_type_enum(f"/{enum_name}")
print(f" Deleted enum '{enum_name}'")
except ghidra.GhidraError as e:
print(f" delete enum failed: {e}", file=sys.stderr)
# Delete struct (renamed)
try:
client.delete_type(f"/{renamed}")
print(f" Deleted struct '{renamed}'")
except ghidra.GhidraError as e:
print(f" delete struct failed: {e}", file=sys.stderr)
print("\nDone.")
if __name__ == "__main__":
main()