The Challenge
like why 67? this is the 3rd ctf where author had to use 67 for a challenge. smh.
The entire challenge is a single line:
assert __import__('re').match('SEKAI{[67]{67}}$',flag:=input()) and not int.from_bytes(flag.encode())%~(6+~7)**67
Two conditions on the flag:
- It matches
SEKAI{[67]{67}}$— a literal 6-character prefix, a 67-character body made entirely of '6' and '7', and a closing brace. - The integer formed by treating the flag's raw bytes as a big-endian number is divisible by
~(6+~7)**67.
Decoding the Operator Soup
Python's ~x is -x - 1. Walking through the expression:
~7=-86 + (-8)=-2(-2)**67=-(2**67)(odd exponent keeps the sign)~(-(2**67))=2**67 - 1
So the divisor is 2**67 - 1 — a Mersenne number, and the real constraint is simply:
int(flag_bytes) ≡ 0 (mod 2**67 - 1)
The Insight That Makes This Solvable
With a 67-character free body, this looks like a search over 2**67 possibilities; way too big to brute force (i tried), and (as the long trial-and-error in this thread showed) naive hill-climbing or "reconstruct bits from a target integer" approaches either don't converge or produce bytes outside {0x36, 0x37}.
The trick is recognizing that flipping a single body character between '6' (0x36) and '7' (0x37) changes the overall flag integer by a known, fixed amount: 256^k for whatever byte-position k that character sits at (mod the modulus). Since the modulus is 2**67 - 1, and 256 = 2**8, those contributions are really just powers of 2 modulo 2**67 - 1 — and multiplication by 2 mod a Mersenne number 2^n - 1 is just a cyclic bit rotation.
Because gcd(8, 67) = 1, stepping through the 67 body positions and computing 2^(8k mod 67) for each one visits every residue from 2^0 to 2^66 exactly once — no repeats, full coverage. That's the whole challenge in one sentence: the 67 "flip" operations correspond bijectively to the 67 bits of a 67-bit number.
Solving It
- Compute the remainder when the flag is all sixes (
SEKAI{666...6}) — call itbase_rem. - Compute
target = -base_rem mod (2**67 - 1). This is a 67-bit number. - For each body position
i, figure out which bit oftargetthat position controls (via the(8*(67-i)) mod 67rotation), and flip '6' → '7' exactly where that bit is 1.
Done. no search, no iteration, just a direct bit-to-character mapping computed in microseconds.
MOD = 2**67 - 1
base_rem = int.from_bytes(b"SEKAI{" + b"6"*67 + b"}") % MOD
target = (-base_rem) % MOD
body = bytearray(b"6"*67)
for i in range(67):
exp = (8 * (67 - i)) % 67
if target & (1 << exp):
body[i] = 0x37
print(b"SEKAI{" + body + b"}")
Why the Earlier Attempts Failed
Worth noting for the record, since the thread shows the failure modes clearly:
- Manual bit extraction from a "target" integer assumed each byte's contribution was independent and base-256-aligned, but ignored that the modulus itself doesn't respect 256-alignment — it only respects 2-alignment with period 67. That mismatch is exactly what produced bytes like
0xff. - Random hill-climbing treated this as a fuzzy optimization problem, but it's actually a clean linear (GF(2)-style) system — there's no need for randomness once you see the rotation structure, and random walks over a 67-bit modular space are astronomically unlikely to hit exactly zero by luck.
The "muhehe" is purely number-theoretic: recognizing 2**67 - 1 as a Mersenne number turns "find a magic 67-character string" into "read off the bits of one integer."