在本文中,我们将学习在版本2.x中输入函数如何以不良方式运行。或更早。在2.x版中。raw_input()函数可替换该input()函数。在较新的版本3.x中。或稍后,将两个功能的所有所需特征和功能合并到该input()方法中。
首先,让我们看一下在Python 2.x中接受输入的内置函数的输入类型。
# Input Given : String
str1 = raw_input("Output of raw_input() function: ")
print type(str1)
str2 = input("Output of input() function: ")
print type(str2)
# Input Given : Float
str3 = raw_input("Output of raw_input() function: ")
print type(str3)
str4 = input("Output of input() function: ")
print type(str4)
# Input Given : Integer
str5 = raw_input("Output of raw_input() function: ")
print type(str5)
str6 = input("Output of input() function: ")
print type(str6)输出结果
Output of raw_input() function: Output of input() function: Output of raw_input() function: Output of input() function: Output of raw_input() function: Output of input() function:
解释-从输出中可以很明显地看出raw_input函数将输入显式转换为字符串类型,而与提供的输入类型无关。相反,输入函数保留与输入期间提供的数据类型相同的数据类型。
现在,在看到上面的示例之后,您可能想知道,如果输入函数保留了数据类型,那么为什么它容易受到攻击?让我们用插图来澄清一下-
import random as rd
number = random.randint(1,6)
print ("Pick a number between 1 to 6")
while True:
user_input = input("Guess the number: ")
if user_input==number:
print ("你猜对了。")
break
else:
print ("OOPS! try it next time.")
continue解释-如果用户提供整数输入,则将根据条件表达式计算所需的输出。
如果用户提供了一个字符串输入,即与变量名相同,在该变量名中我们使用random模块存储了由色子生成的随机整数,那么还将计算输出。但这不一定是我们要计算的期望输出。实际上,当输入字符串时,它必须引发一个错误,错误的输入类型。它认为变量名称等效于用户直接输入的数字,该表达式产生True布尔值,游戏结束。相反,如果我改用raw_input(),则不会遇到此类问题。
如果我们存储登录凭据,用户详细信息和帐户密码,则此漏洞可能致命。
stored_value = 7863 def return_function(): return stored_value inp = input()if inp == stored_value: print "You Entered Correctly" else: print "Oops! It's Incorrect"
正如我们在前面的ILLUSTRATION中讨论的那样,如果提供的输入为整数类型,则该函数正常工作。但是在任何情况下,用户提供与函数的返回值相同的输入,条件变为True并产生输出。
在处理重要且机密的信息(例如密码和密码)的情况下使用,这非常危险。这可以通过使用Python 2.x中提供的raw_input()来克服。
从以上两个说明中可以很明显地看出,输入函数使程序可以直接进行变量攻击。
在本文中,我们了解了input()在Python 2.x中使用该函数时遇到的所有问题和漏洞。需要。