我正在尝试在 PHP 中进行定时攻击,并使用 PHP 7.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);
这是搜索具有以下约束的单词
- 仅限 az
- 最多 10 个字符
该脚本可以毫无问题地找到单词的长度,但是由于时间攻击,单词的值永远不会按预期返回。
有什么我做错了吗?
该脚本循环遍历长度,正确识别长度。然后它遍历每个字母 (az) 并检查这些字母的速度。理论上,'haaaa' 应该比 'aaaaa' 稍微慢一些,因为第一个字母是 h。然后它对五个字母中的每一个都进行。
跑步给出了类似'brhas'的东西,这显然是错误的(每次都不同,但总是错误的)。