JavaScript bindings for Ghidra (Reverse Engineering framework)
npm install ghidra.jsFile -> Install Extensions, click the + symbol, and select the downloaded archive. The extension will be active in the next Ghidra launch. Alternatively, you can install via npm, ensuring Ghidra's installation folder is in your PATH (the folder containing the ghidraRun script). For example:``bash`
export PATH="$PATH:/path/to/your/Ghidra"
npm install -g ghidra.js
`javascript
// JavaHelper.getClass is a helper method to import Java classes
const EmulatorHelper = JavaHelper.getClass('ghidra.app.emulator.EmulatorHelper');
const domainFile = currentProgram.getDomainFile();
console.log('Program Name:', currentProgram.getName());
console.log('Program Path:', domainFile.getPathname());
console.log('File Format:', currentProgram.getExecutableFormat());
console.log('Language:', currentProgram.getLanguageID().getIdAsString());
console.log('Compiler Spec:', currentProgram.getCompilerSpec().getCompilerSpecID().getIdAsString());
// To make changes, use Ghidra's transaction API
// This is to give users more flexibility without automatic setup
const id = currentProgram.startTransaction('Hello world comment');
const functionManager = currentProgram.getFunctionManager();
const symbols = currentProgram.getSymbolTable().getGlobalSymbols('main')
if (symbols) {
const [mainSymbol] = symbols;
const main = functionManager.getFunctionAt(mainSymbol.getAddress());
main.setComment('Hello world from JavaScript');
}
else {
console.log('[!] Main function not found');
}
currentProgram.endTransaction(id, true);
`
command. This is particularly useful for automated analysis or batch processing. Here’s an example command:`bash
/path/to/Ghidra/support/analyzeHeadless /path/to/projectDir -process yourExecutable -scriptPath /path/to/scripts -postScript YourScript.js
``Replace /path/to/Ghidra with the installation directory of Ghidra, /path/to/projectDir with the path to your project directory, yourExecutable with file you want to analyze, /path/to/scripts with the directory containing your script, and YourScript.js with the name of your JavaScript file.
These arguments are not criticisms of the alternatives but rather my reasoning for choosing the backend for the extension. The repository still contains code for working through Rhino and GraalJS, just in case.