-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_gadgets.py
173 lines (133 loc) · 4.69 KB
/
get_gadgets.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
from capstone import *
from argparse import ArgumentParser
from elftools.elf.elffile import ELFFile
import sys
import re
import general
allGadgets = []
SpecialInstructions = []
interrupt = []
syscall = []
def GetGadgets(textsection,retptr,retaddress,n):
reti=retptr
addr=retaddress
md = Cs(CS_ARCH_X86, CS_MODE_64)
count=1
while count<=n:
code=textsection[reti-count:reti+1]
x=md.disasm(code,addr)
L = []
for i in md.disasm(code,addr):
L.append(i)
if(len(L) > 0):
# print(L[0].op_str)
# operands = L[0].op_str.split(',')
# operands = getStrippedOperands(operands)
# try:
# print(L[0].operands[0])
# except:
# pass
# break
if (L[-1].mnemonic == "ret"):
finalGadget = list()
for insn in L:
InsnDict = dict()
InsnDict['address'] = int(insn.address) - 1
InsnDict['mnemonic'] = insn.mnemonic
InsnDict['operands'] = list()
InsnDict['op_str'] = insn.op_str
try:
for cnt in range(insn.op_count):
InsnDict['operands'].append(insn.operands[cnt])
except:
operands = insn.op_str.split(',')
InsnDict['operands'] = getStrippedOperands(operands)
finalGadget.append(InsnDict)
allGadgets.append(finalGadget)
addr=addr-1
count=count+1
def GetAllGadgets(instructions,code, EntryAddress, SpecialInstructions, N):
oldaddress = movingaddress = EntryAddress
count = 0
SpecialInstructions = specialinstructions(code, EntryAddress)
print(len(SpecialInstructions))
if(len(SpecialInstructions[1])):
interrupt.append(SpecialInstructions[1])
if(len(SpecialInstructions[0])):
syscall.append(SpecialInstructions[0])
for i in range(len(code)):
if(int(code[i])==195 or int(code[i]) == 203 or int(code[i]) == 194 or int(code[i]) == 202):
GetGadgets(code, i, movingaddress, N)
movingaddress += 1
print("Found %d gadgets\n" % len(allGadgets))
def getPopGadgets(Gadgets):
popGadgets = list()
for gadget in Gadgets:
if len(gadget) == 2:
if gadget[-2]['mnemonic'] == 'pop':
popGadgets.append(gadget)
return popGadgets
def getMovQwordGadgets(Gadgets):
movQwordGadgets = list()
for gadget in Gadgets:
if len(gadget) >= 2:
if gadget[-2]['mnemonic'] == 'mov' and re.search("^qword ptr \[[a-z]+\]$",gadget[-2]['operands'][0]) and gadget[-2]['operands'][1] in general.REGISTERS :
if(gadget[-2:] not in movQwordGadgets): # no duplicates please
movQwordGadgets.append(gadget[-2:])
return movQwordGadgets
def getMovRcx(Gadgets):
movQwordGadgets = list()
for gadget in Gadgets:
if len(gadget) >= 2:
if gadget[-2]['mnemonic'] == 'xor' and re.search("rcx",gadget[-2]['operands'][0]) and gadget[-2]['operands'][1] in general.REGISTERS :
if(gadget[-2:] not in movQwordGadgets): # no duplicates please
movQwordGadgets.append(gadget[-2:])
return movQwordGadgets
def getSyscallList():
if(len(syscall)):
# print(syscall)
return syscall
else:
return list()
def specialinstructions(instructions,address):
addr=address
count=0
l=[]
interrupt = 0
interruptList = list()
syscall = 0
syscallList = list()
while count<len(instructions)-1:
code=instructions[count:count+2]
count=count+1
# print(code[0])
# print(str(code[0]) + " " + str(code[1]))
if (int(code[0]) == 205 and int(code[1]) == 128):
interruptList.append(addr)
interrupt = addr
if (int(code[0]) == 15 and int(code[1]) == 5):
syscallList.append(addr)
syscall = addr
# if(interrupt and syscall):
# break
addr=addr+1
return (syscallList, interruptList)
def getStrippedOperands(operands) :
a = ""
b = ""
op_list = []
for op in operands:
op = op.lstrip()
op = op.rstrip()
op_list.append(op)
# 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 op_list