-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsession_lifecycle.py
More file actions
136 lines (117 loc) · 4.4 KB
/
Copy pathsession_lifecycle.py
File metadata and controls
136 lines (117 loc) · 4.4 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
#!/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/.
#
# session_lifecycle: Session management, mutations, revisions, discard, and save.
#
# Usage: python session_lifecycle.py [host_url]
#
# Demonstrates status checks, capabilities, revision tracking,
# function renaming, discard (undo), and save.
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. Check host status
print("--- Host status ---")
try:
status = client.get_status()
except ghidra.GhidraError as e:
print(f"Cannot reach host at {url}: {e}", file=sys.stderr)
sys.exit(1)
print(f" Service: {status.service_name} v{status.service_version}")
print(f" Mode: {status.host_mode}")
print(f" Revision: {status.modification_number}")
print(f" OK: {status.ok}")
if status.warnings:
for w in status.warnings:
print(f" Warning: {w}")
# 2. List capabilities
print("\n--- Capabilities ---")
try:
caps = client.get_capabilities()
for cap in caps:
note = f" ({cap.note})" if cap.note else ""
print(f" {cap.id}: {cap.status}{note}")
except ghidra.GhidraError as e:
print(f"GetCapabilities failed: {e}", file=sys.stderr)
# 3. Get current revision
print("\n--- Current revision ---")
try:
rev_resp = client.get_revision()
rev_before = rev_resp.modification_number
print(f" Revision: {rev_before}")
except ghidra.GhidraError as e:
print(f"GetRevision failed: {e}", file=sys.stderr)
sys.exit(1)
# 4. Find a function to rename
try:
funcs = client.list_functions(limit=10)
except ghidra.GhidraError as e:
print(f"ListFunctions failed: {e}", file=sys.stderr)
sys.exit(1)
if not funcs.functions:
print("No functions available.", file=sys.stderr)
sys.exit(1)
target = funcs.functions[0]
addr = target.entry_address
original_name = target.name
# 5. Rename the function to create a mutation
temp_name = "lifecycle_test_renamed"
print(f"\n--- Renaming 0x{addr:x} '{original_name}' -> '{temp_name}' ---")
try:
resp = client.rename_function(addr, temp_name)
print(f" Renamed: {resp.renamed}, new name: {resp.name}")
except ghidra.GhidraError as e:
print(f"RenameFunction failed: {e}", file=sys.stderr)
sys.exit(1)
# 6. Check revision after mutation
print("\n--- Revision after mutation ---")
try:
rev_resp = client.get_revision()
rev_after = rev_resp.modification_number
print(f" Revision before: {rev_before}")
print(f" Revision after: {rev_after}")
print(f" Changed: {rev_after != rev_before}")
except ghidra.GhidraError as e:
print(f"GetRevision failed: {e}", file=sys.stderr)
# 7. Discard changes (undo the rename)
print("\n--- Discarding changes ---")
try:
resp = client.discard_program()
print(f" Discarded: {resp.discarded}")
except ghidra.GhidraError as e:
print(f"DiscardProgram failed: {e}", file=sys.stderr)
sys.exit(1)
# 8. Verify the function name is restored
print("\n--- Verifying name restored ---")
try:
funcs = client.list_functions(limit=10)
restored = None
for f in funcs.functions:
if f.entry_address == addr:
restored = f
break
if restored:
print(f" Address: 0x{addr:x}")
print(f" Name: {restored.name}")
print(f" Restored: {restored.name == original_name}")
else:
print(f" Function at 0x{addr:x} not found after discard.")
except ghidra.GhidraError as e:
print(f"ListFunctions failed: {e}", file=sys.stderr)
# 9. Save program (commits current state to disk)
print("\n--- Saving program ---")
try:
resp = client.save_program()
print(f" Saved: {resp.saved}")
except ghidra.GhidraError as e:
print(f"SaveProgram failed: {e}", file=sys.stderr)
print("\nDone.")
if __name__ == "__main__":
main()