SlideShare a Scribd company logo
1 of 23
Download to read offline
Hunting and exploiting bugs
      in kernel drivers

       Andras Kabai

       DefCamp 2012
Who am I
Andras Kabai
   OSCP, OSCE, OSEE, GPEN, GWAPT, GREM, GXPN, CEH
   Manager of Deloitte Hungary's Security & Privacy group

Previously
   Senior IT Security Specialist / R&D Manager
   CERT-Hungary, National CyberSecurity Center

10+ years in IT Security
   Penetration testing, reverse engineering, malware analysis,
   vulnerability research, exploitation, incident handling

Member of the Hungarian “gula.sh” team
  (bronze medal @CyberLympics 2012 World Finals)

Email: contact (_at_) kabaiandras.hu
Introduction
●Exploiting kernel drivers has become increasingly
popular

● Exploiting a vulnerability in kernel space may give us
the possibility to take control of the entire system

●   Thousands of possible target
     (drivers shipped with OS, 3rd party drivers)

●   General goal: privilege escalation

●   Focus on Win32 kernel drivers
Ring3 vs Ring0
●Different access levels to resources, usually enforced by the
hardware

●   User mode (Ring3)
    ● No access to hardware, user mode programs has to call the

      system to interact with the hardware
    ● Restricted environment, separated process memory

    ● Memory: 0x00000000 to 0x7FFFFFFF

    ● Hard to crash the system



●   Kernel mode (Ring0)
    ● Full access to hardware

    ● Unrestricted access to... everything

      ● Kernel code, kernel structures, memory, processes,

        hardware
    ● Memory: 0x80000000 to 0xFFFFFFFF

    ● Easy to crash the system
Windows Driver Model (WDM)
                Drivers
●   Framework for “Device drivers”

●   Introduced with Win98 and Win2000 to replace VxD

●   Windows Driver Kit (WDK)
    ● Tools

    ● Documentation

    ● Samples



●   Communicates with I/O Request Packet (IRP)
Communication basics
● Driver creates a DRIVER_OBJECT with
IoCreateDevice()

●DRIVER_OBJECT holds “Major function” pointers to
driver functions
 ● IRP_MJ_DEVICE_CONTROL points to a function

   that is responsible for IOCTL calls

● Driver will be available for user space programs
through its symbolic link (e.g. DeviceDEVICENAME)

●User space program can open the symbolic link to
send I/O requests for the driver
I/O Control (IOCTL) code
●   The code itself describes:
    ● DeviceType

    ● FunctionCode

    ● TransferType

    ● RequiredAccess



● IOCTL code is a 32 bit value that contains several
fields:
(msdn.microsoft.com)
I/O transfer types
●   Buffered I/O
    ●   The operating system creates a nonpaged system buffer, equal in
        size to the application's buffer. For write operations, the I/O
        manager copies user data into the system buffer before calling the
        driver stack. For read operations, the I/O manager copies data from
        the system buffer into the application's buffer after the driver stack
        completes the requested operation. (MSDN)
●   Direct I/O
    ●   The operating system locks the application's buffer in memory. It
        then creates a memory descriptor list (MDL) that identifies the
        locked memory pages, and passes the MDL to the driver stack.
        Drivers access the locked pages through the MDL. (MSDN)
●   Neither Buffered Nor Direct I/O
    ●   The operating system passes the application buffer's virtual starting
        address and size to the driver stack. The buffer is only accessible
        from drivers that execute in the application's thread context. (MSDN)
Communication between user mode
  program and kernel driver - 1

●CreateFile() to initialize access to device through its
symbolic link

●   Communication with
    ● DeviceIoControl() // IOCTL call
    ● WriteFile()       // pass “stream” data
    ● ReadFile()        // receive “stream” data
Communication between user mode
  program and kernel driver - 2
