Home / Vulnerability Database / Python : Command injection
Python

Python : Command injection

Overview

Executing commands obtained from data from an untrusted source is insecure.

Injection vulnerabilities take the first place in the “OWASP Top 10 2017” web-application vulnerabilities ranking. Command injection vulnerabilities are divided into two categories:

  1. An attacker modifies the command itself;
  2. An attacker replaces the value of the environment variables, which implicitly changes the semantics of the command being executed.

In the given case, the application is prone to the vulnerability of the first type.

A possible attack scenario:

  1. The application receives input data from an untrusted source, for example, user input.
  2. The data obtained is used as a part of the string that defines the command.
  3. Execution of the command gives an attacker the privileges which he did not previously possess.

A lot of the time, code uses shell=True because it’s convenient. The shell provides the ability to pipe things around without buffering them in memory, and allows a malicious user to chain

  1. additional commands after a legitimate command is run.
  2. There are a lot of functions that uses curl to grab a page from a website. The functions is insecure because it uses shell=True, which allows shell injection. A user to who instructs your code to fetch the website ; rm -rf / can do terrible things to what used to be your machine.
  3. If we convert the function to use shell=False, it doesn’t work. This does not fix the issue, rather it causes it to be more broken than before.
  4. The following modules and functions are the most common for shell injections: ['subprocess.Popen', 'subprocess.call', 'subprocess.check_call', 'subprocess.check_output', 'subprocess.run', 'os.system', 'os.popen', 'os.popen2', 'os.popen3', 'os.popen4', 'popen2.popen2', 'popen2.popen3', 'popen2.popen4', 'popen2.Popen3', 'popen2.Popen4']
  5. When a command is executed with the shell = True parameter, it is executed through the shell. The program is interpreted in accordance with the syntax and semantic rules of the called shell. In this case, the user must follow these rules. Thus, the user can execute the programs being listened to.
MEDIUM

DerScanner Severity Score

Do you want to fix Python : Command injection in your application?

See also

Python

Python : Debug mode on

Python

Python : Web3: Deprecated method

Python

Python : Unsafe padding