Writeup · 31 Nights of Root

JANUS

cryptounknownsolvedFlag: r00t{REDACTED}

Overview

A decommissioned sensor node encrypted its telemetry with JANUS-20, a custom stream cipher. Provided files: janus, janus2, check, validator, ciphertext.bin, nonce.bin, nonce2.bin. The category is Crypto + Reverse Engineering. The intended path is to reverse the check verifier rather than brute-force the cipher.

Prerequisites / Setup

  • strings, xxd, checksec, radare2 (r2), objdump, Python 3
  • The seven provided files in the working directory

Recon

Step 1 — Survey the directory

Goal: List the provided files and check binary protections.

ls -la
checksec --file=janus
checksec --file=check
checksec --file=validator

All are PIE, NX enabled, no canary, Partial RELRO — standard modern binaries.

Step 2 — Extract strings from janus

Goal: Find obvious hints and the usage string.

strings janus

Two strings stand out:

r00t{REDACTED}
Usage: %s <hex32_seed> <hex24_nonce>

The r00t{REDACTED} string is a **fake flag**: it tells us there is a real flag, and this one is a decoy. The usage string shows janus` takes a 32-bit hex seed and a 24-hex-char (12-byte) nonce.

Step 3 — Inspect the nonces

Goal: Identify the nonce value.

xxd nonce.bin
xxd nonce2.bin

nonce2.bin contains:

4a41 4e55 5332 3032 3652 3030   →  "JANUS2026R00"

This is 12 bytes — the exact size required by a ChaCha20-style cipher. Nonce identified.

Understanding the Cipher — Static Analysis

Step 4 — Disassemble janus with radare2

Goal: Recover the calling convention and confirm the cipher structure.

r2 -A janus
pdf @ main

main reveals the calling convention:

janus20(seed, nonce, plaintext_buf, ciphertext_buf, length);
  • argv[1] → parsed with strtoul(..., 16) → 32-bit hex seed
  • argv[2] → parsed as 24 hex chars → 12-byte nonce
  • stdin → plaintext (read into a 64 KB buffer)
  • stdout → ciphertext

Key snippet from the hex decoding loop:

mov byte [rsp + rbx + 4], al   ; store nonce[i] = hex_byte
add rbx, 1
cmp rbx, 0xc                   ; loop exactly 12 times
jne 0x10f0

Confirmed: nonce is 12 bytes, seed is 32-bit.

Step 5 — Disassemble the cipher core

Goal: Identify the cipher family and key schedule.

pdf @ fcn.000012c0

The function is a ChaCha20 variant. Notable constants:

movdqa xmm4, xmmword [str.UNAJ02_SHPIC]   ; bytes reversed: "JANUS-20CIPH"
...
mov r10d, 0x43495048    ; "HPIC"
mov r11d, 0x532d3230    ; "02-S"
mov r13d, 0x4a414e55    ; "UNAJ"
mov r9d,  0x45520000    ; "..RE"  (little-endian "ER\0\0")

These form the ChaCha constant "JANUS-20CIPHER" (reversed bytes stored little-endian).

The main loop performs ChaCha’s classic quarter-round pattern with rotations of 16, 12, 8, 5 bits:

rol ecx, 0x10   ; 16
rol ebp, 0xc    ; 12
rol ecx, 8      ; 8
rol ebp, 5      ; 5

And the key schedule uses:

imul eax, eax, 0x6c078965   ; 1812433253 — classic MT19937 multiplier!
add eax, 0x9d               ; 157

This is the MT19937 seeding constant, used here to expand the 32-bit seed into the ChaCha key stream. The 32-bit seed is stretched into 8 key words via a Linear Congruential Generator (LCG).

Finally, the XOR step:

movzx edx, byte [rdi + rax]     ; plaintext byte
xor dl, byte [rcx]              ; XOR with keystream
mov byte [r8 + rax], dl         ; store ciphertext

Confirmed: stream cipher, encryption = decryption.

Step 6 — Recognize the “trap”

Goal: Understand why brute-forcing janus is the wrong path.

The fake flag + the fact that janus accepts an arbitrary seed means you could try to brute-force the seed. But the challenge designer left a hint that the seed is not 9a3f, and that flags are useful — meaning the real answer path is via check/validator, not brute force.

Exploitation — Decoding the Verifier

Step 7 — Inspect check

Goal: Recover how check builds and compares the expected flag.

r2 -A check
pdf @ main

The logic:

  1. Read input with fgets into a 256-byte buffer.
  2. Strip trailing \n/\r.
  3. Build the expected flag by XOR-ing two .rodata arrays.
  4. Compare with strcmp.

The XOR loop:

mov rcx, rax
add rdx, 1
and ecx, 3                     ; i % 4
movzx ecx, byte [rdi + rcx]    ; key[i % 4]
xor cl, byte [rsi + rax]       ; ^ ct[i]
add rax, 1
mov byte [rdx - 1], cl
cmp r8, rdx
jne 0x1240

Critical detail: rax starts at 1, not 0. And the first output byte is hardcoded:

mov byte [s2], 0x72            ; 'r'
lea rdx, [s2 + 1]              ; output starts at index 1

So ct[0] is a decoy. The real formula is:

flag[0]   = 'r'
flag[i]   = key[i % 4] ^ ct[i]   for i = 1 .. 43

Step 8 — Dump the .rodata arrays

Goal: Extract the key and ciphertext arrays.

objdump -s -j .rodata check

Output:

  2020 ffc6a5e2 00000000 00000000 00000000
  2030 00000000 00000000 00000000 00000000
  2040 8df69596 84ac918c 8ab5fa8f 8ab29196
  2050 cca2fad1 c7a5c681 c6fe91d0 c7f590da
  2060 cba7c0d0 99f091da caf5c49f
  • Key at 0x2020: ff c6 a5 e2
  • Ciphertext at 0x2040: 44 bytes

Step 9 — Decode the flag

Goal: Decode flag[i] = key[i % 4] ^ ct[i] for i = 1..43, with flag[0] = 'r'.

key = bytes.fromhex('ffc6a5e2')
ct  = bytes.fromhex(
    '8df6959684ac918c8ab5fa8f8ab29196'
    'cca2fad1c7a5c681c6fe91d0c7f590da'
    'cba7c0d099f091dacaf5c49f'
)

flag = 'r'
for i in range(1, len(ct)):
    flag += chr(key[i % 4] ^ ct[i])

print(flag)

Output:

r00t{REDACTED}

Step 10 — Verify

Goal: Confirm the decoded flag is accepted.

echo 'r00t{REDACTED}' | ./check
CORRECT
Flag: r00t{REDACTED}

(Optional) Confirm with the validator:

echo 'r00t{REDACTED}' | ./validator
VALID
Formula verified: r00t{REDACTED}
Submit to ./check for official scoring.

Flag

r00t{REDACTED}

Lessons / Takeaways

  • Read the assembly carefully. The loop counter rax starting at 1 (not 0) and the hardcoded 'r' at s2[0] were the two gotchas that caused the first wrong flag (38cc984 vs 38ccc984).
  • ct[0] is a decoy. The ciphertext array includes a throwaway byte at index 0.
  • Recognize standard constants. 0x6c078965 + 0x9d is the MT19937 seeding LCG. JANUS-20CIPHER reversed is the ChaCha constant. Spotting these tells you the cipher is a ChaCha variant with a custom key schedule.
  • The XOR obfuscation is trivial once you see it. A 4-byte repeating key XOR is cryptographically meaningless but pedagogically perfect for a CTF — the twist is reversing the loop bounds and hardcoded first byte to get the indexing right.
  • Brute-forcing the 32-bit seed (4 billion candidates) is infeasible without a known-plaintext crib; reversing the verifier is the intended path.

Techniques

References