SlideShare a Scribd company logo
CNIT 127: Exploit Development

Ch 8: Windows Overflows

Part 2
Topics
• Stack Protection
• Heap-Based Buffer Overflows
• Other Overflows
Stack Protection
Windows Stack Protections
• Microsoft Visual C++ .NET provides
– /GS compiler flag is on by default
– Tells compiler to place security cookies on
the stack to guard the saved return address
– Equivalent of a canary
– 4-byte value (dword) placed on the stack after
a procedure call
– Checked before procedure return
– Protects saved return address and EBP
CNIT 127: Ch 8: Windows overflows (Part 2)
How is the Cookie Generated?
• When a process starts, Windows combines
these values with XOR
– DateTime (a 64-bit integer counting time
intervals of 100 nanoseconds)
– Process ID
– Thread ID
– TickCount (number of milliseconds since the
system started up)
– Performance Counter (number of CPU cycles)
Predicting the Cookie
• If an attacker can run a process on the
target to get system time values
• Some bits of the cookie can be predicted
Effectively 17 bits of Randomness
How Good is 17 Bits?
• 2^17 = 131,072
• So an attacker would have to run an
attack 100,000 times or so to win by
guessing the cookie
Prologue Modification
• __security_cookie value placed in the
stack at a carefully calculated position
• To protect the EBP and Return value
– From link Ch 8m
Epilogue Modification
• Epilogue to a function now includes these
instructions
– From link Ch 8m
__security_check_cookie
• Current cookie value is in ecx
• Compared to authoritative value stored in
the .data section of the image file of the
procedure
• If the check fails, it calls a security handler,
using a pointer stored in the .data section
Parameter Order
• Before Windows Server 2003, local
variables were placed on the stack in the
order of their declaration in the C++ source
code
• Now all arrays are moved to the bottom of
the list, closest to the saved return address
• This prevents buffer overflows in the
arrays from changing the non-array
variables
CNIT 127: Ch 8: Windows overflows (Part 2)
Overwriting Parameters
Overwriting Parameters
• We've changed the cookie, but if the
parameters are used in a write operation
before the function returns, we could
– Overwrite the authoritative cookie value in
the .data section, so the cookie check passes
– Overwrite the handler pointer to the security
handler, and let the cookie check fail
• Handler could point to injected code
• Or set handler to zero and overwrite the default
exception handler value
Heap-Based Buffer Overflows
Purpose of the Heap
• Consider a Web server
• HTTP requests vary in length
• May vary from 20 to 20,000 bytes or
longer (in principle)
• Once processed, the request can be
discarded, freeing memory for re-use
• For efficiency, such data is best stored on
the heap
The Process Heap
• Every process running on Win32 has a
process heap
• The C function GetProcessHeap() returns a
handle to the process heap
• A pointer to the process heap is also
stored in the Process Environment Block
The Process Heap
• This code returns that pointer in eax
• Many of the underlying functions of the
Windows API use this default process heap
Dynamic Heaps
• A process can create as many dynamic
heaps as required
• All inside the default process heap
• Created with the HeapCreate() function
• From link Ch 8o
Working with the Heap
• Application uses HeapAllocate() to borrow
a chunk of memory on the heap
– Legacy functions left from Win16 are
LocalAlloc() & GlobalAlloc(), but they do the
same thing—there's no difference in Win32
• When the application is done with the
memory, if calls HeapFree()
– Or LocalFree() or GlobalFree()
How the Heap Works
• The stack grows downwards, towards
address 0x00000000
• The heap grows upwards
• Heap starts with 128 LIST_ENTRY
structures that keep track of free blocks
Vulnerable Heap Operations
• When a chunk is freed, forward and
backward pointers must be updated
• This enables us to control a write
operation, to write to arbitrary RAM
locations
– Image from mathyvanhoef.com, link Ch 5b
Details
• There is a lot more to it, involving these
structures
– Segment list
– Virtual Allocation list
– Free list
– Lookaside list
• For details, see link Ch8o
Exploiting Heap-Based Overflows:

