Replies: 2 comments 12 replies
-
I think my answer to this question is what you are looking for: #7932 |
Beta Was this translation helpful? Give feedback.
4 replies
-
@bdemick I've been working on a brand new PyGhidra API and was interested in getting your feedback. Here's an example script that does various generic things. Note there are no explicit imports of any Java classes to do this stuff. import os, jpype, pyghidra
pyghidra.start()
# Open our existing "Test" project
with pyghidra.open_project(os.environ["GHIDRA_PROJECT_DIR"], "ExampleProject", create=True) as project:
# Walk a Ghidra release zip file, load every decompiler binary, and save them to the project
with pyghidra.open_filesystem(f"{os.environ['DOWNLOADS_DIR']}/ghidra_11.4_PUBLIC_20250620.zip") as fs:
loader = pyghidra.program_loader().project(project)
for f in fs.files(lambda f: "os/" in f.path and f.name.startswith("decompile")):
loader.source(f.getFSRL()).projectFolderPath("/" + f.parentFile.name)
with loader.load() as load_results:
load_results.save(pyghidra.dummy_monitor())
# Analyze the windows decompiler program
with pyghidra.program_context(project, "/win_x86_64/decompile.exe") as program:
analysis_props = pyghidra.analysis_properties(program)
with pyghidra.transaction(program):
analysis_props.setBoolean("Non-Returning Functions - Discovered", False)
pyghidra.analyze(program)
program.save("Analyzed", pyghidra.dummy_monitor())
# Walk the project and set a propery in each decompiler program
def set_property(domain_file, program):
with pyghidra.transaction(program):
program_info = pyghidra.program_info(program)
program_info.setString("PyGhidra Property", "Set by PyGhidra!")
program.save("Setting property", pyghidra.dummy_monitor())
pyghidra.walk_programs(project, set_property, program_filter=lambda f, p: p.name.startswith("decompile"))
# Load some bytes as a new program
ByteArrayCls = jpype.JArray(jpype.JByte)
my_bytes = ByteArrayCls(b"\xaa\xbb\xcc\xdd\xee\xff")
loader = pyghidra.program_loader().project(project).source(my_bytes).name("my_bytes")
loader.loaders("BinaryLoader").language("DATA:LE:64:default")
with loader.load() as load_results:
load_results.save(pyghidra.dummy_monitor())
# Run a GhidraScript
pyghidra.ghidra_script(f"{os.environ['GHIDRA_SCRIPTS_DIR']}/HelloWorldScript.java", project) |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to run a Ghidra Java script from a standalone Python script?
pyghidra.run_script
doesn't quite seem to fit the bill as it looks to be mimickinganalyzeHeadless
, and if I remember correctly, the recommendation is to stay away from using theGhidraScript
API in a standalone Python script. The ideal workflow for me would be to be able to choose existing scripts to run after loading the binary.Beta Was this translation helpful? Give feedback.
All reactions