我有一个小请求。我在学校有一个分配,我需要通过nc连接到服务器,然后猜测号码以便找到秘密消息。我有这个服务器的源代码,我上周试图破解这个。如果你能指出我的方向是正确的那么好。
#!/usr/bin/python
import socket
import os, sys
import ast
import random
def write(msg):
sys.stdout.write(msg)
sys.stdout.flush()
def read(prompt=''):
if prompt:
write(prompt)
return sys.stdin.readline().strip()
def loop():
RANGE = 1000000
num = read('Select a number (0, 10000000000000000000000): ')
try:
num = ast.literal_eval(num) + 0
assert 0 < num < 10000000000000000000000
except:
write("Oops! Wrong number!\n\n")
return
rand = num + int(random.random()*RANGE)
guess = read('Guess a number in range: [{:n}, {:n}]: '.format(num, num+RANGE))
try:
guess = ast.literal_eval(guess)
except:
write("Oops! You probably didn't enter a number!\n\n")
return
if guess == rand:
write('Wow! The secret is [...].\n')
sys.exit(0)
write('Better luck next time!\n\n')
def main():
write('Hello there in "Guess the number!"\nYou have 30 seconds.\n')
while True:
loop()
main()
答案 0 :(得分:4)
关键是ast.literal_eval
。它允许输入浮点数。浮点数的精度为<16位。输入0.999999999999999e22
后,您也会在求和后0.999999999999999e22
得到最终结果,因此您可以输入两次以进入。
示例会话:
Hello there in "Guess the number!"
You have 30 seconds.
Select a number in range (0, 10000000000000000000000): 0.999999999999999e22
Now guess a number in range: [1e+22, 1e+22]: 0.999999999999999e22
Wow! The secret is [...].
原因是
>>> 0.999999999999999e22 + 1000000 == 0.999999999999999e+22
True
然而令人惊讶的是,0.9e22
可能会失败,但是 也不会花费很多时间:
>>> 0.9e22 + 999999 == 0.9e22
False