duanchun6148 2017-03-20 17:53
浏览 43

PHP套接字绑定权限端口

I am trying to bind and listen to a privilege port however I keep receiving this message "Permission denied"

Here are the things I have tried to do:

chgrp www-data file.php
chmod 770 file.php

Gave the file the correct permissions to execute and still nothing

setcap cap_net_bind_service=+ep file.php

Did not work, probably because it is not a executable file...

posix_setuid(0)

Gave my script root privileges however it did not change the user id for the script. It remained the same.

  • 写回答

1条回答 默认 最新

  • duanqin9507 2017-03-20 19:11
    关注

    You have a misunderstanding regarding file permissions and the permissions required to execute, in this case bind to a port number lower than 1024.

    File permissions only deal with who can read and write to the file. I.e. when you do:

    chmod 770 file.php
    

    You are allowing file.php to rw by owner and group, but no access for users outside the group.

    What you need is a privileged user to execute the file (root in this case since that is the only account able to bind to privileged ports). However letting PHP anywhere near root is a security risk (outside the scope of this question to elaborate). Since you require root your best bet is allowing the standard (unprivileged) user that runs your web-server obtain a shell with root permissions only when needed. Then trigger your script in this priviliged shell.

    Here is a project that allows PHP to obtain and interact with a real Bash shell as root. Get it here: https://github.com/merlinthemagic/MTS

    After downloading you would simply use the following code:

    $shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
    $return1  = $shell->exeCmd('php /full/path/to/file.php');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题