|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { use, useEffect } from 'react'; |
| 4 | +import { useExerciseContext } from '@/state/ExerciseContext'; |
| 5 | +import { getExercise } from '@/exercises/registry'; |
| 6 | +import { StackSim } from '@/engine/simulators/StackSim'; |
| 7 | +import { HeapSim } from '@/engine/simulators/HeapSim'; |
| 8 | +import { X86Emulator } from '@/engine/x86/emulator'; |
| 9 | +import { ArmEmulator } from '@/engine/arm/emulator'; |
| 10 | +import { MipsEmulator } from '@/engine/mips/emulator'; |
| 11 | +import { BASE_SYMBOLS } from '@/exercises/shared/symbols'; |
| 12 | +import SourcePanel from '@/components/panels/SourcePanel/SourcePanel'; |
| 13 | +import VizPanel from '@/components/panels/VizPanel/VizPanel'; |
| 14 | +import InputPanel from '@/components/panels/InputPanel/InputPanel'; |
| 15 | +import LogPanel from '@/components/panels/LogPanel/LogPanel'; |
| 16 | + |
| 17 | +function retAddrInMain(symbols: Record<string, number>): number { |
| 18 | + return (symbols.main || BASE_SYMBOLS.main) + 0x25; |
| 19 | +} |
| 20 | + |
| 21 | +function computeSymbols(exercise: ReturnType<typeof getExercise>): Record<string, number> { |
| 22 | + const symbols = { ...BASE_SYMBOLS }; |
| 23 | + |
| 24 | + if (exercise?.aslr) { |
| 25 | + const offset = ((Math.random() * 0x10000) >>> 0) & ~0xfff; |
| 26 | + for (const key of Object.keys(symbols)) { |
| 27 | + symbols[key] = (symbols[key] + offset) >>> 0; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if (exercise?.srop) { |
| 32 | + symbols['sigreturn'] = 0x080481b0; |
| 33 | + if (exercise.aslr) { |
| 34 | + const offset = symbols['main'] - BASE_SYMBOLS.main; |
| 35 | + symbols['sigreturn'] = (0x080481b0 + offset) >>> 0; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return symbols; |
| 40 | +} |
| 41 | + |
| 42 | +export default function ExercisePageClient({ params }: { params: Promise<{ id: string }> }) { |
| 43 | + const { id } = use(params); |
| 44 | + const { dispatch, stackSim, heapSim, asmEmulator } = useExerciseContext(); |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + const exercise = getExercise(id); |
| 48 | + if (!exercise) return; |
| 49 | + |
| 50 | + const symbols = computeSymbols(exercise); |
| 51 | + dispatch({ type: 'SET_SYMBOLS', symbols }); |
| 52 | + |
| 53 | + const vizMode = exercise.vizMode; |
| 54 | + const needsStack = vizMode === 'stack' || vizMode === 'both'; |
| 55 | + const needsHeap = vizMode === 'heap' || vizMode === 'both'; |
| 56 | + |
| 57 | + // Initialize StackSim |
| 58 | + if (needsStack || exercise.mode.startsWith('input-')) { |
| 59 | + const sim = new StackSim({ |
| 60 | + bufSize: exercise.bufSize ?? 16, |
| 61 | + retAddr: retAddrInMain(symbols), |
| 62 | + savedEbp: 0xbfff0200, |
| 63 | + useCanary: exercise.canary, |
| 64 | + }); |
| 65 | + |
| 66 | + if (exercise.mode === 'step') { |
| 67 | + sim.clearBlank(); |
| 68 | + } |
| 69 | + |
| 70 | + if (exercise.srop || exercise.ret2libc) { |
| 71 | + const bigSim = new StackSim( |
| 72 | + exercise.bufSize ?? 16, |
| 73 | + retAddrInMain(symbols), |
| 74 | + 0xbfff0200, |
| 75 | + undefined, |
| 76 | + exercise.canary, |
| 77 | + ); |
| 78 | + stackSim.current = bigSim; |
| 79 | + } else { |
| 80 | + stackSim.current = sim; |
| 81 | + } |
| 82 | + } else { |
| 83 | + stackSim.current = null; |
| 84 | + } |
| 85 | + |
| 86 | + // Initialize HeapSim |
| 87 | + if (needsHeap) { |
| 88 | + const heap = new HeapSim(exercise.heapSize ?? 256); |
| 89 | + |
| 90 | + if (exercise.mode === 'heap-uaf') { |
| 91 | + const userResult = heap.malloc(16); |
| 92 | + if (userResult) { |
| 93 | + setTimeout(() => { |
| 94 | + dispatch({ type: 'SET_HEAP_NAME', name: 'User', addr: userResult.addr }); |
| 95 | + heap._writeLE(userResult.dataAddr, symbols.normal, 4); |
| 96 | + heap.funcPtrs['action'] = { original: symbols.normal, current: symbols.normal } as any; |
| 97 | + dispatch({ type: 'BUMP_VIZ' }); |
| 98 | + }, 0); |
| 99 | + } |
| 100 | + } else if (exercise.mode === 'heap-double-free') { |
| 101 | + const aResult = heap.malloc(16); |
| 102 | + if (aResult) { |
| 103 | + setTimeout(() => { |
| 104 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 105 | + dispatch({ type: 'BUMP_VIZ' }); |
| 106 | + }, 0); |
| 107 | + } |
| 108 | + } else if (exercise.mode === 'heap-overflow') { |
| 109 | + const aResult = heap.malloc(16); |
| 110 | + const bResult = heap.malloc(16); |
| 111 | + if (aResult && bResult) { |
| 112 | + heap._writeLE(bResult.dataAddr, symbols.normal, 4); |
| 113 | + heap.funcPtrs['handler'] = { original: symbols.normal, current: symbols.normal } as any; |
| 114 | + setTimeout(() => { |
| 115 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 116 | + dispatch({ type: 'SET_HEAP_NAME', name: 'B', addr: bResult.addr }); |
| 117 | + dispatch({ type: 'BUMP_VIZ' }); |
| 118 | + }, 0); |
| 119 | + } |
| 120 | + } else if (exercise.mode === 'heap-tcache-poison') { |
| 121 | + const aResult = heap.malloc(16); |
| 122 | + const bResult = heap.malloc(16); |
| 123 | + if (aResult && bResult) { |
| 124 | + setTimeout(() => { |
| 125 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 126 | + dispatch({ type: 'SET_HEAP_NAME', name: 'B', addr: bResult.addr }); |
| 127 | + dispatch({ type: 'BUMP_VIZ' }); |
| 128 | + }, 0); |
| 129 | + } |
| 130 | + } else if (exercise.mode === 'heap-fastbin-dup') { |
| 131 | + for (let i = 0; i < 7; i++) { |
| 132 | + const tmp = heap.malloc(16); |
| 133 | + if (tmp) heap.free(tmp.addr); |
| 134 | + } |
| 135 | + const aResult = heap.malloc(16); |
| 136 | + const bResult = heap.malloc(16); |
| 137 | + if (aResult && bResult) { |
| 138 | + setTimeout(() => { |
| 139 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 140 | + dispatch({ type: 'SET_HEAP_NAME', name: 'B', addr: bResult.addr }); |
| 141 | + dispatch({ type: 'BUMP_VIZ' }); |
| 142 | + }, 0); |
| 143 | + } |
| 144 | + } else if (exercise.mode === 'heap-unsorted-bin') { |
| 145 | + const aResult = heap.malloc(128); |
| 146 | + const guardResult = heap.malloc(16); |
| 147 | + if (aResult && guardResult) { |
| 148 | + setTimeout(() => { |
| 149 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 150 | + dispatch({ type: 'SET_HEAP_NAME', name: 'guard', addr: guardResult.addr }); |
| 151 | + dispatch({ type: 'BUMP_VIZ' }); |
| 152 | + }, 0); |
| 153 | + } |
| 154 | + } else if (exercise.mode === 'heap-house-force' || exercise.mode === 'heap-house-orange') { |
| 155 | + const aResult = heap.malloc(16); |
| 156 | + if (aResult) { |
| 157 | + setTimeout(() => { |
| 158 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 159 | + dispatch({ type: 'BUMP_VIZ' }); |
| 160 | + }, 0); |
| 161 | + } |
| 162 | + } else if (exercise.mode === 'heap-house-einherjar') { |
| 163 | + const aResult = heap.malloc(16); |
| 164 | + const bResult = heap.malloc(16); |
| 165 | + const cResult = heap.malloc(16); |
| 166 | + const guardResult = heap.malloc(16); |
| 167 | + if (aResult && bResult && cResult && guardResult) { |
| 168 | + setTimeout(() => { |
| 169 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 170 | + dispatch({ type: 'SET_HEAP_NAME', name: 'B', addr: bResult.addr }); |
| 171 | + dispatch({ type: 'SET_HEAP_NAME', name: 'C', addr: cResult.addr }); |
| 172 | + dispatch({ type: 'SET_HEAP_NAME', name: 'guard', addr: guardResult.addr }); |
| 173 | + dispatch({ type: 'BUMP_VIZ' }); |
| 174 | + }, 0); |
| 175 | + } |
| 176 | + } else if (exercise.mode === 'heap-house-lore') { |
| 177 | + const aResult = heap.malloc(80); |
| 178 | + const guardResult = heap.malloc(16); |
| 179 | + if (aResult && guardResult) { |
| 180 | + setTimeout(() => { |
| 181 | + dispatch({ type: 'SET_HEAP_NAME', name: 'A', addr: aResult.addr }); |
| 182 | + dispatch({ type: 'SET_HEAP_NAME', name: 'guard', addr: guardResult.addr }); |
| 183 | + dispatch({ type: 'BUMP_VIZ' }); |
| 184 | + }, 0); |
| 185 | + } |
| 186 | + } else if (exercise.mode === 'final-blind') { |
| 187 | + const ptrs: Array<ReturnType<typeof heap.malloc>> = []; |
| 188 | + for (let i = 0; i < 4; i++) { |
| 189 | + ptrs.push(heap.malloc(32)); |
| 190 | + } |
| 191 | + if (ptrs[0]) { |
| 192 | + heap._writeLE(ptrs[0].dataAddr, symbols.normal, 4); |
| 193 | + heap.funcPtrs['fn'] = { original: symbols.normal, current: symbols.normal } as any; |
| 194 | + } |
| 195 | + if (ptrs[1]) heap.free(ptrs[1].addr); |
| 196 | + if (ptrs[2]) heap.free(ptrs[2].addr); |
| 197 | + |
| 198 | + setTimeout(() => { |
| 199 | + if (ptrs[0]) dispatch({ type: 'SET_HEAP_NAME', name: 'ptr0', addr: ptrs[0].addr }); |
| 200 | + if (ptrs[1]) dispatch({ type: 'SET_HEAP_NAME', name: 'ptr1', addr: ptrs[1].addr }); |
| 201 | + if (ptrs[2]) dispatch({ type: 'SET_HEAP_NAME', name: 'ptr2', addr: ptrs[2].addr }); |
| 202 | + if (ptrs[3]) dispatch({ type: 'SET_HEAP_NAME', name: 'ptr3', addr: ptrs[3].addr }); |
| 203 | + dispatch({ type: 'BUMP_VIZ' }); |
| 204 | + }, 0); |
| 205 | + } |
| 206 | + |
| 207 | + heapSim.current = heap; |
| 208 | + } else { |
| 209 | + heapSim.current = null; |
| 210 | + } |
| 211 | + |
| 212 | + // Initialize ASM emulator |
| 213 | + if (exercise.asmInstructions) { |
| 214 | + const arch = exercise.asmArch ?? 'x86'; |
| 215 | + let emu; |
| 216 | + if (arch === 'arm') { |
| 217 | + emu = new ArmEmulator( |
| 218 | + exercise.asmInstructions, |
| 219 | + exercise.asmInitialRegs, |
| 220 | + exercise.asmStackBase ?? 0xbfff0200, |
| 221 | + ); |
| 222 | + } else if (arch === 'mips') { |
| 223 | + emu = new MipsEmulator( |
| 224 | + exercise.asmInstructions, |
| 225 | + exercise.asmInitialRegs, |
| 226 | + exercise.asmStackBase ?? 0x7ffffffc, |
| 227 | + ); |
| 228 | + } else { |
| 229 | + emu = new X86Emulator( |
| 230 | + exercise.asmInstructions, |
| 231 | + exercise.asmInitialRegs, |
| 232 | + exercise.asmStackBase ?? 0xbfff0200, |
| 233 | + arch, |
| 234 | + ); |
| 235 | + } |
| 236 | + if (exercise.asmInitialMemory) { |
| 237 | + for (const { addr, value, size } of exercise.asmInitialMemory) { |
| 238 | + emu.writeMem(addr, value, size); |
| 239 | + } |
| 240 | + } |
| 241 | + asmEmulator.current = emu; |
| 242 | + } else { |
| 243 | + asmEmulator.current = null; |
| 244 | + } |
| 245 | + |
| 246 | + if (exercise.rop || exercise.srop) { |
| 247 | + dispatch({ type: 'SET_REGISTERS', registers: { |
| 248 | + eax: 0, ebx: 0, ecx: 0, edx: 0, esp: 0, eip: 0, |
| 249 | + }}); |
| 250 | + } |
| 251 | + |
| 252 | + dispatch({ type: 'LOAD_EXERCISE', exerciseId: id }); |
| 253 | + }, [id]); // eslint-disable-line react-hooks/exhaustive-deps |
| 254 | + |
| 255 | + return ( |
| 256 | + <> |
| 257 | + <SourcePanel /> |
| 258 | + <VizPanel /> |
| 259 | + <InputPanel /> |
| 260 | + <LogPanel /> |
| 261 | + </> |
| 262 | + ); |
| 263 | +} |
0 commit comments