CRITICAL
Rule Definition
The software constructs all or part of an OS command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended OS command when it is sent to a downstream component.
This could allow attackers to execute unexpected, dangerous commands directly on the operating system. This weakness can lead to a vulnerability in environments in which the attacker does not have direct access to the operating system, such as in web applications.
Remediation
Assume all input is malicious.
Avoid using inputs. If it is not possible, use an "accept known good" input validation strategy, i.e., use stringent white-lists that limit the character set based on the expected value of the parameter in the request. This will indirectly limit the scope of an attack. Use high-level libraries for common OS tasks.
Violation Code Sample
import os
# tainted input
path = input("File path")
# high-level API for OS operations
os.remove(path) # VIOLATION
# OS command execution API
command = "rm {}".format(path)
os.system(command) # VIOLATION
Fixed Code Sample
path = input("File path")
basedir = "/mypath/"
# use of high-level OS operation API and validation of path
if os.path.abspath(path).startswith(basedir):
os.remove(path)
else:
print("Error: incorrect base directory.")
Reference
CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
https://cwe.mitre.org/data/definitions/78.html
Open Web Application Security Project (OWASP)
https://www.owasp.org/index.php/Top_10_2007
CISQ rule: ASCSM-CWE-078.
Related Technologies
Technical Criterion
CWE-78 - Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.