Three Techniques
• Overwrite the pointer to the exception
handler
• Overwrite the pointer to the Unhandled
Exception Filter
• Overwrite a pointer in the PEB
Overwrite a Pointer in the PEB
• RtlEnterCriticalSection, called by
RtlAcquirePebLock() and RtlReleasePebLock()
• Called whenever a process exits with
ExitProcess()
• PEB location is fixed for all versions of Win
NT
• Your code should restore this pointer, and
you may also need to repair the heap
Win 2003 Server
• Does not use these pointers in the PEB
• But there are Ldr* functions that call
pointers we can control
– Including LdrUnloadDll()
Vectored Exception Handling
• Introduced with Windows XP
• Traditional frame-based exception
handling stores exception registration
records on the stack
• Vectored exception handling stores
information about handlers on the heap
• A heap overflow can change them
Overwrite a Pointer to the Unhandled
Exception Filter
• First proposed at Blackhat Amsterdam
(2001)
• An application can set this value using
SetUnhandledExceptionFilter()
– Disassemble that function to find the pointer
Repairing the Heap
• The overflow corrupts the heap
• Shellcode will probably cause an access
violation
• Simplest repair process is to just make the
heap look like a fresh, empty heap
– With the one block we are using on it
Restore the Exception Handler you
Abused
• Otherwise, you could create an endless
loop
• If your shellcode causes an exception
COM Objects and the Heap
• Component Object Model (COM) Objects
– An object that can be created when needed
by another program
– It has methods that can be called to perform
a task
– It also has attributes (stored data)
• COM objects are created on the heap
Vtable in Heap
• All COM classes
have one or more
interfaces, which
are used to connect
them to a program
– Figure from link Ch
8p
COM Objects Contain Data
• If the programmer doesn't check, these
data fields could be overflowed, into the
next object's vtable
– Image from link Ch 8q
• Vunerable COM objects are often not fixed
• Just added to the "killbit" list
• Which can be circumvented
• From link Ch 8qq; Image on next slide from link
Ch 8r
CNIT 127: Ch 8: Windows overflows (Part 2)
Other Overflows
Overflows in the .data Section
• If a buffer is placed before function pointers in
the .data section
• Overflowing the buffer can change the pointers
TEB/PEB Overflows
• In principle, buffers in the TEB used for
converting ASCII to Unicode could be
overflowed
• Changing pointers
• There are no public examples of this type
of exploit

More Related Content

What's hot (20)

PDF
CNIT 127: Ch 4: Introduction to format string bugs
Sam Bowne
 
PDF
CNIT 127 Ch 8: Windows overflows (Part 1)
Sam Bowne
 
PDF
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
Sam Bowne
 
PDF
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
PDF
CNIT 127: Ch 3: Shellcode
Sam Bowne
 
PDF
CNIT 127: Ch 2: Stack Overflows in Linux
Sam Bowne
 
PDF
127 Ch 2: Stack overflows on Linux
Sam Bowne
 
PDF
CNIT 127: 3: Shellcode
Sam Bowne
 
PDF
CNIT 127 Ch Ch 1: Before you Begin
Sam Bowne
 
PDF
CNIT 127 Ch 2: Stack overflows on Linux
Sam Bowne
 
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
Sam Bowne
 
PDF
CNIT 127 Ch 5: Introduction to heap overflows
Sam Bowne
 
PDF
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
PDF
CNIT 127: Ch 2: Stack overflows on Linux
Sam Bowne
 
PDF
CNIT 127 Ch 3: Shellcode
Sam Bowne
 
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
Sam Bowne
 
PDF
CNIT 127 Ch 3: Shellcode
Sam Bowne
 
PDF
CNIT 127 Ch 6: The Wild World of Windows
Sam Bowne
 
PDF
CNIT 127 Ch 4: Introduction to format string bugs
Sam Bowne
 
PDF
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
Sam Bowne
 
CNIT 127: Ch 4: Introduction to format string bugs
Sam Bowne
 
CNIT 127 Ch 8: Windows overflows (Part 1)
Sam Bowne
 
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
Sam Bowne
 
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
CNIT 127: Ch 3: Shellcode
Sam Bowne
 
