1. Home
  2. Web App Vulnerabilities
  3. How to remediate – OS Command Injection

How to remediate – OS Command Injection

1. Introduction

2. Technical Explanation

The root cause is a lack of input validation in the web application allowing arbitrary OS commands to be executed. An attacker can inject shell metacharacters into an input field which are then interpreted by the operating system when the command is run. This requires the application to use user-controlled data directly within an OS command without sanitisation.

  • Exploit mechanism: An attacker submits a malicious payload containing shell metacharacters through an input field, which is then executed by the server’s operating system. For example, submitting “test; ls -la” to a vulnerable form could list the contents of the current directory.
  • Scope: Web applications running on various platforms (Linux, Windows, macOS) are affected if they improperly handle user input in OS commands. Specific versions aren’t usually at fault but rather application code.

3. Detection and Assessment

Confirming vulnerability requires identifying where the application uses system commands with unsanitised data. A quick check involves examining application source code or configuration files for command execution patterns. More thorough assessment can be done through dynamic analysis, attempting to inject payloads during runtime.

  • Quick checks: Examine web application source code for functions like `system()`, `exec()`, `popen()` in PHP, or similar functions in other languages.
  • Scanning: Static Application Security Testing (SAST) tools can identify potential command injection vulnerabilities by analysing the source code. Example signatures include searches for unsanitised user input being passed to shell commands.
  • Logs and evidence: Look for unexpected processes spawned by the web server process in system logs, or unusual activity related to command execution. Event IDs will vary depending on the operating system.
ps aux | grep 

4. Solution / Remediation Steps

4.1 Preparation

  • Ensure you have access to the application source code or configuration files. A rollback plan involves restoring from the backup taken in step 1.
  • Changes should be deployed during a scheduled maintenance window with appropriate approval from IT management.

4.2 Implementation

  1. Step 1: Identify all instances where user input is used to construct OS commands within the application code.
  2. Step 2: Replace direct command construction with safer alternatives, such as using parameterized queries or pre-defined allowed values.
  3. Step 3: If direct command execution is unavoidable, implement a strict sanitisation function that removes or escapes all shell metacharacters (e.g., `;`, `|`, `&`, `>`, `<`, etc.).

4.3 Config or Code Example

Before

After

4.4 Security Practices Relevant to This Vulnerability

Several security practices can help prevent OS Command Injection vulnerabilities. Least privilege reduces the impact of exploitation by limiting the permissions available to the web application process. Input validation ensures that only safe data is processed, blocking malicious payloads. Safe defaults minimise the risk of accidental command execution.

  • Practice 1: Implement least privilege for the web application user account, restricting access to only necessary system resources.

4.5 Automation (Optional)

If using a CI/CD pipeline, integrate SAST tools to automatically scan for potential command injection vulnerabilities during code commits.

# Example Bash script snippet for automated scanning
  sast-scan --target /path/to/application/code --ruleset command_injection

5. Verification / Validation

Confirm the fix by attempting to inject malicious payloads through the application’s input fields and verifying that they are not executed as commands. A negative test involves submitting a known-malicious payload and confirming no unexpected behaviour occurs.

  • Post-fix check: Submit “test; ls -la” to the vulnerable form. Expected output should be “test” only, without any directory listing or error messages.
  • Re-test: Re-run the SAST scan used during detection and confirm that no command injection vulnerabilities are reported.
  • Monitoring: Monitor web server logs for any unexpected processes spawned or unusual activity related to command execution. Example query: search for `system()` calls with suspicious arguments.
#Example output from post-fix test
test

6. Preventive Measures and Monitoring

Regularly update security baselines and policies to include specific rules against OS Command Injection vulnerabilities, such as requiring input validation for all user-supplied data. Incorporate checks in CI/CD pipelines (SAST, SCA) to identify potential issues early in the development lifecycle. Implement a sensible patch or configuration review cycle based on risk assessment.

  • Baselines: Update security baselines to include CIS controls related to input validation and command execution restrictions.
  • Pipelines: Add SAST tools to CI/CD pipelines to scan for potential command injection vulnerabilities during code commits.
  • Asset and patch process: Review application configurations and dependencies regularly, applying patches promptly to address known vulnerabilities.

7. Risks, Side Effects, and Roll Back

  • Risk or side effect 2: Incorrectly implemented sanitisation could still allow some malicious payloads to bypass security checks. Mitigation: use well-tested and validated sanitisation functions.

8. References and Resources

Related Articles