解决 PHP 中不安全的随机数生成问题
我们在各种 Drupal 模块上运行 Fortify 扫描,一个常见的严重/高结果是不安全的随机性".它指出 rand() 函数无法承受密码攻击.
we are running Fortify scans on various Drupal modules, and a common critical/high result is "Insecure Randomness". It states that the rand() function cannot withstand a cryptographic attack.
我的问题 - 这是一个严重的问题吗?如何在 PHP 中修复它?
My question - is this a serious concern? How to fix it in PHP?
谢谢.
推荐答案
这个问题的答案完全取决于您使用 rand() 调用的结果.
The answer to this question entirely depends on what you are using the results from the rand() call for.
如果您将它们用于诸如加密密钥之类的事情,其中工具的安全性取决于您的随机数的随机性,那么,是的,这是一个严重的问题.在这种情况下,您不应调用 rand() 或 mt_rand(),因为两者都不会产生足够随机的随机"数字,可以依赖于加密使用.你真的想利用你正在运行的平台的底层伪随机数生成器 (PRNG) - Unix/Linux 系统上的/dev/urandom 或 Windows 系统上的crypto-api - 因为这些已经被广泛研究并产生适用于密码系统的真正随机数.PHP 并没有让访问这些随机源变得那么容易,但是确实存在关于如何这样做的示例(像这样 -> http://www.php.net/manual/en/function.mt-rand.php#83655).
If you're using them for things like cryptographic keys, where the security of the tool depends on the randomness of your random numbers, then, yes, this is a serious concern. In that case, you should not call rand() or mt_rand(), as both do not produce "random" numbers that are sufficiently random to rely on for cryptographic use. You really want to make use of the platform you're running on's underlying pseudorandom number generator (PRNG) - /dev/urandom on a Unix/Linux system or the crypto-api on a Windows system - as these have been studied extensively and produce really random numbers that are suitable for use in cryptosystems. PHP doesn't make accessing these random sources all that easy, but examples do exist on how to do so (like this -> http://www.php.net/manual/en/function.mt-rand.php#83655).
如果您将随机数用于其他用途,例如随机化哪个选项首先呈现给用户,或者类似的事情,其中生成的数字没有以任何加密方式使用,那么您可能 能够逃脱使用 rand() 或 mt_rand().但是,如果您的应用程序/模块的安全性依赖于良好的随机数,那么您确实需要利用操作系统来源,如上所述.
If you're using randoms for something else, like to randomize which option is presented to the user first, or something like that, where the numbers produced are not being used in any cryptographical way, then you may be able to get away with using rand() or mt_rand(). But, if your applications/modules rely on good random numbers for their security, you really need to take advantage of the OS sources, as described above.
相关文章