CNIT 127: Ch 2: Stack Overflows in Linux
Sam Bowne
 
127 Ch 2: Stack overflows on Linux
Sam Bowne
 
CNIT 127: 3: Shellcode
Sam Bowne
 
CNIT 127 Ch Ch 1: Before you Begin
Sam Bowne
 
CNIT 127 Ch 2: Stack overflows on Linux
Sam Bowne
 
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
Sam Bowne
 
CNIT 127 Ch 5: Introduction to heap overflows
Sam Bowne
 
CNIT 127 14: Protection Mechanisms
Sam Bowne
 
CNIT 127: Ch 2: Stack overflows on Linux
Sam Bowne
 
CNIT 127 Ch 3: Shellcode
Sam Bowne
 
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
Sam Bowne
 
CNIT 127 Ch 3: Shellcode
Sam Bowne
 
CNIT 127 Ch 6: The Wild World of Windows
Sam Bowne
 
CNIT 127 Ch 4: Introduction to format string bugs
Sam Bowne
 
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
Sam Bowne
 

Viewers also liked (20)

PDF
CISSP Prep: Ch 3. Asset Security
Sam Bowne
 
PDF
CISSP Prep: Ch 2. Security and Risk Management I (part 2)
Sam Bowne
 
PDF
CISSP Prep: Ch 4. Security Engineering (Part 2)
Sam Bowne
 
PDF
CISSP Prep: Ch 6. Identity and Access Management
Sam Bowne
 
PDF
CISSP Prep: Ch 4. Security Engineering (Part 1)
Sam Bowne
 
PDF
CISSP Prep: Ch 5. Communication and Network Security (Part 1)
Sam Bowne
 
PDF
CISSP Prep: Ch 1: Security Governance Through Principles and Policies
Sam Bowne
 
PDF
CNIT 128 Ch 6: Mobile services and mobile Web (part 2: SAML to end)
Sam Bowne
 
PDF
CISSP Prep: Ch 7. Security Assessment and Testing
Sam Bowne
 
PDF
Ch 10: Hacking Web Servers
Sam Bowne
 
PDF
CISSP Prep: Ch 9. Software Development Security
Sam Bowne
 
PDF
CISSP Prep: Ch 8. Security Operations
Sam Bowne
 
PDF
CISSP Prep: Ch 5. Communication and Network Security (Part 2)
Sam Bowne
 
PDF
CNIT 126 5: IDA Pro
Sam Bowne
 
PDF
CNIT 126 8: Debugging
Sam Bowne
 
PDF
Ch 12: Cryptography
Sam Bowne
 
PDF
Ch 13: Network Protection Systems
Sam Bowne
 
PDF
CNIT 126 9: OllyDbg
Sam Bowne
 
PDF
CNIT 123: Ch 3: Network and Computer Attacks
Sam Bowne
 
PPTX
Reversing & Malware Analysis Training Part 9 - Advanced Malware Analysis
securityxploded
 
CISSP Prep: Ch 3. Asset Security
Sam Bowne
 
CISSP Prep: Ch 2. Security and Risk Management I (part 2)
Sam Bowne
 
CISSP Prep: Ch 4. Security Engineering (Part 2)
Sam Bowne
 
CISSP Prep: Ch 6. Identity and Access Management
Sam Bowne
 
CISSP Prep: Ch 4. Security Engineering (Part 1)
Sam Bowne
 
CISSP Prep: Ch 5. Communication and Network Security (Part 1)
Sam Bowne
 
CISSP Prep: Ch 1: Security Governance Through Principles and Policies
Sam Bowne
 
CNIT 128 Ch 6: Mobile services and mobile Web (part 2: SAML to end)
Sam Bowne
 
CISSP Prep: Ch 7. Security Assessment and Testing
Sam Bowne
 
Ch 10: Hacking Web Servers
Sam Bowne
 
CISSP Prep: Ch 9. Software Development Security
Sam Bowne
 
CISSP Prep: Ch 8. Security Operations
Sam Bowne
 
