UofTCTF 2024 : All Worbled Up

Subject

last time we had a worbler, it failed miserably and left everyone sad, and no one got their flags. now we have another one, maybe it'll work this time?

output:

                      _     _             
                     | |   | |            
  __      _____  _ __| |__ | | ___ _ __   
  \ \ /\ / / _ \| '__| '_ \| |/ _ \ '__|  
   \ V  V / (_) | |  | |_) | |  __/ |     
    \_/\_/ \___/|_|  |_.__/|_|\___|_|     

==========================================
Enter flag: *redacted*
Here's your flag:  a81c0750d48f0750

Writeup

import re
from itertools import compress, product

def worble(s):
    s1 = 5
    s2 = 31
    for n in range(len(s)):
        s1 = (s1 + ord(s[n]) + 7) % 65521
        s2 = (s1 * s2) % 65521
    return (s2 << 16) | s1

def shmorble(s):
    r = ''
    for i in range(len(s)):
        r += s[i - len(s)]
    return r

def blorble(a, b):
    return format(a, 'x') + format(b, 'x')

print("Welcome")
pattern = '^uoftctf\\{([bdrw013]){9}\\}$'
flag = "uoftctf{111111111}"
if (re.match(pattern, flag)):
    a = worble(flag)
    b = worble(flag[::-1])
    ct_test = shmorble(blorble(a,b))
    print("Here's your flag:", ct_test)
else :
    print("KO")

from itertools import product
ct = "a81c0750d48f0750"
characters = 'bdrw013'
for combination in product(characters, repeat=9):
    combination = ''.join(combination)
    flag = "uoftctf{"+combination+"}"
    if not (re.match(pattern, flag)):
        print("KO")
    a = worble(flag)
    b = worble(flag[::-1])
    ct_test = shmorble(blorble(a,b))
    if (ct_test == ct):
        print(flag)
        exit(0)

print("EXHAUSTED")