Lab 03: OS Command Injection

Objective

Exploit OS command injection in a live network diagnostic API from Kali Linux:

  1. Basic injection — append ;id and &&whoami to a ping host parameter

  2. Pipe chaining — use | to replace command output entirely

  3. Blind injection — write files to prove code execution when output is hidden

  4. os.popen / subprocess.run variants — exploit three different vulnerable code patterns


Background

Command injection occurs when user input is passed to a system shell without sanitisation. Unlike SQLi which is database-specific, OS command injection gives an attacker direct shell access on the host.

Real-world examples:

  • 2021 Pulse Connect Secure (CVE-2021-22893) — command injection via file path parameter; CVSS 10.0; exploited by state-sponsored groups against US government agencies before a patch existed.

  • 2022 Confluence Server (CVE-2022-26134) — OGNL template injection leading to OS command execution; mass exploitation within 24 hours of disclosure.

  • 2014 Shellshock (CVE-2014-6271) — Bash environment variable injection via () { :; };; millions of servers exploitable via HTTP headers, CGI scripts, DHCP, SSH.

OWASP: A03:2021 Injection


Architecture

Time

40 minutes


Lab Instructions

Step 1: Setup


Step 2: Launch Kali


Step 3: Basic Injection — Semicolon and Pipe

📸 Verified Output:


Step 4: Exploit All Vulnerable Endpoints

📸 Verified Output:


Step 5: Blind Injection — When Output Is Hidden

📸 Verified Output:


Step 6: Read Application Secrets


Step 7: Automated Discovery with Commix


Step 8: Cleanup


Remediation

Defence
What it prevents

Avoid shell=True

Shell metacharacter injection (;, `

Input allowlist

Only allow expected characters for each parameter type

Parameterised exec

Arguments passed as list, never concatenated into shell string

Principle of least privilege

Web process runs as non-root; limits blast radius

Further Reading

Last updated