CISSP Prep: Ch 5. Communication and Network Security (Part 2)
Sam Bowne
 
CNIT 126 5: IDA Pro
Sam Bowne
 
CNIT 126 8: Debugging
Sam Bowne
 
Ch 12: Cryptography
Sam Bowne
 
Ch 13: Network Protection Systems
Sam Bowne
 
CNIT 126 9: OllyDbg
Sam Bowne
 
CNIT 123: Ch 3: Network and Computer Attacks
Sam Bowne
 
Reversing & Malware Analysis Training Part 9 - Advanced Malware Analysis
securityxploded
 
Ad

Similar to CNIT 127: Ch 8: Windows overflows (Part 2) (20)

PDF
CNIT 127: 8: Windows overflows (Part 2)
Sam Bowne
 
PPT
exploiting heap overflows
primelude
 
PDF
Ch 5: Introduction to heap overflows
Sam Bowne
 
PDF
CNIT 127 Ch 5: Introduction to heap overflows
Sam Bowne
 
PDF
Heap overflows for humans – 101
Craft Symbol
 
PDF
Advanced Windows Exploitation
UTD Computer Security Group
 
PPTX
Buffer overflow – Smashing The Stack
Tomer Zait
 
PPT
Third Generation Exploitation Black .ppt
KarimAhmed722436
 
PDF
Low Level Exploits
hughpearse
 
PDF
Marat-Slides
Marat Vyshegorodtsev
 
PPTX
Stack-Based Buffer Overflows
Daniel Tumser
 
PDF
Buffer Overflow - Smashing the Stack
ironSource
 
ODP
Debugging With Id
guest215c4e
 
PPTX
How Safe is your Link ?
Peter Hlavaty
 
PPTX
A survey on Heap Exploitation
Alireza Karimi
 
PDF
Ceh v5 module 20 buffer overflow
Vi Tính Hoàng Nam
 
PDF
Heap Base Exploitation
UTD Computer Security Group
 
PDF
[ENG] Hacktivity 2013 - Alice in eXploitland
Zoltan Balazs
 
PDF
Software Security
Roman Oliynykov
 
CNIT 127: 8: Windows overflows (Part 2)
Sam Bowne
 
exploiting heap overflows
primelude
 
Ch 5: Introduction to heap overflows
Sam Bowne
 
CNIT 127 Ch 5: Introduction to heap overflows
Sam Bowne
 
Heap overflows for humans – 101
Craft Symbol
 
Advanced Windows Exploitation
UTD Computer Security Group
 
Buffer overflow – Smashing The Stack
Tomer Zait
 
Third Generation Exploitation Black .ppt
KarimAhmed722436
 
Low Level Exploits
hughpearse
 
Marat-Slides
Marat Vyshegorodtsev
 
Stack-Based Buffer Overflows
Daniel Tumser
 
Buffer Overflow - Smashing the Stack
ironSource
 
Debugging With Id
guest215c4e
 
How Safe is your Link ?
Peter Hlavaty
 
A survey on Heap Exploitation
Alireza Karimi
 
Ceh v5 module 20 buffer overflow
Vi Tính Hoàng Nam
 
Heap Base Exploitation
UTD Computer Security Group
 
[ENG] Hacktivity 2013 - Alice in eXploitland
Zoltan Balazs
 
Software Security
Roman Oliynykov
 
Ad

More from Sam Bowne (20)

PDF
Introduction to the Class & CISSP Certification
Sam Bowne
 
PDF
Cyberwar
Sam Bowne
 
PDF
3: DNS vulnerabilities
Sam Bowne
 
PDF
8. Software Development Security
Sam Bowne
 
PDF
4 Mapping the Application
Sam Bowne
 
PDF
3. Attacking iOS Applications (Part 2)
Sam Bowne
 
PDF
12 Elliptic Curves
Sam Bowne
 
PDF
11. Diffie-Hellman
Sam Bowne
 
PDF
2a Analyzing iOS Apps Part 1
Sam Bowne
 
PDF
9 Writing Secure Android Applications
Sam Bowne
 
