Skip to content

Commit 0cd3da1

Browse files
committed
qiwi ctf writeups
1 parent 598f770 commit 0cd3da1

File tree

15 files changed

+3416
-0
lines changed

15 files changed

+3416
-0
lines changed

2016-11-17-qiwi-2016/bfs/README.md

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# BFS (ppc 400)
2+
3+
###ENG
4+
[PL](#pl-version)
5+
6+
In the task we get a database [file](maze.db) with maze definition.
7+
We dumped the data to [txt](data.txt) to simplify usage.
8+
Each node stores a single byte of data.
9+
We guess that the point is to find the path through the maze.
10+
We used simple BFS search for that:
11+
12+
```python
13+
import codecs
14+
import collections
15+
16+
17+
def print_matrix(matrix):
18+
for i in range(50):
19+
print(" ".join(matrix[i]))
20+
21+
22+
def add_unvisited_node(to_process, visited, x, y, backtrace, prev):
23+
if (x, y) not in visited:
24+
visited.add((x, y))
25+
backtrace[(x, y)] = prev
26+
to_process.append((x, y))
27+
28+
29+
def main():
30+
visited = {(0, 0)}
31+
to_process = [(0, 0)]
32+
graph = collections.defaultdict(dict)
33+
backtrace = {}
34+
with codecs.open("data.txt") as input_file:
35+
for line in input_file:
36+
# eg. 1|0|0|gate|1
37+
data = line[:-2].split("|")
38+
node_id = int(data[0])
39+
x = int(data[1])
40+
y = int(data[2])
41+
wall_type = data[3]
42+
payload = data[4]
43+
graph[x][y] = (node_id, wall_type, payload)
44+
45+
while len(to_process) > 0:
46+
xy = to_process.pop(0)
47+
x = xy[0]
48+
y = xy[1]
49+
if x in graph and y in graph[x]:
50+
node = graph[x][y]
51+
if node[0] == 2500:
52+
break
53+
if node[1] == "gate":
54+
add_unvisited_node(to_process, visited, x - 1, y, backtrace, xy)
55+
add_unvisited_node(to_process, visited, x, y - 1, backtrace, xy)
56+
add_unvisited_node(to_process, visited, x, y + 1, backtrace, xy)
57+
add_unvisited_node(to_process, visited, x + 1, y, backtrace, xy)
58+
print(backtrace)
59+
current = (49, 49)
60+
data = []
61+
matrix = [[' ' for i in range(50)] for j in range(50)]
62+
i = 0
63+
while current != (0, 0):
64+
x = current[0]
65+
y = current[1]
66+
matrix[x][y] = "%3d" % i
67+
i += 1
68+
data.append(graph[x][y][2])
69+
current = backtrace[(x, y)]
70+
data.reverse()
71+
print_matrix(matrix)
72+
result = "".join(data)
73+
print(result)
74+
75+
76+
main()
77+
78+
```
79+
80+
Which prints out the maze solution and the bytes picked up on the way:
81+
82+
```
83+
54C82F36487A9157315AADFDDED1BB83ECD98E49EADAFEB03DB563A94E0851478C408CFD6B0BB42B030F61A82E655B7FCA0E1FA68DF676758DC60FBFD1016F0EB8E7A2B5170A157497EF711E4009653BC9B20726C98B6561EFBE316AC2AB2DCBE56494F05B44ED3EB62DA4109BEEC2537266FEDE44ACB12A17CA8C8A5BA9E1A4D24ACAD900FFBD228AC187B9024BEDC941D137EA3A92F9F8506740CD8C62DBEDB9990F3E0259434D9FCF070FEC9E60C5697BABA83A4E59EB4C3F0E7AFD44B1B8D9D93933962B27237560B5F8F7D19904D790842FA596FBB52B2A3F7EE15B7F589D28A6F20F747615E7ED135E17AFE8FE073B6606F5C893D40CB78B635AA5FE4E0EE10C572D5E7AECEAF743953D05F78BBC10A9BB3D53B0011AE5F269C806E5F9E6026C954A0CDF9C797953360602B96FC06324C3160701505C24597F6F7C77D5B76CBE25CD2B706A41DA324A1B79CFC4BA8B11F800593514D27754
84+
```
85+
86+
As far as I remember this was the flag.
87+
88+
###PL version
89+
90+
W zadaniu dostajemy [plik](maze.db) z definicją labiryntu.
91+
Zrzuciliśmy dane do [txt](data.txt) żeby ułatwić sobie pracę.
92+
Każdy węzeł uzyskanego grafu przechowuje jeden bajt danych.
93+
Domyślaliśmy się, ze zadaniem jest znaleźć drogę w labiryncie.
94+
Użyliśmy do tego BFSa:
95+
96+
```python
97+
import codecs
98+
import collections
99+
100+
101+
def print_matrix(matrix):
102+
for i in range(50):
103+
print(" ".join(matrix[i]))
104+
105+
106+
def add_unvisited_node(to_process, visited, x, y, backtrace, prev):
107+
if (x, y) not in visited:
108+
visited.add((x, y))
109+
backtrace[(x, y)] = prev
110+
to_process.append((x, y))
111+
112+
113+
def main():
114+
visited = {(0, 0)}
115+
to_process = [(0, 0)]
116+
graph = collections.defaultdict(dict)
117+
backtrace = {}
118+
with codecs.open("data.txt") as input_file:
119+
for line in input_file:
120+
# eg. 1|0|0|gate|1
121+
data = line[:-2].split("|")
122+
node_id = int(data[0])
123+
x = int(data[1])
124+
y = int(data[2])
125+
wall_type = data[3]
126+
payload = data[4]
127+
graph[x][y] = (node_id, wall_type, payload)
128+
129+
while len(to_process) > 0:
130+
xy = to_process.pop(0)
131+
x = xy[0]
132+
y = xy[1]
133+
if x in graph and y in graph[x]:
134+
node = graph[x][y]
135+
if node[0] == 2500:
136+
break
137+
if node[1] == "gate":
138+
add_unvisited_node(to_process, visited, x - 1, y, backtrace, xy)
139+
add_unvisited_node(to_process, visited, x, y - 1, backtrace, xy)
140+
add_unvisited_node(to_process, visited, x, y + 1, backtrace, xy)
141+
add_unvisited_node(to_process, visited, x + 1, y, backtrace, xy)
142+
print(backtrace)
143+
current = (49, 49)
144+
data = []
145+
matrix = [[' ' for i in range(50)] for j in range(50)]
146+
i = 0
147+
while current != (0, 0):
148+
x = current[0]
149+
y = current[1]
150+
matrix[x][y] = "%3d" % i
151+
i += 1
152+
data.append(graph[x][y][2])
153+
current = backtrace[(x, y)]
154+
data.reverse()
155+
print_matrix(matrix)
156+
result = "".join(data)
157+
print(result)
158+
159+
160+
main()
161+
162+
```
163+
164+
Co wypisuje na koniec rozwiązanie labiryntu oraz dane zebrane po drodze:
165+
166+
```
167+
54C82F36487A9157315AADFDDED1BB83ECD98E49EADAFEB03DB563A94E0851478C408CFD6B0BB42B030F61A82E655B7FCA0E1FA68DF676758DC60FBFD1016F0EB8E7A2B5170A157497EF711E4009653BC9B20726C98B6561EFBE316AC2AB2DCBE56494F05B44ED3EB62DA4109BEEC2537266FEDE44ACB12A17CA8C8A5BA9E1A4D24ACAD900FFBD228AC187B9024BEDC941D137EA3A92F9F8506740CD8C62DBEDB9990F3E0259434D9FCF070FEC9E60C5697BABA83A4E59EB4C3F0E7AFD44B1B8D9D93933962B27237560B5F8F7D19904D790842FA596FBB52B2A3F7EE15B7F589D28A6F20F747615E7ED135E17AFE8FE073B6606F5C893D40CB78B635AA5FE4E0EE10C572D5E7AECEAF743953D05F78BBC10A9BB3D53B0011AE5F269C806E5F9E6026C954A0CDF9C797953360602B96FC06324C3160701505C24597F6F7C77D5B76CBE25CD2B706A41DA324A1B79CFC4BA8B11F800593514D27754
168+
```
169+
170+
O ile dobrze pamiętam to była flaga.

0 commit comments

Comments
 (0)