Vulnerability in input() function – Python 2.x
Last Updated :
29 Mar, 2024
This article aims to explain and explore the vulnerability in the input() function in Python 2.x. In Python 3, the raw_input() function was erased, and its functionality was transferred to a new built-in function known as input().
Different Ways to Input Data in Python 2.x
There are two common methods to receive input in Python 2.x:
- input() function: This function takes the value and type of the input you enter as it is without modifying any type.
- raw_input() function: This function explicitly converts the input you give to type string,
Let us use the following program to determine the difference between the two:
Python
s1 = raw_input("Enter input to test raw_input() function: ")
print type(s1)
s2 = raw_input("Enter input to test raw_input() function: ")
print type(s2)
s3 = raw_input("Enter input to test raw_input() function: ")
print type(s3)
s4 = input("Enter input to test input() function: ")
print type(s4)
s5 = input("Enter input to test input() function: ")
print type(s5)
s6 = input("Enter input to test input() function: ")
print type(s6)
|
Input:
Hello
456
[1,2,3]
45
"goodbye"
[1,2,3]
Output:
Enter input to test raw_input() function: <type 'str'>
Enter input to test raw_input() function: <type 'str'>
Enter input to test raw_input() function: <type 'str'>
Enter input to test input() function: <type 'int'>
Enter input to test input() function: <type 'str'>
Enter input to test input() function: <type 'list'>
Note: While giving string input in the input() function, we have to enclose to value in double-quotes. This is not required in raw_input()
Vulnerability in input() Method
The vulnerability in input() method lies in the fact that the variable accessing the value of input can be accessed by anyone just by using the name of the variable or method. Below are some vulnerability in input() method:
- Variable name as input parameter:
- Function name as parameter
Variable Name as Input Parameter
The variable having the value of input variable is able to access the value of the input variable directly.
Python3
import random
secret_number = random.randint(1,500)
print ("Pick a number between 1 to 500")
while True:
res = input("Guess the number: ")
if res==secret_number:
print ("You win")
break
else:
print ("You lose")
continue
|
Python
import random
secret_number = random.randint(1,500)
print "Pick a number between 1 to 500"
while True:
res = input("Guess the number: ")
if res==secret_number:
print "You win"
break
else:
print "You lose"
continue
|
Input1:
15
Output1:
Pick a number between 1 to 500
Guess the number: You lose
Guess the number:
Input2:
secret_number
Output2:
Pick a number between 1 to 500
Guess the number: You win
As it can be seen, in the second case the variable “secret_number” can be directly given as input and the answer is always “You won”. It evaluates the variable as if a number was directly entered, which means it returns a True Boolean always. Using raw_input, would not be possible as it disallows reading the variable directly.
Python 3 shows different results. If “secret_number” is given as input, answer is ‘You lose’.
Function Name as Parameter
The vulnerability lies here as we can even provide the name of a function as input and access values that are otherwise not meant to be accessed. In this set of input/output, we can see that when we use raw_input, we necessarily have to input the correct number. However while using the input() function, we can even provide the name of a function or variable, and the interpreter will evaluate that. Here for example, the input for input() function has been given as the name of a function ‘secretfunction()’. The interpreter evaluates this function call and returns the secret number that we wish to find and hence our if the condition evaluates to be true, even though we did not enter the secret number.
Python
secret_value = 500
def secretfunction():
return secret_value
input1 = raw_input("Raw_input(): Guess secret number: ")
if input1 == secret_value:
print "You guessed correct"
else:
print "wrong answer"
input2 = input("Input(): Guess the secret number: ")
if input2 == secret_value:
print "You guessed correct"
else:
print "wrong answer"
|
Input1:
400
secretfunction()
Output1:
Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct
As explained in the first point, in this example also we were able to simply enter the variable name ‘secret_number’ in the input for ‘input()’ function and we were able to gain access to the secret value. However, while trying to call secretfunction() in the input for the raw_input() function, it gives us false as the interpreter converts our argument to a string, and doesn’t evaluate it as a function call.
Input2:
secretfunction()
secret_value
Output2:
Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct
Preventing Input Vulnerabilities
It is always better to use raw_input() in python 2.x and then explicitly convert the input to whatever type we require. For example, if we wish to take the input of an integer, we can do the following
n = int(raw_input())
This prevents the malicious calling or evaluation of functions.
Similar Reads
Vulnerability in input() function – Python 2.x
This article aims to explain and explore the vulnerability in the input() function in Python 2.x. In Python 3, the raw_input() function was erased, and its functionality was transferred to a new built-in function known as input(). Different Ways to Input Data in Python 2.xThere are two common method
5 min read
turtle.numinput() function in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.numinput() This function is used to pop up a dialog window for the input of
1 min read
Python 3 - input() function
In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string. Python input() Function SyntaxSyntax: input(prompt) Parameter: Prompt: (optio
3 min read
randint() Function in Python
randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python. Python randint() Method SyntaxSyntax: randint(st
6 min read
Python input() Function
Python input() function is used to take user input. By default, it returns the user input in form of a string. input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input message Ex: input("What is your name? ") Returns: Return a string value as input by the user.
4 min read
type() function in Python
The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three argument types (object, bases, dict) are passed, it re
5 min read
id() function in Python
In Python, id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location. Python id() Fun
3 min read
Assign Function to a Variable in Python
In this article, we are going to see how to assign a function to a variable in Python. In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Implementation Simply assign a function to the de
3 min read
Pass a List to a Function in Python
In Python, we can pass a list to a function, allowing to access or update the list's items. This makes the function more versatile and allows us to work with the list in many ways. Passing list by Reference When we pass a list to a function by reference, it refers to the original list. If we make an
2 min read
Pass a List to a Function in Python
In Python, we can pass a list to a function, allowing to access or update the list's items. This makes the function more versatile and allows us to work with the list in many ways. Passing list by Reference When we pass a list to a function by reference, it refers to the original list. If we make an
2 min read
Using a Class with Input in Python
In this article, we will see how to take input using class in Python. Using a Class with Input in Python It is to be noted that while using class in Python, the __init__() method is mandatory to be called for declaring the class data members, without which we cannot declare the instance variable (da
2 min read
Python | Binding function in Tkinter
Tkinter is a GUI (Graphical User Interface) module that is widely used in desktop applications. It comes along with the Python, but you can also install it externally with the help of pip command. It provides a variety of Widget classes and functions with the help of which one can make our GUI more
3 min read
Collections.UserList in Python
Python Lists are array-like data structure but unlike it can be homogeneous. A single list may contain DataTypes like Integers, Strings, as well as Objects. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a
2 min read
turtle.textinput() function in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.textinput() This function is used to pop up a dialog window for the input of
1 min read
Python - assertLess() function in unittest
assertLess() in Python is an unittest library function that is used in unit testing to check whether the first given value is less than the second value or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. This function check that
2 min read
Python | Accepting Script Input
A lot of people use Python as a replacement for shell scripts, using it to automate common system tasks, such as manipulating files, configuring systems, and so forth. This article aims to describe accepting Script Input via Redirection, Pipes, or Input Files. Problem - To have a script to be able t
2 min read
How to call a function in Python
Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them. In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "d
5 min read
ord() function in Python
Python ord() function returns the Unicode code from a given character. This function accepts a string of unit length as an argument and returns the Unicode equivalence of the passed argument. In other words, given a string of length 1, the ord() function returns an integer representing the Unicode c
3 min read
Python | __import__() function
While writing a code, there might be a need for some specific modules. So we import those modules by using a single-line code in Python. But what if the name of the module needed is known to us only during runtime? How can we import that module? One can use Python's inbuilt __import__() function. It
3 min read