本文旨在阐明和探索Python 2.x中input()函数中的漏洞。在Python 3中,raw_input()函数已被删除,其功能已转移到称为input()的新内置函数中。
在Python 2.x中输入数据的办法
在Python 2.x中有两种常见的吸收输入的方法:

1)利用input()函数:此函数按原样利用您输入的输入的值和类型,而无需修正任何类型。
2)利用raw_input()函数:此函数将您供应的输入明确转换为字符串类型。
让我们利用以下程序确定两者之间的差异:
# 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)
输入:
Hello456[1,2,3]45"goodbye"[1,2,3]
输出:
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 totest input() function: <type 'str'>Enter input to test input() function: <type 'list'>
把稳:在input()函数中供应字符串输入时,我们必须将值括在双引号中。 raw_input()不须要此操作。
input()方法中的漏洞
input()方法中的漏洞在于,任何人都可以利用变量或方法的名称来访问访问输入值的变量。让我们逐一磋商这些:
1)变量名称作为输入参数:具有输入变量值的变量可以直接访问输入变量的值。
# 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
输入:
15
输出:
Pick a number between 1 to 500Guess the number: You loseGuess the number:
输入:
secret_number
输出:
Pick a number between 1 to 500Guess the number: You win
可以看出,在第二种情形下,变量“ secret_number”可以直接作为输入给出,答案始终是“ You won”。它像直接输入数字一样评估变量,这意味着它将始终返回True布尔值。利用raw_input,由于它不许可直接读取变量,以是将是不可能的。
2)函数名称作为参数:漏洞就在这里,由于我们乃至可以供应函数名称作为输入和访问值,否则这些值将不被访问。
# 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"
输入:
400secretfunction()
输出:
Raw_input(): Guess secret number: wrong answerInput(): Guess the secret number: You guessed correct
在这组输入/输出中,我们可以看到,当利用raw_input时,必须输入精确的数字。 但是,在利用input()函数时,我们乃至可以供应函数或变量的名称,编译器将对其进行评估。
例如,在这里,已将input()函数的输入作为函数“secretfunction()”的名称给出。编译器会评估此函数调用并返回我们希望找到的密码,因此纵然我们未输入密码,我们的if条件也将为真。
输入:
secretfunction()secret_value
输出:
Raw_input(): Guess secret number: wrong answerInput(): Guess the secret number: You guessed correct
如第一点所述,在本示例中,我们还可以在“ input()”函数的输入中大略地输入变量名“secret_number”,并且可以访问secret值。
但是,当考试测验在raw_input()函数的输入中调用secretfunction()时,由于编译器将参数转换为字符串,并且不将其评估为函数调用,因此它为false。
防止输入漏洞
始终最好在python 2.x中利用raw_input(),然后将输入显式转换为我们须要的任何类型。例如,如果我们希望输入整数,则可以实行以下操作
n = int(raw_input())
这可以防止恶意调用或评估函数。