SlideShare a Scribd company logo
Structured Exception Handler
                                 EXPLOITATION




http://www.htbridge.ch/                                  Brian Mariani
What is an exception

•   An exception is an event that occurs during the execution of a program

•   Requires the execution of code outside the normal flow of control
Structured Exception Handling
•   Blocks of code are encapsulated, with each block having one or more associated
    handlers.

•   Each handler specifies some form of filter condition on the type of exception it
    handles

•   When an exception is raised by code in a protected block, the set of corresponding
    handlers is searched in order, and the first one with a matchingfilter condition is
    executed

•   A single method can have multiple structured exception handling blocks, and the
    blocks can also be nested within each other
Exception pointers structure (1)

•   Contains an exception record with a machine-independent description of an
    exception

•   A context record with a machine-dependent description of the processor context at
    the time of the exception
Exception pointers structure (2)
•   A pointer to the next exception registration structure

•   A pointer to the address of the actual code of the exception handler
Thread information block
•   The Thread Information Block (TIB) is a data structure in Win32 that stores
    information
    about the currently running thread

•   At the position FS:[0x00] we found the current exception handler
Dumping SEH chain in Inmunity debugger
How SEH works?


•   The exception handlers are linked to each other

•   They form a linked list chain on the stack, and sit relatively close to the bottom of the
    stack

•   When an exception occurs, Windows retrieves the head of the SEH chain walks
    through the list and tries to find the suitable handler to close the application properly
Abusing the SEH
•   When exploiting an SEH overwrite and attacker clobbers the handler attribute of the
    EXCEPTION_REGISTRATION_RECORD with the address of an instruction sequence
    similar to POP POP RET

•   When the exception occurs, this causes Windows to pass execution to this address,
    which subsequently returns to the location on the stack of the Next attribute of the

    EXCEPTION_REGISTRATION_RECORD

•   The Next attribute is also controlled by the attacker, but if we recall the stack layout
    from earlier, the Next attribute is below the Handler attribute

•   This limits the attacker to 4 bytes before running into the Handler address he
    previously supplied to originally obtain code execution

•   However, by overwriting the Next attribute with the instructions that jump the Handler
    attribute, the attacker typically has enough room for arbitrary shellcode, and this is
    exactly what happens
Overwriting the Next SEH record and SE
                handler
  •   To check a chain of exception handlers before and after an overflow we can use
      WinDbg !exchain command

  •   At the left we can see the SEH chain and the stack before the overflow occurs

  •   At the right we can see the pointers were successfully overwritten
What are we overwriting?


•   When we performs a regular stack based buffer overflow, we overwrite the return
    address of the Extended Instruction Pointer (EIP)

•   When doing a SEH overflow, we will continue overwriting the stack after overwriting
                       overflow
    EIP, so we can overwrite the default exception handler as well
    EIP
Viewing the SEH before the overflow
•   Before the overflow occurs we can see the stack and the SEH chain.

•   The SEH chain starts from 0x015fd044 down to 0x015fffdc which indicates the end of
    the SEH chain

•   Directly below 0x015fffe0, we see 0x7c839ad8, which is the address of the default SE
                    0x015fffe0          0x7c839ad8
    handler for this application. This address sits in the address space of kernel32.dll
Viewing the SEH after the overflow
•   Dumping the TIB confirms that the SEH was overwritten

•   Code execution is successfully passed to the injected address 0x61616161

•   Addresses 0x015fd044 and 0x015fd048 which were the Next SEH record and SE
    handler are now controlled.




                                  EIP point to 0x61616161,
                                  so we can control the flow
                                       of the program




                                             Pointer to the Next record
                                              and SEH handler was
                                                     overwritten




                                                                TIB dumping let us know
                                                                   the SEH chain was
                                                                sucessfully overwritten
See an exception analysis
•   The command !analyze –v in Windbg give us more details about the triggering of the
    exception
How SEH base exploit works
•   When the exception is triggered the program flow go to the SE Handler

•   All we need is just put some code to jump to our payload

•   Faking a second exception makes the application goes to the next SEH pointer

•   As the Next SEH pointer is before the SE handler we can overwrite the Next SEH

•   Since the shellcode sits after the Handler, we can trick the SE Handler to execute POP

    POP RET instructions so the address to the Next SEH will be placed in EIP, therefore
    executing the code in Next SEH

