When an application sends untrusted user input to a system shell (such as bash, sh, or cmd) without adequate validation or sanitization, it creates a critical security flaw known as OS Command Injection. Attackers can insert extra commands that the operating system will run if user-controlled input is directly included in a command string that is run by functions like system(), exec(), popen(), or backticks.
Example of vulnerable code in python:
If a user inputs:
The resulting command becomes:
This allows the attacker to run arbitrary system commands — in this case, potentially deleting all files on the server.
Because it gives attackers direct access to the host operating system, frequently with the same privileges as the web server or backend application, OS Command Injection is extremely dangerous. Possible repercussions:
Attackers have the ability to install malware, read, write, or remove files, and open reverse shells through arbitrary command execution.
The system as a whole may be compromised if the program is executed with elevated privileges (such as root/admin).
Database dumps, credentials, logs, and environment variables are examples of sensitive data that can be exfiltrated.
Attackers have the ability to erase important files or overload system resources.
Attacks on other internal systems can be launched from compromised systems.
Mitigating OS Command Injection involves strict input handling, avoiding unsafe execution methods, and using secure alternatives wherever possible.