Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我们要加上一个“;“睡眠”;使恒定时间攻击成功的方法?_Python_Security_Constant Time - Fatal编程技术网

Python 为什么我们要加上一个“;“睡眠”;使恒定时间攻击成功的方法?

Python 为什么我们要加上一个“;“睡眠”;使恒定时间攻击成功的方法?,python,security,constant-time,Python,Security,Constant Time,在这个网站上:作者很好地描述了什么是持续时间攻击,以及如何防止这种类型的漏洞 但在作者编写的代码中: # 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""&q

在这个网站上:作者很好地描述了什么是持续时间攻击,以及如何防止这种类型的漏洞

但在作者编写的代码中:

# 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
机密模块中有一个内置函数,可以解决此问题。

在“匹配”循环中有两种可能的路径:

  • 如果是[i]!=b[i]
    计算结果为
    True
    -不匹配,退出该函数
  • 如果是[i]!=b[i]
    计算结果为
    False
    -匹配,继续
    Sleep(0.15)
    ,然后离开函数
  • Sleep(0.15)
    如果字符匹配,则会在这两条路径之间增加显著的时间差。这反过来允许简单地使用所有尝试的
    max
    ,以识别机密的正确字符。在不夸张的情况下,您需要寻找匹配时间的统计显著差异

    作者在这里提到:

    (对作者来说)最重要的是,我们不需要使用StatisticsTM来 找出秘密,对每个输入进行多次评估 收集/处理这些计时数据,已经需要大约一个小时 计算匹配字母的时间比计算匹配字母的时间长 评估不匹配的字母

    使用调试行查看有无睡眠时间的不同

    # Uncomment the following line for fun debug output
    print('max {} min {}'.format(max(guess_times), min(guess_times)))
    
    # Add this line to see full guess_times list    
    print(['{:.2f}'.format(elem) for elem in guess_times])
    

    @BoriszJuasz,你没有回答这个问题。问题不在于使用什么来防止攻击,而在于博客文章中演示攻击的细节。“为什么我们需要夸大时间?我已经尝试过了,我们需要一种“睡眠”方法来让这次攻击成功。为什么?”谢谢你的回答,我误解了这个问题。我编辑了我的答案。
    # Uncomment the following line for fun debug output
    print('max {} min {}'.format(max(guess_times), min(guess_times)))
    
    # Add this line to see full guess_times list    
    print(['{:.2f}'.format(elem) for elem in guess_times])