Skip to content

Instantly share code, notes, and snippets.

@leighleighleigh
Last active February 9, 2025 08:15
Show Gist options
  • Save leighleighleigh/d044339144d50ad19c4792a256cf4fe2 to your computer and use it in GitHub Desktop.
Save leighleighleigh/d044339144d50ad19c4792a256cf4fe2 to your computer and use it in GitHub Desktop.
Ghidra bulk file import with dynamic library search for portable executable files
#!/usr/bin/env bash
### Ghidra project bulk file import with DLL library resolution
### Warning, this file takes bloody ages to run! (40 minutes for 140 files)
### ... but it's still faster than using the GUI!
### Leigh Oliver 23rd September 2024
###
### EXAMPLE USAGE
# Import one file...
# ./import.sh ./MyProject.gpr ./coredll.dll
# Import a list of files...
# xargs -a ./list_of_files_to_import.txt -I{} ./import.sh ./MyProject.gpr {}
GPR_FILE="${1?}"
INPUT_FILE="${2?}"
# CHANGE THESE!!!
PROCESSOR='SuperH4:LE:32:default'
COMPILERSPEC='windows'
# This is needed if your DLLS live in a different folder, than the one your imported file lives in
EXTRADLLSEARCHDIR='/path/to/your/DLL/files/folder/with/a/trailing/forward/slash/'
echo "$PROCESSOR"
echo "$COMPILERSPEC"
echo "$EXTRADLLSEARCHDIR"
WGHD=$(which ghidra)
GHDBIN=$(realpath "$WGHD")
GHDDIR=$(dirname "$GHDBIN")
GHD="$GHDDIR/support/analyzeHeadless"
PROJ=$(realpath "$GPR_FILE")
PROJDIR=$(dirname "$PROJ")
PROJNAME=$(basename "${PROJ%.gpr}")
echo "$GHDBIN"
echo "$GHDDIR"
echo "$GHD"
echo "$PROJ"
echo "$PROJDIR"
## IMPORT ONLY VERSION, doesnt resolve libraries (faster?)
#$GHD "${PROJDIR}" "${PROJNAME}" \
#-processor $PROCESSOR -cspec $COMPILERSPEC \
#-import "${INPUT_FILE}"
### LOADER VERSION, resolves libraries, using flags from 'analyzeHeadlessREADME.html'
### If you aren't loading a PE file (Portable Executable), YOU WILL NEED TO CHANGE THESE SETTINGS!!!
### May need to be run multiple times, because files will depend on each other, and so will fail to link.
### Pay careful attention to the logs to figure that out!
$GHD "${PROJDIR}" "${PROJNAME}" \
-processor "$PROCESSOR" -cspec "$COMPILERSPEC" \
-loader PeLoader -loader-applyLabels true -loader-anchorLabels true \
-loader-linkExistingProjectLibraries true -loader-loadLocalLibraries true \
-loader-loadSystemLibraries true \
-loader-projectLibrarySearchFolder "$EXTRADLLSEARCHDIR" \
-import "$INPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment