]> git.somenet.org - pub/jan/ctf-seminar.git/blob - writeups/mli/hxp-36c3-ctf/exploit.py
Add hxp writeup
[pub/jan/ctf-seminar.git] / writeups / mli / hxp-36c3-ctf / exploit.py
1 #!/usr/bin/env python3
2
3 from pwn import *
4 from itertools import chain
5 import pickle, readchar
6
7 host = '78.47.17.200'
8 port = 7888
9
10 conn = remote(host, port)
11
12 pos = (0, 0)  # x, y
13 servermap = [[]]
14
15 flag_chars = []
16
17 def print_map(printpos=None):
18     temp = servermap
19     if printpos:
20         while len(temp) <= printpos[1]:
21             temp.append(['?'])
22         while len(temp[printpos[1]]) <= printpos[0]:
23             temp[printpos[1]].append('?')
24         temp[printpos[1]][printpos[0]] = str(temp[printpos[1]][printpos[0]])+'O'
25     print(repr(temp)
26           .replace('], ', ']\n ')
27           .replace("breeze", "B")
28           #.replace("smell", "i")
29           .replace("'?'", "' '")
30           .replace("O'", "O")
31           .replace("iwO", "|")
32           )
33
34
35 def read_map():
36     global servermap
37     with open("servermap", 'rb') as f:
38         servermap = pickle.load(f)
39
40
41 def set_map_val(val):
42     while len(servermap) <= pos[1]:
43         servermap.append(['?'])
44     while len(servermap[pos[1]]) <= pos[0]:
45         servermap[pos[1]].append('?')
46     # print(f'setting {pos} {pos[1]} {pos[0]} to {val}')
47     servermap[pos[1]][pos[0]] = val
48
49
50 def get_current_position():
51     if not conn.connected():
52         return
53     line = (conn.recvuntil('\n', True).decode())
54     x = line.split('\x00')
55     print(f'{pos}: {x}')
56     set_map_val(x[-1])
57     if x[0] == 'f':
58         flag_chars.append(x)
59     return x
60
61 def get_map_val(position=pos):
62     try:
63         return servermap[position[1]][position[0]]
64     except:
65         return '?'
66
67 def move(c):
68     x = get_current_position()
69     try:
70         conn.sendline(c)
71     except:
72         print('sendline error')
73         return None
74     return x[-1]
75
76
77 def down():
78     global pos
79     res = move('s')
80     if res not in ('iw', 'd'):
81         x, y = pos
82         pos = (x, y+1)
83     set_map_val(res)
84     return res
85
86
87 def up():
88     global pos
89     res = move('w')
90     if res not in ('iw', 'd'):
91         x, y = pos
92         pos = (x, y-1)
93     return res
94
95
96 def right():
97     global pos
98     res = move('d')
99     if res not in ('iw', 'd'):
100         x, y = pos
101         pos = (x+1, y)
102     return res
103
104
105 def left():
106     global pos
107     res = move('a')
108     if res not in ('iw', 'd'):
109         x, y = pos
110         pos = (x-1, y)
111     return res
112
113
114 read_map()
115 print_map()
116
117 resp = None
118
119 if 0 == 1:
120     for i in range(33):
121         resp = down()
122     resp = right()
123     resp = right()
124     resp = right()
125
126     resp = down()
127     resp = down()
128     resp = down()
129     resp = down()
130
131     resp = left()
132     resp = left()
133     resp = left()
134
135     while resp != 'd':
136         resp = down()
137         #resp = right()
138
139 while resp != 'd':
140     print_map(pos)
141     c = readchar.readchar()
142     if c == 'w':
143         resp = up()
144         continue
145     if c == 'a':
146         resp = left()
147         continue
148     if c == 's':
149         resp = down()
150         continue
151     if c == 'd':
152         resp = right()
153         continue
154     if c == 'Ü':
155         break
156     print('??')
157     continue
158
159     #x, y = pos
160     #if get_map_val((x, y+1)) in ('iw', 'B'):  # B below
161     #    resp = up()
162     #    continue
163     #if get_map_val((x, y-1)) in ('iw', 'B'):  # B above
164     #    resp = down()
165     #    continue
166     #if get_map_val((x+1, y)) in ('iw', 'B'):  # B right
167     #    resp = left()
168     #    continue
169     #if get_map_val((x-1, y)) in ('iw', 'B'):  # B left
170     #    resp = right()
171     #    continue
172     #print('no B in sight - go right')
173     #resp = right()
174
175 with open("servermap", "wb") as f:
176     pickle.dump(servermap, f)
177
178 print_map()
179
180
181 with open("flagchars.txt", "a") as myfile:
182     myfile.write(repr(flag_chars)+'\r\n')