59 lines
2.5 KiB
Java
59 lines
2.5 KiB
Java
// Export relocation-tolerant Function ID hashes for cross-binary matching.
|
|
// @category Iggy
|
|
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.feature.fid.hash.FidHashQuad;
|
|
import ghidra.feature.fid.service.FidService;
|
|
import ghidra.program.model.listing.Function;
|
|
import ghidra.program.model.listing.FunctionIterator;
|
|
import ghidra.program.model.mem.MemoryBlock;
|
|
|
|
import java.io.File;
|
|
import java.io.PrintWriter;
|
|
|
|
public class ExportFidHashes extends GhidraScript {
|
|
@Override
|
|
protected void run() throws Exception {
|
|
String[] args = getScriptArgs();
|
|
if (args.length != 1) {
|
|
throw new IllegalArgumentException("expected output directory");
|
|
}
|
|
File outputDirectory = new File(args[0]);
|
|
if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) {
|
|
throw new IllegalStateException("could not create " + outputDirectory);
|
|
}
|
|
|
|
String programName = currentProgram.getName();
|
|
File output = new File(outputDirectory, programName.replaceAll("[^A-Za-z0-9_.-]", "_") + ".tsv");
|
|
FidService service = new FidService();
|
|
int exported = 0;
|
|
try (PrintWriter writer = new PrintWriter(output, "UTF-8")) {
|
|
writer.println("address\tname\tcode_units\tfull_hash\tspecific_hash\tspecific_extra");
|
|
FunctionIterator functions = currentProgram.getFunctionManager().getFunctions(true);
|
|
for (Function function : functions) {
|
|
if (!isRecoverableFunction(function) || monitor.isCancelled()) {
|
|
continue;
|
|
}
|
|
FidHashQuad hash = service.hashFunction(function);
|
|
if (hash == null) {
|
|
continue;
|
|
}
|
|
writer.printf("%s\t%s\t%d\t%016x\t%016x\t%d%n",
|
|
function.getEntryPoint(), function.getName(true),
|
|
hash.getCodeUnitSize() & 0xffff, hash.getFullHash(),
|
|
hash.getSpecificHash(), hash.getSpecificHashAdditionalSize() & 0xff);
|
|
exported++;
|
|
}
|
|
}
|
|
println("Exported " + exported + " FID hashes from " + programName + " to " + output);
|
|
}
|
|
|
|
private boolean isRecoverableFunction(Function function) {
|
|
if (function.isExternal() || function.getName(true).startsWith("<EXTERNAL>::")) {
|
|
return false;
|
|
}
|
|
MemoryBlock block = currentProgram.getMemory().getBlock(function.getEntryPoint());
|
|
return block != null && !block.getName().equalsIgnoreCase("EXTERNAL");
|
|
}
|
|
}
|