本文旨在解釋和探索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>
這可以防止惡意調用或評估函數。
閱讀更多 少兒編程Prog61 的文章