Skip to content

Commit

Permalink
ghidra: validate path before running docker and fix os name in ghidra…
Browse files Browse the repository at this point in the history
… script
  • Loading branch information
kumarak authored and xlauko committed Sep 20, 2024
1 parent 5ac4dbf commit 0285d81
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
26 changes: 16 additions & 10 deletions scripts/ghidra/PatchestryDecompileFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,21 @@ private String inferOSType(String executableFormat) throws Exception {
return "unknown";
}

return switch (executableFormat.toLowerCase()) {
case "pe" -> "windows";
case "elf" -> "linux";
case "mach-o" -> "macos";
default -> "unknown";
};
executableFormat = executableFormat.toLowerCase();
if (executableFormat.contains("pe")) {
return "windows";
} else if (executableFormat.contains("elf")) {
return "linux";
} else if (executableFormat.contains("mach-o")) {
return "macos";
} else {
return "unknown";
}
}

private String getOS() throws Exception {
String executableFormat = currentProgram.getExecutableFormat();
println("executableFormat " + executableFormat);
return inferOSType(executableFormat);
}

Expand Down Expand Up @@ -96,13 +101,13 @@ public JsonWriter serialize(Varnode node) throws Exception {

if (node.isConstant()) {
value("const");
}else if (node.isUnique()) {
} else if (node.isUnique()) {
value("unique");
}else if (node.isRegister()) {
} else if (node.isRegister()) {
value("register");
}else if (node.isAddress()) {
} else if (node.isAddress()) {
value("ram");
}else {
} else {
throw new Exception("Unknown Varnode kind.");
}

Expand Down Expand Up @@ -203,6 +208,7 @@ private void decompileSingleFunction() throws Exception {
}
String functionNameArg = getScriptArgs()[1];
String outputFilePath = getScriptArgs()[2];
println("OutputFilePath: " + outputFilePath);
final var functions = getGlobalFunctions(functionNameArg);
if (functions.isEmpty()) {
println("Function not found: " + functionNameArg);
Expand Down
1 change: 1 addition & 0 deletions scripts/ghidra/decompile-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options:
Specify the command to execute. Available commands are:
- list-functions: List all functions in the binary.
- decompile: Decompile a single function.
- decompile-all: Decompile all functions in the binary.
--function <FUNCTION_NAME>
Decompile a specific function to extract pcode. This option is required when using the 'decompile' command.
Expand Down
36 changes: 34 additions & 2 deletions scripts/ghidra/decompile-headless.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ validate_args() {

prepare_paths() {
INPUT_PATH=$(realpath "$INPUT_PATH")
OUTPUT_PATH=$(realpath "$OUTPUT_PATH")

if [ ! -e "$INPUT_PATH" ]; then
echo "Error: Input file does not exist: $INPUT_PATH"
Expand All @@ -99,6 +98,38 @@ prepare_paths() {
fi
touch "$OUTPUT_PATH"
fi
# realpath may fail of OUTPUT_PATH does not exist
OUTPUT_PATH=$(realpath "$OUTPUT_PATH")
}

is_not_absolute_path() {
case "$1" in
/*)
return 1
;;
*)
return 0
;;
esac
}

validate_paths() {
# CI_OUTPUT_FOLDER should be absolute to avoid any issue with mounting locations
if [ -n "$CI_OUTPUT_FOLDER" ] && is_not_absolute_path "$CI_OUTPUT_FOLDER"; then
echo "$CI_OUTPUT_FOLDER path is not absolute. Exiting!"
exit 1
fi

# Expect both input and output file to exist
if [! -f "$INPUT_PATH" ]; then
echo "Input file $INPUT_PATH doesn't exist. Exiting!"
exit 1
fi

if [! -f "$OUTPUT_PATH" ]; then
echo "Output file $OUTPUT_PATH doesn't exist. Exiting!"
exit 1
fi
}

build_docker_command() {
Expand Down Expand Up @@ -151,6 +182,7 @@ main() {
parse_args "$@"
validate_args
prepare_paths
validate_paths
build_docker_command

if [ "$VERBOSE" = true ]; then
Expand All @@ -161,4 +193,4 @@ main() {
eval "$RUN"
}

main "$@"
main "$@"

0 comments on commit 0285d81

Please sign in to comment.