Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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
用PHP进行定时攻击_Php_Timing Attack - Fatal编程技术网

用PHP进行定时攻击

用PHP进行定时攻击,php,timing-attack,Php,Timing Attack,我试图在PHP中产生计时攻击,并使用PHP7.1和以下脚本: <?php $find = "hello"; $length = array_combine(range(1, 10), array_fill(1, 10, 0)); for ($i = 0; $i < 1000000; $i++) { for ($j = 1; $j <= 10; $j++) { $testValue = str_r

我试图在PHP中产生计时攻击,并使用PHP7.1和以下脚本:

<?php
    $find = "hello";
    $length = array_combine(range(1, 10), array_fill(1, 10, 0));
    for ($i = 0; $i < 1000000; $i++) {
        for ($j = 1; $j <= 10; $j++) {
            $testValue = str_repeat('a', $j);
            $start = microtime(true);
            if ($find === $testValue) {
                // Do nothing
            }
            $end = microtime(true);
            $length[$j] += $end - $start;
        }
    }

    arsort($length);
    $length = key($length);
    var_dump($length . " found");

    $found = '';
    $alphabet = array_combine(range('a', 'z'), array_fill(1, 26, 0));
    for ($len = 0; $len < $length; $len++) {
        $currentIteration = $alphabet;
        $filler = str_repeat('a', $length - $len - 1);
        for ($i = 0; $i < 1000000; $i++) {
            foreach ($currentIteration as $letter => $time) {
                $testValue = $found . $letter . $filler;
                $start = microtime(true);
                if ($find === $testValue) {
                    // Do nothing
                }
                $end = microtime(true);
                $currentIteration[$letter] += $end - $start;
            }
        }
        arsort($currentIteration);
        $found .= key($currentIteration);
    }
    var_dump($found);

我做错什么了吗

我不这么认为。我试过你的代码,我也像你和其他在评论中试过的人一样,在第二个循环中得到了完全随机的结果。第一个(长度)是最可靠的,虽然不是100%的时间。顺便说一句,所建议的
$argv[1]
技巧并没有真正提高结果的一致性,老实说,我真的不明白为什么应该这样做

由于好奇,我看了一下PHP7.1源代码。字符串标识函数(
zend_是相同的
)如下所示:

    case IS_STRING:
        return (Z_STR_P(op1) == Z_STR_P(op2) ||
            (Z_STRLEN_P(op1) == Z_STRLEN_P(op2) &&
             memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) == 0));
现在很容易理解为什么第一次对长度的计时攻击效果很好。如果长度不同,则永远不会调用
memcmp
,因此它返回的速度要快得多。即使没有太多的迭代,差异也很容易被注意到

一旦计算出长度,在第二个循环中,您基本上是在尝试攻击底层的
memcmp
。问题在于,时间上的差异在很大程度上取决于:

  • memcmp
  • 电流负载与干扰过程
  • 机器的结构
  • 我推荐这篇标题为的文章,以获得更详细的解释。他们做了一个更精确的基准,但仍然无法在时间上得到明显的差异。我只想引用这篇文章的结论:

    总之,如果
    memcmp()
    受到定时攻击,这在很大程度上取决于环境


    你能澄清一下你在哪里期待结果吗?最后一个结果“var_dump($found);”应该显示“Hello”-我会更新问题运行你的代码会产生不一致的结果,它会弹出5个随机字母。这是您遇到的错误吗?为什么要使用
    1000000
    ?只是选择一个相当大的数量,它可能会更小。我没有测试它在我的机器上运行得足够快,是的,5个随机字母表示它找到了正确的长度(5),但没有找到特定的字母。随机字母是Mircotime发现的最慢的字母,只是为了给这个答案增加一点,不完全确定它是否有用,php的速度可能会因为字节码系统而提高。所以它可能会缓存代码,所以有时根据您安装的扩展,您的速度可能会更快。正如我们所知,php7的炒作是其速度的提高。