input()函数中的漏洞– Python 2.x

input()函数中的漏洞– Python 2.x

本文旨在解释和探索Python 2.x中input()函数中的漏洞。在Python 3中,raw_input()函数已被删除,其功能已转移到称为input()的新内置函数中。

在Python 2.x中输入数据的方式

在Python 2.x中有两种常见的接收输入的方法:

1)使用input()函数:此函数按原样使用您输入的输入的值和类型,而无需修改任何类型。

2)使用raw_input()函数:此函数将您提供的输入明确转换为字符串类型。

让我们使用以下程序确定两者之间的区别:

<code># Python 2.x program to show differences between 
# input() and rawinput()function

# 3 inputs using raw_input() function,
# after which data type of the value
# entered is displayed
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)

# 3 inputs using input() function,
# after which data type of the value
# entered is displayed
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) /<code>

输入:

<code>Hello
456
[1,2,3]
45
"goodbye"
[1,2,3]/<code>

输出:

<code>Enter input to test raw_input() function: <type>
Enter input to test raw_input() function: <type>
Enter input to test raw_input() function: <type>

Enter input to test input() function: <type>
Enter input totest input() function: <type>
Enter input to test input() function: <type>/<type>/<type>/<type>/<type>/<type>/<code>

注意:在input()函数中提供字符串输入时,我们必须将值括在双引号中。 raw_input()不需要此操作。

input()方法中的漏洞

input()方法中的漏洞在于,任何人都可以使用变量或方法的名称来访问访问输入值的变量。让我们一一探讨这些:

1)变量名称作为输入参数:具有输入变量值的变量可以直接访问输入变量的值。

<code># Python 2.x program to show Vulnerabilities 
# in input() function using a variable

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/<code>

输入:

<code>15/<code>

输出:

<code>Pick a number between 1 to 500
Guess the number: You lose
Guess the number: /<code>

输入:

<code>secret_number/<code>

输出:

<code>Pick a number between 1 to 500
Guess the number: You win/<code>

可以看出,在第二种情况下,变量“ secret_number”可以直接作为输入给出,答案始终是“ You won”。它像直接输入数字一样评估变量,这意味着它将始终返回True布尔值。使用raw_input,因为它不允许直接读取变量,所以将是不可能的。

2)函数名称作为参数

:漏洞就在这里,因为我们甚至可以提供函数名称作为输入和访问值,否则这些值将不被访问。

<code># Python 2.x program to demonstrate input() function 
# vulnerability by passing function name as parameter
secret_value = 500

# function that returns the secret value
def secretfunction():
return secret_value

# using raw_input() to enter the number
input1 = raw_input("Raw_input(): Guess secret number: ")

# input1 will be explicitly converted to a string
if input1 == secret_value:
print "You guessed correct"
else:
print "wrong answer"

# using input() to enter the number
input2 = input("Input(): Guess the secret number: ")

#input2 is evaluated as it is entered
if input2 == secret_value:
print "You guessed correct"
else:
print "wrong answer"/<code>

输入:

<code>400
secretfunction()/<code>

输出:

<code>Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct/<code>

在这组输入/输出中,我们可以看到,当使用raw_input时,必须输入正确的数字。 但是,在使用input()函数时,我们甚至可以提供函数或变量的名称,编译器将对其进行评估。

例如,在这里,已将input()函数的输入作为函数“secretfunction()”的名称给出。编译器会评估此函数调用并返回我们希望找到的密码,因此即使我们未输入密码,我们的if条件也将为真。

输入:

<code>secretfunction()
secret_value/<code>

输出:

<code>Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct/<code>

如第一点所述,在本示例中,我们还可以在“ input()”函数的输入中简单地输入变量名“secret_number”,并且可以访问secret值。

但是,当尝试在raw_input()函数的输入中调用secretfunction()时,由于编译器将参数转换为字符串,并且不将其评估为函数调用,因此它为false。

防止输入漏洞

始终最好在python 2.x中使用raw_input(),然后将输入显式转换为我们需要的任何类型。例如,如果我们希望输入整数,则可以执行以下操作

<code>n = int(raw_input())/<code>

这可以防止恶意调用或评估函数。


分享到:


相關文章: