我可以使用open_basedir访问/ dev / urandom吗?

问题描述 投票:9回答:4

我想在Codeigniter中使用phpass-0.3,但由于open_basedir我得到以下错误:

遇到PHP错误 严重性:警告 消息:is_readable()[function.is-readable]:open_basedir限制生效。文件(/ dev / urandom)不在允许的路径中:(/ home / phginep:/ usr / lib / php:/ usr / local / lib / php:/ tmp) 文件名:phpass-0.3 / PasswordHash.php 行号:51

以下代码:

function get_random_bytes($count)
{
    $output = '';
    if (is_readable('/dev/urandom') &&    //Line Number: 51
        ($fh = @fopen('/dev/urandom', 'rb'))) {
        $output = fread($fh, $count);
        fclose($fh);
    }

    if (strlen($output) < $count) {
        $output = '';
        for ($i = 0; $i < $count; $i += 16) {
            $this->random_state =
                md5(microtime() . $this->random_state);
            $output .=
                pack('H*', md5($this->random_state));
        }
        $output = substr($output, 0, $count);
    }

    return $output;
}

我能做些什么来解决这个问题吗?

php codeigniter phpass
4个回答
16
投票

你有一些选择:

1 - 从真正的RNG下载转储(this one提供基于放射性衰变的转储)并使用它,只需确保不要继续读取相同的nn字节。有点笨重,但一个选项。

2 - 让PHP执行代表/dev/urandom读取的内容(UGLY)

3 - 回到mt_rand()(也很难看,但我已经看到了这一点):

 for ($i = 0; $i < $count / 8; $i++) {
   $output .= dechex(mt_rand(0, 0x7fffffff));
 }

不幸的是,所有选项都很笨拙和丑陋。最好的办法是确保你不必处理open_basedir。尽管如此,这种特殊的烦恼还是可以解决的。

最后 - 不太可能与你的主人一起飞行,但也许值得一试:

您可以要求您的主人在您的主目录中提供urandom,以便您阅读。告诉他们您需要访问urandom以生成随机数,以便为用户提供更好的安全性,然后让他们运行:

mknod urandom c 1 9

在您的主目录中。我只是在我自己的服务器上试过它,它可以工作(但root需要为你做)。没有任何实际的理由阻止您使用系统的伪随机数生成器,除了PHP之外,您可以使用其他任何方法。这实际上是让他们可以访问urandom的最简单方法,因为它不需要PHP或vhost配置中的例外。

不允许访问/dev/random是一件合理的事情,因为/dev/random必须通过可用的(新的)系统熵补充,并且可能导致重要的事情在读取时阻止读取,这可能经常发生在低流量服务器上。但是,/dev/urandom保证永远不会阻塞,因为它只是在重新使用后重新使用内部熵池,这就是为什么它是一个质量较差的源。

注意

我不是说open_basedir的想法很糟糕,但它也打破了良好的代码。一个经典的chroot要好得多,但更难,这就是为什么你遇到open_basedir比你做一个真正的chroot更多。至少,任何程序都应该能够访问服务器上的nullzerourandom设备。


6
投票

phpass试图访问/dev/urandom,这是php.ini中不允许的。要解决此问题,您必须禁止警告。要做到这一点,只需在@之前添加is_readable,如下所示:

...
@is_readable('/dev/urandom')
...

0
投票

看起来你在共享托管主机上,他们已经配置PHP只允许你访问帐户中的文件和目录(这是有道理的)。如果是这种情况,您可以做的事情不多,因为共享主机不允许更改以允许您访问该资源。如果您有专用服务器或VPS,则可以更改PHP配置(php.ini)以允许访问该资源。


0
投票
cd /nginx/chroot/
touch random
touch urandom
mount --bind /dev/random /nginx/chroot/dev/random
mount --bind /dev/urandom /nginx/chroot/dev/urandom

我的phpmailer现在已经在nginx chroot centos 7中工作了

php nginx RAND_BYTES stream_socket_enable_crypto php nginx stream_socket_enable_crypto Uncaught Exception:无法打开源设备php nginx RAND_BYTES stream_socket_enable_crypto stream_socket_enable_crypto():SSL

© www.soinside.com 2019 - 2024. All rights reserved.