stranger-flag-checker
yet another challenge i made for ictf
problem
stranger danger!!!!!!!
stranger.py
,>++++++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+<]>[-<+>],+>+++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-[[-]>+<]>[-<+>],[->+>>>>+<<<<<]>[-<+>]<+>+++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<----[[-]>+<]>[-<+>],>>>>[-<<<<->>>>]<<<<>+++>+++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],+++>++++++++++>++++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<--[[-]>+<]>[-<+>],>,>++++++++>++++++<[->[->+<<<-<->>>]>[-<+>]<<]>[-]<<<[[-]>>+<<]>>[-<<+>>]<->+++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],>++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+<]>[-<+>],>++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<+++++[[-]>+<]>[-<+>],>++++++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<+++++[[-]>+<]>[-<+>],-->++++++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<--[[-]>+<]>[-<+>],>+++++++++++++>++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-[[-]>+<]>[-<+>],---------------------------------------------------[[-]>+<]>[-<+>],>++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-[[-]>+<]>[-<+>],-->++++++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<--[[-]>+<]>[-<+>],->,>++++++++>++++++<[->[->+<<<-<->>>]>[-<+>]<<]>[-]<<<[[-]>>+<<]>>[-<<+>>]<+++>+++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],>++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+<]>[-<+>],+>+++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<+[[-]>+<]>[-<+>],-----------------------------------------------------------------------------------------------[[-]>+<]>[-<+>],>+++++++++>++++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],------------------------------------------------[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-[[-]>+<]>[-<+>],----->-------->---------------<[+>[+<<->>>-<]>[+<->]<<]>[+]<<[[-]>+<]>[-<+>]+>+++>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-[>-]>---[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----.---.-.-------.[-]>[-]<]>[[-]>>>>>>>>>>>>>>>>>>>>>>>>>>>>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.----------.++++++.>[-]++++++++++.<++.--------------.+++++.[-]]
solution
taking a look in a debugger, we can inspect memory to see that each correct character is marked as a 0, and each incorrect one as a 1
then, we can brute force each character
lots of people did this by hand, which was not the intended solution
queue = ""
def getchar():
global queue
next_c, queue = queue[0], queue[1:]
return next_c
def evaluate(code, rstatus_callback):
code = cleanup(list(code))
total = ""
bracemap = buildbracemap(code)
cells, codeptr, cellptr = [0, 0, 0, 0, 0, 0, 0, 0], 0, 6
while codeptr < len(code):
command = code[codeptr]
if command == ">":
cellptr += 1
if cellptr == len(cells):
cells.append(0)
if command == "<":
cellptr = 0 if cellptr <= 0 else cellptr - 1
if command == "+":
cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0
if command == "-":
cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255
if command == "[" and cells[cellptr] == 0:
codeptr = bracemap[codeptr]
if command == "]" and cells[cellptr] != 0:
codeptr = bracemap[codeptr]
if command == ".":
total += chr(cells[cellptr])
if command == ",":
cells[cellptr] = ord(getchar())
if command == "$":
rstatus_callback(cells)
codeptr += 1
return total
def cleanup(code):
return ''.join(filter(lambda x: x in ['.', ',', '[', ']', '<', '>', '+', '-', '$'], code))
def buildbracemap(code):
temp_bracestack, bracemap = [], {}
for position, command in enumerate(code):
if command == "[":
temp_bracestack.append(position)
if command == "]":
# noinspection PyUnresolvedReferences
start = temp_bracestack.pop()
bracemap[start] = position
bracemap[position] = start
return bracemap
def main():
global queue
code = """,>++++++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+<]>[-<+>],+>+++++++++++>+++++++++<[->[->
+<<<->>]>[-<+>]<<]>[-]<<-[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-[[-]>+<]>[-<
+>],[->+>>>>+<<<<<]>[-<+>]<+>+++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<----[[-]>+<]>[-<+>],>>>>[-<<<<->>>
>]<<<<>+++>+++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],+++>++++++++++>++++++++++++<[->[->+<<<->>]>[-<+>]<
<]>[-]<<--[[-]>+<]>[-<+>],>,>++++++++>++++++<[->[->+<<<-<->>>]>[-<+>]<<]>[-]<<<[[-]>>+<<]>>[-<<+>>]<->+++++++>+++++
+++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],>++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+
<]>[-<+>],>++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<+++++[[-]>+<]>[-<+>],>++++++++++>++++++++++<[->[->+
<<<->>]>[-<+>]<<]>[-]<<+++++[[-]>+<]>[-<+>],-->++++++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<--[[-]>+<]>[-<+>
],>+++++++++++++>++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[
-<+>]<<]>[-]<<-[[-]>+<]>[-<+>],---------------------------------------------------[[-]>+<]>[-<+>],>++++++++++>+++++
++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]
<<-[[-]>+<]>[-<+>],-->++++++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<--[[-]>+<]>[-<+>],->,>++++++++>++++++<[->
[->+<<<-<->>>]>[-<+>]<<]>[-]<<<[[-]>>+<<]>>[-<<+>>]<+++>+++++++>++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[
-<+>],>++++++++++>+++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-----[[-]>+<]>[-<+>],+>+++++++++++>+++++++++<[->[->+<<<->
>]>[-<+>]<<]>[-]<<+[[-]>+<]>[-<+>],--------------------------------------------------------------------------------
---------------[[-]>+<]>[-<+>],>+++++++++>++++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<[[-]>+<]>[-<+>],--------------
----------------------------------[[-]>+<]>[-<+>],++++++>+++++++++++>+++++++++++<[->[->+<<<->>]>[-<+>]<<]>[-]<<-[[-
]>+<]>[-<+>],----->-------->---------------<[+>[+<<->>>-<]>[+<->]<<]>[+]<<[[-]>+<]>[-<+>]$+>+++>+<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<-[>-]>---[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----.---.-.-------.[-]>[-]<]>[[-]>>>>>>>>>>>>>>>>>>>>>>>
>>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++.----------.++++++.>[-]++++++++++.<++.--------------.+++++.[-]]"""
faulty = []
best = ["\x00"]*100
def get_faulty_registers(arg: list):
nonlocal faulty
faulty = [index-6 for index, val in enumerate(arg) if val == 1]
last_faulty = 0
while True:
queue = "".join(best)
evaluate(code, get_faulty_registers)
if len(faulty) == 0:
print("".join([_ for _ in best if ord(_)]))
quit()
first_bad = faulty[0]