Python 教程之输入输出(5)—— input() 函数中的漏洞 – Python 2.x

举报
海拥 发表于 2022/07/31 14:29:21 2022/07/31
【摘要】 本文旨在解释和探讨 Python 2.x 中 input() 函数的漏洞。在 Python 3 中,raw_input() 函数被删除,它的功能被转移到一个新的内置函数,称为 input()。 在 Python 2.x 中输入数据的不同方式在 Python 2.x 中有两种常用的接收输入的方法:使用input() 函数: 此函数按原样获取您输入的输入的值和类型,而无需修改任何类型。使用raw...

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

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

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

  1. 使用input() 函数: 此函数按原样获取您输入的输入的值和类型,而无需修改任何类型。
  2. 使用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: <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'>

注意: 在 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())

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

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。