•   The code will basically jump over some bytes and execute the shellcode
Exploiting the application
•   We will exploit a vulnerability in Gogago Youtube Downloader Video ActiveX
    www.gogago.net/download/youtube_video_downloader_setup.exe

•   A buffer overflow is triggered after injecting more that 2230 bytes in the Download()
    function

•   This vulnerability could be exploited using a basic RET CALL technique

•   We will use SEH based exploitation which is also functioning in this particular case
Creating the POC
•   We craft an html page calling the method Download using the CLASSID

•   When we overflow the method with 2250 bytes with junk data we trigger an exception
Overwriting Next pointer and SE
                handler
•   To successfully overwrite the Next Pointer and SE Handler we must calculate the exact
            number of bytes to inject

•   You can use tools as pattern_create and pattern_search from Metasploit, or you can do
    it manually injecting buffers with different patterns
Finding POP POP RET instructions
•   Finding opcodes it’s not a difficult task you can use findjump or IDA

•   In this tutorial we will use WinDBG

•   We launch our prove of concept and we attach to Internet Explorer. After the overflow
                                                             Explorer
    occurs we search the base memory address of the Gogago module MDIEex.dll

•   Finally we can search for the opcodes using the s command
Building the exploit
•   After calculating the number of bytes to overwrite the Next pointer and SE handler we
       inject 4 bytes of code to jump to our shellcode this will replace the old SE handler
•
    Following the SE handler we inject the POP POP RET opcodes from the same module
    of the exploited application

•   Finally we inject our payload
Executing the exploit (1)
•   We place a breakpoint before entering in the vulnerable method. The SE handler that
    will be overwritten sits at 0x15fa79c, and corresponds to the jscript.dll module
                                0x15fa79c
Executing the exploit (2)
•   After the overflow occurs we successfully overwrites the old jscript SE handler later
    code execution will be redirected to our POP POP RET instructions
Redirect code execution
•   The code is redirected to our fake SE Handler address
Jumping to our payload
•   Jumping over 6 bytes to reach ou shellcode starting at address 0x015fa7a4
Shellcode execution
•   Time to dance 
Questions




brian.mariani@htbridge.ch
References
•   http://msdn.microsoft.com/en-us/library/ms680663%28v=VS.85%29.aspx

•   http://msdn.microsoft.com/en-us/library/c68xfk56%28v=vs.71%29.aspx

•   http://en.wikipedia.org/wiki/Win32_Thread_Information_Block

•   http://corelan.be

More Related Content

What's hot (20)

PDF
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
PDF
CNIT 127: Ch 18: Source Code Auditing
Sam Bowne
 
PDF
CNIT 127: 3: Shellcode
Sam Bowne
 
PDF
CNIT 127 Ch 3: Shellcode
Sam Bowne
 
PDF
Practical Malware Analysis Ch13
Sam Bowne
 
PDF
CNIT 126 8: Debugging
Sam Bowne
 
PDF
CNIT 126 7: Analyzing Malicious Windows Programs
Sam Bowne
 
PDF
CNIT 127 Ch 4: Introduction to format string bugs
Sam Bowne
 
PDF
CNIT 141 8. Authenticated Encryption
Sam Bowne
 
PDF
CNIT 141: 1. Encryption
Sam Bowne
 
PDF
CNIT 126: 13: Data Encoding
Sam Bowne
 
PDF
CNIT 141: 1. Encryption
Sam Bowne
 
PDF
CNIT 126 9: OllyDbg
Sam Bowne
 
PDF
Introduction to Raft algorithm
muayyad alsadi
 
PDF
CNIT 127: 8: Windows overflows (Part 2)
Sam Bowne
 
PDF
Practical Malware Analysis Ch12
Sam Bowne
 
PDF
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
PDF
CNIT 126 13: Data Encoding
Sam Bowne
 
PDF
Raft presentation
Patroclos Christou
 
PDF
CNIT 141: 4. Block Ciphers
Sam Bowne
 
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
CNIT 127: Ch 18: Source Code Auditing
Sam Bowne
 
CNIT 127: 3: Shellcode
Sam Bowne
 
CNIT 127 Ch 3: Shellcode
Sam Bowne
 
