windows kernel fuzzing for intermediate learners - ben nagy, coseinc

87
Windows Kernel Fuzzing for Intermediate Learners Ben Nagy

Upload: shakacon

Post on 09-Jun-2015

1.731 views

Category:

Technology


2 download

DESCRIPTION

This talk will cover some approaches for kernel instrumentation, including the use of my scriptable dbgeng wrapper (rBuggery) to do local kernel debugging - previously only possible with windbg or cdb/kd themselves. rBuggery is handy for scripting up extraction of kernel structs, Object Directory traversal and such, WITHOUT slow and irritating serial connections. It can also be used to hack up a pretty awful kernel tracer, but one that at least works better than windbg or anything else I found. Next up I'll jump off from Alex Ionescu's SyScan talk this year where he talked about ALPC and some bugs he found. We'll go through how to build a delivery system to expose the ALPC Services to at least basic attack from a client fuzzer. Because there's a lot of internals and boilerplate code required and nobody else uses Go / Ruby, I'll probably also release a simple-to-use tool that exposes the services via REST and lets you deliver tests using any language. This talk will include a great deal of Tutelary Fail.

TRANSCRIPT

Page 1: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Windows Kernel Fuzzingfor

Intermediate Learners

Ben Nagy

Page 2: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

PSA WARNINGS

- ALLERGY: Some Recycled Material

- SPOILER: Not Really About Kernel Fuzzing

- TRIGGER: Neckbeards

Page 3: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

About Me:

- Not oldsk00l. Just old.

- ~ 11 weeks kernel experience

- ~ 8 years fuzzing experience

- ~ 25 years nerding experience

- Hate all Technology

- Certified Windows Internals Expert!

Disclaimer:I am aware of the prevailing opinion that fuzzing talks

without bugs suck, by definition. I do not have any bugs. Even

if I did have bugs, I wouldn’t tell you. There are no bugs.

Page 4: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 5: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALL LIES!

Not fuzzing ALPC - Fuzzing with ALPC

Page 6: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALL LIES!

Not kernel fuzzing - new attack surface for userland

Page 7: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALL LIES!

… but we need to understand the kernel first

Page 8: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Fuzzing Made Simple

• Select a Good Target

• Acquire Essential Knowledge

• Apply Fuzzing Canon

– How do we Deliver

– How do we Instrument

– How do we Generate

– How does that Scale

Page 9: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Phase I - Target Selection

Target: ALPC

Page 10: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Why ALPC?

• New

• Tricky

• Undocumented

• Everywhere

Page 11: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

What Bug Classes?

• Privesc to SYSTEM(+) from anywhere

• Memory Helpers

– Fill memory

– Disclose?

• DoS

• “Jackpot” bug?

Page 12: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC What Do?

• Interprocess Communication

• New in Vista+

• Low Level

• Sync / Async, Fast, Awesome

http://www.syscan.org/index.php/download/get/d596c7dc486175148fc038387dc80be2/SyScan2014_AlexIonescu_AllabouttheRPCLRPCALPCa

ndLPCinyourPC.zip

Page 13: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC What Do?

• Shared Memory Views

• IO Completion Ports

• Lots of security, enforced by the kernel

• TOCTOU Safe

http://www.syscan.org/index.php/download/get/d596c7dc486175148fc038387dc80be2/SyScan2014_AlexIonescu_AllabouttheRPCLRPCALPCa

ndLPCinyourPC.zip

Page 14: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC What Do?

• RPC / RPC-DCOM run on it

• Can also be used directly

• Imagine it like a network

http://www.syscan.org/index.php/download/get/d596c7dc486175148fc038387dc80be2/SyScan2014_AlexIonescu_AllabouttheRPCLRPCALPCa

ndLPCinyourPC.zip

Page 15: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Userland

service.exefoo.exe

RPC

Page 16: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

© Sven Micklish

Not how it works, yo

Page 17: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

© Sven Micklish

(ohai I’m Barry)

Page 18: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Kernel Recap

