INE Exploit Development Buffer Overflows Course File (3)
INE Exploit Development Buffer Overflows Course File (3)
Overflows
Course Introduction
Alexis Ahmed
Senior Penetration Tester @HackerSploit
Offensive Security Instructor @INE
aahmed@ine.com
@HackerSploit
@alexisahmed
Course
Topic
+ Buffer Overflow Fundamentals
Overview
+ Finding Buffer Overflow
Vulnerabilities
+ Stack-Based Overflows
+ Windows SEH Overflow
+ Programming Proficiency: Students should have a strong
Prerequisites understanding of programming concepts and experience with at
least one programming language, such as C or C++, as exploit
development often involves writing and analyzing low-level
code.
+ Operating System Fundamentals: A solid understanding of
operating system concepts, particularly memory management,
process execution, and system architecture, is essential for
understanding how exploits work on different platforms.
+ Networking Fundamentals: Basic knowledge of networking
protocols (TCP/IP, UDP, HTTP, etc.) and network communication
is important for understanding how exploits can be delivered
over a network.
+ Assembly Language: An understanding of assembly language
programming is crucial for analyzing and crafting exploits, as
exploits often involve manipulating machine-level instructions
and memory.
Learning 1. Understand the Basics of Memory Exploitation: Students will grasp the
fundamentals of memory management in operating systems and how
Objectives: vulnerabilities in memory handling can lead to security exploits.
2. Identify and Exploit Buffer Overflow Vulnerabilities: Students will learn
to recognize buffer overflow vulnerabilities in software applications
and develop the skills to craft exploits to exploit them.
3. Analyze Stack-Based Overflow Vulnerabilities: Students will gain
proficiency in analyzing stack-based overflow vulnerabilities,
understanding their root causes, and exploiting them to gain control
over program execution.
4. Explore Structured Exception Handling (SEH) Overflows: Students will
delve into the intricacies of SEH-based overflow vulnerabilities in
Windows systems, mastering techniques to manipulate SEH
structures and execute arbitrary code.
Let’s Get Started!
Introduction To Buffer Overflows
Buffer Overflows
● In the previous course (System Security), we used the term “buffer
overflow,” numerous times. But what exactly does that mean?
● The term buffer is loosely used to refer to any area in memory where
more than one piece of data is stored.
● An overflow occurs when we try to fill more data than the buffer can
handle.
Task
● Try to copy more data than the buffer can handle, using strcpy.
Demo: Triggering A Buffer Overflow
Vulnerable Code Sample
Outcome
● We can see that argv[1] contains 35 “A” characters, while the buffer can
handle only 10. When the program runs, the exceeding data has to go
somewhere, and it will overwrite something in the memory: this is a
buffer overflow.
Code Outcome
● The Program Crashes.
● This means that whatever was in the memory location right after the
buffer, is overwritten with our input.
● In the modified code, the function will only copy 10 bytes of data from
argv[1], while the rest will be discarded.
● Now that we have understood what caused the buffer overflow, we can
take a closer look at what happens in the stack; this will help you
understand what happens at the process memory level.
Analyzing The Stack
Analyzing The Stack
The following is the new stack frame process
review:
● Push the function parameters
● Call the function
● Execute the prologue (which updates EBP
and ESP to create the new stack frame)
● Allocate local variable
Analyzing The Stack
● When the strcpy function gets executed, it starts copying our input into
the memory address allocated for buffer[10].
● Since there is not enough space, our input will be copied in the next
memory address and will continue to fill memory addresses until there is
no more input.
● While this is happening, it will also be overwriting all the data in those
memory locations and causing the overflow.
Analyzing The Stack
What is getting overwritten?
● As you can see in the stack representation, this data includes the EBP, the EIP and
all the other bytes related to the previous stack frame.
● Therefore, at the end of the strcpy instructions, our stack will look like the
following:
Analyzing The Stack
What can a pentester do with this?
● Since the EIP has been overwritten with AAAA, once the epilogue takes
place, the program will try to return to a completely wrong address.
● Remember that EIP points to the next instruction. An attacker can craft
the payload in the input of the program to get the control of the program
flow and return the function to a specific memory address location.
● Any application that uses unsafe operations, such as those below (there
are many others), might be vulnerable to buffer overflows.
○ strcpy
○ strcat
○ gets / fgets
○ vsprintf
○ printf
○ Memcpy
● All the interpreted languages such as C#, Visual Basic, .Net, JAVA, etc.
are safe from such vulnerabilities.
Finding Buffer Overflows
● Moreover, buffer overflows can be triggered by any of the following
buffer operations:
○ User input
○ Data loaded from a disk
○ Data from the network
2- Buffer Overflow
● A buffer overflow occurs when a program writes more data to a buffer (an
array of memory) than it can hold. In the case of a stack-based buffer
overflow, this buffer is typically located on the stack.
● The buffer overflow can occur due to insufficient bounds checking or
improper handling of user input.
How It Works
3- Overwriting Return Address
● When a function is called in a program, the return address, which points to the
instruction to be executed after the function completes, is pushed onto the stack.
● In a stack overflow attack, the attacker exploits the buffer overflow vulnerability to
overwrite the return address with a malicious address pointing to code they
control.
4 - Control Hijacking:
● By overwriting the return address with a malicious address, the attacker can
redirect the program's control flow to execute arbitrary code of their choice.
● This code is often referred to as the "shellcode" and is typically injected into the
program's memory by the attacker. Once the control flow is redirected to the
shellcode, the attacker can execute commands, escalate privileges, or perform
other malicious actions.
How It Works
5 -Exploitation
● With control of the program's execution flow and the ability to execute
arbitrary code, the attacker can exploit the vulnerability to achieve their
objectives, such as gaining unauthorized access to the system, stealing
sensitive information, or launching further attacks.
.
Structured Exception Handling (SEH)
Structured Exception Handling (SEH)