{"id":228,"date":"2012-09-24T19:47:14","date_gmt":"2012-09-25T02:47:14","guid":{"rendered":"http:\/\/seanmurphree.com\/blog\/?p=228"},"modified":"2012-09-24T19:47:14","modified_gmt":"2012-09-25T02:47:14","slug":"blackbox-level-5-pathfile-hijacking-with-symlinks","status":"publish","type":"post","link":"https:\/\/seanmurphree.com\/blog\/?p=228","title":{"rendered":"Blackbox Level 5 &#8211; Path\/File Hijacking with Symlinks"},"content":{"rendered":"<p>Today we&#8217;re going to be looking at level 5 of the <a title=\"Blackbox\" href=\"http:\/\/blackbox.smashthestack.org:85\/\" target=\"_blank\">Blackbox<\/a> wargame from the <a title=\"Smash The Stack\" href=\"http:\/\/smashthestack.org\/\" target=\"_blank\">Smash The Stack<\/a> network. \u00a0As always, the final password will be replaced with Y&#8217;s, and if you wish to follow a long, you will already need to have the password to level 5. \u00a0So without further adieu, let&#8217;s get started by ssh&#8217;ing into blackbox as level5.<\/p>\n<p>Upon logging in, a quick directory listing shows us an executable called &#8220;list&#8221; and what we can assume is it&#8217;s source code, &#8220;list.c&#8221;. \u00a0To evaluate the program, let&#8217;s move forward by viewing the file &#8220;list.c&#8221;.<\/p>\n<blockquote><p>level5@blackbox:~$ more list.c<br \/>\n#include &lt;stdio.h&gt;<br \/>\nint main(int argc, char **argv)<br \/>\n{<br \/>\nchar buf[100];<br \/>\nsize_t len;<br \/>\nchar fixedbuf[10240];<br \/>\nFILE *fh;<br \/>\nchar *ptr = fixedbuf;<br \/>\nint i;<\/p>\n<p>fh = fopen(&#8220;somefile&#8221;, &#8220;r&#8221;);<br \/>\nif(!fh)<br \/>\nreturn 0;<\/p>\n<p>while((len = fread(buf, 1, 100, fh)) &gt; 0) {<br \/>\nfor(i = 0; i &lt; len; i++) {<br \/>\n\/\/ Disable output modifiers<br \/>\nswitch(buf[i]) {<br \/>\ncase 0xFF:<br \/>\ncase 0x00:<br \/>\ncase 0x01:<br \/>\nbreak;<br \/>\ndefault:<br \/>\n*ptr = buf[i];<br \/>\nptr++;<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nprintf(&#8220;%s&#8221;, fixedbuf);<\/p>\n<p>fclose(fh);<br \/>\n}<\/p><\/blockquote>\n<p>Upon inspection we can see a few members of the main function declared, followed by the procedure. \u00a0Some of the members are: a 100 character array called buf, a variable called len which is of size_t, a 10K char array called fixedbuf, a FILE pointer called fh, a char pointer called ptr (instantiated to point to the start of fixedbuf), and an int called i. \u00a0Following member declaration, the procedure starts.<\/p>\n<p>First, a local file called &#8220;somefile&#8221; is opened for reading and passed to the FILE pointer &#8220;fh&#8221;. \u00a0If there is a problem opening the file for reading, the program exits returning 0. \u00a0Next, 100 bytes are read from the file at a time into the &#8220;buf&#8221; array. \u00a0These bytes are read through progressively and as long as they aren&#8217;t an instance of 0xFF, 0x00, or 0x01, they are copied into &#8220;fixedbuf&#8221;, and the next byte is assessed. \u00a0Once the full file has been copied into &#8220;fixedbuf,&#8221; the printf is called to print the string represented at &#8220;fixedbuf.&#8221; \u00a0Finally &#8220;fh&#8221; is closed and the program exits.<\/p>\n<p>So, let&#8217;s think about how this program works, and how we might be able to alter it&#8217;s assumptions to get the password for level 6. \u00a0We know that blowfish stores passwords in the home directories, so we need to read \/home\/level6\/password. \u00a0We also know this program is running SUID as level6 and thus should be able to read that file. \u00a0However, the program is set to read a hard-coded file name, the file called &#8220;somefile&#8221; in the current directory. \u00a0Thus, what can we do? \u00a0Even if we switched directories to \/home\/level6, how could we get the program to read &#8220;password&#8221; file instead of &#8220;somefile&#8221; file?<\/p>\n<p>Well, we can use links. \u00a0Links are available in many operating systems and essentially allow for multiple virtual locations (such as &#8220;\/tmp\/.tt\/file1&#8221; and &#8220;\/tmp\/.tt\/file2&#8221;) to point to the same physical file on a disk. \u00a0This is useful to save space on a disk or help organize files that are shared. \u00a0Symbolic links or symlinks make it so even though two different paths point to the same file, they still resolve as the separate paths. \u00a0We can make use of symlinks in this level to create a symlink called &#8220;somefile&#8221; in the local directory which points to the file we want to read, \/home\/level6\/password. \u00a0To make a link we&#8217;re going to use the linux command &#8220;ln&#8221;. \u00a0To get information on the command, please perform &#8220;ln &#8211;help&#8221; on your own, I don&#8217;t want to post it here just to save space. \u00a0However, in creating symlinks, we&#8217;re going to have to use the -s flag. \u00a0Let&#8217;s look at making the symlink in action:<\/p>\n<blockquote><p>level5@blackbox:\/tmp\/.ttt$ ln -s \/home\/level6\/password .\/somefile<br \/>\nlevel5@blackbox:\/tmp\/.ttt$ ls -la<br \/>\ntotal 16<br \/>\ndrwxr-xr-x 2 level5 gamers 4096 Sep 25 02:40 .<br \/>\ndrwx-wx-wt 95 root root 12288 Sep 25 02:40 ..<br \/>\nlrwxrwxrwx 1 level5 gamers 21 Sep 25 02:40 somefile -&gt; \/home\/level6\/password<\/p><\/blockquote>\n<p>From the file listing, we can see that there is now a file called &#8220;somefile&#8221; and on the information about the file, it is marked as &#8220;l&#8221; for link. \u00a0We also see that it points to \/home\/level6\/password. \u00a0Now let&#8217;s go a head and run the vulnerable program and see if it resolves the link and reads the password for us!<\/p>\n<blockquote><p>level5@blackbox:\/tmp\/.ttt$ ~\/list<br \/>\nYYYYYYYYYYYY<\/p><\/blockquote>\n<p>There we have it! \u00a0Using a simple symlink we were able to take a program which reads and displays the contents of a file with a hardcoded filename, and cause it to output any file of our choosing, even one with sensitive information such as a password!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re going to be looking at level 5 of the Blackbox wargame from the Smash The Stack network. \u00a0As always, the final password will be replaced with Y&#8217;s, and if you wish to follow a long, you will already &hellip; <a href=\"https:\/\/seanmurphree.com\/blog\/?p=228\">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":[7,6,4],"tags":[48,49,11,47],"_links":{"self":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/228"}],"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=228"}],"version-history":[{"count":8,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}