首页>>python >> Python2.x中input(函数的漏洞)

Python2.x中input(函数的漏洞)

时间:2023-10-28 19:02:33 网络整理 点击:207

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

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

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

使用input() 函数: 此函数按原样获取您输入的输入的值和类型,而无需修改任何类型。使用raw_input() 函数:此函数将您提供的输入显式转换为字符串类型,

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

# Python 2.x 程序显示两者之间的差异
# input() 和 rawinput() 函数
# 使用 raw_input() 函数的 3 个输入,
# 之后显示输入值的数据类型
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)
# 使用 input() 函数的 3 个输入,
# 之后显示输入值的数据类型
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)

输入:

Hello
456
[1,2,3]
45
"goodbye"
[1,2,3]

输出:

Enter input to test raw_input() function: 
Enter input to test raw_input() function: 
Enter input to test raw_input() function: 
Enter input to test input() function: 
Enter input to test input() function: 
Enter input to test input() function: 

注意: 在 input() 函数中输入字符串时,我们必须用双引号将值括起来。这在 raw_input() 中不是必需的

input() 方法中的漏洞

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

变量名作为输入参数:

具有输入变量值的变量能够直接访问输入变量的值。

Python 2.x 程序使用变量显示 input() 函数中的漏洞

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 3 演示 input() 函数的差异

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 500
Guess the number: You lose
Guess the number: 

输入:

secret_number

输出:

Pick a number between 1 to 500
Guess the number: You win

可以看出,在第二种情况下,变量“secret_number”可以直接作为输入给出,答案总是“你赢了”。它像直接输入数字一样评估变量,这意味着它始终返回 True Boolean。无法使用 raw_input,因为它不允许直接读取变量。

Python 3 显示了不同的结果。如果“secret_number”作为输入,答案是“You lose”。

函数名作为参数:

漏洞就在这里,因为我们甚至可以提供函数的名称作为输入并访问原本不应该访问的值。

# Python 2.x 程序通过传递函数名作为参数来演示 input() 函数漏洞
secret_value = 500
# 返回秘密值的函数
def secretfunction():
	return secret_value
# 使用 raw_input() 输入数字
input1 = raw_input("Raw_input(): Guess secret number: ")
# input1 将被显式转换为字符串
if input1 == secret_value:
	print "You guessed correct"
else:
	print "wrong answer"
	
# 使用 input() 输入数字
input2 = input("Input(): Guess the secret number: ")
# input2 在输入时进行评估
if input2 == secret_value:
	print "You guessed correct"
else:
	print "wrong answer"

输入:

400
secretfunction()

输出:

Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct

在这组输入/输出中,我们可以看到,当我们使用 raw_input 时,我们必然要输入正确的数字。然而,在使用 input() 函数时,我们甚至可以提供函数或变量的名称,解释器将对其进行评估。例如,这里的 input() 函数的输入被指定为函数“secretfunction()”的名称。解释器评估这个函数调用并返回我们希望找到的秘密数字,因此即使我们没有输入秘密数字,如果条件评估为真,我们也会返回:

secretfunction()
secret_value

输出:

Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct

正如第一点所解释的,在这个例子中,我们也能够在“input()”函数的输入中简单地输入变量名“secret_number”,我们就能够访问秘密值。然而,当试图在 raw_input() 函数的输入中调用 secretfunction() 时,它给了我们错误,因为解释器将我们的参数转换为字符串,并且不将其评估为函数调用。

防止输入漏洞

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

n = int(raw_input())

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

《Python2.x中input(函数的漏洞)》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
下载文档

文档为doc格式