SEH overwrite and its exploitability Shuichiro Suzuki Fourteenforty Research Institute Inc. Research Engineer.

Slides:



Advertisements
Similar presentations
State of the Exploit Matt Miller / Trust Boundary VulnerabilityExploitation.
Advertisements

Smashing the Stack for Fun and Profit
Defenses. Preventing hijacking attacks 1. Fix bugs: – Audit software Automated tools: Coverity, Prefast/Prefix. – Rewrite software in a type safe languange.
David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Andrew Roths Fermin J. Serna MSRC Engineering and MSEC Science Microsoft Corporation.
Proprietary and Confidential Information – Copyright© 2010 – All Rights Reserved Preventing Intrusion Prevention April 21, 2010 Ryan MacArthur, Labs
Part III Counter measures The best defense is proper bounds checking but there are many C/C++ programmers and some are bound to forget  Are there any.
Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions.
PCIT417. CVE (UAF) CVE (UAF) CVE (ICARDIE) CVE (UAF) CVE (UAF) CVE (UAF) IE CVE
Review: Software Security David Brumley Carnegie Mellon University.
Breno de MedeirosFlorida State University Fall 2005 Buffer overflow and stack smashing attacks Principles of application software security.
Intro to Exploitation Stack Overflows James McFadyen UTD Computer Security Group 10/20/2011.
1 CHAPTER 8 BUFFER OVERFLOW. 2 Introduction One of the more advanced attack techniques is the buffer overflow attack Buffer Overflows occurs when software.
Chapter 15 : Attacking Compiled Applications Alexis Kirat - International Student.
CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War.
Beyond Stack Smashing: Recent Advances in Exploiting Buffer Overruns Jonathan Pincus Microsoft Research Brandon Baker Microsoft Carl Hartung CSCI 7143:
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Windows XP SP2 Stack Protection Jimmy Hermansson Johan Tibell.
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Maziéres, Dan Boneh
September 22, 2014 Pengju (Jimmy) Jin Section E
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Win32 Programming Lesson 24: More SEH That’s right… you’ll never generate an exception, will you?
Software attacks Lorenzo Dematté Software attacks Advanced buffer overflow: heap smashing.
Mitigation of Buffer Overflow Attacks
Automatic Diagnosis and Response to Memory Corruption Vulnerabilities Presenter: Jianyong Dai Jun Xu, Peng Ning, Chongkyung Kil, Yan Zhai, Chris Bookhot.
MICHALIS POLYCHRONAKIS(COLUMBIA UNIVERSITY,USA), KOSTAS G. ANAGNOSTAKIS(NIOMETRICS, SINGAPORE), EVANGELOS P. MARKATOS(FORTH-ICS, GREECE) ACSAC,2010 Comprehensive.
Exploitation possibilities of memory related vulnerabilities
Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
Operating Systems Lecture 14 Segments Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 2.
Reminder Bomb lab is due tomorrow! Attack lab is released tomorrow!!
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
CNIT 127: Exploit Development Ch 14: Protection Mechanisms.
Group 9. Exploiting Software The exploitation of software is one of the main ways that a users computer can be broken into. It involves exploiting the.
Foundations of Network and Computer Security J J ohn Black CSCI 6268/TLEN 5550, Spring 2013.
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 1.
Beyond Stack Smashing: Recent Advances In Exploiting Buffer Overruns Jonathan Pincus and Brandon Baker Microsoft Researchers IEEE Security and.
1 Introduction to Information Security , Spring 2016 Lecture 2: Control Hijacking (2/2) Avishai Wool.
Control Hijacking: Defenses
Translation Lookaside Buffer
Buffer Overflows ...or How I Learned to Never Trust the User
Mitigation against Buffer Overflow Attacks
Return Oriented Programming
Protecting Memory What is there to protect in memory?
Mingwei Zhang Aravind Prakash Xiaolei Li Zhenkai Liang Heng Yin
Basic Memory Corruption Attacks
Introduction to Information Security
CSC 495/583 Topics of Software Security Stack Overflows (2)
Chapter 10 And, Finally... The Stack
Recitation: Attack Lab
CMSC 414 Computer and Network Security Lecture 21
Midterm Review & Exploit Development 105
Software Security.
Trust Boundary Vulnerability Exploitation State of the Exploit
Advanced Buffer Overflow: Pointer subterfuge
DEP IN DEPTH.
Translation Lookaside Buffer
DEP IN DEPTH And other stuff too.
Following Malware Execution in IDA
Return-to-libc Attacks
Presentation transcript:

