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

Shell Metacharacters and Command Chaining

Learning dangerous shell characters like semicolons, pipes, ampersands, and backticks that enable command chaining.

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/passwd runs both commands one after another.
  • & (ampersand): Runs a command in the background. ping 8.8.8.8 & whoami executes both, with ping continuing in the background.
  • && (double ampersand): Runs the second command only if the first succeeds. ls && whoami runs whoami only if ls worked.
  • | (pipe): Sends one command's output into another. cat /etc/passwd | grep root chains processing.
  • || (double pipe): Runs the second command only if the first fails.
  • ` (backtick) and $(): Command substitution—executes the inner command first. echo `whoami` runs whoami and 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.