#!/usr/bin/env python3
"""
Quick test of common/interesting passcodes
"""
import requests

def lcg_step(seed):
    return (seed * 0x52c6425d + 0xcc52c) % (2**32)

def generate_keys(passcode):
    seed = passcode
    keys = []
    for _ in range(6):
        seed = lcg_step(seed)
        keys.append(seed % 0xffffff)
    return keys

def try_passcode(pc):
    keys = generate_keys(pc)
    
    key_bytes = []
    for key in keys[4:6]:
        key_bytes.extend([key >> 16, (key >> 8) & 0xFF, key & 0xFF])
    key_hex = ''.join(f'{b:02x}' for b in key_bytes)
    
    sector_22 = '0292640464020a820860081608cd0833'
    auth_code = sector_22 + key_hex
    
    data = {
        'uid': '04f6555b',
        'username': '6178656c5f6f757472756e',
        'authorization_code': auth_code,
        'access_level': '085e0831084d084f0886083408cd081f'
    }
    
    try:
        r = requests.post('http://154.57.164.76:32127/api', data=data, timeout=2)
        result = r.json()
        return result
    except:
        return None

print("[*] Testing interesting passcodes...")

# Common CTF/security values
interesting = [
    0, 1, 42, 69, 123, 255, 256, 420, 666, 1234, 1337, 2600, 4242,
    8080, 13337, 31337, 32768, 65535, 65536, 99999, 
    0xDEAD, 0xBEEF, 0xCAFE, 0xBABE, 0xFACE, 0x1337, 0x31337,
    0xDEADBEEF & 0xFFFF,  # Lower 16 bits of DEADBEEF
    # ASCII values
    int.from_bytes(b'pass', 'big') & 0xFFFF,
    int.from_bytes(b'key', 'big'),
    int.from_bytes(b'door', 'big') & 0xFFFF,
    int.from_bytes(b'flag', 'big') & 0xFFFF,
    # Numbers that might relate to our data
    0x0292, 0x6404, 0x640464, 0x820860,
    0x1608, 0xcd08, 0x0833,
]

for pc in interesting:
    result = try_passcode(pc)
    if result:
        flag = result.get('flag', '')
        status = result.get('door_status', 'Unknown')
        
        if len(flag) > 5:
            print(f"\n{'='*70}")
            print(f"SUCCESS!")
            print(f"Passcode: {pc} (0x{pc:x})")
            print(f"FLAG: {flag}")
            print(f"{'='*70}")
            
            keys = generate_keys(pc)
            print(f"\nLCG Keys:")
            for i, k in enumerate(keys):
                print(f"  Key {i}: {k:06x}")
            exit(0)
        else:
            print(f"  {pc:6d} (0x{pc:06x}): {status}")

print("\n[-] None of the interesting values worked")
print("[*] Brute force is the way...")
