PHP and RAW SOCKETS on linux

我是研究僧i 提交于 2019-12-10 14:06:28

问题


Is it enough to do a

sudo setcap cap_net_raw=eip /usr/bin/php5

to be able to use RAW SOCKETS in PHP (not CLI) in Linux ?

If yes, well it is not working (but started to work in CLI but not using Apache)

So I guess I have to give those permissions to Apache as well, but I couldn't find out how.

Can you help me?

errors from the PHP script : Warning: socket_create(): Unable to create socket [1]: Operation not permitted in

the php script :

$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
        $socket = socket_create(AF_INET, SOCK_RAW, 1);
        if ($socket !== false) {
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 10, 'usec' => 0));
            echo 'Creating PING Socket !';

            socket_connect($socket, gethostbyname('noczone.com'), null);
            socket_send($socket, $package, strLen($package), 0);
            if (socket_read($socket, 255)) {
                $result = microtime(true) - $ts;
            } else {
                echo 'Error Code : No PING';
            }
            socket_close($socket);
        } else {
            echo 'Failed Creating PING Socket !';
        }

回答1:


Have you tried: sudo setcap cap_net_raw=eip $(which httpd) ?

What's the error you're getting?




回答2:


Most likely the PHP interpreter is implemented as a module in Apache (you can see this from php_sapi_name() or the PHP_SAPI constant in PHP, or from the httpd ini file).

In this case you need to give the httpd executable the capability.



来源:https://stackoverflow.com/questions/16687206/php-and-raw-sockets-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!