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

Command Injection Fundamentals

Understanding how attackers inject OS commands through application inputs and the difference from code injection.

Command Injection Fundamentals

You'll learn: What command injection is, how it differs from other injection attacks, and why it's one of the most dangerous web vulnerabilities.

What is Command Injection?

Command injection occurs when an attacker tricks an application into executing operating system commands by inserting malicious input into fields that the application passes to the system shell. Instead of injecting SQL or NoSQL queries, you're injecting shell commands that run directly on the server's operating system.

How It Works

Imagine a web application that lets users check if a website is online by running a ping command behind the scenes. The app might take your input like google.com and run:

ping -c 4 google.com

An attacker could input: google.com; ls -la or google.com && cat /etc/passwd

The application would then execute:

ping -c 4 google.com; ls -la

This runs TWO commands: the intended ping, followed by listing all server files.

Command Injection vs Code Injection

While both are injection attacks, they target different layers:

  • SQL/NoSQL Injection: Targets the database layer, manipulating queries
  • Command Injection: Targets the operating system layer, executing shell commands
  • Code Injection: Targets the application runtime (like PHP or JavaScript eval), executing application code

Command injection is typically more severe because it gives direct OS access—attackers can read sensitive files, install malware, pivot to other systems, or completely compromise the server.

Common Vulnerable Scenarios

Applications that use user input in system calls for tasks like: file processing, network diagnostics, image manipulation, or backup operations.

Key Takeaway: Command injection exploits applications that pass unsanitized user input to OS shell commands, giving attackers direct control over the server's operating system—making it critically dangerous.