back to blog

Cyber Jawara International 2024 — Misc: Stone Game (100 pts)

Overview

Connect via netcat and play a Nim game — take turns removing stones from 7 sets, last to take wins.

nc 68.183.177.211 10001

Solution

The key is nim-sum: XOR all stone quantities. If the result is 0, you’re in a losing position.

Initial configuration: (3, 4, 5, 2, 6, 3, 5) → XOR = 0 → we start in a losing position.

The trick: take zero stones on the first move. This forces the computer to break the balanced state, shifting the advantage to us. From then on, always make a move that returns the nim-sum to 0.

from pwn import *

def nim_move(piles):
    xor_sum = 0
    for p in piles:
        xor_sum ^= p
    if xor_sum == 0:
        return 0, 0  # losing position: take 0
    for i, p in enumerate(piles):
        target = p ^ xor_sum
        if target < p:
            return i, p - target
    return 0, 0

r = remote('68.183.177.211', 10001)
# parse piles, play optimal moves until victory

Flag

CJ{why_I_allowed_zero_:(}