Shell injection
As the name suggests, shell injection or command injection allows an attacker to inject malicious code into a system shell such as bash. Even web applications use command-line programs for convenience and their functionality. Such processes are typically run within a shell.
For example, if you want to show all the details of a file whose name is given by the user, a naïve implementation would be as follows:
os.system("ls -l {}".format(filename))
An attacker can enter the filename as manage.py; rm -rf *
 and delete all the
files in your directory. In general, it is not advisable to use os.system
. The subprocess module is a safer alternative (or even better, you can use os.stat()
 to get the file's attributes).
Since a shell will interpret the command-line arguments and environment variables, setting malicious values in them can allow the attacker to execute arbitrary system commands.
How Django helps
Django primarily depends on WSGI for deployment. Since WSGI, unlike CGI, does not...