/
find_dynamic_strings.py
269 lines (221 loc) · 10.3 KB
/
find_dynamic_strings.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#Find dynamically allocated string structures in Go binaries.
# type stringStruct struct {
# str unsafe.Pointer
# len int
# }
#Different instructions per architecture. Multiple solutions are possible.
#Future ToDo: add newly discovered instruction sequences.
#@author padorka@cujoai
#@category goscripts
#@keybinding
#@menupath
#@toolbar
from ghidra.program.model.lang import OperandType
#x86
#LEA REG, [STRING_ADDRESS]
#MOV [ESP + ..], REG
#MOV [ESP + ..], STRING_SIZE
def string_rename_x86():
for block in getMemoryBlocks():
if block.getName() != ".text":
continue
start = block.getStart()
ins = getInstructionAt(start)
while ins:
op_type = ins.getOperandType(1)
reg = ins.getRegister(0)
#Check first instruction: LEA REG, [STRING_ADDRESS]
if ins.getMnemonicString() != "LEA" or reg is None or OperandType.isAddress(op_type) is False:
ins = getInstructionAfter(ins)
continue
ins_next = getInstructionAfter(ins)
#Check second instruction: MOV [ESP + ..], REG (where REG is the same as in previous instruction)
if ins_next.getMnemonicString() != "MOV" or ins_next.getRegister(1) != reg or ins_next.getOpObjects(0)[0].toString() != "ESP":
ins = getInstructionAfter(ins)
continue
ins_next2 = getInstructionAfter(ins_next)
op_type = ins_next2.getOperandType(1)
#Check third instruction: MOV [ESP + ..], STRING_SIZE
if ins_next2.getMnemonicString() != "MOV" or ins_next2.getOpObjects(0)[0].toString() != "ESP" or OperandType.isScalar(op_type) is False:
ins = getInstructionAfter(ins)
continue
address = ins.getPrimaryReference(1).getToAddress()
length = ins_next2.getOpObjects(1)[0].getValue()
try:
#Create string.
createAsciiString(address, length)
#print "SUCCESS at %s" % address
except:
#print "ERROR at address %s" % ins.getAddress()
ins = getInstructionAfter(ins)
continue
ins = getInstructionAfter(ins)
#x86_64
#LEA REG, [STRING_ADDRESS]
#MOV [RSP + ..], REG
#MOV [RSP + ..], STRING_SIZE
def string_rename_x86_64():
for block in getMemoryBlocks():
if block.getName() != ".text":
continue
start = block.getStart()
ins = getInstructionAt(start)
while ins:
op_type = ins.getOperandType(1)
reg = ins.getRegister(0)
#Check first instruction: LEA REG, [STRING_ADDRESS]
if ins.getMnemonicString() != "LEA" or reg is None or OperandType.isAddress(op_type) is False:
ins = getInstructionAfter(ins)
continue
ins_next = getInstructionAfter(ins)
#Check second instruction: MOV [RSP + ..], REG (where REG is the same as in previous instruction)
if ins_next.getMnemonicString() != "MOV" or ins_next.getRegister(1) != reg or ins_next.getOpObjects(0)[0].toString() != "RSP":
ins = getInstructionAfter(ins)
continue
ins_next2 = getInstructionAfter(ins_next)
op_type = ins_next2.getOperandType(1)
#Check third instruction: MOV [RSP + ..], STRING_SIZE
if ins_next2.getMnemonicString() != "MOV" or ins_next2.getOpObjects(0)[0].toString() != "RSP" or OperandType.isScalar(op_type) is False:
ins = getInstructionAfter(ins)
continue
address = ins.getPrimaryReference(1).getToAddress()
length = ins_next2.getOpObjects(1)[0].getValue()
try:
#Create string.
createAsciiString(address, length)
#print "SUCCESS at %s" % address
except:
#print "ERROR at address %s" % ins.getAddress()
ins = getInstructionAfter(ins)
continue
ins = getInstructionAfter(ins)
#ARM, 32-bit
#LDR REG, [STRING_ADDRESS_POINTER]
#STR REG, [SP, ..]
#MOV REG, STRING_SIZE
#STR REG, [SP, ..]
def string_rename_arm():
for block in getMemoryBlocks():
if block.getName() != ".text":
continue
start = block.getStart()
ins = getInstructionAt(start)
while ins:
op_type = ins.getOperandType(1)
#Check first instruction: LDR REG, [STRING_ADDRESS_POINTER]
if ins.getMnemonicString() != "ldr" or ins.getRegister(0) is None or OperandType.isAddress(op_type) is False or OperandType.isScalar(op_type) is False:
ins = getInstructionAfter(ins)
continue
reg = ins.getRegister(0)
ins_next = getInstructionAfter(ins)
#Check second instruction: STR REG, [SP + ..] (where REG is the same as in previous instruction)
if ins_next.getMnemonicString() != "str" or ins_next.getRegister(0) != reg or ins_next.getOpObjects(1)[0].toString() != "sp":
ins = getInstructionAfter(ins)
continue
ins_next2 = getInstructionAfter(ins_next)
op_type = ins_next2.getOperandType(1)
#Check third instruction: MOV REG, STRING_SIZE
if ins_next2.getMnemonicString() != "mov" or ins_next2.getRegister(0) is None or OperandType.isScalar(op_type) is False:
ins = getInstructionAfter(ins)
continue
reg = ins_next2.getRegister(0)
ins_next3 = getInstructionAfter(ins_next2)
#Check fourth instruction: STR REG, [SP + ..] (where REG is the same as in previous instruction)
if ins_next3.getMnemonicString() != "str" or ins_next3.getRegister(0) != reg or ins_next3.getOpObjects(1)[0].toString() != "sp":
ins = getInstructionAfter(ins)
continue
#print "ins: %s" % ins
address_pointer = getInt(ins.getPrimaryReference(1).getToAddress())
address = currentProgram.getAddressFactory().getAddress(hex(address_pointer))
length = ins_next2.getOpObjects(1)[0].getValue()
try:
#Create string.
createAsciiString(address, length)
#print "SUCCESS at %s" % address
except:
#print "ERROR at address %s" % ins.getAddress()
ins = getInstructionAfter(ins)
continue
ins = getInstructionAfter(ins)
#ARM, 64-bit - version 1
#ADRP REG, [STRING_ADDRESS_START]
#ADD REG, REG, INT
#STR REG, [SP, ..]
#ORR REG, REG, STRING_SIZE
#STR REG, [SP, ..]
#ARM, 64-bit - version 2
#ADRP REG, [STRING_ADDRESS_START]
#ADD REG, REG, INT
#STR REG, [SP, ..]
#MOV REG, STRING_SIZE
#STR REG, [SP, ..]
def string_rename_arm_64():
for block in getMemoryBlocks():
if block.getName() != ".text":
continue
start = block.getStart()
ins = getInstructionAt(start)
while ins:
op_type = ins.getOperandType(1)
reg = ins.getRegister(0)
#Check first instruction: ADRP REG, [STRING_ADDRESS_START]
if ins.getMnemonicString() != "adrp" or reg is None or OperandType.isScalar(op_type) is False:
ins = getInstructionAfter(ins)
continue
ins_next = getInstructionAfter(ins)
op_type = ins_next.getOperandType(2)
#Check second instruction: ADD REG, REG, INT (where REG is the same as in previous instruction)
if ins_next.getMnemonicString() != "add" or ins_next.getRegister(0) != reg or OperandType.isScalar(op_type) is False:
ins = getInstructionAfter(ins)
continue
ins_next2 = getInstructionAfter(ins_next)
#Check third instruction: STR REG, [SP + ..] (where REG is the same as in previous instruction)
if ins_next2.getMnemonicString() != "str" or ins_next2.getRegister(0) != reg or ins_next2.getOpObjects(1)[0].toString() != "sp":
ins = getInstructionAfter(ins)
continue
ins_next3 = getInstructionAfter(ins_next2)
reg = ins_next3.getRegister(0)
#Check fourth instruction: ORR REG, REG, STRING_SIZE
if ins_next3.getMnemonicString() == "orr" and reg is not None and OperandType.isScalar(ins_next3.getOperandType(2)) is True:
length = ins_next3.getOpObjects(2)[0].getValue()
#Check fourth instruction: MOV REG, STRING_SIZE
elif ins_next3.getMnemonicString() == "mov" and reg is not None and OperandType.isScalar(ins_next3.getOperandType(1)) is True:
length = ins_next3.getOpObjects(1)[0].getValue()
else:
ins = getInstructionAfter(ins)
continue
ins_next4 = getInstructionAfter(ins_next3)
#Check fifth instruction: STR REG, [SP + ..] (where REG is the same as in previous instruction)
if ins_next4.getMnemonicString() != "str" or ins_next4.getRegister(0) != reg or ins_next4.getOpObjects(1)[0].toString() != "sp":
ins = getInstructionAfter(ins)
continue
#print "ins: %s" % ins
address_int = int(ins.getOpObjects(1)[0].getValue() + ins_next.getOpObjects(2)[0].getValue())
address = currentProgram.getAddressFactory().getAddress(hex(address_int))
try:
#Create string.
createAsciiString(address, length)
#print "SUCCESS at %s" % address
except:
#print "ERROR at address %s" % ins.getAddress()
ins = getInstructionAfter(ins)
continue
ins = getInstructionAfter(ins)
#Check program architecture.
language_id = currentProgram.getLanguageID()
print "lang: %s" % language_id
pointer_size = currentProgram.getDefaultPointerSize()
if language_id.toString().startswith("ARM"):
print "32 BIT ARM"
string_rename_arm()
elif language_id.toString().startswith("AARCH64"):
print "64 BIT ARM"
string_rename_arm_64()
elif language_id.toString().startswith("x86") and pointer_size == 4:
print "32 BIT x86"
string_rename_x86()
elif language_id.toString().startswith("x86") and pointer_size == 8:
print "64 BIT x86"
string_rename_x86_64()
else:
print "ERROR: unknown arch."