PDF
12 Investigating Windows Systems (Part 2 of 3)
Sam Bowne
 
PDF
10 RSA
Sam Bowne
 
PDF
12 Investigating Windows Systems (Part 1 of 3
Sam Bowne
 
PDF
9. Hard Problems
Sam Bowne
 
PDF
8 Android Implementation Issues (Part 1)
Sam Bowne
 
PDF
11 Analysis Methodology
Sam Bowne
 
PDF
8. Authenticated Encryption
Sam Bowne
 
PDF
7. Attacking Android Applications (Part 2)
Sam Bowne
 
PDF
7. Attacking Android Applications (Part 1)
Sam Bowne
 
PDF
5. Stream Ciphers
Sam Bowne
 
Introduction to the Class & CISSP Certification
Sam Bowne
 
Cyberwar
Sam Bowne
 
3: DNS vulnerabilities
Sam Bowne
 
8. Software Development Security
Sam Bowne
 
4 Mapping the Application
Sam Bowne
 
3. Attacking iOS Applications (Part 2)
Sam Bowne
 
12 Elliptic Curves
Sam Bowne
 
11. Diffie-Hellman
Sam Bowne
 
2a Analyzing iOS Apps Part 1
Sam Bowne
 
9 Writing Secure Android Applications
Sam Bowne
 
12 Investigating Windows Systems (Part 2 of 3)
Sam Bowne
 
10 RSA
Sam Bowne
 
12 Investigating Windows Systems (Part 1 of 3
Sam Bowne
 
9. Hard Problems
Sam Bowne
 
8 Android Implementation Issues (Part 1)
Sam Bowne
 
11 Analysis Methodology
Sam Bowne
 
8. Authenticated Encryption
Sam Bowne
 
7. Attacking Android Applications (Part 2)
Sam Bowne
 
7. Attacking Android Applications (Part 1)
Sam Bowne
 
5. Stream Ciphers
Sam Bowne
 

Recently uploaded (20)

PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 

CNIT 127: Ch 8: Windows overflows (Part 2)

  • 1. CNIT 127: Exploit Development
 Ch 8: Windows Overflows
 Part 2
  • 2. Topics • Stack Protection • Heap-Based Buffer Overflows • Other Overflows
  • 4. Windows Stack Protections • Microsoft Visual C++ .NET provides – /GS compiler flag is on by default – Tells compiler to place security cookies on the stack to guard the saved return address – Equivalent of a canary – 4-byte value (dword) placed on the stack after a procedure call – Checked before procedure return – Protects saved return address and EBP
  • 6. How is the Cookie Generated? • When a process starts, Windows combines these values with XOR – DateTime (a 64-bit integer counting time intervals of 100 nanoseconds) – Process ID – Thread ID – TickCount (number of milliseconds since the system started up) – Performance Counter (number of CPU cycles)
  • 7. Predicting the Cookie • If an attacker can run a process on the target to get system time values • Some bits of the cookie can be predicted
  • 8. Effectively 17 bits of Randomness
  • 9. How Good is 17 Bits? • 2^17 = 131,072 • So an attacker would have to run an attack 100,000 times or so to win by guessing the cookie
  • 10. Prologue Modification • __security_cookie value placed in the stack at a carefully calculated position • To protect the EBP and Return value – From link Ch 8m
  • 11. Epilogue Modification • Epilogue to a function now includes these instructions – From link Ch 8m
  • 12. __security_check_cookie • Current cookie value is in ecx • Compared to authoritative value stored in the .data section of the image file of the procedure • If the check fails, it calls a security handler, using a pointer stored in the .data section
  • 13. Parameter Order • Before Windows Server 2003, local variables were placed on the stack in the order of their declaration in the C++ source code • Now all arrays are moved to the bottom of the list, closest to the saved return address • This prevents buffer overflows in the arrays from changing the non-array variables
  • 16. Overwriting Parameters • We've changed the cookie, but if the parameters are used in a write operation before the function returns, we could – Overwrite the authoritative cookie value in the .data section, so the cookie check passes – Overwrite the handler pointer to the security handler, and let the cookie check fail • Handler could point to injected code • Or set handler to zero and overwrite the default exception handler value
  • 18. Purpose of the Heap • Consider a Web server • HTTP requests vary in length • May vary from 20 to 20,000 bytes or longer (in principle) • Once processed, the request can be discarded, freeing memory for re-use • For efficiency, such data is best stored on the heap
  • 19. The Process Heap • Every process running on Win32 has a process heap • The C function GetProcessHeap() returns a handle to the process heap • A pointer to the process heap is also stored in the Process Environment Block
  • 20. The Process Heap • This code returns that pointer in eax • Many of the underlying functions of the Windows API use this default process heap
  • 21. Dynamic Heaps • A process can create as many dynamic heaps as required • All inside the default process heap • Created with the HeapCreate() function
  • 22. • From link Ch 8o
  • 23. Working with the Heap • Application uses HeapAllocate() to borrow a chunk of memory on the heap – Legacy functions left from Win16 are LocalAlloc() & GlobalAlloc(), but they do the same thing—there's no difference in Win32 • When the application is done with the memory, if calls HeapFree() – Or LocalFree() or GlobalFree()
  • 24. How the Heap Works • The stack grows downwards, towards address 0x00000000 • The heap grows upwards • Heap starts with 128 LIST_ENTRY structures that keep track of free blocks
  • 25. Vulnerable Heap Operations • When a chunk is freed, forward and backward pointers must be updated • This enables us to control a write operation, to write to arbitrary RAM locations – Image from mathyvanhoef.com, link Ch 5b
  • 26. Details • There is a lot more to it, involving these structures – Segment list – Virtual Allocation list – Free list – Lookaside list • For details, see link Ch8o
  • 27. Exploiting Heap-Based Overflows:
 Three Techniques • Overwrite the pointer to the exception handler • Overwrite the pointer to the Unhandled Exception Filter • Overwrite a pointer in the PEB
  • 28. Overwrite a Pointer in the PEB • RtlEnterCriticalSection, called by RtlAcquirePebLock() and RtlReleasePebLock() • Called whenever a process exits with ExitProcess() • PEB location is fixed for all versions of Win NT • Your code should restore this pointer, and you may also need to repair the heap
  • 29. Win 2003 Server • Does not use these pointers in the PEB • But there are Ldr* functions that call pointers we can control – Including LdrUnloadDll()
  • 30. Vectored Exception Handling • Introduced with Windows XP • Traditional frame-based exception handling stores exception registration records on the stack • Vectored exception handling stores information about handlers on the heap • A heap overflow can change them
  • 31. Overwrite a Pointer to the Unhandled Exception Filter • First proposed at Blackhat Amsterdam (2001) • An application can set this value using SetUnhandledExceptionFilter() – Disassemble that function to find the pointer
  • 32. Repairing the Heap • The overflow corrupts the heap • Shellcode will probably cause an access violation • Simplest repair process is to just make the heap look like a fresh, empty heap – With the one block we are using on it
  • 33. Restore the Exception Handler you Abused • Otherwise, you could create an endless loop • If your shellcode causes an exception
  • 34. COM Objects and the Heap • Component Object Model (COM) Objects – An object that can be created when needed by another program – It has methods that can be called to perform a task – It also has attributes (stored data) • COM objects are created on the heap
  • 35. Vtable in Heap • All COM classes have one or more interfaces, which are used to connect them to a program – Figure from link Ch 8p
  • 36. COM Objects Contain Data • If the programmer doesn't check, these data fields could be overflowed, into the next object's vtable – Image from link Ch 8q
  • 37. • Vunerable COM objects are often not fixed • Just added to the "killbit" list • Which can be circumvented • From link Ch 8qq; Image on next slide from link Ch 8r
  • 40. Overflows in the .data Section • If a buffer is placed before function pointers in the .data section • Overflowing the buffer can change the pointers
  • 41. TEB/PEB Overflows • In principle, buffers in the TEB used for converting ASCII to Unicode could be overflowed • Changing pointers • There are no public examples of this type of exploit