SEH overwrite and its exploitability Shuichiro Suzuki Fourteenforty Research Institute Inc. Research Engineer

Agenda Theme and Goal Review of SEH overwrites Protection mechanisms for SEH overwrites Bypassing protection mechanisms. Demonstration Conclusion

Theme and goal Theme SEH overwriting is one of the major methods for exploiting Windows software. We already have several protection mechanisms for SEH overwrites. Is the protection provided by DEP and SEHOP enough? How about the protection from SafeSEH and SEHOP? Goal To reveal which combinations of the known protection mechanisms for SEH overwrites are really effective. On the way to the goal, I will show you that we can bypass SafeSEH and Software DEP and Hardware DEP and SEHOP, all at the same time, under certain conditions.

Target environment Windows XP SP3 Windows Vista SP1 Windows 7 32bit processes on x86 32bit processes on x64 (WOW64) Visual Studio 2008 was used to compile all the programs in this presentation.

Review of SEH Overwrite

About SEH SEH is “Structured Exception Handling” Exception handling system provided by Windows int test(void){ __try{ // Exception may occur here } __except( EXCEPTION_EXECUTE_HANDLER ){ // This handles the exception } return 0; } int test(void){ __try{ // Exception may occur here } __except( EXCEPTION_EXECUTE_HANDLER ){ // This handles the exception } return 0; }

Inside Windows Exception Dispatching 0x0012ff50 0x0012ff7C ExceptionList FS Register NT_TIB 0x0012ff90 0x x Stack EXCEPTION_RECORD typedef struct _EXCEPTION_RECORD{ struct _EXCEPTION_RECORD *_next; PEXCEPTION_ROUTINE _handler; } EXCEPTION_RECORD; typedef struct _EXCEPTION_RECORD{ struct _EXCEPTION_RECORD *_next; PEXCEPTION_ROUTINE _handler; } EXCEPTION_RECORD; SEH chain 0xffffffff 0x070BBCC Thread Information Block _next _handler

SEH Overwrite Attack 0x0012ff50 0x StackProcess Memory Space Module(EXE or DLL) Handler1 Buffer Overflow! 0x

Stack after an exception handler is called 0x Stack 0x909006eb EXCEPTION_DISPOSITION __cdecl _except_handler ( struct _EXCEPTION_RECORD *_ExceptionRecord, void * _EstablisherFrame, struct _CONTEXT *_ContextRecord, void * _DispatcherContext ); EXCEPTION_DISPOSITION __cdecl _except_handler ( struct _EXCEPTION_RECORD *_ExceptionRecord, void * _EstablisherFrame, struct _CONTEXT *_ContextRecord, void * _DispatcherContext ); ESP _DispatcherContext _ContextRecord _EstablisherFrame _ExceptionRecord Address to return _handler ESP+8 pop eax retn pop eax retn Attackers Arbitrary Code

Protection mechanisms

/GS SafeSEH SoftwareDEP SEHOP Hardware DEP ASLR

/GS Stack Guard by Visual Studio compiler This checks if a buffer overflow occurs before returning from a function. This is irrelevant to SEH overwrites because an exception can be generated before the check.

Protection mechanisms SEH overwrites specific

SafeSEH Handler10x Handler20x Handler3… …… A module protected by SafeSEH Handler1 Handler2 0x x Header Code “pop,pop,ret” sequence ×

Weakness of SafeSEH Module SafeSEH Protected Module SafeSEH Protected Module Not SafeSEH Protected Module Not SafeSEH Protected pop pop ret × Memory Space Data area without executable attribute Data area without executable attribute Data area with executable attribute Data area with executable attribute Data area in a module

Modules not protected by SafeSEH From Thunderbird * All modules in Thunderbird are compiled with /SafeSEH

pop pop ret in the wild From thunderbird.exe

Additional check by Software DEP Module SafeSEH Protected Module SafeSEH Protected Module Not SafeSEH Protected Module Not SafeSEH Protected pop pop ret × Memory Space Data area without executable attribute Data area without executable attribute Data area with executable attribute Data area with executable attribute Data area in a module Weekness

Software DEP 0: kd> dt nt!_KEXECUTE_OPTIONS +0x000 ExecuteDisable : Pos 0, 1 Bit +0x000 ExecuteEnable : Pos 1, 1 Bit +0x000 DisableThunkEmulation : Pos 2, 1 Bit +0x000 Permanent : Pos 3, 1 Bit +0x000 ExecuteDispatchEnable : Pos 4, 1 Bit +0x000 ImageDispatchEnable : Pos 5, 1 Bit +0x000 DisableExceptionChainValidation : Pos 6, 1 Bit +0x000 Spare : Pos 7, 1 Bit 0: kd> dt nt!_KEXECUTE_OPTIONS +0x000 ExecuteDisable : Pos 0, 1 Bit +0x000 ExecuteEnable : Pos 1, 1 Bit +0x000 DisableThunkEmulation : Pos 2, 1 Bit +0x000 Permanent : Pos 3, 1 Bit +0x000 ExecuteDispatchEnable : Pos 4, 1 Bit +0x000 ImageDispatchEnable : Pos 5, 1 Bit +0x000 DisableExceptionChainValidation : Pos 6, 1 Bit +0x000 Spare : Pos 7, 1 Bit KPROCESS structure _KEXECUTE_OPTIONS

Software DEP pop pop ret × Memory Space × × Data area in a module × Module SafeSEH Protected Module SafeSEH Protected Module Not SafeSEH Protected Module Not SafeSEH Protected Data area without executable attribute Data area without executable attribute Data area with executable attribute Data area with executable attribute

Memory Space 0x SEHOP 0x12ff50 0x0012ff90 0x x x12ffe4 0x xffffffff FinalExceptionHandler 0x909006eb × Overwrite! NTDLL.DLL Module 2 pop pop ret Module 1 Proper Handler SEH Chain Stack SEH overwrites protection

Protection mechanisms Generic protections

Hardware DEP and ASLR Hardware DEP prevents code without executable attribute from being executed. ASLR has several impacts on the SEH overwrites.( Explain later )

Bypassing protection mechanisms

SafeSEH and Software DEP pop pop ret Memory Space Data area in a module pop pop ret × Memory Space Data area in a module × × × × × × × × Module SafeSEH Protected Module Not SafeSEH Protected Non module area (Data area) Non module area with executable attribute Non module area with executable attribute

Bypassing SEHOP It is weak if the address of a buffer overflow and FinalExceptionHandler is known. Attackers can recreate a proper SEH chain. 0x x12ff50 0x0012ff90 0x x x12ffe4 0x xffffffff FinalExceptionHandler 0x12ff50 Overwrite!

Bypassing Hardware DEP Return-into-libc Return-oriented programming VirutalAlloc Stack Args for VirtualAlloc memcpy Args for memcpy CreateFile Args for CreateFile... ESP Buffer overflow

Bypass /GS, SafeSEH, Software DEP, Hardware DEP, SEHOP Recreate SEH Chain (bypassing SEHOP) Overwritten exception handler address must be in a module which is not SafeSEH enabled ( bypassing SafeSEH ) Create a stack to execute desired code ( bypassing Hardware DEP ) To execute the code using the stack above, we have to set an exception handler to some stack rewind and return instructions ( bypassing Hardware DEP ) Trigger an exception

Recreating the SEH Chain and Setting the Exception Handler Address 0x0012ff50 0x Stack Process Memory Space Module(EXE or DLL) No SafeSEH Buffer 0xffffffff FinalExceptionHandler Module(EXE or DLL) Handler1 SEH Chain Exception! 0xffffffff FinalExceptionHandler 0x x0012ff4C Attackers Arbitrary Code and Data

Good instruction to bypass Hardware DEP 0x0012ff50 Module(EXE or DLL) No SafeSEH Buffer 0x xffffffff FinalExceptionHandler Module(EXE or DLL) Handler1 add esp, 0x0C24 retn Exception Information 0x0c24 Attackers Arbitrary Code StackProcess Memory Space

Create proper stack for return-into-libc 0x0012ff50 Module(EXE or DLL) No SafeSEH Buffer 0x xffffffff FinalExceptionHandler Module(EXE or DLL) Handler1 add esp, 0x0C24 retn Exception Information 0x0c24 Attackers Arbitrary Code StackProcess Memory Space 0x x x x x x x0012F758 0x Address of VirtualAlloc Address of memcpy Address to be allocated The size to be allocated MEM_COMMIT|MEM_RESERVE PAGE_EXECUTE_READWRITE Address to return after memcpy Dest to copy Src of copy The size to be copied 0x7c8622A4

Exception handlers which can be used 0x0012ff50 Module(EXE or DLL) No SafeSEH Buffer 0x xffffffff FinalExceptionHandler Module(EXE or DLL) Handler1 add esp, 0x0C24 retn Exception Information 0x0CF8 Attackers Arbitrary Code StackProcess Memory Space 0x x x x x x x0012F758 0x Address of VirtualAlloc Address of memcpy Address to be allocated The size to be allocated MEM_COMMIT|MEM_RESERVE PAGE_EXECUTE_READWRITE Address to return after memcpy Dest to copy Src of copy The size to be copied 0x7c8622A4 add esp, 0x0024 retn Doesn’t work Too small rewind add esp, 0xFF24 retn May be too big. Depends on the stack size. add esp, 0x0CF8 retn

Example of “add esp + retn” From Thunderbird

Summarize the condition The address of the stack where buffer overflow occurs is known. The address of the FinalExceptionHandler in ntdll.dll is known. A process has a module not protected by /SafeSEH “add esp + retn” instructions which matches followings can be found in the module. – The amount of “add esp” rewind is larger than the stack made by windows exception dispatcher. – The amount of “add esp” rewind is smaller than the stack size when the exception handler is called. The address of VirtualAlloc and memcpy is known ( or some alternatives can be used). Attacker can write 0x00 on the stack by using buffer overflow, which means string functions can’t be used to exploit. ( At least the method I showed here can not be used if this doesn’t match)

Demonstration Demonstration on Windows 7 I assumed that “add esp,0x0c24 – retn” can be found in a non /SafeSEH protected module. /GS, Software DEP, Hardware DEP, SEHOP are all enabled. ASLR is disabled.

Importance of ASLR Makes it difficult to recreate proper SEH chain Makes it difficult to find “add esp + retn” instructions at a fixed address. Makes it difficult to set FinalExceptionHandler address in the last element of SEH chain.

Conclusion ProtectionsWindows XP SP3Windows Vista SP1Windows 7 /GS + SafeSEH Exploitable by using data area as an exception handler Exploitable by using data area as an exception handler Exploitable by using data area as an exception handler /GS + SafeSEH + Software DEP If all modules are SafeSEH protected its difficult to exploit /GS + Software DEP + Hardware DEP Exploitable by Return- into-libc or Return- oriented programming /GS + Software DEP + SEHOP - Exploitable by recreating proper SEH chain /GS + SafeSEH + SEHOP- Exploitable by recreating proper SEH chain and using data area as an exception handler /GS + Software DEP + SEHOP + Hardware DEP - Exploitable by recreating proper SEH Chain and using data area and return-oriented programming /GS + SEHOP + ASLR-Difficult to exploit /GS + Software DEP + SEHOP + Hardware DEP + ASLR -Difficult to exploit

ASLR + DEP “Bypassing Browser Memory Protections” ( Alexander Sotirov, Mark Dowd ) shows how to bypass them. But it was without SEHOP

Questions?