<?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>TAZ: TheTAZZone Network &#187; buffer overflows</title>
	<atom:link href="http://www.thetazzone.com/category/security-tutorials/buffer-overflows/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thetazzone.com</link>
	<description>Welcome to Internet Chaos: 960+ Games; Security, Networking, and General Tutorials; IRC Chat; and an Active Forum Community</description>
	<lastBuildDate>Sat, 21 Nov 2009 13:25:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial &#8211; Buffer Overflows Part 2</title>
		<link>http://www.thetazzone.com/tutorial-buffer-overflows-part-2/</link>
		<comments>http://www.thetazzone.com/tutorial-buffer-overflows-part-2/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 20:27:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[buffer overflows]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.thetazzone.com/?p=645</guid>
		<description><![CDATA[ORIGINALLY POSTED BY NOKIA FOR THETAZZONE/TAZFORUM HERE
Do not use, republish, in whole or in part, without the consent of the Author. TheTAZZone policy is that Authors retain the rights to the work they submit and/or post…we do not sell, publish, transmit, or have the right to give permission for such…TheTAZZone merely retains the right to [...]]]></description>
			<content:encoded><![CDATA[<p>ORIGINALLY POSTED BY NOKIA FOR THETAZZONE/TAZFORUM <a href="http://tazforum.thetazzone.com/viewtopic.php?f=28&amp;t=645">HERE</a></p>
<p>Do not use, republish, in whole or in part, without the consent of the Author. TheTAZZone policy is that Authors retain the rights to the work they submit and/or post…we do not sell, publish, transmit, or have the right to give permission for such…TheTAZZone merely retains the right to use, retain, and publish submitted work within it’s Network</p>
<p><span style="text-decoration: underline;">Buffer Overflows Part 2!</span></p>
<p>This is a continuation from my first Buffer Overflow Tutorial; I would highly recommend that you read that tut, before you read this one! (Even if you’ve read it before re-read it to refresh your memory!)</p>
<p><span style="text-decoration: underline;"> Ok, so in part one we looked at</span></p>
<p>How a Buffer Overflow happens and the security implications of this.<br />
Ways to find what programs have SUID privileges using the</p>
<p><span style="font-style: italic;">find / -type f –perm -04000 –ls  command</span> .</p>
<p>We also looked at how to change a program to give it #root privileges.</p>
<p>I will start this tutorial off at the point where, you have compiled a piece of code that is vulnerable to an overflow (see part 1), you have changed the ownership of the program to root and you have made it a SUID root program. We will call this program “pool” <img title="Smile" src="http://tazforum.thetazzone.com/images/smilies/icon_smile.gif" alt=":)" /></p>
<p>So, we know pool can be exploited with a buffer overflow (as we programmed it) and we know it has root privileges (as we gave them to it)</p>
<p>Now what we need to do is generate a program with a buffer <span style="font-style: italic;">(remember a buffer is a portion of memory assigned to a program)</span> that contains the shellcode, which will spawn a shell that can be “fed” into the pool program. We will call this program “snooker&#8221; <img title="Smile" src="http://tazforum.thetazzone.com/images/smilies/icon_smile.gif" alt=":)" /></p>
<p><span style="font-weight: bold;">Our aim is to make the program pool overflow in to the buffer that the program snooker has given it and make the EIP point to where our shellcode within this buffer and then to make snooker accidentally execute this shellcode.</span></p>
<p>Obviously for this to work we need to know where our shellcode is stored in the buffer!</p>
<p>There two main ways of doing this: Using a NOP sled or by flooding the end of a buffer with quite a few “return addresses”.</p>
<p><span style="font-weight: bold;"><span style="text-decoration: underline;">No Operation (NOP)</span> </span></p>
<p>As the name suggests this is an instruction that does nothing, its sole purpose is to take up a byte of memory so nothing else can occupy it.</p>
<p>Why is this useful to us?</p>
<p>For a few reasons really.</p>
<p>1.	If our NOP is occupying a byte of memory we know that our shell code couldn’t possibly be there!<br />
2.	The processor will skip over a NOP until it gets to something that tells it to do something, i.e. our shellcode (hopefully)<br />
3. A program wont crash if it hits a NOP as the NOP isn’t telling it to do anything, it will just move down or up to the next memory “block”</p>
<p>Consider this (each number represents a bock of memory in our buffer)</p>
<p>Pool Snooker Snooker Shellcode Snooker Snooker Snooker Snooker<br />
1&#8230;&#8230;&#8230;..2&#8230;&#8230;&#8230;&#8230;3&#8230;&#8230;&#8230;&#8230;.4&#8230;&#8230;&#8230;&#8230;.5&#8230;&#8230;&#8230;&#8230;.6&#8230;&#8230;&#8230;..7&#8230;&#8230;&#8230;&#8230;.8&#8230;..<br />
For us to execute our shellcode we would have to ensure the EIP was pointing at “4” and no where else, otherwise snooker would continue to run. There would be a lot of trial and error not to mention luck in executing the shell code!</p>
<p>But, what about if we done this:</p>
<p>Pool	Snooker	NOP	NOP	NOP	NOP	NOP	Shellcode<br />
&#8230;.1&#8230;&#8230;2&#8230;&#8230;..3&#8230;&#8230;.4&#8230;&#8230;.5&#8230;&#8230;.6&#8230;&#8230;.7&#8230;&#8230;.8<br />
As long as the EIP was pointing at either 3,4,5,6,7 or 8 our shellcode would be executed, because as I said earlier the processor would just skip along them NOP’s until it got to something it could execute!<br />
Obviously this gives us a bigger area of error for the EIP to execute the shellcode<br />
So along as we overwrite the EIP with any address in the NOP we can be assured our shellcode will be executed</p>
<p>*The NOP sled is not limited in size, it can be 200+ bytes if you want it to be.<br />
Use common sense though as if you fill the memory up with NOP’s then nothing is going to work!</p>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">Return Addresses:</span> </span></p>
<p>The other way of doing it is with return address; this method can be and is, used in conjunction with the NOP method!</p>
<p>This is a very simple method.</p>
<p>All it does is “floods” the last part of our buffer with lots and lots of return address to where our shellcode or NOP is located.</p>
<p>Snooker	Snooker	NOP	NOP	NOP	NOP	Shellcode	R/A7	R/A5	R/A3<br />
&#8230;..1&#8230;&#8230;&#8230;&#8230;2&#8230;&#8230;.3&#8230;&#8230;.4&#8230;&#8230;.5&#8230;..6&#8230;&#8230;&#8230;.7&#8230;&#8230;&#8230;..8&#8230;&#8230;.9&#8230;.10</p>
<p>So the return address (R/A) is telling the EIP to go to “7 , 5 or 3”</p>
<p>Have a look at the room for error we now have!<br />
As long as the EIP is NOT pointing at 1 or 2 our shellcode is going to be executed</p>
<p>So compared to the first diagram:</p>
<p>Pool Snooker Snooker Shellcode Snooker Snooker Snooker Snooker<br />
1&#8230;&#8230;&#8230;..2&#8230;&#8230;&#8230;&#8230;3&#8230;&#8230;&#8230;&#8230;.4&#8230;&#8230;&#8230;&#8230;.5&#8230;&#8230;&#8230;&#8230;.6&#8230;&#8230;&#8230;..7&#8230;&#8230;&#8230;&#8230;.8&#8230;..</p>
<p>Where we needed to hit “4” right on the head to execute our shellcode</p>
<p>Now all we need to do is make sure the EIP stays away from 1 + 2!</p>
<p>Of course both of these methods require us to know the approximate location of the buffer in the system memory!</p>
<p>How do we find this?</p>
<p>We find this by finding out where the current Stack Pointer is.<br />
(Remember a stack pointer, amongst other things, is used to tell the EIP where to return to after a function has been completed), hence if we can control the stack pointer, we can control the EIP!!</p>
<p>In the snooker program them first place the stack will be telling the EIP to look at will be the start of the buffer, remember our shellcode is located in this buffer and we know how big the buffer is because we created it in our snooker program, so already we have a rough idea where our shellcode will be!</p>
<p>I feel it may be starting to get confusing, so I will break it all down:</p>
<p><span style="text-decoration: underline;">We have:</span></p>
<p>1. Created a program that is susceptible to Buffer Overflows called Pool<br />
2. Made Pool run with root privileges, so that, when we make it spawn a shell, the shell will have the same privileges (#root)<br />
3. Created a program called Snooker, which gave pool a buffer, containing the shellcode to be executed.<br />
4. Filled this buffer with NOP’s and Return Addresses, to maximise the chance of getting the shellcode to be run.<br />
5. Found out where snookers buffer is by using the current stack pointer<br />
6. We know how many bytes pool will take up, so when we coded (programmed) snooker we leave the first bytes free that pool will require to run, then we put some NOP’s to leave space between pool and our shellcode, this increases the %age of our shellcode executing<br />
7. Filled the end of the buffer with return address that point to the NOP’s that are after pool.</p>
<p>So Pool overflows, the EIP will go where ever it likes in snookers buffer, if we are lucky it goes straight to the shellcode and spawns a root shell for us.<br />
If we are unlucky it lands on a NOP, it then goes to the next NOP and the next and the next until it comes to our shell code and spawns a root shell!<br />
Or, it land on a return address, which is telling it to go to either a NOP or if we are lucky again, the shellcode, if it goes to the shellcode, happy days we get a root shell, if it goes to a NOP, it slides down and down and down until low and behold….. yup, we get a root shell!</p>
<p>Did it make scene to anyone??</p>
<p>•This is a very very simplified version of it.<br />
•There is a bit more to finding our buffer using stack pointer offsets, but I will cover this in a future tutorial!<br />
•Again I have took liberties with memory addressing, just to keep it simple<br />
•In Part 1 Snooker was called vuln but I felt like renaming it for this tut!<br />
•I have has a few pm’s from people asking how to compile the code from my last tutorial, hence my next one will be about how to do that using Linux and a Windows based compiler.</p>
<p>Please bear in mind that this is a simplified version and liberties have been taken!</p>
<p>I hope you enjoyed reading it and maybe learnt something from it; I’m now off to buy me a big bottle of JD as I’ve drank a whole one whilst typing this! (Thank god for spell checkers!)</p>
<p>Thanks!<br />
Nokia</p>


<!-- Begin TwitThis script (http://twitthis.com/) -->
<div style="text-align:left;">
<script type="text/javascript" src="http://s3.chuug.com/chuug.twitthis.scripts/twitthis.js"></script>
<script type="text/javascript">
<!--
document.write('<a href="javascript:;" onclick="TwitThis.pop();"><img src="http://s3.chuug.com/chuug.twitthis.resources/twitthis_grey_72x22.gif" alt="TwitThis" style="border:none;" /></a>');
//-->
</script>
</div>
<!-- /End -->

]]></content:encoded>
			<wfw:commentRss>http://www.thetazzone.com/tutorial-buffer-overflows-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial &#8211; Buffer Overflows Part 1</title>
		<link>http://www.thetazzone.com/tutorial-buffer-overflows-part-1/</link>
		<comments>http://www.thetazzone.com/tutorial-buffer-overflows-part-1/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 20:19:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[buffer overflows]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.thetazzone.com/?p=641</guid>
		<description><![CDATA[ORIGINALLY POSTED BY NOKIA FOR THETAZZONE/TAZFORUM HERE
Do not use, republish, in whole or in part, without the consent of the Author. TheTAZZone policy is that Authors retain the rights to the work they submit and/or post…we do not sell, publish, transmit, or have the right to give permission for such…TheTAZZone merely retains the right to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.thetazzone.com/wp-content/tazicon4.gif"><img class="size-full wp-image-615 alignleft" title="tazicon4" src="http://www.thetazzone.com/wp-content/tazicon4.gif" alt="tazicon4" width="73" height="60" /></a>ORIGINALLY POSTED BY NOKIA FOR THETAZZONE/TAZFORUM <a href="http://tazforum.thetazzone.com/viewtopic.php?f=28&amp;t=644">HERE</a></p>
<p>Do not use, republish, in whole or in part, without the consent of the Author. TheTAZZone policy is that Authors retain the rights to the work they submit and/or post…we do not sell, publish, transmit, or have the right to give permission for such…TheTAZZone merely retains the right to use, retain, and publish submitted work within it’s Network</p>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">Buffer Overflows – what they are and how they work. </span></span></p>
<p>This can be quite a complicated issue, so I will try to break it down into different parts and put it into everyday language.</p>
<p>I will assume that if you are reading this you understand a little programming (functions, integers etc)</p>
<p>To understand buffer overflows it helps to know a bit about how a program utilizes memory.</p>
<p>First it will help to understand what an EIP is:</p>
<p>[it is essential to understand an EIP to understand how a buffer overflow works]</p>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">EIP:</span></span><br />
<span style="font-style: italic;"><span style="font-weight: bold;">Extended Instruction Pointer.</span></span></p>
<p>The processor has a very small chunk of memory itself, divided into what is called registers.<br />
The most common register is the EIP; this tells the processor where to look in the system memory for the function (or piece of code) that it has to execute.</p>
<p>I.e. the code could be to print the word TAZZone on to your monitor and has been written to the memory at the address of 0&#215;12345678. (Memory uses the hex numbering system).<br />
The EIP would now tell the processor to go to 0&#215;12345678 and do what ever the code is telling it to do, hence the word “TAZZone” will be printed on the screen.</p>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">Program memory:</span></span></p>
<p>There are five types of program memory, text, heap, stack, bss and data.<br />
Each one of these is a special piece of memory reserved for a certain type of purpose.</p>
<p>I will cover text and stack for the purpose of this paper.</p>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">Text: </span></span></p>
<p>This is where the compiled machine language is stored. Write permissions are disabled here as it is used only to store the code, which is being executed.</p>
<p>When you compile a program, what you are doing is converting it from human readable form into a language the computer understands, it is the output of a compiled program that is stored in the text segment)</p>
<p>So for a very simplified example say you wanted to print the words Hello, goodbye, thank you, and Microsoft rules <img title="Wink" src="http://tazforum.thetazzone.com/images/smilies/icon_wink.gif" alt=";-)" /></p>
<p>(For ease I will use 1,2,3,4,5 etc for memory addresses instead of the correct addresses.</p>
<p>So hello is stored at 1, goodbye at 2, thank you at 3 and Microsoft rules at 4.<br />
Here is what the processor will do</p>
<p>1. Get the address for the first function to complete from the EIP and go there<br />
2. Add the number of bytes in the instruction to the EIP<br />
3. Do what ever the piece of code is telling it to do, (print Hello.)<br />
4. Go back to the EIP to get the next address.</p>
<p>The EIP will know when the instruction has been completed because in step 2 the processor told it how many bytes there was.</p>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">Stack: </span></span></p>
<p>The stack memory is used as a tempory storage space for functions.</p>
<p>When a function (print) is called by a program it will have its own variables (hello,goodbye,thank you etc)</p>
<p>and the code will be at a different place in the text segment of memory.</p>
<p>(I.e. hello cannot be at the same memory address as goodbye otherwise they would over write each other.)</p>
<p>So the function is to print Hello (1) Goodbye (2) and Thank you (3)</p>
<p>The whole function will be read from the text segment and get passed to the stack segment.<br />
The stack segment will remember the addresses (1,2,3) of each variable and pass this data to the EIP to tell it which memory address to return to when the function is finished.</p>
<p>There is a lot more to the stack segment but it’s not really relevant at this point!</p>
<p><span style="text-decoration: underline;"><span style="font-weight: bold;">BUFFER OVERFLOWS </span></span></p>
<p>Ok, so the programmer has specified that the word Hello with need 5 bytes of memory, but what happens when 7 characters try to write them selves to this piece of memory instead, the word goodbye for example:</p>
<p>|H|E|L|L|O| &#8211; No probs here<br />
1 2 3 4 5</p>
<p>|G|O|O|D|B| &#8212;&#8212; |Y|E| &#8211; | &#8211; | &#8211; | &#8211; They overflow into memory held for something else<br />
-1- 2- 3-4-5 &#8212;&#8212; -6- 7- 8- 9 &#8211; 0</p>
<p>5 bytes are allocated but the variable was 7 bytes long. Now it can’t just disappear, it has to be written somewhere so a buffer overflow occurs. If the data that was overwritten in 6 + 7 were a critical part of the program, the program would have crashed.</p>
<p>Here is a well know piece of code to cause a buffer overflow (its very well known and is in most books about the subject, so know one jump on my back for posting it, please)</p>
<dl class="codebox">
<dt>Code: <a onclick="selectCode(this); return false;" href="http://tazforum.thetazzone.com/viewtopic.php?f=28&amp;t=644#">Select all</a></dt>
<dd><code>Void overflow_function (char *str)</code></p>
<p>{</p>
<p>char buffer[20];            // size of the buffer is 20 bytes</p>
<p>strcpy(buffer, str);</p>
<p>}</p>
<p>int main()</p>
<p>{</p>
<p>char big_string[128];</p>
<p>int I;</p>
<p>for(i=0; I &lt; 128; i++0                          // this makes it loop 128 times</p>
<p>}</p>
<p>big_string[i] = ‘A’;                             // Fill the big_string with AAAAA’s</p>
<p>}</p>
<p>overflow_function(big_string);</p>
<p>exit(0);</p>
<p>}</p>
</dd>
</dl>
<p>There are a few subtle things left out here, (unlucky skiddies), it should be easy for someone to fix who has a basic knowledge of C.</p>
<p>This program should crash as a result of the overflow</p>
<p>How can this be utilized to take control of a program?</p>
<p>Refer to the sample code above, 128 bytes where wrote to a space 20 bytes big.</p>
<p>The remaining 108 bytes overflow overwriting amongst other things the return address for the EIP, so now the EIP aint got a scoobie doo where to go and the program can no longer carry on so it just stops.</p>
<p>BUT, what if the return address was overwrote with an address of your choosing????</p>
<p>An address that contained code to be run?????</p>
<p>The program wouldn’t crash because as far as the processor is concerned its just gone to the next part of the program, it doesn’t know what’s meant to come next, it just reads what’s there and does what it says</p>
<p>What if the return address that you have specified to the EIP, contains information that a user has entered into your program?</p>
<p>So your program could have been to get someone to type their name and then make it print out on the screen.</p>
<p>But the user doesn’t type his name; instead he has typed a small piece of shellcode!</p>
<p>Say, you know where this input is stored so you cause a buffer overflow in your code on purpose, one that give the return address for the EIP as the address the user input is stored (user name)<br />
The EIP will now tell the processor to go here and execute the shellscript that is there.</p>
<p><span style="font-style: italic;">Shellcode is bytecode that just spawns a shell.</span></p>
<p>Now say a SUID program with root privileges was made to spawn a shell, the shell that was spawned would have the same privileges as the program (#root!)</p>
<p><span style="font-style: italic;">How do you find out what SUID files have root??</span></p>
<p>find / -type f –perm -04000 –ls &#8211; this will list all suid programs with root privileges on *nix systems.</p>
<p>The following code is taken from the book: Hacking, the art of exploitation</p>
<dl class="codebox">
<dt>Code: <a onclick="selectCode(this); return false;" href="http://tazforum.thetazzone.com/viewtopic.php?f=28&amp;t=644#">Select all</a></dt>
<dd><code>vuln.c code</code></p>
<p>Int main(int argc, char *argv[])</p>
<p>{</p>
<p>char buffer[500];</p>
<p>strcpy(buffer, argv[1]);</p>
<p>return 0;</p>
<p>}</p>
</dd>
</dl>
<p>This code just miss-manages memory, but if the ownership was changed to root and was changed to a suid root program that is susseptable to buffer overflows (Bugtraq!!)</p>
<p>$ sudo chown root vuln<br />
$ sudo chmod +s vuln<br />
$ ls –l vuln</p>
<p>-rwsr-sr-x 1 root users etc</p>
<p>vuln is now a root suid program.</p>
<p>Now all you need is code to make a buffer containing the shellcode to be fed to the vuln program that will overwrite the return address to execute the shellcode.</p>
<p>Obviously the address of the shellcode must be known in advance – this is a tutorial in itself though.</p>
<p>I do have the code required to create a buffer to be fed to the vuln program to on some systems trick it into executing shellcode when it crashes, hence, spawning a #root shell for the user to do as he wishes</p>
<p>However I am not going to post it here, if you would like it PM me!</p>
<p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p>
<p>*I did take a few liberties with how code, functions memory addressing etc works just for the sake of keeping it simple.</p>
<p>There is a hell of a lot more to buffer overflows and loads of different types; I have covered &#8220;Stack Based Overflows&#8221; here.</p>


<!-- Begin TwitThis script (http://twitthis.com/) -->
<div style="text-align:left;">
<script type="text/javascript" src="http://s3.chuug.com/chuug.twitthis.scripts/twitthis.js"></script>
<script type="text/javascript">
<!--
document.write('<a href="javascript:;" onclick="TwitThis.pop();"><img src="http://s3.chuug.com/chuug.twitthis.resources/twitthis_grey_72x22.gif" alt="TwitThis" style="border:none;" /></a>');
//-->
</script>
</div>
<!-- /End -->

]]></content:encoded>
			<wfw:commentRss>http://www.thetazzone.com/tutorial-buffer-overflows-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
