Brainfork custom Brainfuck-style VM

rev
flag d4rk{1mag1n3_d01ng_th4t_by_h4nd}c0de

d4rk{1mag1n3_d01ng_th4t_by_h4nd}c0de

Summary

output.txt is a program for a custom Brainfork token VM — a Brainfuck-family interpreter whose whole instruction set is written out as bracketed tokens. The handout also embeds the machine's memory dump (the state the program prints when it runs). Each token mutates a tape of cells: [}n] / [{n] move the data pointer, [vn] / [^n] write/add to the current cell, and a handful of single-character ops (most importantly ~) drive control and output. The flag is baked straight into the program: the 36 characters are set with [vC]~ pairs where C is the character's ASCII value in hex — 36 set-then-emit blocks, one per character, 16 tokens apart. The challenge overrides the usual format (see the description: d4rk{...}c0de, exactly 36 chars, starting d4rk{ and ending }c0de), and the emitted string matches that exactly.

Solution / Exploit

1. The handout

output.txt has five lines:

output
line 0 : the program        (32159 chars of tokens)
line 1 : "MEMORY DUMP"
line 2 : "Pointer : 0"
line 3 : a 500-element memory dump array
line 4 : "Code Successful"

The program is a single line of tokens; the dump is the VM's memory after it runs.

2. The token VM

brainfork_sim.py (kept with the source) reconstructs the interpreter. Tokens are either a bracketed op or a single character:

output
[}n]   data pointer += n            (n is hex)
[{n]   data pointer -= n
[vn]   mem[ptr]  = n
[^n]   mem[ptr] += n

plus single-character ops that the tokenizer keeps verbatim (~ % @ $ # ( ? = ! i). The handout's program is 9912 tokens; the single-char op counts are:

output
~ 2952   @ 288   $ 252   % 143   } 70   ? 36   ( 36   # 36   = 36   ! 36   { 35   i 1

~ (the emit/output op) dominates; the rare ops (? = ! # ( i) appear ~36 times each — one per flag character — which is the first hint that the flag is unrolled into the program body.

3. The setup block reproduces the dump

The first 113 tokens are pure bracket ops that initialise the constant region of the tape. brainfork_sim.py runs just those tokens and checks the result against the dump:

python
1toks, dump = load()
2print('tokens', len(toks), 'dump len', len(dump))
3# First: only run tokens 0..113 (pure bracket ops) to check the setup
4mem, oc = simulate(toks[:114], {})
5print('after setup tokens 0..113:')
6print('  mem[113..148] =', mem[113:149])
7print('  expected consts=', dump[113:149])
8print('  match:', mem[113:149] == dump[113:149])
9print('  nonzero:', [(k, v) for k, v in enumerate(mem) if v])

The token stream opens with [}71][v0][^21b][}1][v0][^1a9][}1][v0][^305]… — i.e. jump the pointer to cell 113 and fill cells 113, 114, 115, … with 0x21b, 0x1a9, 0x305, … which is exactly the constant region seen in the dump.

4. The memory layout

The 500-cell dump breaks down as:

cells contents
0..40 small header / scratch (mostly zeros, a few small values)
41..76 a block of 36 large values
77..112 the same 36 values again
113..148 the same 36 values a third time
149..184 a 36-entry table (values in 144..191)
185..440 a 256-byte permutation of 0..255 (a substitution box)
441..499 zeros

The three identical 36-value blocks and the 256-byte bijective S-box are the program's working state; the 36-value count lines up with a 36-character flag.

5. The flag is emitted by [vC]~ pairs

The ~ op emits the current cell. Scanning the token stream for [vC] immediately followed by ~ and decoding C as a byte gives, in order, the 36 flag characters. The first block is:

output
… ~ [}94] ~ [{93][v0] ~ [{1]% ~ [}28][v64] ~ [}1] ~ [{29][vf6] ~ [}95] ~ [{94][v92] ~ [{1]% ~
    [}29][v34] ~ [}1] ~ [{2a][va5] ~ [}96] ~ [{95][v91] ~ [{1]% ~ [}2a][v72] ~ [}1] ~ …

The printable [vC]~ pairs spell the flag; the other ~ ops in each 16-token block emit intermediate, non-printable values. Reproducing this filter over the whole program:

python
1import re
2prog = open("output.txt", newline="").read().split("\n")[0].rstrip("\r")
3toks = re.findall(r"\[[^\]]*\]|.", prog)
4vals = [int(re.fullmatch(r"\[v([0-9a-f]+)\]", toks[i]).group(1), 16)
5        for i in range(len(toks) - 1)
6        if re.fullmatch(r"\[v([0-9a-f]+)\]", toks[i]) and toks[i + 1] == "~"]
7print("".join(chr(v) for v in vals if 32 <= v < 127)[:36])

The 36 flag characters are set at token indices 1646, 1662, 1678, … 2206 — evenly spaced 16 tokens apart (one [vC]~ block per character). The extracted string is:

output
d4rk{1mag1n3_d01ng_th4t_by_h4nd}c0de

6. Talking to the service

The challenge ships nc <target>, and three probe scripts are kept with the source:

  • bp.py — connects and sends a token program, printing the raw response (run(code) opens the socket, drains the banner, sends, reads back).
  • probe.py — one-line probes: i, [v41]i, [v41][}2][v42]i.
  • probe2.py — sends the handout's program itself (output.txt[:32158], with and without a trailing newline) and saves the responses to out_*.txt.

The service consumes the program and returns its output (the MEMORY DUMP block), which is how the dump in the handout was obtained.

Flag(s)

output
d4rk{1mag1n3_d01ng_th4t_by_h4nd}c0de

Alternates

  • The deliverable README table lists d4rk{1mag1n3_d01ng_th4t_by_h4nd} and notes the server appends c0de after the brace. Combined with the challenge's stated format d4rk{...}c0de, the full 36-character flag is the form above (the emitted string is exactly 36 chars, d4rk{ … }c0de).

Notes / Pitfalls

  • The flag is literally embedded, not brute-forced. No search is needed — the 36 characters are unrolled into the program as [vC]~ set-then-emit pairs; you just decode them.
  • The dump's 36-value blocks are not the flag. Cells 41..112 hold large transformed values (the program's working state), not the plaintext. The plaintext is in the token stream itself (§5). Reading the blocks as bytes gives nothing printable.
  • The single-char ops are the hard part of a full disassembly. The sim scripts only implement the bracket ops ([}n] [{n] [vn] [^n]); the semantics of ~ % @ $ # ( ? = ! i are what the real VM adds, and ~ (emit) is the one that matters here. The name "Brainfork" is a Brainfuck-fork nod, and the token soup is heavily obfuscated by design.
  • The live checker never gave a clean verdict. The service at nc <target> consumes input rather than cleanly accepting/rejecting; per the shipped notes it never returned a clean "correct" for any input. The flag is recovered from the artifact and matches the stated 36-char format.
  • Format quirk. This challenge uses d4rk{...}c0de — a different format from the rest of the CTF (the description calls this out explicitly).