0
hidden = random.random()
val = input("Guess the value of the randomly generated number. ")
if float(val) == hidden:
     print("You got it!")
else:
     print("Sorry. You missed.")

嘿伙计们,有没有办法利用这个原始输入,并从程序中泄露“隐藏”变量的值?换句话说,你能单独从输入中执行一行代码吗?我尝试格式化字符串攻击程序,但没有奏效。注意:您不能更改代码。

有什么建议么?

4

2 回答 2

1

在 Python 2 中,是的,因为你没有使用raw_input,所以你使用了input. 所以你可以在提示符下输入“hidden”并神奇地把它弄好(因为它会评估名为 的变量的值hidden):

Guess the value of the randomly generated number. hidden
You got it!

在 Python 3 中,不,因为input现在做了raw_input过去所做的事情,即仅将值视为字符串,以避免这种鬼鬼祟祟。

于 2014-12-01T03:02:43.697 回答
0

您可以input(在 Python 2.x 中)计算任意表达式,其中包括write适当对象(例如,文件对象)的方法:

$ python tmp.py
Guess the value of the randomly generated number. open("/dev/tty", "w").write(str(hidden))
0.111568033994Traceback (most recent call last):
  File "tmp.py", line 6, in <module>
    if float(val) == hidden:
TypeError: float() argument must be a string or a number

输入字符串open("/dev/tty", "w").write(str(hidden))将触发 a TypeError,但不会在评估字符串之前触发,这会导致将 的值hidden写入当前终端。这个例子有点依赖于操作系统,因为它假设/dev/tty存在,但我只用它来使值立即显示。用户有权写入的任何文件都可以使用,并且可以在 Python 脚本存在后查看文件的内容。

如果代码已包含import sys,则可以改用输入sys.stdout.write(hidden)

如果代码已包含from __future__ import print_function,则可以改用输入print(hidden)

于 2014-12-01T03:23:01.653 回答