在这个网站:http ://elijahcaine.me/remote-timing-attacks/作者很好地描述了什么是恒定时间攻击以及如何防范这种类型的漏洞。
但是在作者所做的代码中:
# secret.py
from time import sleep # Used to exaggerate time difference.
from sys import argv # Used to read user input.
def is_equal(a,b):
"""Custom `==` operator"""
# Fail if the strings aren't the right length
if len(a) != len(b):
return False
for i in range(len(a)):
# Short-circuit if the strings don't match
if a[i] != b[i]:
return False
sleep(0.15) # This exaggerates it just enough for our purposes
return True
# Hard-coded secret globals FOR DEMONSTRATIONS ONLY
secret = 'l33t'
# This is python for "If someone uses you as a script, do this"
if __name__ == '__main__':
try:
# The user got it right!
if is_equal(str(argv[1]), secret):
print('You got the secret!')
# The user got it wrong
else:
print('Try again!')
# The user forgot to enter a guess.
except IndexError:
print('Usage: python secret.py yourguess\n' \
+'The secret may consist of characters in [a-z0-9] '\
+'and is {} characters long.'.format(len(secret)))
我不明白为什么我们必须添加这一行才能使恒定时间攻击成功:
sleep(0.15) # This exaggerates it just enough for our purposes
在网站上,作者说:
它夸大了评估 is_equal 函数所需的时间。
我试过了,我们需要一个“休眠”的方法来让这个攻击成功。为什么我们需要夸大时间?