我注意到在PHP中,一些框架使用microtime()函数生成密码重置令牌,如下所示:
$token = md5(microtime());
这是一个安全问题吗?如果攻击者能够将时钟与服务器同步到一定的精度,他们可以通过暴力破解令牌。1秒的同步只需要尝试100万次,这并不是一个过于疯狂的问题。
这种攻击成功的可能性有多大?应该使用/dev/urandom或openssl_pseudo_bytes()生成令牌吗?microtime()是不好的实践吗?
Tokens should always be created at random, not microtime. Now to give some extra securty (because, let's face it once again: computers cannot do anything but calculate stuff), you'll want to see php's example: http://php.net/manual/de/function.srand.php Also see http://php.net/manual/de/function.chr.php to easily create a "completely" random string. Here's how I do it
mt_srand((double) microtime() * 1000000);
function random_string ($length = 16)
{
$string = "";
for ($n = 0; $n < count($length); $n++) $string .= chr(rand(32, 126));
return $string;
}
是的,我认为是这样的。
黑客可能会在1到2小时内猜出密码。 (如果服务器使用限制重置次数的方法,黑客可以通过使用 Botnet或拥有很多时间和尝试次数来避免它。
如果我是你,我会添加一个固定的盐值。盐应该从配置文件(或其他安全位置)加载。盐不应由服务器自己或任何人设置。它应该由真正随机的API生成(谷歌应该能帮助您,但我想random.org提供了一个)。
如果您无法使用该API,伪盐也应该有所帮助,只要黑客不知道服务器何时设置(或服务器管理员有时会将其删除,但不是自动删除!)
例如:
$salt = null;
include('protectedDir/salt.php'); // Here should stand "$salt = ''"
if ($salt == null){
// Using real random api
file_put_contents('protectedDir/salt.php', '<?php \$salt = \'$salt\'; ?>)";
}
$resetKey = sha1(microtime() . $salt);
// 可能包含错误。未经测试。这只是一个示例,不应通过复制和粘贴来实现。
如果黑客可以访问php文件('protectedDir/salt.php')的“源代码”,他也应该能够看到数据库配置信息。这将是一个更大的问题。
md5(microtime().mt_rand());
? - Green Black