Course contentsShow
← Security
Lesson 1229 of 3,05225. Application Security - Secure CodingFree lesson

Command Execution and Shell Injection Risks

Understand why system(), exec() with shell=True, and eval() are dangerous and enable command injection vulnerabilities.

Command Execution and Shell Injection Risks

What You'll Learn

You'll discover why functions like system(), exec() with shell interpretation, and eval() are considered dangerous functions that can let attackers run arbitrary commands on your server.

The Core Problem

Some programming functions don't just process data—they execute it as code. When you pass user input to these functions, you're essentially handing attackers a remote control to your system.

Think of it like giving someone a Post-it note to write a message on your whiteboard. If they write "Hello!" that's fine. But if they write "Transfer all money to Account X and delete the security logs," suddenly that Post-it note becomes a dangerous weapon.

The Dangerous Trio

system() and Similar OS Command Functions

Functions like system(), os.system(), or shell_exec() pass strings directly to the operating system's command shell. If user input sneaks into these strings, attackers can inject additional commands using shell metacharacters like ;, |, or &&.

exec() with shell=True

Many languages offer exec() functions. When you enable shell interpretation (like Python's shell=True parameter), the function spawns a shell to parse the command, creating the same injection risks.

eval()

The eval() function executes strings as code in your programming language itself. Passing user input to eval() is like allowing attackers to write and run their own program inside yours.

Why They Enable Command Injection

These functions lack context awareness—they can't distinguish between your intended command and malicious input. User data isn't treated as passive parameters; it's interpreted as active instructions.

Key Takeaway: Functions that execute strings as commands or code (system(), exec() with shell mode, eval()) are prime command injection vectors because they treat user input as executable instructions rather than passive data.