{"id":123,"date":"2012-07-28T18:35:06","date_gmt":"2012-07-29T01:35:06","guid":{"rendered":"http:\/\/seanmurphree.com\/blog\/?p=123"},"modified":"2012-07-28T18:35:06","modified_gmt":"2012-07-29T01:35:06","slug":"blackbox-level-3-step-back","status":"publish","type":"post","link":"https:\/\/seanmurphree.com\/blog\/?p=123","title":{"rendered":"Blackbox Level 3 &#8211; Step Back"},"content":{"rendered":"<p>Today we&#8217;re going to be looking at level 3 of the <a title=\"Smash The Stack\" href=\"http:\/\/www.smashthestack.org\">Smash The Stack<\/a> wargame, <a title=\"Blackbox\" href=\"http:\/\/blackbox.smashthestack.org:85\/\">Blackbox<\/a>. \u00a0As usual, the password will be stripped from the page and replaced with Y&#8217;s. \u00a0Now let&#8217;s move onto the game by ssh&#8217;ing into the server as level 3.<\/p>\n<p>Upon arrival the first thing we should do is look for our vulnerability and any source code, if available. \u00a0A quick ls of the home directory rewards us with a level4 SUID program called proclist, as well as a readable file called proclist.cc. \u00a0Examining further we can see that proclist.cc is (most likely) the source file:<\/p>\n<blockquote><p>level3@blackbox:~$ more proclist.cc<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>int main(int main, char **argv)<br \/>\n{<br \/>\nstd::string command;<br \/>\nstd::string program;<\/p>\n<p>std::cout &lt;&lt; &#8220;Enter the name of the program: &#8220;;<br \/>\nstd::cin &gt;&gt; program;<\/p>\n<p>for(unsigned int i = 0; i &lt; program.length(); i++) {<br \/>\nif(strchr(&#8220;;^&amp;|&gt;&lt;&#8220;, program[i]) != NULL) {<br \/>\nstd::cout &lt;&lt; &#8220;Fatal error&#8221; &lt;&lt; std::endl;<br \/>\nreturn 1;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ Execute the command to list the programs<br \/>\ncommand = &#8220;\/bin\/ps |grep &#8220;;<br \/>\ncommand += program;<br \/>\nsystem(command.c_str());<\/p>\n<p>return 0;<br \/>\n}<\/p><\/blockquote>\n<p>It is always important to understand a file when looking for a vulnerability, and today provides no exception. \u00a0Analyzing the code we can see that the program does a few things. \u00a0First, it gets input from the user using cin&#8217;s &gt;&gt; operator and puts it into the variable &#8220;program&#8221;. \u00a0Next, it searches through the user input stored in &#8220;program&#8221;, making sure it doesn&#8217;t consist of any of the characters ; ^ &amp; | &gt; or &lt; \u00a0If one of the characters is found in the input string, the program exits. \u00a0Afterwards, the program appends the input string in &#8220;program&#8221; to the end of the command string with value &#8220;\/bin\/ps |grep &#8220;. \u00a0Finally the entire command string is sent to a system call via a non-mutable C string provided by the c_str() function.<\/p>\n<p>Thinking about the flow of the program, it seems like the system call might be where we want to make our attack. \u00a0After all, user input in put into the system call. \u00a0So, if we can use maliciously formed input, maybe we can control what they system call does. \u00a0To do this, we must note a few things. \u00a0One is that the program uses cin &gt;&gt; and therefore we cannot include any white-space in our input string. \u00a0Second, in Linux we can separate shell or system call commands via a semi-colon. \u00a0Third, in Linux shell there are three quotes one can use, single quote, double quote and backtick. \u00a0Single quotes don&#8217;t resolve variables and only additional single quotes need to be escaped. \u00a0Double quotes allow for resolving variables to their values. \u00a0Lastly, backticks execute the command string inside of them.<\/p>\n<p>First, let&#8217;s look at point two. \u00a0We could potentially cause multiple command execution (such as \/bin\/sh) if we could include a semi-colon in our user input string. \u00a0If the system call ran as system(&#8220;\/bin\/ps |grep ;\/bin\/sh&#8221;) we would get a shell. \u00a0Unfortunately, as we noted when reading through the program, semi-colons are one of the terminating characters for the program. \u00a0Thus, we must look for a different approach as this program is attempting to validate user input, good!<\/p>\n<h3>-Backtick Attack:<\/h3>\n<p>While the user input\u00a0sanitization checks for a few characters, it doesn&#8217;t check for the backtick. \u00a0Observing this and knowing that the backtick causes execution of the command string within, we may be able to cause the program to run a command. \u00a0However, looking back at point one, our backtick&#8217;ed string cannot contain any spaces. \u00a0Thus, we will have to write a little shell script to do what we want. \u00a0In this case, we want to have the program get the contents of the password file for the next level and save it into a file we can read. \u00a0This is due to the fact that we won&#8217;t be able to access a shell directly (even by executing \/bin\/sh)\u00a0since the execution is only being used as a parameter to grep and will close immediately. \u00a0However, an executed shell script can write to a file and quit. \u00a0Let&#8217;s look at the shell file we&#8217;re going to use:<\/p>\n<blockquote><p>level3@blackbox:\/tmp\/.t$ more catpass<br \/>\n#!\/bin\/sh<br \/>\n\/bin\/cat \/home\/level4\/password &gt; \/tmp\/.t\/p<\/p><\/blockquote>\n<p>As stated before, the shell script runs cat on the password file for the next level and saves it to a file of our choosing. \u00a0Now let&#8217;s set up our files, don&#8217;t forget to set the correct privileges:<\/p>\n<blockquote><p>level3@blackbox:\/tmp\/.t$ chmod 755 catpass<br \/>\nlevel3@blackbox:\/tmp\/.t$ touch p<br \/>\nlevel3@blackbox:\/tmp\/.t$ chmod 777 p<\/p><\/blockquote>\n<p>Now we&#8217;re ready to run the program and provide it with our malicious string.<\/p>\n<blockquote><p>level3@blackbox:\/tmp\/.t$ \/home\/level3\/proclist<br \/>\nEnter the name of the program: `.\/catpass`<br \/>\nUsage: grep [OPTION]&#8230; PATTERN [FILE]&#8230;<br \/>\nTry `grep &#8211;help&#8217; for more information.<br \/>\nlevel3@blackbox:\/tmp\/.t$ more p<br \/>\nYYYYYYYYY<\/p><\/blockquote>\n<p>There we have it, that is one way to do Blackbox Level 3. \u00a0Remember the backticks cause the contents to be executed. \u00a0Here we saw the system call ran ps and piped it&#8217;s results to grep. \u00a0grep then executed with a variable and that variable was executed (because of the backticks), causing the password to be ripped. \u00a0Finally grep finished, leading to the program&#8217;s exit. \u00a0Moral of the story is to provide better user input validation and sanitization. \u00a0Additionally, it&#8217;s usually a bad idea to directly make a system call on data which is under the control of users.<\/p>\n<p>&nbsp;<\/p>\n<h3>-Path Attack:<\/h3>\n<p>In most, if not all, modern day operating systems there exists something called the path. \u00a0The path is a list of directories in which the operating system will look for programs with the same name of programs run by the user (when the user doesn&#8217;t provide a full path to the file). \u00a0In the program which we are exploiting today, there exists a vulnerability based on this PATH variable.<\/p>\n<p>In our program when the system call is executed, two commands are run by default. \u00a0The first is \/bin\/ps. \u00a0This command is provided as a full path and thus cannot be easily re-directed. \u00a0The second command however, is simply grep. \u00a0Since a full, or relative, path wasn&#8217;t provided for the file, the system will look in the PATH environmental variable for directories to look for a file with the same name. \u00a0If we can re-write the PATH and we can cause the system call to execute a program called grep, but in the directory of our choosing. \u00a0Using this we can put our password-acquiring shell script in our chosen directory with the name grep, and watch it be executed. \u00a0Let&#8217;s go a head and setup our files, similar to the backtick attack above:<\/p>\n<blockquote><p>level3@blackbox:\/tmp\/.t$ more grep<br \/>\n#!\/bin\/sh<br \/>\n\/bin\/cat \/home\/level4\/password &gt; \/tmp\/.t\/p<br \/>\nlevel3@blackbox:\/tmp\/.t$ chmod 755 grep<br \/>\nlevel3@blackbox:\/tmp\/.t$ touch p<br \/>\nlevel3@blackbox:\/tmp\/.t$ chmod 777 p<\/p><\/blockquote>\n<p>Now let&#8217;s re-write the PATH to be set to the current directory, where our fake grep program is:<\/p>\n<blockquote><p>level3@blackbox:\/tmp\/.t$ export PATH=.\/<\/p><\/blockquote>\n<p>Finally we can run the program and see what happens:<\/p>\n<blockquote><p>level3@blackbox:\/tmp\/.t$ \/home\/level3\/proclist<br \/>\nEnter the name of the program: a<br \/>\nlevel3@blackbox:\/tmp\/.t$ \/bin\/cat p<br \/>\nYYYYYYYYY<\/p><\/blockquote>\n<p>There we have it again, Blackbox Level 3 through PATH exploitation. \u00a0Remember if you don&#8217;t provide a full file name, the operating system will look for it in your PATH, something under the control of the user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re going to be looking at level 3 of the Smash The Stack wargame, Blackbox. \u00a0As usual, the password will be stripped from the page and replaced with Y&#8217;s. \u00a0Now let&#8217;s move onto the game by ssh&#8217;ing into the &hellip; <a href=\"https:\/\/seanmurphree.com\/blog\/?p=123\">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,13,49,28,11,47],"_links":{"self":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123"}],"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=123"}],"version-history":[{"count":4,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}