Skip to content

Commit bbc1992

Browse files
committed
changed everything to use pyGhidra
1 parent b657cf2 commit bbc1992

21 files changed

Lines changed: 453 additions & 352 deletions

codecut-gui/ghidra_scripts/OutputObjFile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#@category CodeCut
2+
#@runtime PyGhidra
3+
#
14
## Copyright 2022 The Johns Hopkins University Applied Physics Laboratory LLC
25
## (JHU/APL). All Rights Reserved.
36
#
Binary file not shown.
Binary file not shown.
-8.88 KB
Binary file not shown.

codecut-gui/ghidra_scripts/decomprange.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def getFunctions(start_address, end_address):
3535
current_program.getAddressFactory().getAddress(end_address_str)
3636

3737
if start_address is None or end_address is None:
38-
print 'Invalid address range specified.'
38+
print('Invalid address range specified.')
3939
return
4040

4141
if start_address >= end_address:
42-
print 'Invalid address range: start address should be less than end address.'
42+
print('Invalid address range: start address should be less than end address.')
4343
return
4444

4545
decompiler = DecompInterface()

codecut-gui/ghidra_scripts/funcsig.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# @category CodeCut
12
from ghidra.program.model.listing import Function
23
from ghidra.program.model.symbol import SourceType
34

@@ -39,6 +40,7 @@ def get_referenced_function_signatures_base(function, monitor):
3940
return signatures
4041

4142

43+
4244
def getFunctionReferences(function, monitor):
4345
refs = set()
4446
instructions = \
@@ -47,9 +49,10 @@ def getFunctionReferences(function, monitor):
4749
for instr in instructions:
4850
flowType = instr.getFlowType()
4951
if flowType.isCall():
50-
target = instr.getOperandReferences(0)[0].getToAddress()
51-
func = \
52-
function.getProgram().getFunctionManager().getFunctionAt(target)
52+
oprefs = instr.getOperandReferences(0)
53+
if not oprefs: continue
54+
target = oprefs[0].getToAddress()
55+
func = function.getProgram().getFunctionManager().getFunctionAt(target)
5356
if func is not None:
5457
refs.add(func)
5558
return refs

codecut-gui/ghidra_scripts/globalvars.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get_global_variables(program, start_addr, end_addr):
1717
#set.addRange(start_addr, end_addr)
1818
print(start_address, end_address)
1919
print(addrset)
20-
20+
2121
#for symbol in symbol_table.getAllSymbols(False):
2222
for symbol in symbol_table.getSymbols(addrset,SymbolType.LABEL,True):
2323
print(symbol)
@@ -27,30 +27,30 @@ def get_global_variables(program, start_addr, end_addr):
2727
if (program.getListing().getDataAt(symbol.getAddress())):
2828
global_vars.append(symbol)
2929

30-
'''
30+
'''
3131
def is_user_defined(var):
3232
var_name = var.getName()
3333
var_addr = var.getAddress()
34-
34+
3535
if var_name.startswith('__') or var_name.startswith('_'):
3636
return False
37-
37+
3838
if var_name.startswith('imp_') or var_name.startswith('thunk_'):
3939
return False
40-
40+
4141
if var_name.startswith('fde_') or var_name.startswith('cie_'):
4242
return False
43-
43+
4444
if var_name.startswith('completed.0') \
4545
or var_name.startswith('data_start'):
4646
return False
47-
47+
4848
if var_addr.toString().startswith('EXTERNAL:'):
4949
return False
5050
section_name = program.getMemory().getBlock(var_addr).getName()
5151
#if section_name not in ['.data', '.bss']:
5252
# return False
53-
53+
5454
return True
5555
'''
5656

codecut-gui/ghidra_scripts/modnaming.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
# under Contract Number N66001-20-C-4024.
2222
#
2323

24+
import sys
25+
print(sys.executable)
2426

2527
import sys
2628
import math

codecut-gui/ghidra_scripts/range.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#@category AMP-Improved
1+
#@category CodeCut
2+
#@runtime PyGhidra
3+
24
from generate_c import generate_recompilable_c_code
35
import os
46
from ghidra.util.task import TaskMonitor

codecut-gui/src/main/java/codecutguiv2/CodeCutGUIPlugin.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import docking.widgets.OptionDialog;
5151
import docking.widgets.filechooser.GhidraFileChooser;
5252
import functioncalls.plugin.FcgProvider;
53+
import generic.jar.ResourceFile;
5354
import ghidra.app.CorePluginPackage;
5455
import ghidra.app.context.ProgramActionContext;
5556
import ghidra.app.context.ProgramContextAction;
@@ -58,9 +59,12 @@
5859
import ghidra.app.events.ProgramLocationPluginEvent;
5960
import ghidra.app.plugin.PluginCategoryNames;
6061
import ghidra.app.plugin.ProgramPlugin;
62+
import ghidra.app.plugin.core.analysis.AutoAnalysisManager;
6163
import ghidra.app.plugin.core.decompile.DecompilerProvider;
6264
import ghidra.app.plugin.core.symboltree.actions.*;
6365
import ghidra.app.script.GhidraScript;
66+
import ghidra.app.script.GhidraScriptProvider;
67+
import ghidra.app.script.GhidraScriptUtil;
6468
import ghidra.app.script.GhidraState;
6569
import ghidra.app.services.BlockModelService;
6670
import ghidra.app.services.GoToService;
@@ -88,8 +92,7 @@
8892
import ghidra.program.util.GhidraProgramUtilities;
8993
import ghidra.program.util.ProgramChangeRecord;
9094
import ghidra.program.util.ProgramLocation;
91-
import ghidra.jython.GhidraJythonInterpreter;
92-
import ghidra.jython.JythonScript;
95+
import ghidra.program.util.ProgramSelection;
9396
import ghidra.util.HTMLUtilities;
9497
import ghidra.util.HelpLocation;
9598
import ghidra.util.Msg;
@@ -1222,7 +1225,7 @@ public void exportC(String startAddr, String endAddr) {
12221225
}
12231226

12241227
}
1225-
private class CExporter extends JythonScript{
1228+
private class CExporter extends GhidraScript{
12261229
Program program = GhidraProgramUtilities.getCurrentProgram(tool);
12271230
GhidraState state = new GhidraState(tool, tool.getProject(), program, null, null, null);
12281231
String start_addr;
@@ -1234,7 +1237,6 @@ public CExporter(String start, String end, File file) {
12341237
this.start_addr = start;
12351238
this.end_addr = end;
12361239
this.outfile = file.getAbsolutePath();
1237-
this.state.addEnvironmentVar("ghidra.python.interpreter", GhidraJythonInterpreter.get());
12381240
this.path = this.outfile.substring(0, this.outfile.lastIndexOf("/")+1);
12391241
}
12401242
@Override
@@ -1245,7 +1247,8 @@ public void run() {
12451247
String[] args = {start_addr, end_addr, outfile};
12461248
try {
12471249
//runScript("ghidra2dwarf.py", args);
1248-
runScript("range.py",args);
1250+
runScript("range.py", args);
1251+
12491252
} catch (Exception e) {
12501253
e.printStackTrace();
12511254
}

0 commit comments

Comments
 (0)