GP-5521: pyghidra.open_program() now accepts a program_name parameter,

which can be used to override the program name derived from the
binary_path parameter
This commit is contained in:
Ryan Kurtz
2025-03-25 09:28:03 -04:00
parent 3acd4aede7
commit 907e5c563f
3 changed files with 23 additions and 11 deletions

View File

@@ -105,7 +105,8 @@ def open_program(
analyze=True,
language: str = None,
compiler: str = None,
loader: Union[str, JClass] = None
loader: Union[str, JClass] = None,
program_name: str = None
) -> ContextManager["FlatProgramAPI"]: # type: ignore
"""
Opens given binary path in Ghidra and returns FlatProgramAPI object.
@@ -122,6 +123,8 @@ def open_program(
(Defaults to the Language's default compiler)
:param loader: The `ghidra.app.util.opinion.Loader` class to use when importing the program.
This may be either a Java class or its path. (Defaults to None)
:param program_name: The name to of the program to open in Ghidra.
(Defaults to None, which results in the name being derived from "binary_path")
:return: A Ghidra FlatProgramAPI object.
:raises ValueError: If the provided language, compiler or loader is invalid.
:raises TypeError: If the provided loader does not implement `ghidra.app.util.opinion.Loader`.
@@ -306,7 +309,9 @@ import pdb # imports Python's pdb
import pdb_ # imports Ghidra's pdb
```
## Change History
__2.0.2:__
__2.1.0:__
* [`pyghidra.open_program()`](#pyghidraopen_program) now accepts a `program_name` parameter, which
can be used to override the program name derived from the `binary_path` parameter.
* [`pyghidra.open_program()`](#pyghidraopen_program) now properly throws an exception if the project
exists and is locked.

View File

@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
##
__version__ = "2.0.2"
__version__ = "2.1.0"
# stub for documentation and typing
# this is mostly to hide the function parameter

View File

@@ -81,13 +81,16 @@ def _setup_project(
project_name: str = None,
language: str = None,
compiler: str = None,
loader: Union[str, JClass] = None
loader: Union[str, JClass] = None,
program_name: str = None
) -> Tuple["GhidraProject", "Program"]:
from ghidra.base.project import GhidraProject
from java.lang import ClassLoader # type:ignore @UnresolvedImport
from ghidra.framework.model import ProjectLocator # type:ignore @UnresolvedImport
if binary_path is not None:
binary_path = Path(binary_path)
if program_name is None and binary_path is not None:
program_name = binary_path.name
if project_location:
project_location = Path(project_location)
else:
@@ -117,9 +120,9 @@ def _setup_project(
else:
project_location.mkdir(exist_ok=True, parents=True)
project = GhidraProject.createProject(project_location, project_name, False)
if binary_path is not None:
if project.getRootFolder().getFile(binary_path.name):
program = project.openProgram("/", binary_path.name, False)
if program_name is not None:
if project.getRootFolder().getFile(program_name):
program = project.openProgram("/", program_name, False)
# NOTE: GhidraProject.importProgram behaves differently when a loader is provided
# loaderClass may not be null so we must use the correct method override
@@ -146,7 +149,7 @@ def _setup_project(
else:
message += f"The provided language ({language}) may be invalid."
raise ValueError(message)
project.saveAs(program, "/", program.getName(), True)
project.saveAs(program, "/", program_name, True)
return project, program
@@ -198,10 +201,11 @@ def open_program(
analyze=True,
language: str = None,
compiler: str = None,
loader: Union[str, JClass] = None
loader: Union[str, JClass] = None,
program_name: str = None
) -> ContextManager["FlatProgramAPI"]: # type: ignore
"""
Opens given binary path in Ghidra and returns FlatProgramAPI object.
Opens given binary path (or optional program name) in Ghidra and returns FlatProgramAPI object.
:param binary_path: Path to binary file, may be None.
:param project_location: Location of Ghidra project to open/create.
@@ -215,6 +219,8 @@ def open_program(
(Defaults to the Language's default compiler)
:param loader: The `ghidra.app.util.opinion.Loader` class to use when importing the program.
This may be either a Java class or its path. (Defaults to None)
:param program_name: The name of the program to open in Ghidra.
(Defaults to None, which results in the name being derived from "binary_path")
:return: A Ghidra FlatProgramAPI object.
:raises ValueError: If the provided language, compiler or loader is invalid.
:raises TypeError: If the provided loader does not implement `ghidra.app.util.opinion.Loader`.
@@ -234,7 +240,8 @@ def open_program(
project_name,
language,
compiler,
loader
loader,
program_name
)
GhidraScriptUtil.acquireBundleHostReference()