forked from theo-abel/snapchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghidra_basic_blocks.py
85 lines (71 loc) · 2.38 KB
/
ghidra_basic_blocks.py
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
#!/usr/bin/env python
import tempfile
import subprocess
import os
import sys
import argparse
from pathlib import Path
ghidra_dir_guess = None
if "SNAPCHANGE_ROOT" in os.environ:
ghidra_dir_guess = Path(os.environ["SNAPCHANGE_ROOT"]) / "ghidra"
else:
ghidra_dir_guess = Path(__file__).parent.parent / "ghidra"
ghidra_dir_guess = ghidra_dir_guess.resolve()
# Prepare command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("binary", help="The binary to analyze with Ghidra", type=Path)
parser.add_argument(
"--ghidra-dir",
help="The path to the Ghidra directory",
default=ghidra_dir_guess,
type=Path,
)
parser.add_argument(
"--base-addr", help="Base address to rebase the binary", type=lambda i: int(i, 0)
)
# Parse command line arguments
args = parser.parse_args()
# Get the path to the headless binary
headless = Path(args.ghidra_dir) / "support" / "analyzeHeadless"
if not headless.exists():
print(
"Couldn't find ghidra directory... please specify with --ghidra-dir",
file=sys.stderr,
)
sys.exit(1)
if not args.binary.exists():
print("non-existing binary path:", args.binary, file=sys.stderr)
sys.exit(1)
# Analyze the binary using a temp directory
with tempfile.TemporaryDirectory() as tempdir:
project_dir = tempfile.TemporaryDirectory(dir=tempdir)
# output_dir = tempfile.TemporaryDirectory(dir=tempdir)
this_dir = Path(__file__).resolve().parent
# output_file = output_dir.name + "/output"
# print(f"This dir {this_dir}")
# Create the command to execute the python script
command = [
headless,
project_dir.name,
"temp",
"-import",
args.binary.resolve(),
"-scriptPath",
this_dir,
"-postScript",
"ghidra_script_bb_worker.py",
]
# If there is a new base address via command line, add it to the command
# to be picked up by Ghidra
if args.base_addr:
command.append(hex(args.base_addr))
command = list(map(str, command))
# Execute the command
output = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# print(output.stdout.decode())
print(output.stderr.decode(), file=sys.stderr)
# Only print lines from Ghidra for our executed script
for line in output.stdout.decode().split("\n"):
if "bb_worker.py>" not in line:
continue
print(line)