|
| 1 | + |
| 2 | +#!/usr/bin/env python |
| 3 | +from pwn import * |
| 4 | + |
| 5 | +# if direction is <, then the first item is the icon to the left of said direction, 2nd item is icon infront of said direction |
| 6 | +''' |
| 7 | +0 1 2 |
| 8 | +3 4 5 |
| 9 | +6 7 8 |
| 10 | +This is the how the map icons indexes are interpreted when converted to a list |
| 11 | +''' |
| 12 | + |
| 13 | +''' |
| 14 | +E.g when a runner faces like this <, |
| 15 | +the 7th index in the map list is to the left of that runner, |
| 16 | +the 3th index in the map list is to the front of the runner, |
| 17 | +the 1st index in the map list is to the right of the runner |
| 18 | +''' |
| 19 | +# lefting stores the indicies of the map on what would be the left, front and right of the runner relative to its position |
| 20 | +lefting = { |
| 21 | + "<": (7, 3, 1), |
| 22 | + "^": (3, 1, 5), |
| 23 | + ">": (1, 5, 7), |
| 24 | + "v": (5, 7, 3), |
| 25 | +} |
| 26 | + |
| 27 | +# This is to store the up, down, left, right indices of the 3x3 grid to the direction of the keyboard |
| 28 | +key_map = { |
| 29 | + 1: "W", |
| 30 | + 3: "A", |
| 31 | + 5: "D", |
| 32 | + 7: "S", |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def turn_right(curr_dir): |
| 37 | + if curr_dir == "<": |
| 38 | + return "^" |
| 39 | + if curr_dir == "^": |
| 40 | + return ">" |
| 41 | + if curr_dir == ">": |
| 42 | + return "v" |
| 43 | + if curr_dir == "v": |
| 44 | + return "<" |
| 45 | + |
| 46 | + |
| 47 | +def turn_left(curr_dir): |
| 48 | + if curr_dir == "<": |
| 49 | + return "v" |
| 50 | + if curr_dir == "v": |
| 51 | + return ">" |
| 52 | + if curr_dir == ">": |
| 53 | + return "^" |
| 54 | + if curr_dir == "^": |
| 55 | + return "<" |
| 56 | + |
| 57 | + |
| 58 | +def flip_dir(curr_dir): |
| 59 | + if curr_dir == "<": |
| 60 | + return ">" |
| 61 | + if curr_dir == "v": |
| 62 | + return "^" |
| 63 | + if curr_dir == ">": |
| 64 | + return "<" |
| 65 | + if curr_dir == "^": |
| 66 | + return "v" |
| 67 | + |
| 68 | + |
| 69 | +# curr_m is current map |
| 70 | +# curr_d is curr_d |
| 71 | + |
| 72 | +# This is to find what key to press next |
| 73 | +def find_next_key(curr_m, curr_d): |
| 74 | + starting_surroundings = list(curr_m) |
| 75 | + map_strip = [] |
| 76 | + next_d = "" |
| 77 | + # Take in the map and remove unneeded spaces and newlines |
| 78 | + for ind, ico in enumerate(starting_surroundings): |
| 79 | + if ind in range(0, 17, 2): |
| 80 | + map_strip.append(ico) |
| 81 | + # Find what is to the left, right and front of the runner relative to its direction |
| 82 | + front = map_strip[lefting[curr_d][1]] |
| 83 | + left = map_strip[lefting[curr_d][0]] |
| 84 | + right = map_strip[lefting[curr_d][2]] |
| 85 | + print(left, front, right) |
| 86 | + # Runner logic, priority for it to go left is left is open, followed by front if left is closed |
| 87 | + # If front is also closed, turn right and go instead |
| 88 | + # If all 3 are closed, flip direction and go through the same logic |
| 89 | + if left == " " or left == "!": |
| 90 | + next_d = turn_left(curr_d) |
| 91 | + key = key_map[lefting[next_d][1]] |
| 92 | + elif front == " " or front == "!": |
| 93 | + next_d = curr_d |
| 94 | + key = key_map[lefting[next_d][1]] |
| 95 | + elif right == " " or right == "!": |
| 96 | + next_d = turn_right(curr_d) |
| 97 | + key = key_map[lefting[next_d][1]] |
| 98 | + else: |
| 99 | + next_d = flip_dir(curr_d) |
| 100 | + key = key_map[lefting[next_d][1]] |
| 101 | + return key, next_d |
| 102 | + |
| 103 | + |
| 104 | +r = remote('ctf.yadunut.com', 8004) |
| 105 | +r.recvline() |
| 106 | +next_dir = '>' |
| 107 | +while True: |
| 108 | + tmp = r.recv() |
| 109 | + print("MAP") |
| 110 | + print(tmp) |
| 111 | + key, next_dir = find_next_key(tmp, next_dir) |
| 112 | + print(key, next_dir) |
| 113 | + r.sendline(key) |
0 commit comments