Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Created June 29, 2023 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ei-grad/da3146dcaf8b8f603d4f5738a16147ae to your computer and use it in GitHub Desktop.
Save ei-grad/da3146dcaf8b8f603d4f5738a16147ae to your computer and use it in GitHub Desktop.
SPbCTF - iZba
import sys
import random
import requests
http = requests.Session()
http.cert = 'client.pem'
base_url = 'https://its-izba-4ncvjm8.spbctf.ru:13443'
uuid = http.get(f'{base_url}/init').json()['uuid']
class pos:
x = 0
y = 0
m = {} # map
bounds = [-1, -1, 1, 1]
def step(dx, dy):
if pos.x + dx <= bounds[0]:
bounds[0] = pos.x + dx - 1
if pos.x + dx >= bounds[2]:
bounds[2] = pos.x + dx + 1
if pos.y + dy <= bounds[1]:
bounds[1] = pos.y + dy - 1
if pos.y + dy >= bounds[3]:
bounds[3] = pos.y + dy + 1
resp = http.post(f'{base_url}/step/{uuid}', data=f'{dx} {dy}')
resp.raise_for_status()
d = resp.json()
if 'flag' in d:
print(d['flag'])
sys.exit(0)
if pos.x == d['firstCoordinate'] and pos.y == d['secondCoordinate']:
m[(pos.x + dx, pos.y + dy)] = '#'
return False
else:
m[(pos.x + dx, pos.y + dy)] = ' '
assert pos.x + dx == d['firstCoordinate']
assert pos.y + dy == d['secondCoordinate']
pos.x = d['firstCoordinate']
pos.y = d['secondCoordinate']
return True
def draw_map():
sys.stdout.write("\033c")
for x in range(bounds[0], bounds[2] + 1):
print(''.join(
m.get((x, y), '?') if (x, y) != (pos.x, pos.y) else '*'
for y in range(bounds[1], bounds[3] + 1)
))
print()
def depth_first_walk(visited=set()):
draw_map()
moves = [(0, -1), (1, 0), (0, 1), (-1, 0)]
random.shuffle(moves)
for dx, dy in moves:
if (pos.x + dx, pos.y + dy) not in visited:
visited.add((pos.x, pos.y))
if step(dx, dy):
depth_first_walk()
step(-dx, -dy)
depth_first_walk(set([(0, 0)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment