-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmulti_program_strings.py
More file actions
89 lines (73 loc) · 2.73 KB
/
Copy pathmulti_program_strings.py
File metadata and controls
89 lines (73 loc) · 2.73 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
#!/usr/bin/env python3
# Copyright (c) 2024-2026 Elias Bachaalany
# SPDX-License-Identifier: LicenseRef-Human-Origin-Source-1.0
#
# multi_program_strings: launch one live Ghidra headless project, import and
# analyze one or more binaries, count defined strings for each active program,
# save the project, and shut down.
#
# Usage:
# python multi_program_strings.py <ghidra_dir> <project_dir> <project_name> <binary> [binary...]
from __future__ import annotations
import argparse
import sys
import libghidra as ghidra
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("ghidra_dir")
parser.add_argument("project_dir")
parser.add_argument("project_name")
parser.add_argument("binaries", nargs="+")
args = parser.parse_args()
host: ghidra.HeadlessClient | None = None
try:
host = ghidra.launch_headless_project(ghidra.HeadlessProjectOptions(
ghidra_dir=args.ghidra_dir,
project_dir=args.project_dir,
project_name=args.project_name,
port=0,
shutdown="save",
bind_attempts=5,
startup_timeout=600.0,
read_timeout=300.0,
))
for binary in args.binaries:
imported = host.import_program(ghidra.ImportProgramRequest(
source_path=binary,
overwrite=True,
analyze=True,
))
program_path = imported.primary_program_path
if not program_path:
print(f"ImportProgram returned no primary program path for {binary}", file=sys.stderr)
return 1
print(f"imported {program_path} source={binary}")
opened = host.open_program(ghidra.OpenProgramRequest(
program_path=program_path,
analyze=False,
read_only=False,
))
strings = host.list_defined_strings().strings
print(
f"strings {program_path} "
f"program={opened.program_name} count={len(strings)}"
)
closed = host.close_program(ghidra.ShutdownPolicy.SAVE)
if not closed.closed:
print(f"CloseProgram did not close {program_path}", file=sys.stderr)
return 1
exit_code = host.close(save=True)
host = None
if exit_code != 0:
print(f"Ghidra exited with code {exit_code}", file=sys.stderr)
return 1
print(f"saved project={args.project_name}")
return 0
except Exception as exc:
print(f"ERROR: {exc}", file=sys.stderr)
return 1
finally:
if host is not None:
host.close(save=True)
if __name__ == "__main__":
sys.exit(main())