Page 19: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Userland

ntdll

kernel32 Public

Internal

“NT Executive”

IO USER GDI

Drivers

Drivers

Drivers

Hardware

More Complicated Stuff

Boring / Complicated

Page 20: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

“NT Executive”

Userland

ntdll

kernel32

IO USER GDI

Drivers

Drivers

Drivers

Hardware

More Complicated Stuff

Boring / Complicated

1. Setup syscall args

2. syscall number in eax

3. int2e / sysenter / syscall

( “context switch” )

4. Lookup syscall in SSDT

5. Dispatch to correct

component

Page 21: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

© Sven Micklish

Kernel has Objects.

There are many kinds.

Page 22: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

© Sven Micklish

They go in Directories

Object Manager manages them

(duh.)

Page 23: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

… where were we?

Page 24: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Userland

service.exefoo.exe

RPC

Page 25: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Userland

foo.exe

ALPC

Object Manager

ALPC Port ALPC Port

Port Handle

service.exe

Port Handle

First, establish an ALPC

connection…

Page 26: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Userland

foo.exe

ALPC

Object Manager

ALPC Port ALPC Port

Port Handle

service.exe

Port Handle

RPC Bind

RPC Accept

Page 27: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Userland

foo.exe

ALPC

Object Manager

ALPC Port ALPC Port

Port Handle

service.exe

Port Handle

RPC

Page 28: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Phase II - Acquire Knowledge

ALPC Surface

Page 29: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC Attack Surface

• Who talks to whom?

• Which processes have open ports?

Page 30: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 31: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

lrn2kd, n00b!

Page 32: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 33: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 34: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 35: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 36: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 37: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

lrn2code?

Page 38: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

• https://github.com/bnagy/rBuggery• Ruby wrapper for dbgeng.dll ( windbg )• Fully scriptable debugger

– kernel debugging– LOCAL kernel debugging

• Unique Features:–Actually works

Cutting Edge Tech

Page 39: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Know what the Windows Kernel needs?

A JSON API!

–Wrap rBuggery with Sinatra

–Connect with Go

–Map ALPC

–Drink Barry’s salty ragetears

Page 40: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

alpcmap

• Start debugger bridge on Windows

• Connect from anywhere

• Maps ports, serves webapp graph

• https://github.com/bnagy/alpcmap

wat?stahp!

Page 41: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

alpcmap

• Automates and parses:– !alpc /lp, /lpc, /p

–dt nt_OBJECT_HEADER

– !token

– !sd

– !object

– !process

–…

Page 42: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Initiating demonstration…

Page 43: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Phase III - Generation

What to send?

Page 44: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Phase III - Generation

Examine existing messages!

Page 45: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC Message Logging

• Event Tracing for Windows (ETW)?

• advapi32 has StartTrace() …

• EVENT_TRACE_FLAG_ALPC …

• SystemTraceControlGuid …

• CODEZ!

Page 46: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC Message Logging

• Hacked StartTrace() support into w32

–Needs lots of support cruft

Page 47: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ETW

Page 48: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

FAIL

Page 49: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

lrn2google

Page 50: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

DOUBLE

FAIL… The message contents aren’t even in the ETW output

only the Message IDs

Page 51: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Undocumented !alpc switch /lm !

Set “AlpcMessageLog” in

HKLM\CCS\Control\Session !

Use this sweet trick to add private ALPC_MESSAGE_LOG symbol…

Page 52: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

symbol.c

That’s a private symbol!

Page 53: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

cl.exe /Zi /Gz /c /Fdntkrnlmp

/IC:\WinDDK\7600.16385.1\inc\ddk

/IC:\WinDDK\7600.16385.1\inc\crt

/D_X86_=1 symbols.c

Pass in the existing .pdb

It will be modified in-place

(so save a copy)

// FIXME

Page 54: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

FAIL

Page 55: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Oh, BTW, /lm only works in Vista…

(except debug builds)

Page 56: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC Message Logging

