Python 如何破解输入

Joh*_*Doe 2 python security python-2.7

我听说使用“输入”对于密码使用是不安全的,因为它将输入解释为代码,因此有人可以绕过安全性。

你会怎么做呢?例如,你会怎么做这样的事情?

password = 1234

pass_true = input("What is the password?")

if password == pass_true:
    print("Access granted.")
else: 
    print ("Access denied.")
Run Code Online (Sandbox Code Playgroud)

ale*_*xis 5

很简单:要通过您的支票,只需password在提示处输入:-) 试试吧!

What is the password?password
Access granted.
Run Code Online (Sandbox Code Playgroud)

解释:input读取您键入的内容,将其评估为 python 代码,并将结果分配给pass_true. 所以在上面,我只是告诉它将变量password(保存真实密码)的值分配给pass_true.

但是即使经常使用,这段代码也会被破坏:如果您键入的内容看起来不像有效的 Python 表达式,则会触发错误。您的示例使用数字作为“密码”,但您不能对任意文本密码使用相同的方法:即使密码正确,它也根本不起作用。

另一种问题是os.remove(".profile")在提示符下输入一些破坏性的东西(或更糟糕和更复杂的东西)。它不会登录它们,但您可以看到它可以造成的损害。除非您正在编写交互式 Python 解释器,否则评估用户输入是错误的。

Python 2raw_input()只读取输入。(在python 3中,raw_input()已重命名,input()input()的不见了)。