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

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

description

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.

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

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

SEH overwrite and its exploitability

Shuichiro SuzukiFourteenforty Research Institute Inc.

Research Engineer

Page 2: 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

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

Theme and goalTheme

• 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.

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

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.

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

Review of SEH Overwrite

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

About SEH

• SEH is “Structured Exception Handling”• Exception handling system provided by

Windowsint test(void){ __try{ // Exception may occur here} __except( EXCEPTION_EXECUTE_HANDLER ){ // This handles the exception} return 0; }

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

Inside Windows Exception Dispatching

0x0012ff50

0x0012ff7C

ExceptionList

FS Register

NT_TIB

0x0012ff90

0x0401790

0x0408800

Stack

EXCEPTION_RECORD

typedef struct _EXCEPTION_RECORD{ struct _EXCEPTION_RECORD *_next; PEXCEPTION_ROUTINE _handler; } EXCEPTION_RECORD;

SEH chain

0xffffffff0x070BBCC

Thread Information Block

_next_handler

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

SEH Overwrite Attack

0x0012ff500x0401790

Stack Process Memory Space

Module(EXE or DLL)

Handler1

BufferOverflow!

0x0505690

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

Stack after an exception handler is called

0x0401790

Stack

0x909006eb

EXCEPTION_DISPOSITION __cdecl _except_handler ( struct _EXCEPTION_RECORD *_ExceptionRecord, void * _EstablisherFrame, struct _CONTEXT *_ContextRecord, void * _DispatcherContext );

ESP

_DispatcherContext_ContextRecord

_EstablisherFrame_ExceptionRecordAddress to return

_handler

ESP+8

pop eaxpop eaxretn

Attackers Arbitrary Code

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

Protection mechanisms

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

Protection mechanisms

• /GS• SafeSEH• SoftwareDEP• SEHOP• Hardware DEP• ASLR

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

/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.

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

Protection mechanisms

SEH overwrites specific

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

SafeSEH

Handler1 0x00002550

Handler2 0x00049080

Handler3 …

… …

A module protected by SafeSEH

Handler1

Handler2

0x00002550

0x00049080

Header

Code

“pop,pop,ret” sequence×

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

Weakness of SafeSEH

Module SafeSEH Protected

Module Not SafeSEH Protected

pop pop ret×

pop pop ret

pop pop ret

pop pop ret

Memory Space

Data areawithout executable attribute

Data areawith executable attribute

Data area in a module

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

Modules not protected by SafeSEH

From Thunderbird 2.0.0.23* All modules in Thunderbird 3.0.3 are compiled with /SafeSEH

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

pop pop ret in the wild

From thunderbird.exe

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

Additional check by Software DEP

Module SafeSEH Protected

Module Not SafeSEH Protected

pop pop ret×

pop pop ret

pop pop ret

pop pop ret

Memory Space

Data areawithout executable attribute

Data areawith executable attribute

Data area in a module

Weekness

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

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

KPROCESS structure

_KEXECUTE_OPTIONS

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

Software DEP

pop pop ret×

pop pop ret

pop pop ret

pop pop ret

Memory Space

×

×

Data area in a module×

Module SafeSEH Protected

Module Not SafeSEH Protected

Data areawithout executable attribute

Data areawith executable attribute

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

Memory Space

0x00405600

SEHOP

0x12ff50

0x0012ff90

0x04224550

0x0701790

0x12ffe40x0701790

0xffffffffFinalExceptionHandler

0x909006eb

×

Overwrite!

NTDLL.DLL

Module 2pop pop ret

Module 1Proper Handler

SEH Chain

Stack

SEH overwrites protection

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

Protection mechanisms

Generic protections

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

Hardware DEP and ASLR

• Hardware DEP prevents code without executable attribute from being executed.

• ASLR has several impacts on the SEH overwrites.( Explain later )

鈴木秀一郎
ここでのASLR説明で、SEHOPのバイパス用のSEHチェーンの再構築に関しては、SEHOPのところで、アドレスが分かっていると再構築できてしまうという一言があったうえで、言った方がいいかもしれない。
Page 24: SEH overwrite and its exploitability Shuichiro Suzuki Fourteenforty Research Institute Inc. Research Engineer.

Bypassing protection mechanisms

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

SafeSEH and Software DEP

pop pop ret

pop pop ret

pop pop ret

pop pop ret

Memory Space

Data area in a module

pop pop ret×

pop pop ret

pop pop ret

pop pop ret

Memory Space

Data area in a module

×

×

×

×

×

×

×

×

Module SafeSEH Protected Module Not SafeSEH Protected

Non module area (Data area)

Non module areawith executable attribute

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

Bypassing SEHOP

• It is weak if the address of a buffer overflow and FinalExceptionHandler is known.

• Attackers can recreate a proper SEH chain.

0x004056000x12ff50

0x0012ff90

0x04224550

0x07017900x12ffe4

0x07017900xffffffff

FinalExceptionHandler

0x12ff50Overwrite!

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

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

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

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

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

Recreating the SEH Chain andSetting the Exception Handler Address

0x0012ff500x0401790

Stack Process Memory Space

Module(EXE or DLL) No SafeSEH

Buffer

0xffffffffFinalExceptionHandler

Module(EXE or DLL)

Handler1

SEH Chain

Exception!

0xffffffffFinalExceptionHandler

0x05056900x0012ff4C

Attackers Arbitrary Code and Data

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

Good instruction to bypass Hardware DEP

0x0012ff50

Module(EXE or DLL) No SafeSEH

Buffer

0x0505690

0xffffffffFinalExceptionHandler

Module(EXE or DLL)

Handler1

add esp, 0x0C24retn

Exception Information

0x0c24

Attackers Arbitrary Code

Stack Process Memory Space

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

Create proper stack for return-into-libc

0x0012ff50

Module(EXE or DLL) No SafeSEH

Buffer

0x0505690

0xffffffffFinalExceptionHandler

Module(EXE or DLL)

Handler1

add esp, 0x0C24retn

Exception Information

0x0c24

Attackers Arbitrary Code

Stack Process Memory Space

0x004011100x300000000x000008000x000030000x00000040

0x300000000x30000000

0x0012F7580x00000800

Address of VirtualAllocAddress of memcpyAddress to be allocatedThe size to be allocatedMEM_COMMIT|MEM_RESERVEPAGE_EXECUTE_READWRITEAddress to return after memcpyDest to copySrc of copyThe size to be copied

0x7c8622A4

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

Exception handlers which can be used

0x0012ff50

Module(EXE or DLL) No SafeSEH

Buffer

0x0505690

0xffffffffFinalExceptionHandler

Module(EXE or DLL)

Handler1

add esp, 0x0C24retn

Exception Information

0x0CF8

Attackers Arbitrary Code

Stack Process Memory Space

0x004011100x300000000x000008000x000030000x00000040

0x300000000x30000000

0x0012F7580x00000800

Address of VirtualAllocAddress of memcpyAddress to be allocatedThe size to be allocatedMEM_COMMIT|MEM_RESERVEPAGE_EXECUTE_READWRITEAddress to return after memcpyDest to copySrc of copyThe size to be copied

0x7c8622A4

add esp, 0x0024retn

Doesn’t workToo small rewind

add esp, 0xFF24retn

May be too big.Depends on the stack size.

add esp, 0x0CF8retn

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

Example of “add esp + retn”

From Thunderbird 2.0.0.23

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

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)

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

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.

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

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.

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

ConclusionProtections Windows XP SP3 Windows Vista SP1 Windows 7

/GS + SafeSEHExploitable 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

If all modules are SafeSEH protected its difficult to

exploit

If all modules are SafeSEH protected its difficult to

exploit

/GS + Software DEP + Hardware DEP

Exploitable by Return-into-libc or Return-

oriented programming

Exploitable by Return-into-libc or Return-oriented

programming

Exploitable by Return-into-libc or Return-oriented

programming

/GS + Software DEP + SEHOP - Exploitable by recreating

proper SEH chainExploitable by recreating

proper SEH chain

/GS + SafeSEH + SEHOP -Exploitable by recreating

proper SEH chain and using data area as an exception

handler

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

Exploitable by recreating proper SEH Chain and

using data area and return-oriented programming

/GS + SEHOP + ASLR - Difficult to exploit Difficult to exploit

/GS + Software DEP + SEHOP + Hardware DEP +

ASLR- Difficult to exploit Difficult to exploit

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

ASLR + DEP

• “Bypassing Browser Memory Protections” ( Alexander Sotirov, Mark Dowd ) shows how to bypass them.

• But it was without SEHOP

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

Questions?