FINE! Let’s use rBuggery then.

ntdll!ZwAlpcSendWaitReceivePort:

4c8bd1 mov r10,rcx

b882000000 mov eax,82h

0f05 syscall

c3 ret

Message contents added

and removed here ;-)

Page 57: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC Message Logging

x64 fastcall uses registers for first 4 args, but

space is still reserved for them on the stack…

Page 58: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Breakpoint Callback

Page 59: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ᕕ( ಠ‿ಠ)ᕗ

Page 60: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Sappy Moralizing Interlude

• Learned cool stuff while failing

• Presenting failure helps everyone

Page 61: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

DEMO?

Page 62: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 63: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Phase IV - Delivery

ALPC Programming

Page 64: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC
Page 65: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Programming with ALPC

• Very little documentation!–New Edition of Windows Internals

–Some LPC stuff on j00ru’s blog

–Alex Ionescu’s trainings

–ntlpcapi.h

–This project ( didn’t test )– https://github.com/avalon1610/ALPC/tree/master/ALPC

Page 66: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Why use Go?

• Compiled.

–Windows users can ship binaries

• Idiomatic Windows binding ( w32 )

• cgo - use headers directly in a pinch

• Raging code hipster

Page 67: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

foo.exe service.exe

NtAlpcConnectPort()

NtAlpcAcceptConnectPort()

• Server can refuse

• Connection message optional

Connection

Port

Page 68: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

foo.exe service.exe

Connection

Port

Communication

Port

Communication

PortNtAlpcSendWaitReceivePort()

Servers only wait on one port!

Page 69: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Your flippant manner

wearies me. Display your

pathetic code immediately.

Page 70: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Connection - Client

Page 71: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Acceptance - Server

Page 72: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Receive Loop - Client

Note same buffer for send / recv…

Page 73: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Your puerile code lacks all ability to

dispatch to multiple clients.

Clarify the mechanism

whereby this is achieved.

Page 74: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

© Sven Micklish

Context Attributes!

Page 75: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Message Attributes

• Context - opaque struct

• Security

• Data View - share memory

• Handle - share handles

Secured “in transit” by the kernel

Page 76: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Capture

Page 77: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Expose and Cast

Page 78: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

FAILS?

Page 79: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

ALPC Programming Tips

• ntstatus.h - learn it, live it, love it

• Zero out reused buffers / headers

• Initialize struct Length fields

• Double check your flags

–ALPC_PORFLG_*

–ALPC_MSGFLG_*

Page 80: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Code - Go

• https://github.com/bnagy/w32

• https://github.com/bnagy/alpcgo

– High level API

– alpcechocli / alpcechosrv

– alpcbridge ( jsonrpc API )

Whoa! I can connect with 5 lines of python!

Rust or Haskell would clearly

have been a more felicitous choice.

Page 81: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

WHAN RELEASE FUZZER??

Page 82: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

My TODOs

• Add Attribute support to Send()– Rating: EASY (NOW…)

• Add LRPC Parsing?– Rating: HARD

• Add MitM Fuzzing Proxy– Rating: NOT FOR RELEASE

Page 83: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Your TODOs

• Here’s the whole JSONRPC API:

–Connect()

–Send()

–Close()

• Add radamsa and 15 lines of python– Rating: TRIVIAL

Page 84: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Instrumentation

• Userland Issues–“Normal” Exception instrumentation

–RADAR – http://technet.microsoft.com/en-us/library/dd393057(WS.10).aspx

–ProcDump– http://technet.microsoft.com/en-us/sysinternals/dd996900.aspx

Page 85: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Instrumentation

• BSOD Logging

–Dump to disk

–Check for dumps at startup

–Dispatch to a triage server

Page 86: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

My work here is done

Thanks:– Alex Ionescu

– @miaubiz

Contact:– [email protected]

– @rantyben

– github.com/bnagy

ilu, bai

Page 87: Windows Kernel Fuzzing for Intermediate Learners - Ben Nagy, COSEINC

Questions?