cmd injection refers to executing sys command directly on the backend.
- if webapp uses user-controlled input to execute a sys cmd to retrieve + return specific output we can leverage this with malicious input
number 3 risk in owasp 10 https://owasp.org/www-project-top-ten/
Injection occurs when user-controlled input is misinterpreted as part of web query or code being executed
| Injection | Description |
|---|---|
| OS Command Injection | Occurs when user input is directly used as part of an OS command. |
| Code Injection | Occurs when user input is directly within a function that evaluates code. |
| SQL Injections | Occurs when user input is directly used as part of an SQL query. |
| Cross-Site Scripting/HTML Injection | Occurs when exact user input is displayed on a web page. |
Many other types of injection:
LDAP injectionNoSQL InjectionHTTP Header injectionXPath injectionIMAP injectionORM injection
Essentially - any time user input is used within a query without being properly sanitized it is possibly susceptible to command injections, by escaping boundaries of user input string to the parent query leading to further manipulation.
OS Command injections
User input we control directly/indirectly goes into a web query that executes system commands. All web prog langs have different functions that enable devs to execute OS level commands directly on the backend when needed.
<?php
if (isset($_GET['filename'])) {
system("touch /tmp/" . $_GET['filename'] . ".pdf");
}
?>
i.e. webapp in PHP may use exec , system, shell_exec, passthru, or popen functions
The above is vulnerable to command injections.
filename parameter is user-controlled in the GET request and used directly with the touch command without being sanitized or escaped first.
This isn't unique to PHP only. The below is in NodeJS using child_process.exec (child_process.spawn would have the same behavior)
app.get("/createfile", function(req, res){
child_process.exec(`touch /tmp/${req.query.filename}.txt`);
})
Is also vulnerable to cmd inj. Uses filename from GET request directly without sanitization. Both PHP and NodeJS can be exploited the same.