dumpwn

Description: Caught mid air. Figure out what’s going on here:

https://github.com/youssefnoob003/CTFTASK/releases/tag/v1.0

Initial Analysis

~/Vm-Share ❯ file ctf.dmp                                                      09:09:48
ctf.dmp: MS Windows 64bit crash dump, version 15.26100, 2 processors, DumpType (0xa), 4992030524978970960 pages

The link contains a zip file. Once extracted we’re presented with a single dump file. A windows crash dump. To solve it, I’m aware that some players used volatility. As far as I know, it’s not the optimal approach for analyzing Windows crash dumps and it posed some issues during the analysis. You may use a RDMP wrapper but for this writeup I’m sticking with windbg.

Loading it on Windbg works off the bat, and a small hint about the task is displyed:

Unable to load image \??\C:\bomb.sys, Win32 error 0n2

This is a kernel forensics task, and the driver in question is bomb.sys. Let’s dive in.

What’s Happening

1: kd> u rip
bomb+0x1040:
fffff801`7e2c1040 4889542410      mov     qword ptr [rsp+10h],rdx
fffff801`7e2c1045 48894c2408      mov     qword ptr [rsp+8],rcx
fffff801`7e2c104a 4881ec88000000  sub     rsp,88h
fffff801`7e2c1051 488b8c2498000000 mov     rcx,qword ptr [rsp+98h]
fffff801`7e2c1059 e8d2050000      call    bomb+0x1630 (fffff801`7e2c1630)
fffff801`7e2c105e 4889442450      mov     qword ptr [rsp+50h],rax
fffff801`7e2c1063 c7442420100000c0 mov     dword ptr [rsp+20h],0C0000010h
fffff801`7e2c106b 48c744245800000000 mov   qword ptr [rsp+58h],0

Currently, the RIP is pointing at bomb+0x1074. This snippet comes from an IRP_MJ_DEVICE_CONTROL dispatch function, which uses the standard DRIVER_DISPATCH function prototype: NTSTATUS DispatchDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp). Under the 64-bit Microsoft x64 calling convention, RCX receives the PDEVICE_OBJECT pointer and RDX receives the PIRP pointer, returning a 32-bit NTSTATUS code in EAX.

1: kd> !irp rdx
Irp is active with 1 stacks 1 is current (= 0xffffa988bbef8a30)
 No Mdl: System buffer=ffffa988bdd5b800: Thread ffffa988bbd92080:  Irp stack trace.  
     cmd  flg cl Device   File     Completion-Context
>[IRP_MJ_DEVICE_CONTROL(e), N/A(0)]
            0  0 ffffa988be17de10 ffffa988bd43aee0 00000000-00000000    
	       \Driver\ctf
			Args: 00000020 00000020 0x222014 00000000

We can confirm this by casting the pointer in RDX as an IRP object. The driver is registered under the name \Driver\ctf.

Args: 00000020 00000020 0x222014 00000000
       │        │        └── IoControlCode = 0x222014   < THE IOCTL
       │        └── InputBufferLength = 0x20
       └── OutputBufferLength = 0x20

The stack location’s arguments tell us the request was directed at the IOCTL 0x222014, with a 0x20-byte input buffer and a 0x20-byte output buffer.

Note: An IOCTL (I/O Control Code) is a 32-bit identifier a user-mode program sends to a driver via DeviceIoControl() to request a device-specific operation. Each code encodes the device type, a function number, access rights, and the buffer transfer method. A driver checks this code in its dispatch routine to decide which internal function handles the request. 0x222014 decodes to a custom (function 0x805), buffered transfer, meaning user data is copied into the kernel via the IRP’s SystemBuffer field.

Next, we Disassemble the full dispatch handler and find the comparison against the IOCTL code. Simply search the output for 222014:

kd> uf fffff801`7e2c1040
...
fffff801`7e2c10a2 81781814202200  cmp     dword ptr [rax+18h],222014h
fffff801`7e2c10a9 0f8505020000    jne     bomb+0x12b4 (fffff801`7e2c12b4)  Branch
...

Since that comparison will succeed, the logic is after that jne instruction, let’s reverse it.

The Vulnerable Routine

fffff801`7e2c116d  mov  rax,[rsp+28h]        ; rax = input buffer (SystemBuffer)
fffff801`7e2c1172  mov  rax,[rax+18h]        ; rax = qword at input+0x18
fffff801`7e2c1176  mov  [rsp+38h],rax        ; save it in a local

The handler reads the 8 bytes at offset 0x18 of the input buffer and treats them as a kernel pointer. This value is fully controlled by the user process.

fffff801`7e2c1199  mov  rax,[rsp+38h]
fffff801`7e2c119e  mov  [bomb+0x3060],rax    ; global "registered object"

The pointer is then saved into the driver’s global at bomb+0x3060. This global survives across IOCTL calls.

fffff801`7e2c11b5  mov  qword ptr [rsp+68h],0        ; out-param = 0
fffff801`7e2c11be  lea  rax,[bomb+0x3010]
fffff801`7e2c11c5  add  rax,30h                      ; arg setup
fffff801`7e2c11c9  mov  rcx,[rsp+38h]                ; reload YOUR pointer
fffff801`7e2c11ce  mov  rcx,[rcx+20h]                ; rcx = [YOUR_ptr+0x20]
fffff801`7e2c11d2  mov  [rsp+70h],rcx
fffff801`7e2c11d7  lea  rdx,[rsp+68h]                ; rdx = &out-param
fffff801`7e2c11df  mov  rax,[rsp+70h]                ; rax = [YOUR_ptr+0x20]  ← code pointer
fffff801`7e2c11e4  call qword ptr [bomb+0x2048]      ; call through IAT slot

An argument is built from [ptr+0x20] and the driver loads that same value into rax before making the call for bomb+0x2048 bomb+0x2048 is the driver’s Import Address Table (IAT) but resolving it shows it doesn’t point to a system API. It points back inside the driver itself:

1: kd> dps fffff801`7e2c2048 L1
fffff801`7e2c2048  fffff801`7e2c1780 bomb+0x1780

1: kd> u bomb+0x1780
bomb+0x1780:
fffff801`7e2c1780 ffe0            jmp     rax

The target is a one-instruction trampoline: jmp rax. Since the driver deliberately placed the user-controlled value into rax just before the call, the full chain is:

call [bomb+0x2048]  -> jmp rax  ->  [input+0x18]+0x20  ->  ATTACKER CODE

This is an arbitrary kernel execution primitive. By sending an opcode-2 IOCTL the driver jumps directly to attacker-chosen code with kernel privileges.

The Payload

From the crash dump, the IRP’s SystemBuffer is ffffa988bdd5b800 (from !irp rdx). The attacker-controlled structure is read at offset 0x18:

1: kd> dq ffffa988bdd5b800+18 L1
ffffa988`bdd5b818  000001e8`33080000     planted object pointer (user-mode address)

The driver validated this user-space pointer and stored it in its global at bomb+0x3060. Following the driver’s own logic, it loads [pointer+0x20] into rax before the jmp rax trampoline:

1: kd> dq 000001e8`33080000+20 L1
000001e8`33080020  fffff801`e7ae5f9b     value the driver jumps to

Disassembling that address reveals it is not shellcode but a gadget inside ntoskrnl.exe. The attack uses ROP:

1: kd> u fffff801`e7ae5f9b
nt!SymCryptScsTableLoad128Xmm+0x167:
fffff801`e7ae5f9b 8be3            mov     esp,ebx       ; stack pivot: ESP = EBX
fffff801`e7ae5f9d c3              ret                     ; continue at [new ESP]

Why Not Shellcode? SMEP

The obvious question is, if the driver jumps to an attacker-controlled address (jmp rax), why did the attacker point it at a fragment of ntoskrnl.exe instead of directly at shellcode in their own buffer?

The answer is SMEP (Supervisor Mode Execution Prevention). SMEP is a CPU flag (CR4.SMEP).It enforces one rule: While the processor is in kernel mode (Ring 0), it may not execute code from pages marked as user-mode (Ring 3).

With execution of user pages blocked, attackers reuse code that already exists in kernel memory, a Return-Oriented Programming (ROP) chain.

What the hell is a stack pivot??

First, picture a textbook ROP chain as everyone draws it:

        attacker-controlled "stack" (in their own buffer)
        ┌─────────────────────┐
        │  &gadget_1          │──► pop rdi ; ret          (rdi = token addr)
        │  &value_for_rdi     │
        │  &gadget_2          │──► mov [rdi], rax ; ret   (overwrite token)
        │  &gadget_3          │──► restore & return to userland
        └─────────────────────┘
              ▲
              │ each ret pops the next address here
              │ and jumps to it

The most important part is the stack pointer: every ret reads the next address from wherever RSP points. The whole chain works only because RSP walks through memory the attacker filled. A ROP chain implies stack control and in our crash we only control RIP. The moment we land in our first gadget, the stack still belongs to the kernel:

  ORIGINAL KERNEL STACK (we do NOT control this)
  ┌──────────────────────────┐  ◄── RSP points here
  │  saved return address    │     (belongs to bomb's dispatch code)
  │  driver's locals         │
  │  ...other kernel frames  │
  └──────────────────────────┘

  result: gadget executes its first instruction,
  hits **ret** ... and pops the driver's garbage.
  Crash.

The first gadget can’t end in ret over enemy stack. So the first gadget must do one thing and one thing only: steal the stack.

  AFTER  mov esp, ebx
  
  ATTACKER STACK (fully controlled)
  ┌──────────────────────────┐  ◄── RSP now points here
  │  &gadget_2  (pop rdi)    │
  │  value_for_rdi           │
  │  &gadget_3               │
  │  ...rest of ROP chain... │
  └──────────────────────────┘

  result: the very next **ret** pops from OUR buffer.
  The ROP chain can finally begin.

Note: If you’re coming from Linux exploitation, you might be wondering how this works — after all, a stack pivot into a user-mode address means the kernel is now reading return addresses from user pages, and SMAP (Supervisor Mode Access Prevention) is supposed to block exactly that: kernel access to user memory. On Linux, SMAP is enabled by default and this pivot would fault instantly. On Windows, however, SMAP is pre-disabled. For the full breakdown of why Windows ships this way, see: https://sibouzitoun.tech/articles/smap-is-pre-disarmed/

Reconstructing the Chain

The pivot told us where the attacker stack lives: ebx = 0x60070. Dumping from there reveals the chain, one ret at a time. Each qword at an even offset is a gadget address; each qword right after it is the value that gadget pops.

Entry: the pivot

nt!SymCryptScsTableLoad128Xmm+0x167:
fffff801`e7ae5f9b  mov  esp,ebx    ; RSP = 0x60070
fffff801`e7ae5f9d  ret             ; jump to [0x60070]

Stage 1: stage the arguments for a kernel memcpy

1: kd> dq 60070
00000000`00060070  fffff801`e7820c7b 000001e8`33070000   <- gadget 1 + its operand
00000000`00060080  fffff801`e7a82952 ffffe28e`b2d1f000   <- gadget 2 + its operand
00000000`00060090  fffff801`e7a2de15 00000000`00001000   <- gadget 3 + its operand
00000000`000600a0  fffff801`e784f0dc fffff801`e7c6dd40   <- gadget 4 + its operand
00000000`000600b0  fffff801`e77df775 ...

Disassembling the four gadgets shows a classic ROP calling-convention staging. The chain loads the argument registers one by one, exactly as the Windows x64 ABI would:

gadget 1:  nt!HalpDisableInterrupts+0xb
           fffff801`e7820c7b  pop rcx / ret      rcx = 000001e8`33070000  (dst: user buffer)

gadget 2:  nt!HaliAcpiSleep+0x4f2
           fffff801`e7a82952  pop rdx / ret      rdx = ffffe28e`b2d1f000  (src: kernel pool)

gadget 3:  nt!RtlpHpSegHeapAddSegment+0x35
           fffff801`e7a2de15  pop r8 / ret       r8  = 0x1000               (count)

gadget 4:  nt!MiStoreGetWriteSupport+0x18
           fffff801`e784f0dc  pop rax / ret      rax = fffff801`e7c6dd40     (= nt!memcpy)

Stage 2: the indirect call

1: kd> u fffff801`e77df775
nt!ST_STORE<SM_TRAITS>::StWorkItemProcess+0x2a5:
fffff801`e77df775  jmp  rax       ; nt!memcpy

A jmp rax gadget serves as the call site. Execution lands in real, signed kernel code:

nt!memcpy:
fffff801`e7c6dd40  mov  rax,rcx
fffff801`e7c6dd43  cmp  r8,8
...

memcpy runs with kernel privileges and copies 0x1000 bytes from kernel pool address ffffe28eb2d1f000 into the attacker’s user-mode buffer at 000001e833070000. Basically a kernel memory disclosure delivered straight into userland.

The chain so far, diagrammed

 jmp rax (trampoline, in bomb)                mov esp,ebx (pivot)
        │                                          │
        ▼                                          ▼
 [0x60070] pop rcx ──► rcx = user dst ──► ret ──► [0x60080] pop rdx ──► rdx = kernel src
        ──► ret ──► [0x60090] pop r8 ──► r8 = 0x1000 ──► ret ──► [0x600a0] pop rax
        ──► rax = nt!memcpy ──► ret ──► [0x600b0] jmp rax ──► nt!memcpy(dst, src, 0x1000)
        ──► (returns to [0x600b8]... chain continues)

What Did the Attacker Read?

1: kd> dq ffffe28e`b2d1f000
ffffe28e`b2d1f000  6dbaf330`0b72a609 256a8a3d`bc508212
ffffe28e`b2d1f010  57b8fb27`0c4eb033 7a19e328`af4a9d00
ffffe28e`b2d1f020  6dbaf330`0b72a609 256a8a3d`bc508212
ffffe28e`b2d1f030  57b8fb27`0c4eb033 7a19e328`af4a9d00
ffffe28e`b2d1f040  6dbaf330`0b72a609 256a8a3d`bc508212
ffffe28e`b2d1f050  57b8fb27`0c4eb033 7a19e328`af4a9d00
ffffe28e`b2d1f060  6dbaf330`0b72a609 256a8a3d`bc508212
ffffe28e`b2d1f070  57b8fb27`0c4eb033 7a19e328`af4a9d00

This is clearly not the flag, but we can at least assume it is the encrypted flag. Notice the pattern: the exact same 32-byte block repeats over and over. Next step is accessing the exploit code and seeing what it did with that memory.

1: kd> !irp rdx
Irp is active with 1 stacks 1 is current (= 0xffffa988bbef8a30)
 No Mdl: System buffer=ffffa988bdd5b800: Thread ffffa988bbd92080:  Irp stack trace.  
     cmd  flg cl Device   File     Completion-Context
>[IRP_MJ_DEVICE_CONTROL(e), N/A(0)]
            0  0 ffffa988be17de10 ffffa988bd43aee0 00000000-00000000    
	       \Driver\ctf
			Args: 00000020 00000020 0x222014 00000000
1: kd> !thread ffffa988bbd92080
THREAD ffffa988bbd92080  Cid 1884.1890  Teb: 000000e3172fd000 Win32Thread: 0000000000000000 RUNNING on processor 1
IRP List:
    ffffa988bbef8960: (0006,0118) Flags: 00060070  Mdl: 00000000
Not impersonating
DeviceMap                 ffffe28eb4cdb6d0
Owning Process            ffffa988bb82a080       Image:         taskhostw.exe

Analyzing the IRP, we can find the target thread, which is owned by taskhostw.exe; the exploit masquerading as a legitimate service.

Unwinding to the exploit code

The thread’s stack already contains the full return path out of the kernel. We switch to the process context, restore the user-mode register state from the syscall trap frame, and let the debugger unwind the user stack:

1: kd> .process /r /p ffffa988bb82a080
Implicit process is now ffffa988`bb82a080
Loading User Symbols
.....
1: kd> .trap fffff60e`38d76ae0
rax=0000000000000000 rbx=0000000000000000 rcx=0000000000000000
...
rip=00007ff84f3e1d84 rsp=000000e3174ffaa8 rbp=0000000000000060
ntdll!NtDeviceIoControlFile+0x14:
0033:00007ff8`4f3e1d84 c3              ret
1: kd> k
  *** Stack trace for last set context - .thread/.cxr resets it
 # Child-SP          RetAddr               Call Site
00 000000e3`174ffaa8 00007ff8`4c995c43     ntdll!NtDeviceIoControlFile+0x14
01 000000e3`174ffab0 00007ff8`4dbc3335     KERNELBASE!DeviceIoControl+0x73
02 000000e3`174ffb20 00007ff6`b0d6262a     KERNEL32!DeviceIoControlImplementation+0x75
03 000000e3`174ffb70 00000000`00000000     taskhostw+0x262a

The unwind hands us the answer directly: after ntdll!NtDeviceIoControlFile > KERNELBASE!DeviceIoControl > KERNEL32!DeviceIoControlImplementation, execution returns into taskhostw+0x262a. The code inside the masquerading payload itself.

The decryption routine

Disassembling from the return address reveals a simple loop:

1: kd> u taskhostw+0x262a L50
taskhostw+0x262a:
00007ff6`b0d6262b  mov  dword ptr [rsp+60h],0          ; i = 0
00007ff6`b0d62633  jmp  taskhostw+0x263f
00007ff6`b0d62635  mov  eax,dword ptr [rsp+60h]
00007ff6`b0d62639  inc  eax
00007ff6`b0d6263b  mov  dword ptr [rsp+60h],eax        ; i++
00007ff6`b0d6263f  cmp  dword ptr [rsp+60h],20h        ; while (i < 0x20)
00007ff6`b0d62644  jge  taskhostw+0x267c
00007ff6`b0d6264b  mov  rcx,qword ptr [rsp+98h]        ; rcx = leaked buffer
00007ff6`b0d62653  movzx eax,byte ptr [rcx+rax]        ; c = leak[i]
00007ff6`b0d6265b  mov  ecx,dword ptr [rsp+60h]
00007ff6`b0d6265e  and  ecx,0Fh                        ; i % 16
00007ff6`b0d62661  lea  rdx,[taskhostw+0x18870]        ; rdx = key
00007ff6`b0d62668  movzx ecx,byte ptr [rdx+rcx]        ; k = key[i % 16]
00007ff6`b0d6266c  xor  eax,ecx                        ; c ^= k
00007ff6`b0d62673  mov  byte ptr [rsp+rcx+1A0h],al     ; plaintext[i] = c
00007ff6`b0d6267a  jmp  taskhostw+0x2635               ; loop

After the loop:

taskhostw+0x267c:
00007ff6`b0d626a0  mov  rax,qword ptr [rsp+0C8h]
00007ff6`b0d626a8  mov  byte ptr [rsp+rax+1A0h],0      ; NUL-terminate
00007ff6`b0d626b0  lea  rdx,[rsp+1A0h]                 ; rdx = decrypted string
00007ff6`b0d626b8  lea  rcx,[taskhostw+0x187e0]        ; rcx = format string
00007ff6`b0d626bf  call taskhostw+0x2740               ; print it

The decrypted buffer is then printed.

Recovering it

Dump the key and the ciphertext:

kd> db 00007ff6`b0d78870 L10          ; the 16-byte XOR key
kd> dq ffffe28e`b2d1f000 L4           ; the ciphertext (dq shows it as qwords)
ffffe28e`b2d1f000  6dbaf330`0b72a609 256a8a3d`bc508212
ffffe28e`b2d1f010  57b8fb27`0c4eb033 7a19e328`af4a9d00

Remember that dq prints each qword little-endian, so every 8-byte group must be byte-reversed before XORing. Feeding both into Python:

ct = bytes.fromhex(
    '09a6720b30f3ba6d'   # 6dbaf330`0b72a609
    '128250bc3d8a6a25'   # 256a8a3d`bc508212
    '33b04e0c27fbb857'   # 57b8fb27`0c4eb033
    '009d4aaf28e3197a'   # 7a19e328`af4a9d00
)

key = bytes.fromhex('5ac3117e429ad40866f12bc855e3197a')

flag = bytes(c ^ key[i % 16] for i, c in enumerate(ct))
print(flag.split(b'\x00')[0].decode())

Output:

Securinets{this_is_real_flag}

See you in the finals <3

* ▮