Practical Malware Analysis Ch13
Sam Bowne
 
CNIT 126 8: Debugging
Sam Bowne
 
CNIT 126 7: Analyzing Malicious Windows Programs
Sam Bowne
 
CNIT 127 Ch 4: Introduction to format string bugs
Sam Bowne
 
CNIT 141 8. Authenticated Encryption
Sam Bowne
 
CNIT 141: 1. Encryption
Sam Bowne
 
CNIT 126: 13: Data Encoding
Sam Bowne
 
CNIT 141: 1. Encryption
Sam Bowne
 
CNIT 126 9: OllyDbg
Sam Bowne
 
Introduction to Raft algorithm
muayyad alsadi
 
CNIT 127: 8: Windows overflows (Part 2)
Sam Bowne
 
Practical Malware Analysis Ch12
Sam Bowne
 
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
CNIT 126 13: Data Encoding
Sam Bowne
 
Raft presentation
Patroclos Christou
 
CNIT 141: 4. Block Ciphers
Sam Bowne
 

Viewers also liked (20)

PDF
Ebook drupal 7 vn
Nhân Phạm Văn
 
PPSX
Human body is a holy temple
BASKARAN P
 
PPTX
Dallas partner meeting_8.07.12
bscisteam
 
PPTX
4 teknik dan bentuk instrumen
Mukhamad Fathoni
 
PPT
Youth Perspectives 1999
Geoff Glendenning
 
DOCX
Preliminary Evaluation
nctcmedia12
 
PDF
Market snapshot report 19 12-2012
Abhijit Mitra
 
PPTX
職場生存之道:內向心理學(二)
Wan Jen Huang
 
DOCX
Kontrak transaksi perkuliahan1516
Mukhamad Fathoni
 
PPT
การสังเกต Sn
Somprasong friend Ka Nuamboonlue
 
PPT
Gib
mundu1d
 
PPTX
Key Performance Indicators for Energy Efficient Asset Management in a Factory...
FAST-Lab. Factory Automation Systems and Technologies Laboratory, Tampere University of Technology
 
PPTX
Questionnaire results
Tyrrell
 
PPSX
Projecte disseny gràfic 2013
Epv Artistica
 
PPT
Task 10 formal proposal#
Tyrrell
 
PDF
Cartas Proibidas
Rosana Silva
 
PPTX
Uruguay
mcarmeb
 
PDF
Webinar Monitorización Proactiva con HPE AppPulse Active
Globe Testing
 
PPTX
MACHU PICCHU
laucs1975
 
PPTX
Recommendations for bettermeans.com
slideme52
 
Ebook drupal 7 vn
Nhân Phạm Văn
 
Human body is a holy temple
BASKARAN P
 
Dallas partner meeting_8.07.12
bscisteam
 
4 teknik dan bentuk instrumen
Mukhamad Fathoni
 
Youth Perspectives 1999
Geoff Glendenning
 
Preliminary Evaluation
nctcmedia12
 
Market snapshot report 19 12-2012
Abhijit Mitra
 
職場生存之道:內向心理學(二)
Wan Jen Huang
 
Kontrak transaksi perkuliahan1516
Mukhamad Fathoni
 
การสังเกต Sn
Somprasong friend Ka Nuamboonlue
 
Gib
mundu1d
 
Key Performance Indicators for Energy Efficient Asset Management in a Factory...
FAST-Lab. Factory Automation Systems and Technologies Laboratory, Tampere University of Technology
 
Questionnaire results
Tyrrell
 
Projecte disseny gràfic 2013
Epv Artistica
 
Task 10 formal proposal#
Tyrrell
 
Cartas Proibidas
Rosana Silva
 
Uruguay
mcarmeb
 
Webinar Monitorización Proactiva con HPE AppPulse Active
Globe Testing
 
MACHU PICCHU
laucs1975
 
Recommendations for bettermeans.com
slideme52
 
Ad

Similar to Structured Exception Handler Exploitation (20)

PDF
CNIT 127: Ch 8: Windows overflows (Part 1)
Sam Bowne
 
PPTX
Abusing SEH For Fun
Digital Echidna
 
PDF
SEH overwrite and its exploitability
FFRI, Inc.
 
PDF
Riding the Overflow - Then and Now
Miroslav Stampar
 
