Google CTF 2018: Wired CSV solution
import csv | |
FILENAME = 'data.csv' | |
HSYNC = 1 / 15700 | |
SCANCODES = ["L", "J", ";", "{F1}", "{F2}", "K", "+", "*", "O", "{NULL}", "P", "U", "{CR}", "I", "-", "=", "V", "Help", "C", "{F3}", "{F4}", "B", "X", "Z", "4", "{NULL}", "3", "6", "{Esc}", "5", "2", "1", ",", "{Spc}", ".", "N", "{NULL}", "M", "/", "{Inv}", "R", "{NULL}", "E", "Y", "{Tab}", "T", "W", "Q", "9", "{NULL}", "0", "7", "{BS}", "8", "<", ">", "F", "H", "D", "{NULL}", "Caps", "G", "S", "A"] | |
change_offsets = {0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: []} | |
def add_change_offset(line, time, value): | |
if time == '' or time == ' ': | |
return False | |
time = float(time) | |
change_offsets[line].append((time, int(value.strip()))) | |
return True | |
def read(): | |
with open(FILENAME) as csv_file: | |
reader = csv.reader(csv_file) | |
for idx, line in enumerate(reader): | |
if idx == 0: | |
continue | |
_, _, _, timek0, k0, timek1, k1, timek2, k2, timek3, k3, timek4, k4, timek5, k5, timekr1, kr1, timekr2, kr2 = line | |
if not add_change_offset(0, timek0, k0): | |
break | |
add_change_offset(1, timek1, k1) | |
add_change_offset(2, timek2, k2) | |
add_change_offset(3, timek3, k3) | |
add_change_offset(4, timek4, k4) | |
add_change_offset(5, timek5, k5) | |
add_change_offset(6, timekr1, kr1) | |
add_change_offset(7, timekr2, kr2) | |
def get_values(indices, time): | |
values = [] | |
for i in range(len(change_offsets)): | |
index = indices[i] | |
while index < len(change_offsets[i]) and change_offsets[i][index][0] < time: | |
index += 1 | |
indices[i] = index | |
values.append(change_offsets[i][index - 1][1]) | |
return values | |
def parse_values(values): | |
for i in range(8): | |
# if i not in (3, 4): | |
values[i] = 1 - values[i] | |
kr1 = values[6] | |
kr2 = values[7] | |
scancode = 0 | |
for x in values[:6][::-1]: | |
scancode = scancode * 2 + x | |
return kr1, kr2, scancode | |
def run(): | |
time = 0 | |
indices = [0] * len(change_offsets) | |
last_kr1 = 0 | |
last_scancode = 0 | |
answer = '' | |
while time < 20: | |
values = get_values(indices, time) | |
kr1, kr2, scancode = parse_values(values) | |
if last_kr1 == 0 and kr1 == 1: | |
if last_scancode != scancode: | |
print(f'Current time is {time}, values are {values}') | |
print(f'kr1 = {kr1}, kr2 = {kr2}, scancode = {scancode}, char = {SCANCODES[scancode]}') | |
answer += SCANCODES[scancode] | |
last_scancode = scancode | |
last_kr1 = kr1 | |
time += HSYNC | |
print(answer) | |
if __name__ == '__main__': | |
read() | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment