Nytro Posted September 10, 2012 Report Posted September 10, 2012 A Guide To Kernel Exploitation.pdfFile size: 15972 KB (449 pages).ContentsForeword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiAcknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiAbout the Authors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixAbout the Technical Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiPART I A JOURNEY TO KERNEL LANDCHAPTER 1 From User-Land to Kernel-Land Attacks. . . . . . . . . . . . . . . . 3Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Introducing the Kernel and the World of Kernel Exploitation . . . 3The Art of Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Why Doesn’t My User-Land Exploit Work Anymore?. . . . . . . . . . 9Kernel-Land Exploits versus User-Land Exploits. . . . . . . . . 11An Exploit Writer’s View of the Kernel. . . . . . . . . . . . . . . . . . . . . . 13User-Land Processes and the Scheduler . . . . . . . . . . . . . . . . . . 13Virtual Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Open Source versus Closed Source Operating Systems . . . . . . . . 18Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Related Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Endnote. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19CHAPTER 2 A Taxonomy of Kernel Vulnerabilities. . . . . . . . . . . . . . . . 21Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Uninitialized/Nonvalidated/Corrupted Pointer Dereference. . . . . 22Memory Corruption Vulnerabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Kernel Stack Vulnerabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Kernel Heap Vulnerabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Integer Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29(Arithmetic) Integer Overflows . . . . . . . . . . . . . . . . . . . . . . . . . . 29Sign Conversion Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Race Conditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Logic Bugs (a.k.a. the Bug Grab Bag). . . . . . . . . . . . . . . . . . . . . . . . 39Reference Counter Overflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Physical Device Input Validation. . . . . . . . . . . . . . . . . . . . . . . . 40Kernel-Generated User-Land Vulnerabilities. . . . . . . . . . . . . 41Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Endnotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44CHAPTER 3 Stairway to Successful Kernel Exploitation. . . . . . . . . . 47Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47A Look at the Architecture Level. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Generic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48x86 and x86-64. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55The Execution Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Placing the Shellcode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Forging the Shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66The Triggering Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Memory Corruption. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Race Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86The Information-Gathering Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90What the Environment Tells Us. . . . . . . . . . . . . . . . . . . . . . . . . 91What the Environment Would NotWant to Tell Us: Infoleaks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Related Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99PART II THE UNIX FAMILY, MAC OS X, AND WINDOWSCHAPTER 4 The UNIX Family. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103The Members of the UNIX Family. . . . . . . . . . . . . . . . . . . . . . . . . . 104Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Solaris/OpenSolaris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114BSD Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125The Execution Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126Abusing the Linux Privilege Model. . . . . . . . . . . . . . . . . . . . 126Practical UNIX Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Kernel Heap Exploitation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Attacking the OpenSolaris Slab Allocator . . . . . . . . . . . . . . 139Attacking the Linux 2.6 SLAB^H^HUB Allocator. . . . . . 160Attacking (Linux) Kernel Stack Overflows. . . . . . . . . . . . . 177Revisiting CVE-2009-3234. . . . . . . . . . . . . . . . . . . . . . . . . . . . 184Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193Endnotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194CHAPTER 5 Mac OS X. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195An Overview of XNU. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Mach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197BSD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197IOKit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197System Call Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Kernel Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Kernel Extensions (Kext) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208IOKit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214Kernel Extension Auditing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215The Execution Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Exploitation Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Arbitrary Memory Overwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . 229Stack-Based Buffer Overflows. . . . . . . . . . . . . . . . . . . . . . . . . 239Memory Allocator Exploitation . . . . . . . . . . . . . . . . . . . . . . . . 253Race Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266Snow Leopard Exploitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266Endnotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267CHAPTER 6 Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Windows Kernel Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271Kernel Information Gathering. . . . . . . . . . . . . . . . . . . . . . . . . . 272Introducing DVWD: Damn Vulnerable WindowsDriver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276Kernel Internals Walkthrough. . . . . . . . . . . . . . . . . . . . . . . . . . 278Kernel Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282The Execution Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285Windows Authorization Model. . . . . . . . . . . . . . . . . . . . . . . . . 286Building the Shellcode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295Practical Windows Exploitation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308Arbitrary Memory Overwrite . . . . . . . . . . . . . . . . . . . . . . . . . . . 308Stack Buffer Overflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Endnotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340PART III REMOTE KERNEL EXPLOITATIONCHAPTER 7 Facing the Challenges of RemoteKernel Exploitation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343Attacking Remote Vulnerabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Lack of Exposed Information. . . . . . . . . . . . . . . . . . . . . . . . . . 344Lack of Control over the Remote Target. . . . . . . . . . . . . . . 347Executing the First Instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348Direct Execution Flow Redirection . . . . . . . . . . . . . . . . . . . . . 349Arbitrary Write of Kernel Memory. . . . . . . . . . . . . . . . . . . . . 360Remote Payloads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362Payload Migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383Endnote. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384CHAPTER 8 Putting It All Together: A Linux Case Study. . . . . . . . 385Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385SCTP FWD Chunk Heap Memory Corruption . . . . . . . . . . . . . . . 386A Brief Overview of SCTP. . . . . . . . . . . . . . . . . . . . . . . . . . . . 386The Vulnerable Path. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389Remote Exploitation: An Overall Analysis . . . . . . . . . . . . . . . . . . . 393Getting the Arbitrary Memory Overwrite Primitive . . . . . . . . . . . 394Remotely Adjusting the Heap Layout. . . . . . . . . . . . . . . . . . 395Building SCTP Messages: From Relativeto Absolute Memory Overwrite. . . . . . . . . . . . . . . . . . . . . . . . 397Installing the Shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403Directly Jumping from Interrupt Context to UserMode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403Executing the Shellcode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410Checking the Current Process and Emulatingthe gettimeofday() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Executing the Connect-Back. . . . . . . . . . . . . . . . . . . . . . . . . . . 412Recovering the Vsyscall. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414Related Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415Endnote. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415PART IV FINAL WORDSCHAPTER 9 Kernel Evolution: Future Forms of Attackand Defense. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419Kernel Attacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420Confidentiality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425Kernel Defense. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425Kernel Threat Analysis and Modeling. . . . . . . . . . . . . . . . . . 425Kernel Defense Mechanisms. . . . . . . . . . . . . . . . . . . . . . . . . . . 427Kernel Assurance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428Beyond Kernel Bugs: Virtualization . . . . . . . . . . . . . . . . . . . . . . . . . 432Hypervisor Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432Guest Kernel Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434Index.......................................................................................................................437Download:http://www.pdf-archive.com/2011/02/24/a-guide-to-kernel-exploitation/http://www.multiupload.nl/EGTCK09D6R Quote