PPTX
Exploit Development with Python
Thomas Gregory
 
PPTX
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
zakiakhmad
 
PPTX
Reversing & Malware Analysis Training Part 11 - Exploit Development [Advanced]
securityxploded
 
PPTX
Seh based attack
Mihir Shah
 
PPTX
Seh based exploitation
Raghunath G
 
PDF
From SEH Overwrite with Egg Hunter to Get a Shell!
Rodolpho Concurde
 
PDF
From SEH Overwrite with Egg Hunter to Get a Shell_by_RodolphoConcurde
Rodolpho Concurde
 
PPTX
Reversing malware analysis training part11 exploit development advanced
Cysinfo Cyber Security Community
 
PDF
Smashing the Buffer
Miroslav Stampar
 
PDF
Reversing & malware analysis training part 11 exploit development advanced
Abdulrahman Bassam
 
PDF
Riding the Overflow - Then and Now
Miroslav Stampar
 
PPT
Writing Metasploit Plugins
amiable_indian
 
PDF
CyberLink LabelPrint 2.5 Exploitation Process
Thomas Gregory
 
PDF
Fermín J. Serna - Exploits & Mitigations: EMET [RootedCON 2010]
RootedCON
 
PPT
exploiting heap overflows
primelude
 
PDF
[ENG] Hacktivity 2013 - Alice in eXploitland
Zoltan Balazs
 
CNIT 127: Ch 8: Windows overflows (Part 1)
Sam Bowne
 
Abusing SEH For Fun
Digital Echidna
 
SEH overwrite and its exploitability
FFRI, Inc.
 
Riding the Overflow - Then and Now
Miroslav Stampar
 
Exploit Development with Python
Thomas Gregory
 
Exploit Development: EzServer Buffer Overflow oleh Tom Gregory
zakiakhmad
 
Reversing & Malware Analysis Training Part 11 - Exploit Development [Advanced]
securityxploded
 
Seh based attack
Mihir Shah
 
Seh based exploitation
Raghunath G
 
From SEH Overwrite with Egg Hunter to Get a Shell!
Rodolpho Concurde
 
From SEH Overwrite with Egg Hunter to Get a Shell_by_RodolphoConcurde
Rodolpho Concurde
 
Reversing malware analysis training part11 exploit development advanced
Cysinfo Cyber Security Community
 
Smashing the Buffer
Miroslav Stampar
 
Reversing & malware analysis training part 11 exploit development advanced
Abdulrahman Bassam
 
Riding the Overflow - Then and Now
Miroslav Stampar
 
Writing Metasploit Plugins
amiable_indian
 
CyberLink LabelPrint 2.5 Exploitation Process
Thomas Gregory
 
Fermín J. Serna - Exploits & Mitigations: EMET [RootedCON 2010]
RootedCON
 
exploiting heap overflows
primelude
 
[ENG] Hacktivity 2013 - Alice in eXploitland
Zoltan Balazs
 
Ad

More from High-Tech Bridge SA (HTBridge) (15)

PDF
Welcome in the World Wild Web
High-Tech Bridge SA (HTBridge)
 
PDF
Fuzzing: An introduction to Sulley Framework
High-Tech Bridge SA (HTBridge)
 
PDF
Novell GroupWise Multiple Untrusted Pointer Dereferences Exploitation
High-Tech Bridge SA (HTBridge)
 
PDF
Manipulating Memory for Fun & Profit
High-Tech Bridge SA (HTBridge)
 
PDF
In-Memory Fuzzing with Java (Publication from High-Tech Bridge)
High-Tech Bridge SA (HTBridge)
 
PDF
CVE 2012-1889 Microsoft XML core services uninitialized memory vulnerability
High-Tech Bridge SA (HTBridge)
 
PDF
Cybercrime in nowadays businesses - A real case study of targeted attack
High-Tech Bridge SA (HTBridge)
 
PDF
Spying Internet Explorer 8.0
High-Tech Bridge SA (HTBridge)
 
PDF
Frontal Attacks - From basic compromise to Advanced Persistent Threat
High-Tech Bridge SA (HTBridge)
 
PDF
Inline Hooking in Windows
High-Tech Bridge SA (HTBridge)
 
PDF
Userland Hooking in Windows
High-Tech Bridge SA (HTBridge)
 
