This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 369
/
FixJumptable.py
121 lines (114 loc) · 3.96 KB
/
FixJumptable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#Simple jump table fixing. Select LEA instruction and JMP RAX (branchcond) instruction and run. Mostly copied from SwitchOverride.java. Does not always fix table correctly.
#@author Satsuoni
#@category Deobfuscation
#@keybinding
#@menupath
#@toolbar
from binascii import hexlify
from ghidra.app.emulator import EmulatorHelper
from ghidra.util.task import ConsoleTaskMonitor
from ghidra.program.model.pcode import PcodeOp
from ghidra.program.model.symbol import *
from ghidra.program.model.pcode import JumpTable
from java.util import LinkedList, Arrays, ArrayList
from ghidra.app.cmd.function import CreateFunctionCmd
from ghidra.app.cmd.disassemble import DisassembleCommand
def getAddress(offset):
return currentProgram.getAddressFactory().getDefaultAddressSpace().getAddress(offset)
def getProgramRegisterList(currentProgram):
pc = currentProgram.getProgramContext()
return pc.registers
state = getState()
currentProgram = state.getCurrentProgram()
name = currentProgram.getName()
listing = currentProgram.getListing()
print(name)
def getPossibleConstAddressFromInstruction(instr):
raw_pcode = instr.getPcode()
for code in raw_pcode:
if code.getOpcode()==PcodeOp.COPY:
inp=code.getInputs()[0]
print(inp)
if inp.size==8 and inp.isConstant():
return getAddress(inp.getOffset())
return None
#return toAddr(inp.)
def isComputedBranchInstruction( instr):
if instr is None:
return False
flowType = instr.getFlowType()
if flowType == RefType.COMPUTED_JUMP:
return True
if (flowType.isCall()):
#is it a callfixup?
referencesFrom = instr.getReferencesFrom()
for reference in referencesFrom:
if reference.getReferenceType().isCall():
func = currentProgram.getFunctionManager().getFunctionAt(reference.getToAddress())
if func is not None and func.getCallFixup() is not None:
return True
return False
def tryGetSwitchGuard(instr): #also mnemonic based, so fragile. May not work, did not check in this script
prev=instr.getPrevious()
if prev is None: return -1
prev=prev.getPrevious()
nx=prev.getMnemonicString().lower()
if nx == "cmp":
objects=prev.getOpObjects(1)
if len(objects)!=1: return -1
try:
return int(str(objects[0]),0)
except:
return -1
return -1
def fixSwitch():
selection=state.getCurrentSelection()
table_addr=None
first_addr=None
jumpinstr=None
jumpaddr=None
if selection is not None:
for item in selection:
print(item)
instr=getInstructionAt(item.getMinAddress())
if first_addr is None or item.getMinAddress() < first_addr:
first_addr=item.getMinAddress()
if table_addr is None:
table_addr=getPossibleConstAddressFromInstruction(instr)
if isComputedBranchInstruction(instr):
jumpinstr=instr
jumpaddr=item.getMinAddress()
if table_addr is None:
print("could not find table, should be in selection")
return
if jumpinstr is None:
print("could not find jump point, should be in selection")
return
table=table_addr
cnt=0
addr_list=[]
while getInt(table)<0:
naddr=table_addr.add(getInt(table))
if naddr<first_addr or naddr>table_addr: break
addr_list.append(naddr)
cnt+=1
table=table.add(4)
print("Estimated table length: {}".format(len(addr_list)))
sGuard=tryGetSwitchGuard(jumpinstr)
if sGuard>0:
print("Switch guard found: {}".format(sguard))
if sguard<len(addr_list):
addr_list=addr_list[:sguard]
if len(addr_list)==0:
print ("Empty table?")
return
function = getFunctionContaining(jumpaddr)
monitor = ConsoleTaskMonitor()
for addr in addr_list:
discmd = DisassembleCommand(addr, None, True)
discmd.applyTo(currentProgram,monitor)
jumpinstr.addOperandReference(0, addr, RefType.COMPUTED_JUMP, SourceType.USER_DEFINED)
jumpTab = JumpTable(jumpaddr,ArrayList(addr_list),True)
#jumpTab.writeOverride(function)
CreateFunctionCmd.fixupFunctionBody(currentProgram, function, monitor)
fixSwitch()