blog image

Python is an unambiguous, easy-to-read, general-purpose, high-level programming language that considers structured, procedural, and object-oriented programming paradigms. Invented in the late 1980s by Guido van Rossum, Python was designed to provide code readability. It has a relatively uncluttered visual layout and uses English keywords, whereas other languages use punctuation. It also has fewer syntactical constructions than other languages.

Python aims to combine "remarkable power with the self-explanatory syntax," Its standard library is extensive and comprehensive. Its use of indentation for block delimiters improves code readability. Python's clean object model and powerful typing system make it an attractive choice for software development. Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

Vulnerability in input() function – Python 2.x

Python’s input() function allows developers to read data from standard input devices like keyboards. However, due to the way this function processes data, it is vulnerable to malicious user input. This can lead to security issues such as denial-of-service attacks or arbitrary code execution.

The issue stems from the fact that input() evaluates the data it reads as valid Python code. This means that if an attacker injects malicious code into the input stream, it will be executed by the Python interpreter.

There are two ways to mitigate this issue. The first is to use the raw_input() function instead of input(). This function does not evaluate the data it reads, so it is not vulnerable to code injection attacks.

The second way to mitigate this issue is to use the ast.literal_eval() function to evaluate the data read by input(). This function only allows Python literals (such as strings, integers, floats, etc.) to be evaluated, so it will not execute arbitrary code.

It is important to note that ast.literal_eval() can still be bypassed if the attacker uses carefully crafted input data. For example, the following input would cause ast.literal_eval() to raise an exception and return “None”:

‘__import__(“os”).system(“rm -rf /”) or 1/0?

This input would be evaluated as “None”, but the rm command would still be executed. Therefore, it is important to carefully validate all user input before passing it toast.literal_eval().

The best way to prevent code injection attacks is to use a whitelist of approved input characters. This can be done using the re module in Python. For example, the following code will only allow alphanumeric characters and spaces to be passed to input():

import re user_input = input() if not re.match(‘^\w+$’, user_input): print(‘Invalid input!’) else:

print(‘Valid input: ‘ + user_input)

This code will raise an exception if the user input contains any characters that are not alphanumeric or whitespace.

It is also possible to use a blacklist of disallowed characters. However, this approach is less secure than whitelisting, as it is easier for attackers to find an approved character that can be used to bypass the blacklist. For example, the following code will allow all characters except for “=” and “;”:

import re user_input = input() if‘([=;])’, user_input): print(‘Invalid input!’) else: print(‘Valid input: ‘ + user_input)

While this code will prevent most code injection attacks, it can still be bypassed by using Unicode characters that are visually similar to “=” or “;”. For example, the following input would be considered valid:


This input would be evaluated as a string, but it would also execute the JavaScript alert function. Therefore, it is important to use a whitelist of approved characters if possible.

Python 2.x is no longer supported by the Python community. Therefore, all users should upgrade to Python 3. x in order to mitigate this issue.

Python 3. x includes a number of security improvements, including better input handling.

If you’re a developer, it’s important to never use the input() function without first filtering or validating the user input. One way to do this is to use the shlex module to parse the input and only allow certain characters.

If you’re a user, you can protect yourself from shell injection attacks by never entering untrusted input into applications that use the input() function. Additionally, you can install a firewall that blocks connections from untrusted sources. If you are using Python 2. x, you should either switch to using raw_input() or sanitize your input before passing it to input(). This will protect you from this vulnerability.