3 Buffer Overflow
3 Buffer Overflow
Last Name
Last Name O T A V A
Try to copy more data than the buffer can handle, using strcpy.
Program Crashes.
This means that whatever was in the memory location right after
the buffer, is overwritten with our input.
As you can see in this stack representation, this data includes the
EBP, the EIP and all the other bytes related to the previous stack
frame.
..... .....
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. This is where it is important to
know memory addresses of certain registers.
This means that we have overwritten the EIP with our input,
causing the overflow and then crashing the program. The EIP,
instruction pointer, tells the program what to run next, but as a
result of all of our A’s, that address value is A.
This command will invoke the disassembler and will give us the
ASM version of the executable.
00401548 <__Z13good_passwordv>:
through the file, and 401548: 55 push ebp
401549: 89 e5 mov ebp,esp
you will find 40154b:
40154e:
83 ec 18
c7 04 24 00 80 48 00
sub
mov
esp,0x18
DWORD PTR [esp],0x488000
Why not use all A’s? The reason why the final three characters are
different is because by doing that, when we view the exception
error, it will tell us what character we errored at.
Therefore, the helper program will fill the first 22 characters with
some junk bytes and then append the address of
good_function (00401548).
print path
os.system(command)
This will not only help you understand how the application works
but also how our input overwrites the return address in the stack,
causing the crash of the application.
https://www.peach.tech/ https://github.com/orgcandman/Simple-Fuzzer
https://github.com/OpenRCE/sulley https://packetstormsecurity.com/files/39626/FileFuzz.zip.html
Penetration Testing Professional 5.0 – Caendra Inc. © 2018
Let's now see how to find buffer overflows in the binary programs
process. We will consider a very simple program in order to give
you a full understanding of what is going on at almost every stage.
int main()
{
int cookie=0;
char buffer[4];
printf("cookie = %08X\n",cookie);
gets(buffer);
printf("cookie = %08X\n",cookie);
if(cookie == 0x31323334 )
{
printf("you win!\n");
}
else
{
printf("try again!\n");
}
From the stack frame that you should have created on your own,
you can see that: all local variables can be accessed using:
• [EBP – x] → local variables
• [EBP + x] → function parameters
We can say this because, the function gets never verifies the
length of the data (also note stack space is limited) and in this case,
user has full control over the data. Therefore, it is susceptible to an
overflow.
You can also perform the above steps in Immunity Debugger to see
how things change in real time. Open cookie.exe from
Immunity Debugger and repeat the steps from above.
Basically, we need to fill the first 22 bytes (local vars + EBP), with
junk data (NOP’s), rewrite the EIP and then insert the shell code.
Let us keep doing this until we reach the exact number of junk
bytes. As you can imagine, this process may take a while. But, time
is precious, that's why we use scripts and tools!
You are not limited to just these scripts; a simple web search will
allow you to find many other implementations of these same files
(C, Python, etc.).
https://github.com/lattera/metasploit/blob/master/tools/pattern_create.rb
https://github.com/lattera/metasploit/blob/master/tools/pattern_offset.rb
Penetration Testing Professional 5.0 – Caendra Inc. © 2018
The purpose of these scripts are very simple: pattern_create
creates a payload that is as long as the number we specify. Once
the tool creates the pattern, we replace our payload made by A
characters with this new pattern.
We will have to specify the value in the EIP register to the point
when the application crashes. Providing this number to the second
file, pattern_offset will give us the exact number of junk
bytes that we need to reach the EIP.
As we can see from the screenshot, it returns 22! This is the exact
offset that we manually calculated before.
We will talk about the Mona plugin later, but for now, let’s copy
the mona.py file into the PyCommand folder (inside the
Immunity Debugger installation folder) and see how we can use it
to calculate the offset from Immunity Debugger.
https://github.com/corelan/mona
In the following screenshot, we can see the value of EIP once the
application crashes. Once again it is 61413761.
We can safely jump to this line and back from the kernel32 to the
address in ESP (that holds the first line of our shell code).
Once we hit OK, the disassemble will take us to the first occurrence
of the pattern searched.
It is a very easy tool to use. We need to provide the target .dll file
we want to search and then the registry name, which in our case is
ESP.
https://github.com/corelan/mona
https://www.corelan.be/index.php/articles/
char command[2000];
strcat(command, junkbytes);
strcat(command, eip);
strcat(command, shellcode);
So, unless we reboot the machine, the address will still be the
same.
Therefore, after RET gets executed, ESP will point to the beginning
of our shellcode (\x90\x90\x90\x90).
As you can imagine, running calc.exe will not hurt the victim,
but as we will see later on, we can use different shellcodes that
may allow us to obtain complete control of the victim.
If you want to try it on your own, there will be a point where you
can stop reading and give it a try.
The first step is to create a payload that will help us to find the
correct offset that will overwrite the EIP. We already know how to
do this.
Once you’ve done that, keep reading to find out if you are correct.
!mona pc 1100
After running the command, let’s open the file pattern.txt created
by Mona and then copy the HEX version into the payload variable
of our script.
We can now stop the debugger and exit our Python script.
Once again, we will have to fill the first 989 bytes of the shellcode
with junk bytes and then add the JMP ESP address. Then, add the
shellcode we want to execute, which in our case will be
calc.exe.
First, we will show you how to identify the correct offset to use,
then the EIP address to overwrite and then we will exploit the
application.
ASLR is not enabled for all modules. This means that if a process
has ASLR enabled, there could be a dll (or another module) in the
address space that does not use it, making the process vulnerable
to ASLR bypass attack.
https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer
We are not going into the details of each technique since they
require very good experience in reverse engineering, exploit
writing and more.
https://www.corelan.be/
This is the easiest technique that one can use since the process is
very similar to the one we have seen so far.
The advantage of this technique is that the attacker can guess the
jump location with a low degree of accuracy and still successfully
exploit the program.
Here you can also find a good reference from FireEye about
bypassing ASLR.
https://www.fireeye.com/blog/threat-research/2013/10/aslr-bypass-apocalypse-in-lately-zero-day-exploits.html
Otherwise here are some good references that you can use to start
diving into bypassing ASLR+DEP:
• Universal-depaslr-bypass-with-msvcr71-dll-and-mona-py
• https://www.exploit-db.com/docs/english/17914-bypassing-
aslrdep.pdf
• Exploit-writing-tutorial-part-6-bypassing-stack-cookies-safeseh-
hw-dep-and-aslr
https://blogs.technet.microsoft.com/srd/2010/12/08/on-the-effectiveness-of-dep-and-aslr/ https://www.exploit-db.com/docs/17914.pdf
https://www.corelan.be/index.php/2011/07/03/universal-depaslr-bypass-with-msvcr71-dll-and-mona-py/ https://www.corelan.be/index.php/2009/09/21/exploit-writing-
tutorial-part-6-bypassing-stack-cookies-safeseh-hw-dep-and-aslr/
Penetration Testing Professional 5.0 – Caendra Inc. © 2018
Another defensive feature designed for Operating Systems is called
Data Execution Prevention (DEP). It is a hardware and software
defensive measure for preventing the execution of code from
pages of memory that are not explicitly marked as executable.
DEP helps prevent certain exploits where the attacker injects new
code on the stack.
Since the instructions are part of the stack, DEP does not
apply on them.
https://cseweb.ucsd.edu/~hovav/talks/blackhat08.html
The RET is important since it will allow the chain to work and keep
jumping to the next address after executing the small set of
instructions.
Here you can find a list of ROP gadgets from different libraries and
.dll files, while here you can find a good article that goes deeper in
ROP gadgets.
https://www.corelan.be/index.php/security/rop-gadgets/
https://www.corelan.be/index.php/2010/06/16/exploit-writing-tutorial-part-10-chaining-dep-with-rop-the-rubikstm-cube/#buildingblocks
Penetration Testing Professional 5.0 – Caendra Inc. © 2018
In order to avoid the exploit of such techniques, ASLR was
introduced. By making kernel API’s load at random addresses,
bypassing DEP becomes hard.
The term canary comes from the canary in a coal mine, and its
purpose is to modify almost all the function’s prologue and
epilogue instructions in order to place a small random integer
value (canary) right before the return instruction, and detect if a
buffer overflow occurs.
https://en.wiktionary.org/wiki/canary_in_a_coal_mine
When the function returns, the value is checked to make sure that
it was not changed. If so, it means that a stack buffer overflow
occurred.
You can read more about it SafeSEH here, and here you can find a
very good article on how to bypass stack canary.
https://msdn.microsoft.com/en-us/library/9a89h429.aspx
https://www.corelan.be/index.php/2009/09/21/exploit-writing-tutorial-part-6-bypassing-stack-cookies-safeseh-hw-dep-and-aslr/
Penetration Testing Professional 5.0 – Caendra Inc. © 2018
Penetration Testing Professional 5.0 – Caendra Inc. © 2018
Splint Cppcheck
http://www.splint.org/
http://cppcheck.sourceforge.net/
Sfuzz FileFuzz
https://github.com/orgcandman/Simple-Fuzzer https://packetstormsecurity.com/files/39626
/FileFuzz.zip.html
Mona EMET
https://github.com/corelan/mona https://support.microsoft.com/en-
us/help/2458544/the-enhanced-mitigation-
experience-toolkit