-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathasync_explore.py
More file actions
71 lines (59 loc) · 2.62 KB
/
Copy pathasync_explore.py
File metadata and controls
71 lines (59 loc) · 2.62 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
#!/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/.
#
# async_explore: Async client usage with AsyncGhidraClient.
#
# Usage: python async_explore.py [host_url]
#
# Demonstrates the async context manager, status check, function listing,
# and decompilation using asyncio.
import asyncio
import sys
from libghidra import ConnectOptions
from libghidra.async_client import AsyncGhidraClient
async def main() -> None:
url = sys.argv[1] if len(sys.argv) >= 2 else "http://127.0.0.1:18080"
async with AsyncGhidraClient(ConnectOptions(base_url=url)) as client:
# 1. Check host status
print("--- Host status (async) ---")
status = await client.get_status()
print(f" Service: {status.service_name} v{status.service_version}")
print(f" Mode: {status.host_mode}")
print(f" OK: {status.ok}")
# 2. List functions
print("\n--- Functions (first 10, async) ---")
funcs = await client.list_functions(limit=10)
print(f" Total returned: {len(funcs.functions)}")
for f in funcs.functions:
print(f" 0x{f.entry_address:x} {f.name} ({f.size} bytes)")
# 3. Decompile the first function
if funcs.functions:
target = funcs.functions[0]
print(f"\n--- Decompiling {target.name} at 0x{target.entry_address:x} (async) ---")
resp = await client.get_decompilation(target.entry_address, timeout_ms=30000)
if resp.decompilation and resp.decompilation.pseudocode:
lines = resp.decompilation.pseudocode.strip().splitlines()
print(f" Prototype: {resp.decompilation.prototype}")
print(f" Lines of pseudocode: {len(lines)}")
# Show first 10 lines
preview = lines[:10]
for line in preview:
print(f" {line}")
if len(lines) > 10:
print(f" ... ({len(lines) - 10} more lines)")
else:
print(" Decompilation returned empty pseudocode.", file=sys.stderr)
# 4. Get capabilities
print("\n--- Capabilities (async) ---")
caps = await client.get_capabilities()
for cap in caps:
note = f" ({cap.note})" if cap.note else ""
print(f" {cap.id}: {cap.status}{note}")
print("\nDone.")
if __name__ == "__main__":
asyncio.run(main())