●   I/O Request through kernel32.DeviceIoControl

 BOOL WINAPI DeviceIoControl(
   _In_        HANDLE hDevice,
   _In_        DWORD dwIoControlCode,
   _In_opt_    LPVOID lpInBuffer,
   _In_        DWORD nInBufferSize,
   _Out_opt_   LPVOID lpOutBuffer,
   _In_        DWORD nOutBufferSize,
   _Out_opt_   LPDWORD lpBytesReturned,
   _Inout_opt_ LPOVERLAPPED lpOverlapped
);
Identify your target


●   WinObj, DriverView
    ● Gather useful information of loaded drivers



●   IrpTracker
    ● Monitor I/O requests

    ● Detailed view on IRP
Analysis, bug hunting
●   Read and understand the source code – if possible

●   Reverse engineering
    ● IDA



●   Fuzzing
    ● ioctlbf

    ● ioctlfuzzer

    ● Kartoffel

    ● Your own scripts!



●   Kernel debugging
    ● Usually set the first breakpoint on function referenced

      by IRP_MJ_DEVICE_CONTROL
Why we love METHOD_NEITHER?


●   No I/O Manager interaction on our buffer
    ● Buffer pointers are given directly to driver

    ● Try to point to kernel space... you may be able to

      overwrite code, return address, jump tables, function
      pointers, kernel structures with a simple IOCTL call
Write-What-Where
●Possibilities are not limited to METHOD_NEITHER
output pointer manipulation

●   Sometimes the WHAT is limited
    ● In size

         (e.g. mov [eax], ecx)
    ● In value

         (e.g. mov [eax], 1)

● Sometimes the value on the pointed memory
increased or decreased with a constant number only
   (e.g. dec [eax])

●   You still have a lot of opportunity to exploit these cases
Interesting targets to
                Write-What-Where
●   Function pointers

●   Jump tables

●   System Service Dispatch table

●   Interrupt Descriptor Table

●   Global Descriptor Table

●   Etc.
Code execution

●   Our payload will run in kernel mode
    ● User mode shellcodes will not work

    ● It is possible to create fully kernel mode shellcode,

      but it is not so comfortable

●   Build staged shellcodes/payloads
    ● Elevate the attacker privileges in kernel mode

    ● Return to the elevated user space process

    ● Run user mode shellcode
Privilege escalation
             with token stealing - 1
●   Find ETHREAD/KTHREAD (FS:[124h])

●   ETHREAD → EPROCESS

●   EPROCESS
    ● UniqueProcessId (is it system process?)

    ● ActiveProcessLinks (for the next EPROCESS

      structure)
    ● Token (security descriptor of a process)



● Replace our EPROCESS Token pointer with the
identified system process token pointer
Privilege escalation
            with token stealing - 2
nt!_KTHREAD
  +0x000 Header: _DISPATCHER_HEADER
  ...
  +0x040 ApcState: _KAPC_STATE
     +0x000 ApcListHead: [2] _LIST_ENTRY
     +0x010 Process: Ptr32 _KPROCESS
  ...
 nt!_EPROCESS
   +0x000 Pcb: _KPROCESS
   ...
   +0x0b4 UniqueProcessId: Ptr32 Void
   +0x0b8 ActiveProcessLinks: _LIST_ENTRY
      +0x000 Flink: Ptr32 _LIST_ENTRY
      +0x004 Blink: Ptr32 _LIST_ENTRY
   ...
   +0x0f8 Token: _EX_FAST_REF
      +0x000 Object: Ptr32 Void
      +0x000 RefCnt: Pos 0, 3 Bits
      +0x000 Value: Uint4B
Kernel debugging environment


●   Two machines (debugger, debuggee)
    ● Physical machine to physical machine

    ● Virtualization

     ● Physical machine to virtual machine

     ● Virtual machine to virtual machine

     ● Virtual serial port
Enable kernel debugging on target


●   No more boot.ini in modern Windows systems

●   Boot Configuration Data Editor: bcdedit
    ●   bcdedit /dbgsettings serial debugport:1 baudrate:115200
    ●   bcdedit /debug on
DEMO
Conclusion
● It was just an introduction, this is just tip of the
iceberg...

●   It is not magic

● There are tons of bugs in kernel drivers; you should
focus on them

●   You can do anything in kernel space
(also, easily crash the machine)

●Try to search and exploit driver vulnerabilities for fun...
or for profit :)
Thank you for your attention!


            Andras Kabai
    contact (_at_) kabaiandras.hu




    gr33t1ngz 4 @hekkcamp p4rt1c1p4nts!

More Related Content

What's hot

Ищем уязвимости нулевого дня в ядре Linux
Ищем уязвимости нулевого дня в ядре LinuxИщем уязвимости нулевого дня в ядре Linux
Ищем уязвимости нулевого дня в ядре LinuxPositive Hack Days
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_BootingRashila Rr
 
Linux Porting
Linux PortingLinux Porting
Linux PortingChamp Yen
 
Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxRajKumar Rampelli
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackKernel TLV
 
Linux Shellcode disassembling
Linux Shellcode disassemblingLinux Shellcode disassembling
Linux Shellcode disassemblingHarsh Daftary
 
I/O, You Own: Regaining Control of Your Disk in the Presence of Bootkits
I/O, You Own: Regaining Control of Your Disk in the Presence of BootkitsI/O, You Own: Regaining Control of Your Disk in the Presence of Bootkits
I/O, You Own: Regaining Control of Your Disk in the Presence of BootkitsCrowdStrike
 
Unix Programming with Perl
Unix Programming with PerlUnix Programming with Perl
Unix Programming with PerlKazuho Oku
 
iOS Automation Primitives
iOS Automation PrimitivesiOS Automation Primitives
iOS Automation PrimitivesSynack
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to ProveKazuho Oku
 
Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)yang firo
 
syzbot and the tale of million kernel bugs
syzbot and the tale of million kernel bugssyzbot and the tale of million kernel bugs
syzbot and the tale of million kernel bugsDmitry Vyukov
 
Compromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging MechanismsCompromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging MechanismsRussell Sanford
 
Linux Device Driver Training-TutorialsDaddy
Linux Device Driver Training-TutorialsDaddyLinux Device Driver Training-TutorialsDaddy
Linux Device Driver Training-TutorialsDaddyStryker King
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot) Omkar Rane
 
Embedding Linux On The Encore Simputer
Embedding Linux On The Encore SimputerEmbedding Linux On The Encore Simputer
Embedding Linux On The Encore SimputerSatpal Parmar
 

What's hot (20)

Ищем уязвимости нулевого дня в ядре Linux
Ищем уязвимости нулевого дня в ядре LinuxИщем уязвимости нулевого дня в ядре Linux
Ищем уязвимости нулевого дня в ядре Linux
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_Booting
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linux
 
Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network Stack
 
Mach-O Internals
Mach-O InternalsMach-O Internals
Mach-O Internals
 
Linux Shellcode disassembling
Linux Shellcode disassemblingLinux Shellcode disassembling
Linux Shellcode disassembling
 
Android platform
Android platform Android platform
Android platform
 
I/O, You Own: Regaining Control of Your Disk in the Presence of Bootkits
I/O, You Own: Regaining Control of Your Disk in the Presence of BootkitsI/O, You Own: Regaining Control of Your Disk in the Presence of Bootkits
I/O, You Own: Regaining Control of Your Disk in the Presence of Bootkits
 
Unix Programming with Perl
Unix Programming with PerlUnix Programming with Perl
Unix Programming with Perl
 
iOS Automation Primitives
iOS Automation PrimitivesiOS Automation Primitives
iOS Automation Primitives
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to Prove
 
Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)Linux kernel debugging(ODP format)
Linux kernel debugging(ODP format)
 
syzbot and the tale of million kernel bugs
syzbot and the tale of million kernel bugssyzbot and the tale of million kernel bugs
syzbot and the tale of million kernel bugs
 
Compromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging MechanismsCompromising Linux Virtual Machines with Debugging Mechanisms
Compromising Linux Virtual Machines with Debugging Mechanisms
 
Linux Device Driver Training-TutorialsDaddy
Linux Device Driver Training-TutorialsDaddyLinux Device Driver Training-TutorialsDaddy
Linux Device Driver Training-TutorialsDaddy
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot)
 
Embedding Linux On The Encore Simputer
Embedding Linux On The Encore SimputerEmbedding Linux On The Encore Simputer
Embedding Linux On The Encore Simputer
 

Similar to Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012

[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹GangSeok Lee
 
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...44CON
 
Windows内核技术介绍
Windows内核技术介绍Windows内核技术介绍
Windows内核技术介绍jeffz
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Opersys inc.
 
Why kernelspace sucks?
Why kernelspace sucks?Why kernelspace sucks?
Why kernelspace sucks?OpenFest team
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux DevelopersOpersys inc.
 
openioc_scan - IOC scanner for memory forensics
openioc_scan - IOC scanner for memory forensicsopenioc_scan - IOC scanner for memory forensics
openioc_scan - IOC scanner for memory forensicsTakahiro Haruyama
 
Exploiting Llinux Environment
Exploiting Llinux EnvironmentExploiting Llinux Environment
Exploiting Llinux EnvironmentEnrico Scapin
 
cinema_time_new.pdf
cinema_time_new.pdfcinema_time_new.pdf
cinema_time_new.pdfMaxDmitriev
 
Finding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyPriyanka Aash
 
Windows internals Essentials
Windows internals EssentialsWindows internals Essentials
Windows internals EssentialsJohn Ombagi
 
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법GangSeok Lee
 
Csw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physicalCsw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physicalCanSecWest
 
Windows internals
Windows internalsWindows internals
Windows internalsPiyush Jain
 
02 direct3 d_pipeline
02 direct3 d_pipeline02 direct3 d_pipeline
02 direct3 d_pipelineGirish Ghate
 

Similar to Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012 (20)

[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
[2007 CodeEngn Conference 01] dual5651 - Windows 커널단의 후킹
 
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
Reverse Engineering and Bug Hunting on KMDF Drivers - Enrique Nissim - 44CON ...
 
Windows内核技术介绍
Windows内核技术介绍Windows内核技术介绍
Windows内核技术介绍
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013
 
Windows Offender_ Reverse Engineering Windows Defender's Antivirus Emulator
Windows Offender_ Reverse Engineering Windows Defender's Antivirus EmulatorWindows Offender_ Reverse Engineering Windows Defender's Antivirus Emulator
Windows Offender_ Reverse Engineering Windows Defender's Antivirus Emulator
 
Why kernelspace sucks?
Why kernelspace sucks?Why kernelspace sucks?
Why kernelspace sucks?
 
Android Internals
Android InternalsAndroid Internals
Android Internals
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
 
openioc_scan - IOC scanner for memory forensics
openioc_scan - IOC scanner for memory forensicsopenioc_scan - IOC scanner for memory forensics
openioc_scan - IOC scanner for memory forensics
 
Exploiting Llinux Environment
Exploiting Llinux EnvironmentExploiting Llinux Environment
Exploiting Llinux Environment
 
cinema_time_new.pdf
cinema_time_new.pdfcinema_time_new.pdf
cinema_time_new.pdf
 
Finding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated Disassembly
 
Windows internals Essentials
Windows internals EssentialsWindows internals Essentials
Windows internals Essentials
 
Hardware hacking
Hardware hackingHardware hacking
Hardware hacking
 
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
 
Csw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physicalCsw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physical
 
Windows internals
Windows internalsWindows internals
Windows internals
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
02 direct3 d_pipeline
02 direct3 d_pipeline02 direct3 d_pipeline
02 direct3 d_pipeline
 
Vxcon 2016
Vxcon 2016Vxcon 2016
Vxcon 2016
 

More from DefCamp

Remote Yacht Hacking
Remote Yacht HackingRemote Yacht Hacking
Remote Yacht HackingDefCamp
 
Mobile, IoT, Clouds… It’s time to hire your own risk manager!
Mobile, IoT, Clouds… It’s time to hire your own risk manager!Mobile, IoT, Clouds… It’s time to hire your own risk manager!
Mobile, IoT, Clouds… It’s time to hire your own risk manager!DefCamp
 
The Charter of Trust
The Charter of TrustThe Charter of Trust
The Charter of TrustDefCamp
 
Internet Balkanization: Why Are We Raising Borders Online?
Internet Balkanization: Why Are We Raising Borders Online?Internet Balkanization: Why Are We Raising Borders Online?
Internet Balkanization: Why Are We Raising Borders Online?DefCamp
 
Bridging the gap between CyberSecurity R&D and UX
Bridging the gap between CyberSecurity R&D and UXBridging the gap between CyberSecurity R&D and UX
Bridging the gap between CyberSecurity R&D and UXDefCamp
 
Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...DefCamp
 
Drupalgeddon 2 – Yet Another Weapon for the Attacker
Drupalgeddon 2 – Yet Another Weapon for the AttackerDrupalgeddon 2 – Yet Another Weapon for the Attacker
Drupalgeddon 2 – Yet Another Weapon for the AttackerDefCamp
 
Economical Denial of Sustainability in the Cloud (EDOS)
Economical Denial of Sustainability in the Cloud (EDOS)Economical Denial of Sustainability in the Cloud (EDOS)
Economical Denial of Sustainability in the Cloud (EDOS)DefCamp
 
Trust, but verify – Bypassing MFA
Trust, but verify – Bypassing MFATrust, but verify – Bypassing MFA
Trust, but verify – Bypassing MFADefCamp
 
Threat Hunting: From Platitudes to Practical Application
Threat Hunting: From Platitudes to Practical ApplicationThreat Hunting: From Platitudes to Practical Application
Threat Hunting: From Platitudes to Practical ApplicationDefCamp
 
Building application security with 0 money down
Building application security with 0 money downBuilding application security with 0 money down
Building application security with 0 money downDefCamp
 
Implementation of information security techniques on modern android based Kio...
Implementation of information security techniques on modern android based Kio...Implementation of information security techniques on modern android based Kio...
Implementation of information security techniques on modern android based Kio...DefCamp
 
Lattice based Merkle for post-quantum epoch
Lattice based Merkle for post-quantum epochLattice based Merkle for post-quantum epoch
Lattice based Merkle for post-quantum epochDefCamp
 
The challenge of building a secure and safe digital environment in healthcare
The challenge of building a secure and safe digital environment in healthcareThe challenge of building a secure and safe digital environment in healthcare
The challenge of building a secure and safe digital environment in healthcareDefCamp
 
Timing attacks against web applications: Are they still practical?
Timing attacks against web applications: Are they still practical?Timing attacks against web applications: Are they still practical?
Timing attacks against web applications: Are they still practical?DefCamp
 
Tor .onions: The Good, The Rotten and The Misconfigured
Tor .onions: The Good, The Rotten and The Misconfigured Tor .onions: The Good, The Rotten and The Misconfigured
Tor .onions: The Good, The Rotten and The Misconfigured DefCamp
 
Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...
Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...
Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...DefCamp
 
We will charge you. How to [b]reach vendor’s network using EV charging station.
We will charge you. How to [b]reach vendor’s network using EV charging station.We will charge you. How to [b]reach vendor’s network using EV charging station.
We will charge you. How to [b]reach vendor’s network using EV charging station.DefCamp
 
Connect & Inspire Cyber Security
Connect & Inspire Cyber SecurityConnect & Inspire Cyber Security
Connect & Inspire Cyber SecurityDefCamp
 
The lions and the watering hole
The lions and the watering holeThe lions and the watering hole
The lions and the watering holeDefCamp
 

More from DefCamp (20)

Remote Yacht Hacking
Remote Yacht HackingRemote Yacht Hacking
Remote Yacht Hacking
 
Mobile, IoT, Clouds… It’s time to hire your own risk manager!
Mobile, IoT, Clouds… It’s time to hire your own risk manager!Mobile, IoT, Clouds… It’s time to hire your own risk manager!
Mobile, IoT, Clouds… It’s time to hire your own risk manager!
 
The Charter of Trust
The Charter of TrustThe Charter of Trust
The Charter of Trust
 
Internet Balkanization: Why Are We Raising Borders Online?
Internet Balkanization: Why Are We Raising Borders Online?Internet Balkanization: Why Are We Raising Borders Online?
Internet Balkanization: Why Are We Raising Borders Online?
 
Bridging the gap between CyberSecurity R&D and UX
Bridging the gap between CyberSecurity R&D and UXBridging the gap between CyberSecurity R&D and UX
Bridging the gap between CyberSecurity R&D and UX
 
Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...
 
Drupalgeddon 2 – Yet Another Weapon for the Attacker
Drupalgeddon 2 – Yet Another Weapon for the AttackerDrupalgeddon 2 – Yet Another Weapon for the Attacker
Drupalgeddon 2 – Yet Another Weapon for the Attacker
 
Economical Denial of Sustainability in the Cloud (EDOS)
Economical Denial of Sustainability in the Cloud (EDOS)Economical Denial of Sustainability in the Cloud (EDOS)
Economical Denial of Sustainability in the Cloud (EDOS)
 
Trust, but verify – Bypassing MFA
Trust, but verify – Bypassing MFATrust, but verify – Bypassing MFA
Trust, but verify – Bypassing MFA
 
Threat Hunting: From Platitudes to Practical Application
Threat Hunting: From Platitudes to Practical ApplicationThreat Hunting: From Platitudes to Practical Application
Threat Hunting: From Platitudes to Practical Application
 
Building application security with 0 money down
Building application security with 0 money downBuilding application security with 0 money down
Building application security with 0 money down
 
Implementation of information security techniques on modern android based Kio...
Implementation of information security techniques on modern android based Kio...Implementation of information security techniques on modern android based Kio...
Implementation of information security techniques on modern android based Kio...
 
Lattice based Merkle for post-quantum epoch
Lattice based Merkle for post-quantum epochLattice based Merkle for post-quantum epoch
Lattice based Merkle for post-quantum epoch
 
The challenge of building a secure and safe digital environment in healthcare
The challenge of building a secure and safe digital environment in healthcareThe challenge of building a secure and safe digital environment in healthcare
The challenge of building a secure and safe digital environment in healthcare
 
Timing attacks against web applications: Are they still practical?
Timing attacks against web applications: Are they still practical?Timing attacks against web applications: Are they still practical?
Timing attacks against web applications: Are they still practical?
 
Tor .onions: The Good, The Rotten and The Misconfigured
Tor .onions: The Good, The Rotten and The Misconfigured Tor .onions: The Good, The Rotten and The Misconfigured
Tor .onions: The Good, The Rotten and The Misconfigured
 
Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...
Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...
Needles, Haystacks and Algorithms: Using Machine Learning to detect complex t...
 
We will charge you. How to [b]reach vendor’s network using EV charging station.
We will charge you. How to [b]reach vendor’s network using EV charging station.We will charge you. How to [b]reach vendor’s network using EV charging station.
We will charge you. How to [b]reach vendor’s network using EV charging station.
 
Connect & Inspire Cyber Security
Connect & Inspire Cyber SecurityConnect & Inspire Cyber Security
Connect & Inspire Cyber Security
 
The lions and the watering hole
The lions and the watering holeThe lions and the watering hole
The lions and the watering hole
 

Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012

  • 1. Hunting and exploiting bugs in kernel drivers Andras Kabai DefCamp 2012
  • 2. Who am I Andras Kabai OSCP, OSCE, OSEE, GPEN, GWAPT, GREM, GXPN, CEH Manager of Deloitte Hungary's Security & Privacy group Previously Senior IT Security Specialist / R&D Manager CERT-Hungary, National CyberSecurity Center 10+ years in IT Security Penetration testing, reverse engineering, malware analysis, vulnerability research, exploitation, incident handling Member of the Hungarian “gula.sh” team (bronze medal @CyberLympics 2012 World Finals) Email: contact (_at_) kabaiandras.hu
  • 3. Introduction ●Exploiting kernel drivers has become increasingly popular ● Exploiting a vulnerability in kernel space may give us the possibility to take control of the entire system ● Thousands of possible target (drivers shipped with OS, 3rd party drivers) ● General goal: privilege escalation ● Focus on Win32 kernel drivers
  • 4. Ring3 vs Ring0 ●Different access levels to resources, usually enforced by the hardware ● User mode (Ring3) ● No access to hardware, user mode programs has to call the system to interact with the hardware ● Restricted environment, separated process memory ● Memory: 0x00000000 to 0x7FFFFFFF ● Hard to crash the system ● Kernel mode (Ring0) ● Full access to hardware ● Unrestricted access to... everything ● Kernel code, kernel structures, memory, processes, hardware ● Memory: 0x80000000 to 0xFFFFFFFF ● Easy to crash the system
  • 5. Windows Driver Model (WDM) Drivers ● Framework for “Device drivers” ● Introduced with Win98 and Win2000 to replace VxD ● Windows Driver Kit (WDK) ● Tools ● Documentation ● Samples ● Communicates with I/O Request Packet (IRP)
  • 6. Communication basics ● Driver creates a DRIVER_OBJECT with IoCreateDevice() ●DRIVER_OBJECT holds “Major function” pointers to driver functions ● IRP_MJ_DEVICE_CONTROL points to a function that is responsible for IOCTL calls ● Driver will be available for user space programs through its symbolic link (e.g. DeviceDEVICENAME) ●User space program can open the symbolic link to send I/O requests for the driver
  • 7. I/O Control (IOCTL) code ● The code itself describes: ● DeviceType ● FunctionCode ● TransferType ● RequiredAccess ● IOCTL code is a 32 bit value that contains several fields: (msdn.microsoft.com)
  • 8. I/O transfer types ● Buffered I/O ● The operating system creates a nonpaged system buffer, equal in size to the application's buffer. For write operations, the I/O manager copies user data into the system buffer before calling the driver stack. For read operations, the I/O manager copies data from the system buffer into the application's buffer after the driver stack completes the requested operation. (MSDN) ● Direct I/O ● The operating system locks the application's buffer in memory. It then creates a memory descriptor list (MDL) that identifies the locked memory pages, and passes the MDL to the driver stack. Drivers access the locked pages through the MDL. (MSDN) ● Neither Buffered Nor Direct I/O ● The operating system passes the application buffer's virtual starting address and size to the driver stack. The buffer is only accessible from drivers that execute in the application's thread context. (MSDN)
  • 9. Communication between user mode program and kernel driver - 1 ●CreateFile() to initialize access to device through its symbolic link ● Communication with ● DeviceIoControl() // IOCTL call ● WriteFile() // pass “stream” data ● ReadFile() // receive “stream” data
  • 10. Communication between user mode program and kernel driver - 2 ● I/O Request through kernel32.DeviceIoControl BOOL WINAPI DeviceIoControl( _In_ HANDLE hDevice, _In_ DWORD dwIoControlCode, _In_opt_ LPVOID lpInBuffer, _In_ DWORD nInBufferSize, _Out_opt_ LPVOID lpOutBuffer, _In_ DWORD nOutBufferSize, _Out_opt_ LPDWORD lpBytesReturned, _Inout_opt_ LPOVERLAPPED lpOverlapped );
  • 11. Identify your target ● WinObj, DriverView ● Gather useful information of loaded drivers ● IrpTracker ● Monitor I/O requests ● Detailed view on IRP
  • 12. Analysis, bug hunting ● Read and understand the source code – if possible ● Reverse engineering ● IDA ● Fuzzing ● ioctlbf ● ioctlfuzzer ● Kartoffel ● Your own scripts! ● Kernel debugging ● Usually set the first breakpoint on function referenced by IRP_MJ_DEVICE_CONTROL
  • 13. Why we love METHOD_NEITHER? ● No I/O Manager interaction on our buffer ● Buffer pointers are given directly to driver ● Try to point to kernel space... you may be able to overwrite code, return address, jump tables, function pointers, kernel structures with a simple IOCTL call
  • 14. Write-What-Where ●Possibilities are not limited to METHOD_NEITHER output pointer manipulation ● Sometimes the WHAT is limited ● In size (e.g. mov [eax], ecx) ● In value (e.g. mov [eax], 1) ● Sometimes the value on the pointed memory increased or decreased with a constant number only (e.g. dec [eax]) ● You still have a lot of opportunity to exploit these cases
  • 15. Interesting targets to Write-What-Where ● Function pointers ● Jump tables ● System Service Dispatch table ● Interrupt Descriptor Table ● Global Descriptor Table ● Etc.
  • 16. Code execution ● Our payload will run in kernel mode ● User mode shellcodes will not work ● It is possible to create fully kernel mode shellcode, but it is not so comfortable ● Build staged shellcodes/payloads ● Elevate the attacker privileges in kernel mode ● Return to the elevated user space process ● Run user mode shellcode
  • 17. Privilege escalation with token stealing - 1 ● Find ETHREAD/KTHREAD (FS:[124h]) ● ETHREAD → EPROCESS ● EPROCESS ● UniqueProcessId (is it system process?) ● ActiveProcessLinks (for the next EPROCESS structure) ● Token (security descriptor of a process) ● Replace our EPROCESS Token pointer with the identified system process token pointer
  • 18. Privilege escalation with token stealing - 2 nt!_KTHREAD +0x000 Header: _DISPATCHER_HEADER ... +0x040 ApcState: _KAPC_STATE +0x000 ApcListHead: [2] _LIST_ENTRY +0x010 Process: Ptr32 _KPROCESS ... nt!_EPROCESS +0x000 Pcb: _KPROCESS ... +0x0b4 UniqueProcessId: Ptr32 Void +0x0b8 ActiveProcessLinks: _LIST_ENTRY +0x000 Flink: Ptr32 _LIST_ENTRY +0x004 Blink: Ptr32 _LIST_ENTRY ... +0x0f8 Token: _EX_FAST_REF +0x000 Object: Ptr32 Void +0x000 RefCnt: Pos 0, 3 Bits +0x000 Value: Uint4B
  • 19. Kernel debugging environment ● Two machines (debugger, debuggee) ● Physical machine to physical machine ● Virtualization ● Physical machine to virtual machine ● Virtual machine to virtual machine ● Virtual serial port
  • 20. Enable kernel debugging on target ● No more boot.ini in modern Windows systems ● Boot Configuration Data Editor: bcdedit ● bcdedit /dbgsettings serial debugport:1 baudrate:115200 ● bcdedit /debug on
  • 21. DEMO
  • 22. Conclusion ● It was just an introduction, this is just tip of the iceberg... ● It is not magic ● There are tons of bugs in kernel drivers; you should focus on them ● You can do anything in kernel space (also, easily crash the machine) ●Try to search and exploit driver vulnerabilities for fun... or for profit :)
  • 23. Thank you for your attention! Andras Kabai contact (_at_) kabaiandras.hu gr33t1ngz 4 @hekkcamp p4rt1c1p4nts!