-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcfg_analysis.py
More file actions
126 lines (102 loc) · 4.09 KB
/
Copy pathcfg_analysis.py
File metadata and controls
126 lines (102 loc) · 4.09 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
#!/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/.
#
# cfg_analysis: Analyze control flow graphs (basic blocks and edges).
#
# Usage: python cfg_analysis.py [host_url]
#
# Finds a non-trivial function, lists its basic blocks and CFG edges,
# builds an adjacency list, and identifies entry/exit blocks.
import sys
from collections import defaultdict
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. Verify connection
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"Connected: {status.service_name} v{status.service_version}")
# 2. Find a non-trivial function (size > 64 bytes)
print("\n--- Searching for a non-trivial function (size > 64 bytes) ---")
try:
funcs = client.list_functions(limit=100)
except ghidra.GhidraError as e:
print(f"ListFunctions failed: {e}", file=sys.stderr)
sys.exit(1)
target = None
for f in funcs.functions:
if f.size > 64:
target = f
break
if target is None:
print("No function larger than 64 bytes found.", file=sys.stderr)
sys.exit(1)
addr = target.entry_address
print(f"Selected: {target.name} at 0x{addr:x} ({target.size} bytes)")
# 3. List basic blocks for the function
print(f"\n--- Basic blocks for {target.name} ---")
try:
blocks_resp = client.list_basic_blocks(range_start=addr, range_end=addr)
blocks = blocks_resp.blocks
except ghidra.GhidraError as e:
print(f"ListBasicBlocks failed: {e}", file=sys.stderr)
sys.exit(1)
print(f"Total blocks: {len(blocks)}")
for b in blocks:
size = b.end_address - b.start_address
print(f" 0x{b.start_address:x} - 0x{b.end_address:x}"
f" ({size} bytes, in={b.in_degree}, out={b.out_degree})")
# 4. List CFG edges for the function
print(f"\n--- CFG edges for {target.name} ---")
try:
edges_resp = client.list_cfg_edges(range_start=addr, range_end=addr)
edges = edges_resp.edges
except ghidra.GhidraError as e:
print(f"ListCFGEdges failed: {e}", file=sys.stderr)
sys.exit(1)
print(f"Total edges: {len(edges)}")
for e in edges:
print(f" 0x{e.src_block_start:x} -> 0x{e.dst_block_start:x} (kind={e.edge_kind})")
# 5. Build adjacency list
print(f"\n--- Adjacency list ---")
successors = defaultdict(list)
predecessors = defaultdict(list)
block_addrs = {b.start_address for b in blocks}
for e in edges:
successors[e.src_block_start].append(e.dst_block_start)
predecessors[e.dst_block_start].append(e.src_block_start)
for b_addr in sorted(block_addrs):
succs = successors.get(b_addr, [])
succs_str = ", ".join(f"0x{s:x}" for s in succs) if succs else "(none)"
print(f" 0x{b_addr:x} -> [{succs_str}]")
# 6. Identify entry and exit blocks
print(f"\n--- Entry and exit blocks ---")
entry_blocks = [b for b in blocks if b.in_degree == 0]
exit_blocks = [b for b in blocks if b.out_degree == 0]
print(f"Entry blocks ({len(entry_blocks)}):")
for b in entry_blocks:
print(f" 0x{b.start_address:x}")
print(f"Exit blocks ({len(exit_blocks)}):")
for b in exit_blocks:
print(f" 0x{b.start_address:x}")
# 7. Print summary
print(f"\n--- Summary ---")
print(f" Function: {target.name}")
print(f" Address: 0x{addr:x}")
print(f" Size: {target.size} bytes")
print(f" Basic blocks: {len(blocks)}")
print(f" CFG edges: {len(edges)}")
print(f" Entry blocks: {len(entry_blocks)}")
print(f" Exit blocks: {len(exit_blocks)}")
print("\nDone.")
if __name__ == "__main__":
main()