-
Notifications
You must be signed in to change notification settings - Fork 5
/
categorize.py
290 lines (191 loc) · 8.56 KB
/
categorize.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/python3
# This has routines which process the gadgets collected in get_gadgets
# Only gadgets with length 2 are taken and classified into 8 categories.
import sys
from general import *
# This routine will collect gadgets with N assembly instructions in it(excluding ret).
# If N = 1, then the gadget will be of the form "Inst; ret"
def getLNGadgets(GadgetList, n) :
nGadgetsList = list()
for gadget in GadgetList:
# Only ret instruction, don't do anything.
if len(gadget) == 1:
continue
# If gadget length is 2, just append.
elif len(gadget) == 2 :
nGadgetsList.append(gadget)
# # If gadget length is > 2, get only 2 instructions and append.
# elif len(gadget) > 2 :
# newgadget = list()
# newgadget.append(gadget[-2])
# newgadget.append(gadget[-1])
# nGadgetsList.append(newgadget)
return nGadgetsList
# Takes in a list of 2 strings and returns it.
def getStrippedOperands(operands) :
a = ""
b = ""
operands[0].lstrip()
operands[0].rstrip()
operands[1].lstrip()
operands[1].rstrip()
for char in operands[0]:
if(char != ' '):
a+= char
for char in operands[1]:
if(char != ' '):
b+= char
return [a, b]
# Takes in one string and strips it.
def getStrippedOperand(operand) :
a = ""
operand.rstrip()
operand.lstrip()
for char in operand:
if(char != ' '):
a+= char
return a
# This categorises all the 1-instruction gadgets present.
# Returns a list of lists - there are 8 main lists.
# Each of those 8 lists has gadgets of it's category.
# Refer general.py to know what those categories are.
def categorize(TwoInstGadgets):
ALLGADGETS = [[] for x in range(TOTAL_CATEGORIES)]
# print("Length of ALLGADGETS = ", len(ALLGADGETS))
x = 0
while x < len(TwoInstGadgets) :
gadget = TwoInstGadgets[x]
inst = gadget[0]
# TODO: Add the derivatives of "mov"
if inst['mnemonic'] == "mov" :
# operands = inst.op_str.split(',')
# operands = getStrippedOperands(operands)
if (inst['operands'][0] in REGISTERS) and (inst['operands'][1] in REGISTERS) :
ALLGADGETS[MOVREGG].append(gadget)
elif inst['operands'][0] in REGISTERS and inst['operands'][1].isnumeric() :
ALLGADGETS[LOADCONSTG].append(gadget)
elif inst['operands'][0] in REGISTERS and not(inst['operands'][1].isnumeric()) :
ALLGADGETS[LOADMEMG].append(gadget)
elif inst['operands'][0] not in REGISTERS and not(inst['operands'][1].isnumeric()):
ALLGADGETS[STOREMEMG].append(gadget)
#
elif inst['mnemonic'] == "pop" :
# operand = inst.op_str
# operand = getStrippedOperand(operand)
if inst['operands'][0] in REGISTERS :
ALLGADGETS[LOADCONSTG].append(gadget)
else :
ALLGADGETS[STOREMEMG].append(gadget)
# TODO: Add "div"
elif inst['mnemonic'] == "add" or inst['mnemonic'] == "sub" or inst['mnemonic'] == "mul":
# operands = inst.op_str.split(',')
# operands = getStrippedOperands(operands)
# Original condition: operands[0] in REGISTERS) and ((operands[1] in REGISTERS) or (operands[1].isnumeric())
if (inst['operands'][0] in REGISTERS) and (inst['operands'][1].isnumeric()) :
ALLGADGETS[ARITHMETICG].append(gadget)
else :
# print("Found a add / sub / mul instruction playing with memory")
# print("As of now, not doing anything with memory-arithmetic instructions", end = '\n\n')
pass
elif inst['mnemonic'] == "inc" or inst['mnemonic'] == "dec":
# operand = inst.op_str
# operand = getStrippedOperand(operand)
if inst['operands'] in REGISTERS :
ALLGADGETS[ARITHMETICG].append(gadget)
else:
# print("Found a inc / dec instruction playing with memory")
# print("As of now, not doing anything with memory-arithmetic instructions", end = '\n\n')
pass
elif inst['mnemonic'] == "xor":
# operands = inst.op_str.split(',')
# operands = getStrippedOperands(operands)
if (inst['operands'][0] in REGISTERS) and (inst['operands'][0] in REGISTERS) :
if (inst['operands'][0] == inst['operands'][1]) :
ALLGADGETS[LOADCONSTG].append(gadget)
else :
ALLGADGETS[ARITHMETICG].append(gadget)
else :
# print("Found an xor instruction playing with memory")
# print("As of now, not doing anything with memory-arithmetic instruction", end = '\n\n')
pass
elif inst['mnemonic'] == "and" :
# operands = inst.op_str.split(',')
# operands = getStrippedOperands(operands)
# TODO: if int(operands[1]) == 0xffffffffffffffff :
# ALLGADGETS[LOADCONSTG].append(gadget)
# This is like loading (-1) into operands[0]
if (inst['operands'][0] in REGISTERS) and (inst['operands'][0] in REGISTERS) :
ALLGADGETS[ARITHMETICG].append(gadget)
else :
# print("Found an and instruction playing with memory")
# print("As of now, not doing anything with memory-arithmetic instruction", end = '\n\n')
pass
elif inst['mnemonic'] == "or" :
# operands = inst.op_str.split(',')
# operands = getStrippedOperands(operands)
if (inst['operands'][0] in REGISTERS) and (inst['operands'][0] in REGISTERS) :
ALLGADGETS[ARITHMETICG].append(gadget)
else :
# print("Found an or instruction playing with memory")
# print("As of now, not doing anything with memory-arithmetic instruction", end = '\n\n')
pass
# Covering the special instructions without which we would have no job to do :P
elif inst['mnemonic'] == "int" or inst['mnemonic'] == "syscall" :
ALLGADGETS[SPECIAL_INST].append(gadget)
# Hunting for xchg instructions - very useful!
elif inst['mnemonic'] == "xchg" :
ALLGADGETS[XCHANGE].append(gadget)
else :
# print("Found a gadget who has not been categorized")
# print("Need help in adding these!", end = '\n\n')
pass
# Keep the loop going!
x = x + 1
return ALLGADGETS
# From the categorized gadgets, this routine will return a list of gadgets belonging to the queried category and containing target register.
def queryGadgets(GadgetList, category, targetReg):
# Basic error handling!
if category < 0 and category > 7 :
print("Error: category not present")
print("Exited in categorize.queryGadgets")
sys.exit()
L = GadgetList[category]
ReturnList = list()
x = 0
while x < len(L) :
gadget = L[x]
inst = gadget[0]
if targetReg in inst['operands'] :
ReturnList.append(gadget)
# Keep the loop going!
x = x + 1
return ReturnList
# Returns a list of int gadgets if it is found.
# If not found, it returns an empty list
def checkIfIntPresent(GadgetList) :
specialList = GadgetList[SPECIAL_INST]
intList = list()
present = 0
x = 0
while x < len(specialList) :
gadget = specialList[0]
inst = gadget[0]
if inst['mnemonic'] == "int" :# and inst['operands'] == "0x80":
intList.append(gadget)
x = x + 1
return intList
# Returns a list of syscall gadgets if it is found.
# If not found, it returns an empty list
def checkIfSyscallPresent(GadgetList) :
# print("LEN OF SPECIAL_INST "+ str(SPECIAL_INST))
specialList = GadgetList[SPECIAL_INST]
syscallList = list()
present = 0
x = 0
while x < len(specialList) :
gadget = specialList[0]
inst = gadget[0]
if inst['mnemonic'] == "syscall":
syscallList.append(gadget)
x = x + 1
return syscallList