bcsctf{vms_w1th1n_vms_4ll_th3_w4y_d0wn_a39f1c}bcsctf{vms_w1th1n_vms_4ll_th3_w4y_d0wn_a39f1c}challenge (ELF 64-bit executable), challenge_vm.bin (VM bytecode/data)The challenge implements a two-level nested virtual machine architecture:
challenge_vm.bin, accepts the flag via stdin, places it at memory offset 0x4000, and invokes the hypervisor.0x2000. It checks the length of the flag and performs a byte-by-byte cipher transformation with a rolling state before comparing against pre-stored ciphertext at offset 0x2880.Because the cipher operations in the Guest VM are strictly bijective over $\mathbb{Z}_{256}$, the flag can be recovered directly by mathematically inverting each step.
challenge)Inspecting the binary with readelf and objdump:
0x400000), stripped.main: main is located at 0x4010e0.main Function Flow:0x10000 bytes) of memory at 0x4060a0 with memset.0x2900 (10,496) bytes from .rodata (0x402120) to 0x4060a0 with memcpy. This matches the contents of challenge_vm.bin identically."Enter flag: " and reads input via fgets into a local stack buffer.\n and \r.0x40a0a0 (0x4060a0 + 0x4000), meaning the flag resides at offset 0x4000 in the virtual machine's memory space.0x401350 passing 0x4060a0 as the memory base.eax) is 0, it prints:[+] Access Granted! Flag verified successfully.[-] Access Denied! Invalid flag.The outer VM starts at 0x401350.
R0–R7: Eight 16-bit registers stored at [rsp - 0x18 + reg * 2], cleared initially via pxor xmm0, xmm0.SP / r8w): Initialized to 0x7ffe.PC / esi): Initialized to 0x0000.r9b tracks Zero Flag (ZF), r10b tracks Carry/Below Flag (CF).rdi): Pointer to the 64 KB VM memory buffer.At address 0x401370, the VM fetches the opcode at mem[PC] and performs an indirect jump via a jump table at 0x402020 consisting of 32 entries (0x00 to 0x1f):
| Opcode | Mnemonic | Format | Description |
|---|---|---|---|
0x00 |
NOP |
[0x00] |
Advances PC by 1. |
0x01 |
RET |
[0x01] |
PC = pop16() |
0x02 |
PUSH rA |
[0x02, rA] |
push16(R[rA]) |
0x03 |
POP rA |
[0x03, rA] |
R[rA] = pop16() |
0x04 |
HALT rA |
[0x04, rA] |
Terminates VM execution, returns R[rA] |
0x05 |
INC rA |
[0x05, rA] |
R[rA] += 1 |
0x06 |
DEC rA |
[0x06, rA] |
R[rA] -= 1 |
0x07 |
MOV_IMM rA, imm16 |
[0x07, rA, imm16] |
R[rA] = imm16 |
0x08 |
ADD_IMM rA, imm16 |
[0x08, rA, imm16] |
R[rA] += imm16, updates ZF |
0x09 |
SUB_IMM rA, imm16 |
[0x09, rA, imm16] |
R[rA] -= imm16, updates ZF |
0x0a |
CMP_IMM rA, imm16 |
[0x0a, rA, imm16] |
Compares R[rA] with imm16, updates ZF and CF |
0x0b |
AND_IMM rA, imm16 |
[0x0b, rA, imm16] |
R[rA] &= imm16, updates ZF |
0x0c |
MOV rA, rB |
[0x0c, rA, rB] |
R[rA] = R[rB] |
0x0d |
ADD rA, rB |
[0x0d, rA, rB] |
R[rA] += R[rB], updates ZF |
0x0e |
SUB rA, rB |
[0x0e, rA, rB] |
R[rA] -= R[rB], updates ZF |
0x0f |
MUL rA, rB |
[0x0f, rA, rB] |
R[rA] = (R[rA] * R[rB]) & 0xffff |
0x10 |
DIV rA, rB |
[0x10, rA, rB] |
R[rA] = R[rA] // R[rB] (0 if R[rB] == 0) |
0x11 |
MOD rA, rB |
[0x11, rA, rB] |
R[rA] = R[rA] % R[rB] (0 if R[rB] == 0) |
0x12 |
XOR rA, rB |
[0x12, rA, rB] |
R[rA] ^= R[rB] |
0x13 |
AND rA, rB |
[0x13, rA, rB] |
R[rA] &= R[rB] |
0x14 |
OR rA, rB |
[0x14, rA, rB] |
`R[rA] |
0x15 |
CMP rA, rB |
[0x15, rA, rB] |
Compares R[rA] with R[rB], updates ZF and CF |
0x16 |
ROL8 rA, rB |
[0x16, rA, rB] |
R[rA] = rol8(R[rA] & 0xff, R[rB]) |
0x17 |
LOADB rA, [rB] |
[0x17, rA, rB] |
R[rA] = mem[R[rB]] (byte) |
0x18 |
STOREB [rA], rB |
[0x18, rA, rB] |
mem[R[rA]] = R[rB] & 0xff |
0x19 |
LOADW rA, [rB] |
[0x19, rA, rB] |
R[rA] = mem16[R[rB]] (word) |
0x1a |
STOREW [rA], rB |
[0x1a, rA, rB] |
mem16[R[rA]] = R[rB] (word) |
0x1b |
JMP target |
[0x1b, target16] |
PC = target16 |
0x1c |
JZ target |
[0x1c, target16] |
Jump if ZF == 1 |
0x1d |
JNZ target |
[0x1d, target16] |
Jump if ZF == 0 |
0x1e |
JB target |
[0x1e, target16] |
Jump if CF == 1 (unsigned below) |
0x1f |
CALL target |
[0x1f, target16] |
push16(PC + 3); PC = target16 |
Disassembling the outer bytecode at 0x0000–0x02a6 reveals what the hypervisor does:
; 1. Copy 256 bytes from 0x2800 to 0x3800
0x0000: MOV R4, 0x2800
0x0004: MOV R5, 0x3800
0x0008: MOV R0, 0x0100
copy_loop:
0x000c: LOADB R3, [R4]
0x000f: STOREB [R5], R3
0x0012: INC R4
0x0014: INC R5
0x0016: DEC R0
0x0018: CMP R0, 0x0000
0x001c: JNZ copy_loop
; 2. Initialize Guest VM environment registers
0x001f: MOV R1, 0x2000 ; Guest PC starts at offset 0x2000
0x0023: MOV R2, 0x3000 ; Guest SP starts at offset 0x3000
0x0027: MOV R6, 0x3800 ; Guest Data/Memory pointer
0x002b: MOV R7, 0x4000 ; Flag buffer pointer
; 3. Guest VM interpreter loop
fetch:
0x002f: LOADB R3, [R1] ; Fetch guest opcode
0x0032: INC R1 ; Advance guest PC
; ... Compare R3 against opcodes 0x00 to 0x16 ...
The outer VM sets up and runs a nested stack machine interpreter!
The inner VM implements a byte-oriented stack machine:
| Opcode | Mnemonic | Stack Effect | Description |
|---|---|---|---|
0x00 |
NOP |
— | No operation |
0x01 |
PUSH imm8 |
[ ] -> [ imm8 ] |
Pushes immediate byte |
0x02 |
POP |
[ a ] -> [ ] |
Drops top of stack |
0x03 |
DUP |
[ a ] -> [ a, a ] |
Duplicates top element |
0x04 |
SWAP |
[ a, b ] -> [ b, a ] |
Swaps top two elements |
0x05 |
ADD |
[ a, b ] -> [ (a + b) & 0xff ] |
8-bit addition |
0x06 |
SUB |
[ a, b ] -> [ (a - b) & 0xff ] |
8-bit subtraction |
0x07 |
MUL |
[ a, b ] -> [ (a * b) & 0xff ] |
8-bit multiplication |
0x08 |
XOR |
[ a, b ] -> [ a ^ b ] |
Bitwise XOR |
0x09 |
AND |
[ a, b ] -> [ a & b ] |
Bitwise AND |
0x0a |
OR |
[ a, b ] -> [ a | b ] |
Bitwise OR |
0x0b |
ROL8 |
[ a, shift ] -> [ rol8(a, shift) ] |
Rotate left 8-bit |
0x0c |
MOD |
[ a, b ] -> [ a % b ] |
8-bit modulo |
0x0d |
LOAD_FLAG |
[ idx ] -> [ flag[idx] ] |
Reads byte from flag buffer |
0x0e |
LOAD_MEM |
[ idx ] -> [ mem[idx] ] |
Reads byte from guest memory (0x3800 + idx) |
0x0f |
STORE_MEM |
[ val, idx ] -> [ ] |
Stores mem[idx] = val |
0x10 |
EQ |
[ a, b ] -> [ a == b ? 1 : 0 ] |
Tests equality |
0x11 |
LT |
[ a, b ] -> [ a < b ? 1 : 0 ] |
Tests less than (unsigned) |
0x12 |
JMP imm16 |
— | Unconditional jump to 0x2000 + imm16 |
0x13 |
JZ imm16 |
[ cond ] -> [ ] |
Jump if cond == 0 |
0x14 |
JNZ imm16 |
[ cond ] -> [ ] |
Jump if cond != 0 |
0x15 |
ASSERT_EQ |
[ a, b ] -> [ ] |
If a != b, immediately halts with exit(1) |
0x16 |
HALT |
[ code ] -> [ ] |
Halts execution, returning code (0 = success) |
The guest bytecode spans addresses 0x2000 to 0x2051 (relative offsets 0x0000 to 0x0051):
; Length Check:
0x0000: PUSH 0x2e ; 46
0x0002: LOAD_FLAG ; load flag[46]
0x0003: PUSH 0x00 ; 0
0x0005: ASSERT_EQ ; assert flag[46] == 0 (null terminator, length = 46)
; State Initialization:
0x0006: PUSH 0x00 ; val = 0
0x0008: PUSH 0x00 ; idx = 0
0x000a: STORE_MEM ; mem[0] = 0 (loop index i = 0)
0x000b: PUSH 0x5a ; val = 0x5a
0x000d: PUSH 0x01 ; idx = 1
0x000f: STORE_MEM ; mem[1] = 0x5a (running IV / prev state = 0x5a)
loop_start:
0x0010: PUSH 0x00
0x0012: LOAD_MEM ; load i = mem[0]
0x0013: PUSH 0x2e ; 46
0x0015: EQ ; i == 46 ?
0x0016: JNZ loop_end ; if so, exit loop successfully
; Body:
0x0019: PUSH 0x00
0x001b: LOAD_MEM ; i
0x001c: LOAD_FLAG ; flag[i]
0x001d: PUSH 0x73
0x001f: MUL ; (flag[i] * 0x73) & 0xff
0x0020: PUSH 0x45
0x0022: ADD ; + 0x45
0x0023: PUSH 0x9b
0x0025: XOR ; ^ 0x9b
0x0026: PUSH 0x01
0x0028: LOAD_MEM ; load prev (mem[1])
0x0029: XOR ; val = (((flag[i] * 0x73 + 0x45) & 0xff) ^ 0x9b) ^ prev
0x002a: PUSH 0x00
0x002c: LOAD_MEM ; i
0x002d: PUSH 0x05
0x002f: MOD ; i % 5
0x0030: PUSH 0x01
0x0032: ADD ; shift = (i % 5) + 1
0x0033: ROL8 ; rol8(val, shift)
0x0034: PUSH 0x3c
0x0036: XOR ; cur = rol8(val, shift) ^ 0x3c
0x0037: DUP
0x0038: PUSH 0x01
0x003a: STORE_MEM ; mem[1] = cur (update prev for next iteration)
0x003b: PUSH 0x00
0x003d: LOAD_MEM ; i
0x003e: PUSH 0x80
0x0040: ADD ; 0x80 + i
0x0041: LOAD_MEM ; expected = mem[0x80 + i]
0x0042: ASSERT_EQ ; assert cur == expected
; Increment i:
0x0043: PUSH 0x00
0x0045: LOAD_MEM
0x0046: PUSH 0x01
0x0048: ADD
0x0049: PUSH 0x00
0x004b: STORE_MEM ; mem[0] = i + 1
0x004c: JMP loop_start
loop_end:
0x004f: PUSH 0x00
0x0051: HALT ; exit(0) -> Access Granted!
The expected values are read from mem[0x80 + i]. Because mem is initialized by copying 256 bytes from offset 0x2800 of challenge_vm.bin, the expected array is located at offset 0x2880 in challenge_vm.bin for 46 bytes:
expected = [
0x29, 0x0c, 0xf7, 0x11, 0x41, 0xa7, 0xd6, 0x6c, 0x80, 0x82,
0xeb, 0x17, 0x1a, 0x32, 0xb6, 0xf6, 0x37, 0xce, 0x1d, 0x6b,
0x00, 0x99, 0x21, 0x0b, 0x17, 0xc0, 0xd4, 0xa9, 0xfd, 0xae,
0x23, 0x58, 0x27, 0xd8, 0x72, 0x44, 0xa9, 0xd0, 0xa7, 0xa0,
0x16, 0x89, 0x14, 0x40, 0x90, 0xf2
]
cur == expected[i], we know:prev and constant 0x9b:solve.py)#!/usr/bin/env python3
"""
BCS CTF - Nested VM (Hypervisor) Solver
"""
def ror8(val, shift):
shift %= 8
return ((val >> shift) | (val << (8 - shift))) & 0xff
def solve():
with open('challenge_vm.bin', 'rb') as f:
data = f.read()
# Read 46 expected bytes from offset 0x2880
expected = data[0x2880 : 0x2880 + 46]
inv_73 = pow(0x73, -1, 256) # 0xbb
prev = 0x5a
flag = []
for i in range(46):
exp = expected[i]
shift = (i % 5) + 1
val = ror8(exp ^ 0x3c, shift)
t1 = val ^ prev ^ 0x9b
t2 = (t1 - 0x45) % 256
char = (t2 * inv_73) % 256
flag.append(char)
prev = exp
return bytes(flag).decode('ascii')
if __name__ == '__main__':
flag = solve()
print(f"[+] Flag: {flag}")
Running the solver and piping the output into the challenge binary:
$ python3 solve.py
[+] Recovered Flag: bcsctf{vms_w1th1n_vms_4ll_th3_w4y_d0wn_a39f1c}
$ ./challenge <<< "bcsctf{vms_w1th1n_vms_4ll_th3_w4y_d0wn_a39f1c}"
=== BCS CTF: Hypervisor Challenge ===
Enter flag:
[+] Access Granted! Flag verified successfully.
Flag:
bcsctf{vms_w1th1n_vms_4ll_th3_w4y_d0wn_a39f1c}