Shell Metacharacters and Command Chaining
You'll learn which special characters in shell commands let attackers chain multiple commands together to escalate command injection exploits.
What Are Shell Metacharacters?
In command injection attacks, certain characters have special meaning to shells (like bash or cmd.exe). These metacharacters don't just pass through as literal text—they instruct the shell to do something extra, like running additional commands, redirecting output, or executing code in the background.
Think of them like punctuation marks in a sentence: they change how the shell interprets your command.
The Dangerous Characters
When user input containing these characters reaches a system shell, attackers can break out of the intended command and execute their own:
;(semicolon): Runs commands sequentially.whoami; cat /etc/passwdruns both commands one after another.&(ampersand): Runs a command in the background.ping 8.8.8.8 & whoamiexecutes both, withpingcontinuing in the background.&&(double ampersand): Runs the second command only if the first succeeds.ls && whoamirunswhoamionly iflsworked.|(pipe): Sends one command's output into another.cat /etc/passwd | grep rootchains processing.||(double pipe): Runs the second command only if the first fails.`(backtick) and$(): Command substitution—executes the inner command first.echo `whoami`runswhoamiand inserts its output.
Why This Matters
If an application passes unsanitized user input like ; rm -rf / into a system command, the semicolon chains a destructive command to whatever the app intended to run. Attackers exploit this to read files, exfiltrate data, establish reverse shells, or completely compromise systems.
Key Takeaway:
Shell metacharacters like ;, |, &, and backticks enable command chaining, turning simple command injection into full system compromise—always validate and sanitize input before passing it to system shells.