generated from clearbluejar/ghidra-python-vscode-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsample_bridge.py
87 lines (68 loc) · 2.56 KB
/
sample_bridge.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
86
87
# TODO write a description for this script
# @author
# @category Functions
# @keybinding
# @menupath
# @toolbar
# TODO Add User Code Here
# Section to make autocomplete work
try:
import ghidra
from ghidra_builtins import *
except:
pass
####
import ghidra_bridge
# Start ghidra-bridge-server before we are able to connect so we can pass args
import os
import subprocess
def is_port_in_use(port: int) -> bool:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
GHIDRA_HEADLESS = '/ghidra/support/analyzeHeadless'
PROJECT_NAME = 'sample_pyhidra'
PROJECT_LOCATION = '.ghidra_projects'
PROJECT_PATH = os.path.join(PROJECT_LOCATION, PROJECT_NAME)
GHIDRA_BRIDGE_INSTALL_DIR = '.ghidra_bridge'
BINARY = "ls"
args = [GHIDRA_HEADLESS, PROJECT_PATH, PROJECT_NAME, '-scriptPath',
GHIDRA_BRIDGE_INSTALL_DIR, "-postscript", 'ghidra_bridge_server.py', BINARY]
print(' '.join(args))
proc = None
BRIDGE_PORT = 4768
try:
proc = subprocess.Popen(args, shell=False, preexec_fn=os.setsid)
# Wait for ghidra_bridge_server to be ready
import time
while not is_port_in_use(BRIDGE_PORT):
time.sleep(1)
print("waiting for ghidra_bridge_server...")
with ghidra_bridge.GhidraBridge(namespace=globals(), response_timeout=4, ):
project = state.getProject()
projectData = project.getProjectData()
rootFolder = projectData.getRootFolder()
print(project)
print(projectData)
print(rootFolder)
prog = askProgram("program")
print("Program Info:")
program_name = prog.getName()
creation_date = prog.getCreationDate()
language_id = prog.getLanguageID()
compiler_spec_id = prog.getCompilerSpec().getCompilerSpecID()
print("Program: {}: {}_{} ({})\n".format(program_name, language_id, compiler_spec_id, creation_date))
# Get info about the current program's memory layout
print("Memory layout:")
print("Imagebase: " + hex(prog.getImageBase().getOffset()))
for block in prog.getMemory().getBlocks():
start = block.getStart().getOffset()
end = block.getEnd().getOffset()
print("{} [start: 0x{}, end: 0x{}]".format(block.getName(), start, end))
# Give time for bridge connection to close
time.sleep(2)
finally:
# Terminate ghidra_bridge_server to prevent another one starting next time
import signal
print(f"Shutting down ghidra_bridge_server : {proc.pid}")
os.killpg(os.getpgid(proc.pid), signal.SIGINT)