Windowsnt Buffer Overflow's from Start To Finish
I've read most of the articles on BO's (Buffer Overflows) on the net. I have found that they either for * NIX systems, or they are not detailed enough. The author's usually take some known vulnerable software and show you step by step how to exploit it. I am going to take a different approach. I am going to write an app that has a buffer overflow when reading data from a file. Then I will write an app that will create the file, that when read, will Cause The Exploit. I Will Also include an opcode finding Tool.
Tools needed:
Visual C 6.0
Windows NT
* The code and addresses I use are for Windows NT Workstation 4.0 SP6 .First lets write the app that will contain the bufferoverflow. We also want the app to be able to read in some type of file so we can actually exploit this from some typeof script. So in Visual C create a new console application, select "An Application that supports MFC" and click Finish.This does not necessarily have to be a MFC app, but I prefer to use some of the MFC classes. Obviously, I am A Windows Programmer. So Let's Add Some ExploITable Code Here. This is what it will look like:
CWINAPPPP;
Using namespace std;
Void overflow (char * buff);
INT_Tmain (int Argc, tchar * argv [], tchar * envp [])
{
INT nretcode = 0;
// Initialize MFC and Print and Error On Failure
IF (! Afxwininit (:: getModuleHandle (Null), NULL, :: getcommandline (), 0))
{
// Todo: Change Error Code To Suit your Needs
CERR << _T ("Fatal Error: MFC Initialization Faled") << ENDL;
NRETCODE = 1;
}
Else
{
CHAR BUFF [10];
Overflow (BUFF);
}
Return nretcode;
}
Void Overflow (Char * BUFF)
{
CFILE FILE;
CFILEEXCEPTION ER; if (! File.open (_t ("overflow.txt"), cfile :: moderead, & er))
{
Er.ReportError ();
Return;
}
INT x = file.getlength ();
File.read (BUFF, X);
}
Let's analyze the code a bit now and find where the problem actually is. Since this is an MFC console app, the "main" routine may look a little different, but it works the same. Let's skip to the else section inside main. You see the first line, char buff [10]. We have allocated a local variable, buff which is an array of 10 chars. We all know local variables are allocated on the stack right? So now we call the function overflow and pass it our buff. Now lets look inside the overflow function. First we instantiate a CFile object, then a CFileException object. Now we will attempt to open a file named "overflow.txt" from the current directory, with read access. If we open the file successfully we will get the files length, then we will read the entire contents of the file into our buff. Do you see the problem here? buff is only 10 chars. What happens if the file we read is 100? BUFFER OVERFLOW. But, The Big Problem is this we are overflowing a buffer Which exists on the stack. When we can Write to the Stack We Can do Some Strange Things. as you will soon See. So Now Lets Create a Text File Called Overflow.txt And Place It Into The Project Directory of The First Application.
Let's step to the side for a second, a little explanation of WindowsNT memory architecture is in order here. In NT every process (executable) is given 4GB (0xFFFFFFFF) of virtual memory when it is started. Some of this memory is actually shared among all processes, like kernel and device driver areas. But those areas are mapped to each processes virtual address space. No process actually gets 4GB of phyiscal memory, only the memory necessary is actually allocated from physical. So every process has full 4GB of virtual memory , which ranges from 0x00000000 to 0xFFFFFFFF. These areas are divided. 0x00000000 to 0x0000FFFF is reserved for NULL pointer assignments. Attempting to access memory in this area will cause an access violation. 0x00010000 to 0x7FFEFFFF is the processes user space. This is where the exe Image is loaded (Starting at 0x00400000) and DLL'S Are Loaded. If Code (A DLL or EXE) IS Load ATA Certain Address In this Range It Can Be Executed. Accessing An Address Which Does not have code loaded in it will cause an access violation. 0x7FFF0000 to 0x7FFFFFFF is reserved bad pointer assignments and you will get an access violation with any attempt to access it. 0x80000000 to 0xFFFFFFFF is for operating system use only. Things like Device Drivers and other Kernel Level Code Is Stored Here. Attempting to Access This Area from a User Level Application (Ring 3) Will Cause An Access Violation.
Now back to the overflow.txt file. We are going to keep putting characters into our text file until we see the dialog popup informing us of an application error and what memory we attempted to access. Which character you chose to fill this text file with is important, as you will see in minute. Let's start by filling the text file with a's. Lower case a's. We know the buffer will hold ten so lets start with 11 (make sure your application being built in debug mode or your results will Be different. 11 Doesn't Work So We Keep Increasing It. 18 Finally Causes a Crash. this creped isn't annhenhes Special YET. We've Just Totally Screwed Up The Stack and It Shows Lets Add Six More A's, For a total of 24. Run the program and watch the dialog popup explaining to us that instruction at 0x61616161 had referenced memory at 0x61616161. you do know that the hex value for the ascii character a is 0x61 right? If you have Visual C installed you will Be Able To Hit Cancel Now, And It Will Debug The AP plication. Once visual studio is open, open you registers window. To do that go to the view menu, then debug window, and select registers. If you do not know anything about assembly, you should, get a book and READ IT. We See Take EBP AND EIP. The Most Important Thing is Eip. By Being Able To Fill in The Eip with What Makes this eve Easier is there Our ESP is Not Destroyed. It Seem to Point Near The Area on The Stack That We Control. We need to test this to find out.
Now let's get into this. Set a breakpoint on the last bracket of the main routine, we only care about what happens here. Now start the debugger and it will make it to this breakpoint with no errors. Now we need to switch into disassembly view . If you have the standard keyboard setup for Visual C press alt 8, if not go to the view menu, debug windows, and select disassembly Also open your memory and registers windows if you have not already. you should see something similiar to THIS: 004011DB 5F POP EDI
004011dc 5e POP ESI
004011DD 5B POP EBX
004011DE 83 C4 50 Add ESP, 50H
004011E1 3B EC CMP EBP, ESP
004011E3 E8 28 04 00 00 Call _chkesp (00401610)
004011E8 8B E5 MOV ESP, EBP
004011EA 5D POP EBP
004011eb C3 RET
So what is that junk? It's assembly code. You do know assembly right? Even if you do not, I'll try to make this easy to understand. Starting at the top we have pop edi. The pop instruction will remove one item From the top of the stack and place it............................................... a DWORD (4 bytes), put it in whatever register, and increment the stack pointer by 4 (because of the 4 bytes). So before making another step, look at ESP. in the memory window enter ESP. You will now see exactly where esp is pointing to and what is there. Look at the four bytes pointed to by ESP and watch edi. now step over this instruction and notice that edi is now filled with whatever esp pointed to, and esp has been incremented by four. now THE NEXT TWO INSTRUCTIONS, Step over the name. The next. e lines are not very important to us. To understand them you will need to follow the assembly from the beginning of the routine, and we are not doing that. Just step over them, they do nothing special. Now onto the line, mov ESP, EBP. You Read this Line, Right to Left. This Will Mov (e) Whatver IN EBP INTO ESP. This Also Does Nothing Special For US. No ONTO POP EBP.
Here is where this gets interesting. Remember what a pop does, it removes the top element from the stack. Now lets take a look at where we our ESP is pointing to, cause whatever four bytes are there are about to go into EBP. So again type esp into your memory window. We have a bunch of 0x61's there (hex value of 'a'). So 0x61616161 is about to be popped into ebp. Step over the instruction and verify that it does. Sure enough, that is what happens. But that does not really get us anywhere. Now the next line, ret. Ret is the assembly return instruction. But there is more to it than just returning. How does it know where to return to? By the address that is supposed to be sitting on the stack right now. The return would be the equivalent of pop eip (which you can not do). It takes the four bytes that ESP points to and moves them into EIP. and EIP is our 32 bit instruction Pointer. this mean, whatverdress eip points to, is the next instruction to get executed. soable, Type ESP INTO the memory window and see what we are about to put into EIP. Well what do you know, another four bytes of 0x61. So step over the ret instruction and watch what happens. EIP will become 0x61616161 and you will be about to execute the instruction at 0x61616161. Which in my case is nothing ???, invalid memory. So step over again and you get an access violation. Now look at ESP. It correctly points to the next area on the stack. For some reason, if you run The Program Independant of The Debugger and Let IT Crash So You Get The OK / Cancel Dialog, and The Press Cancel. When You Land On 0x61616161 Your ESP WILL BE WRONG. I '
m not sure why that is, but it works as expected when you step through it line by line like we just did. So now we got the program to execute, or attempt to execute code at 0x61616161, which means we can take over the EIP . So lets see if we can overflow the stack some more, so that when we get to 0x61616161 our ESP points to the rest of our overflow. So lets add another 4 a's to our text file and debug again. We now have 28 a's in our text file. So we view the disassembly again, make sure to have your memory window and register windows open. Step through and over the ret instruction. You are now at 0x61616161 again. now type esp into the memory window and look what is there AS WE SUSPECTED, There Are 4 0x61's there. NOW WE ARE IN Business.Let Me Go Back to a point i Made Earlier. We used a's (0x61) to Fill Our Text File to Dermine if Thee Was An Overflow. So Since EIP BECAME 0x61616161 We Attempted to Access Instructions At That Address. In My Case The There Was Invalid Memory THE re so it was an access violation. But what if there had been code there? Maybe a DLL loaded or something. Well, it would have executed that code and probably done something totally different. The same thing could have happened if we would have used , A's instead of a's. A's hex value is 0x41. So we would have jumped to 0x41414141 instead of 0x61616161. there could be code there and it would have executed it. So keep those things in mind.
So We CAN Control The EIP, The ESP Points To The Rest of The Stack, And We can Fill The Stack Weth What? Would IT BE NICE INWE COULD? Well We COULD? WELL WELL WELL WELL WELD WELL WE can, hopefully. jmp ESP is in fact a legal instruction. This instruction would mov (e) whatever is in ESP into EIP and begin executing instructions there. So we need to somehow call jmp esp. Hmm, how can we do that? Well Letrs Think. WE Do Have Control of Eip, SO We can Jump To Where Ever We Want IN OUR Process Space. If We Can Fill Eip with The Address of A JMP ESP INSTRUCTION SOMEWHERE IN MEMORY WE Are IN Business. So How do WE find out if there is a jmp esp instruction somewhere in our process space? It's easier than you think. The first thing we need to do is figure out what the opcodes for jmp esp are. The opcodes are the machine instructions that programs are compiled into So They Can Be Executed. So Let's Create A New App In Visual C . Again a console app, and again with ENTER The FOLLOWING CODE: CWINAPP THEAPP;
Using namespace std;
INT_Tmain (int Argc, tchar * argv [], tchar * envp [])
{
INT nretcode = 0;
// Initialize MFC and Print and Error On Failure
IF (! Afxwininit (:: getModuleHandle (Null), NULL, :: getcommandline (), 0))
{
// Todo: Change Error Code To Suit your Needs
CERR << _T ("Fatal Error: MFC Initialization Faled") << ENDL;
NRETCODE = 1;
}
Else
{
Return 0;
__ASM JMP ESP
}
Return nretcode;
}
Now set a breakpoint on the return 0; statement, because the inline assembly line will not get executed Start the debugger and let it run to the breakpoint Now open up the disassembly debug window Right click on the window to turn on source annotation... and code bytes. now look at the line which contains jmp esp. to the left of jmp esp and to the right of its address, you will see its code bytes or opcodes. The opcodes for jmp esp are FF E4. So now that we KNOW THAT, How do we find what in OOUR Process Space? Let's address: change it to following: cWinApp
Using namespace std;
INT_Tmain (int Argc, tchar * argv [], tchar * envp [])
{
INT nretcode = 0;
// Initialize MFC and Print and Error On Failure
IF (! Afxwininit (:: getModuleHandle (Null), NULL, :: getcommandline (), 0))
{
// Todo: Change Error Code To Suit your Needs
CERR << _T ("Fatal Error: MFC Initialization Faled") << ENDL;
NRETCODE = 1;
}
Else
{
#if 0
Return 0;
__ASM JMP ESP
#ELSE
BOOL WE_LOADED_IT = FALSE;
Hinstance h;
TCHAR DLLNAME [] = _t ("kernel32");
H = getModuleHandle (DLLNAME);
IF (h == NULL)
{
H = loadingLibrary (DLLNAME);
IF (h == NULL)
{
Cout << "Error Loading DLL:" <
<