python - 为什么我们需要添加一个“sleep”的方法来让一个恒定时间攻击成功呢?
问题描述
在这个网站: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 函数所需的时间。
我试过了,我们需要一个“休眠”的方法来让这个攻击成功。为什么我们需要夸大时间?
解决方案
编辑1:
为什么我们需要夸大时间?
我们需要夸大时间来展示两个角色匹配时和不匹配时的时差。所以在这种情况下,如果 a 和 b 的第一个字符匹配,则方法休眠,然后如果第二个字符不匹配,则函数返回。这需要 1 个比较时间 + 睡眠(0.15)+ 1 个比较时间。另一方面,如果第一个字符不匹配,则函数在 1 个比较时间内返回,因此攻击者可以看到它们是否匹配任何字符。该示例使用此睡眠来演示此时间差。
为了不发生这种情况,应该以某种方式实现 is_equal 函数,即函数的响应时间是静态的。
使用您提供的示例:
def is_equal(a,b):
_is_equal = True
if len(a) != len(b):
return False
for i in range(len(a)):
if a[i] != b[i]:
_is_equal = False
return _is_equal
secrets 模块中有一个内置函数可以解决这个问题。 比较摘要()
推荐阅读
- winapi - Nasm - 错误的 win32n.inc 没有为结构定义中的数组保留空间?
- ios - 为 iOS 进行 Xcode 调试时 GStreamer 不需要的启动延迟和单元测试崩溃
- selenium - 无头 chrome 驱动程序将所有 url 从 http 重定向到 https
- r - 在页面上布置多个 ggplot 图
- html - URL 中的 HTML 扩展是否会影响 Google Analytics 跟踪或将营销像素添加到网站内各个页面的能力?
- javascript - Google Sheet 脚本重置下拉框
- laravel - 如何在 Laravel 中安全卸载 Voyager?
- html - 根据用户输入隐藏 HTML 行
- charts - Vega 图表:如何从图表中隐藏 0 值,但在轴上保留信息?
- javascript - Python 中 pywinauto 的 Node.js 等价物是什么?