From bcfa5e26270022bc3626c4b3e3dbfa8112612441 Mon Sep 17 00:00:00 2001 From: Gynt Date: Sat, 14 Nov 2026 15:58:32 +0100 Subject: [PATCH] Import headers from a .gzf without SARIF ghidra-importer.py --gzf (reimport-ghidra.ps1 -Gzf) reads the program straight from the gzf with skink's Project.from_gzf, which needs pyghidra. The headers are the same as from the SARIF route, in about half a minute instead of about ten. Duplicates from find_all_by_location are removed with skink's unique(), which gives the same result as the `obj not in objs` loop without comparing every pair. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XmXCmZDK7Sy2VkmmyZnN39 --- tools/import/README.md | 18 ++++++++++++++++++ tools/import/ghidra-importer.py | 20 +++++++++++--------- tools/import/reimport-ghidra.ps1 | 15 +++++++++++---- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/tools/import/README.md b/tools/import/README.md index 332152a03b..b16420f527 100644 --- a/tools/import/README.md +++ b/tools/import/README.md @@ -18,3 +18,21 @@ python -m pip install -r requirements.txt ``` > By default, all .hpp files are backed up to the tmp\backup\ folder. When the .sarif file has not changed between calls of this script, use `-ClearCache $false` to speed up the conversion. + +## Usage with a .gzf + +`ghidra-skink` can read the program straight from a Ghidra `.gzf` with pyghidra, so the SARIF export is not needed. +It generates the same header files in about half a minute instead of about ten minutes. + +0. Install pyghidra (also used by `gzftosarif.py`), and set `GHIDRA_INSTALL_DIR` to your Ghidra installation: + +```pwsh +python -m pip install pyghidra +``` + +1. Export the program from Ghidra as a .gzf (File > Export Program..., format "Ghidra Zip File"). +2. Execute the following from PowerShell: + +```pwsh + & .\tools\import\reimport-ghidra.ps1 -OverwriteAll $true -Gzf "path/to/gzf/file.gzf" +``` diff --git a/tools/import/ghidra-importer.py b/tools/import/ghidra-importer.py index fdd47dcb98..8ac38aa573 100644 --- a/tools/import/ghidra-importer.py +++ b/tools/import/ghidra-importer.py @@ -18,6 +18,7 @@ from skink.export.enums.enumfamilies import EnumFamily, collect_enum_families import pathlib from skink.export.styles.style3.exporter import Exporter, BinaryContext, TransformationRules, FileRules +from skink.utils.unique import unique from skink.export.context import DEFAULT, Context ctx: Context = DEFAULT.copy() # type: ignore @@ -29,6 +30,7 @@ import argparse parser = argparse.ArgumentParser() parser.add_argument("--sarif", required=False, default="Stronghold Crusader.exe.sarif") +parser.add_argument("--gzf", required=False, default=None, help="Read the program from this .gzf with pyghidra instead of from --sarif") parser.add_argument("--clear-cache", required=False, action=argparse.BooleanOptionalAction, default=False) parser.add_argument("--output-dir", required=False, default='src') parser.add_argument("--verbose", default=False, action=argparse.BooleanOptionalAction) @@ -48,9 +50,14 @@ print(args) exit(0) -project = Project(args.sarif, - cache_objects=args.cache, - cache_path=".cache" if args.cache else None) +if args.gzf: + # Read the program straight from the gzf with pyghidra, no SARIF export and no cache needed + logging.log(logging.INFO, f"reading program from: {args.gzf}") + project = Project.from_gzf(args.gzf) +else: + project = Project(args.sarif, + cache_objects=args.cache, + cache_path=".cache" if args.cache else None) # TODO: permit_overwrite = False logging.log(logging.INFO, "processing all symbol results") @@ -63,12 +70,7 @@ logging.log(logging.INFO, "processing all symbol results: finished") logging.log(logging.INFO, "finding all by location") -objs: List[BasicResult] = [] -objs_i = 0 -for obj in project.find_all_by_location(location="/_HoldStrong", recursive=True, lookup_lsymbols=True): - if obj not in objs: - objs_i += 1 - objs.append(obj) +objs: List[BasicResult] = unique(project.find_all_by_location(location="/_HoldStrong", recursive=True, lookup_lsymbols=True)) logging.log(logging.INFO, "finding all by location: finished") diff --git a/tools/import/reimport-ghidra.ps1 b/tools/import/reimport-ghidra.ps1 index 73fa6ba211..5820b1cdac 100644 --- a/tools/import/reimport-ghidra.ps1 +++ b/tools/import/reimport-ghidra.ps1 @@ -1,6 +1,8 @@ param( [Parameter(Mandatory = $false)] [string]$Sarif = "Stronghold Crusader.exe.sarif", + # Read the program straight from a .gzf with pyghidra (no SARIF export, no cache) + [string]$Gzf = "", [string]$OutputDir = "src", [bool]$ClearCache = $true, [bool]$OverwriteAll = $true, @@ -22,8 +24,13 @@ if ($Interactive) { $pyArgs += "$ScriptPath" -$pyArgs += "--sarif" -$pyArgs += $Sarif +if ($Gzf) { + $pyArgs += "--gzf" + $pyArgs += $Gzf +} else { + $pyArgs += "--sarif" + $pyArgs += $Sarif +} $pyArgs += "--output-dir" $pyArgs += "$OutputDir" @@ -63,12 +70,12 @@ if ($DryRun) { $hppFiles | Remove-Item } - if($GenerateSarif) { + if($GenerateSarif -and -not $Gzf) { Write-Output "Generating sarif file from gzf" & python .\tools\import\gzftosarif.py --sarif $Sarif } - Write-Output "Converting sarif to .hpp files" + Write-Output "Converting $(if ($Gzf) { $Gzf } else { $Sarif }) to .hpp files" # Execute the export & python $pyArgs