PDF
Defeating Data Execution Prevention and ASLR in Windows
High-Tech Bridge SA (HTBridge)
 
PDF
Fake malware and virus scanners
High-Tech Bridge SA (HTBridge)
 
PDF
Become fully aware of the potential dangers of ActiveX attacks
High-Tech Bridge SA (HTBridge)
 
PDF
Client-side threats - Anatomy of Reverse Trojan attacks
High-Tech Bridge SA (HTBridge)
 
Welcome in the World Wild Web
High-Tech Bridge SA (HTBridge)
 
Fuzzing: An introduction to Sulley Framework
High-Tech Bridge SA (HTBridge)
 
Novell GroupWise Multiple Untrusted Pointer Dereferences Exploitation
High-Tech Bridge SA (HTBridge)
 
Manipulating Memory for Fun & Profit
High-Tech Bridge SA (HTBridge)
 
In-Memory Fuzzing with Java (Publication from High-Tech Bridge)
High-Tech Bridge SA (HTBridge)
 
CVE 2012-1889 Microsoft XML core services uninitialized memory vulnerability
High-Tech Bridge SA (HTBridge)
 
Cybercrime in nowadays businesses - A real case study of targeted attack
High-Tech Bridge SA (HTBridge)
 
Spying Internet Explorer 8.0
High-Tech Bridge SA (HTBridge)
 
Frontal Attacks - From basic compromise to Advanced Persistent Threat
High-Tech Bridge SA (HTBridge)
 
Inline Hooking in Windows
High-Tech Bridge SA (HTBridge)
 
Userland Hooking in Windows
High-Tech Bridge SA (HTBridge)
 
Defeating Data Execution Prevention and ASLR in Windows
High-Tech Bridge SA (HTBridge)
 
Fake malware and virus scanners
High-Tech Bridge SA (HTBridge)
 
Become fully aware of the potential dangers of ActiveX attacks
High-Tech Bridge SA (HTBridge)
 
Client-side threats - Anatomy of Reverse Trojan attacks
High-Tech Bridge SA (HTBridge)
 

Recently uploaded (20)

PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 

Structured Exception Handler Exploitation

  • 1. Structured Exception Handler EXPLOITATION http://www.htbridge.ch/ Brian Mariani
  • 2. What is an exception • An exception is an event that occurs during the execution of a program • Requires the execution of code outside the normal flow of control
  • 3. Structured Exception Handling • Blocks of code are encapsulated, with each block having one or more associated handlers. • Each handler specifies some form of filter condition on the type of exception it handles • When an exception is raised by code in a protected block, the set of corresponding handlers is searched in order, and the first one with a matchingfilter condition is executed • A single method can have multiple structured exception handling blocks, and the blocks can also be nested within each other
  • 4. Exception pointers structure (1) • Contains an exception record with a machine-independent description of an exception • A context record with a machine-dependent description of the processor context at the time of the exception
  • 5. Exception pointers structure (2) • A pointer to the next exception registration structure • A pointer to the address of the actual code of the exception handler
  • 6. Thread information block • The Thread Information Block (TIB) is a data structure in Win32 that stores information about the currently running thread • At the position FS:[0x00] we found the current exception handler
  • 7. Dumping SEH chain in Inmunity debugger
  • 8. How SEH works? • The exception handlers are linked to each other • They form a linked list chain on the stack, and sit relatively close to the bottom of the stack • When an exception occurs, Windows retrieves the head of the SEH chain walks through the list and tries to find the suitable handler to close the application properly
  • 9. Abusing the SEH • When exploiting an SEH overwrite and attacker clobbers the handler attribute of the EXCEPTION_REGISTRATION_RECORD with the address of an instruction sequence similar to POP POP RET • When the exception occurs, this causes Windows to pass execution to this address, which subsequently returns to the location on the stack of the Next attribute of the EXCEPTION_REGISTRATION_RECORD • The Next attribute is also controlled by the attacker, but if we recall the stack layout from earlier, the Next attribute is below the Handler attribute • This limits the attacker to 4 bytes before running into the Handler address he previously supplied to originally obtain code execution • However, by overwriting the Next attribute with the instructions that jump the Handler attribute, the attacker typically has enough room for arbitrary shellcode, and this is exactly what happens
  • 10. Overwriting the Next SEH record and SE handler • To check a chain of exception handlers before and after an overflow we can use WinDbg !exchain command • At the left we can see the SEH chain and the stack before the overflow occurs • At the right we can see the pointers were successfully overwritten
  • 11. What are we overwriting? • When we performs a regular stack based buffer overflow, we overwrite the return address of the Extended Instruction Pointer (EIP) • When doing a SEH overflow, we will continue overwriting the stack after overwriting overflow EIP, so we can overwrite the default exception handler as well EIP
  • 12. Viewing the SEH before the overflow • Before the overflow occurs we can see the stack and the SEH chain. • The SEH chain starts from 0x015fd044 down to 0x015fffdc which indicates the end of the SEH chain • Directly below 0x015fffe0, we see 0x7c839ad8, which is the address of the default SE 0x015fffe0 0x7c839ad8 handler for this application. This address sits in the address space of kernel32.dll
  • 13. Viewing the SEH after the overflow • Dumping the TIB confirms that the SEH was overwritten • Code execution is successfully passed to the injected address 0x61616161 • Addresses 0x015fd044 and 0x015fd048 which were the Next SEH record and SE handler are now controlled. EIP point to 0x61616161, so we can control the flow of the program Pointer to the Next record and SEH handler was overwritten TIB dumping let us know the SEH chain was sucessfully overwritten
  • 14. See an exception analysis • The command !analyze –v in Windbg give us more details about the triggering of the exception
  • 15. How SEH base exploit works • When the exception is triggered the program flow go to the SE Handler • All we need is just put some code to jump to our payload • Faking a second exception makes the application goes to the next SEH pointer • As the Next SEH pointer is before the SE handler we can overwrite the Next SEH • Since the shellcode sits after the Handler, we can trick the SE Handler to execute POP POP RET instructions so the address to the Next SEH will be placed in EIP, therefore executing the code in Next SEH • The code will basically jump over some bytes and execute the shellcode
  • 16. Exploiting the application • We will exploit a vulnerability in Gogago Youtube Downloader Video ActiveX www.gogago.net/download/youtube_video_downloader_setup.exe • A buffer overflow is triggered after injecting more that 2230 bytes in the Download() function • This vulnerability could be exploited using a basic RET CALL technique • We will use SEH based exploitation which is also functioning in this particular case
  • 17. Creating the POC • We craft an html page calling the method Download using the CLASSID • When we overflow the method with 2250 bytes with junk data we trigger an exception
  • 18. Overwriting Next pointer and SE handler • To successfully overwrite the Next Pointer and SE Handler we must calculate the exact number of bytes to inject • You can use tools as pattern_create and pattern_search from Metasploit, or you can do it manually injecting buffers with different patterns
  • 19. Finding POP POP RET instructions • Finding opcodes it’s not a difficult task you can use findjump or IDA • In this tutorial we will use WinDBG • We launch our prove of concept and we attach to Internet Explorer. After the overflow Explorer occurs we search the base memory address of the Gogago module MDIEex.dll • Finally we can search for the opcodes using the s command
  • 20. Building the exploit • After calculating the number of bytes to overwrite the Next pointer and SE handler we inject 4 bytes of code to jump to our shellcode this will replace the old SE handler • Following the SE handler we inject the POP POP RET opcodes from the same module of the exploited application • Finally we inject our payload
  • 21. Executing the exploit (1) • We place a breakpoint before entering in the vulnerable method. The SE handler that will be overwritten sits at 0x15fa79c, and corresponds to the jscript.dll module 0x15fa79c
  • 22. Executing the exploit (2) • After the overflow occurs we successfully overwrites the old jscript SE handler later code execution will be redirected to our POP POP RET instructions
  • 23. Redirect code execution • The code is redirected to our fake SE Handler address
  • 24. Jumping to our payload • Jumping over 6 bytes to reach ou shellcode starting at address 0x015fa7a4
  • 25. Shellcode execution • Time to dance 
  • 27. References • http://msdn.microsoft.com/en-us/library/ms680663%28v=VS.85%29.aspx • http://msdn.microsoft.com/en-us/library/c68xfk56%28v=vs.71%29.aspx • http://en.wikipedia.org/wiki/Win32_Thread_Information_Block • http://corelan.be