cgi权限错误运行二进制文件

时间:2017-02-24 10:45:46

标签: python networking

我正在测试一个简单的cgi示例,但我无法解决权限错误。以下是详细信息。

交流转换器

#include <stdio.h>

int main(void)
{
    puts("Content-type: text/html\n");
    puts("Hello, world!");
    return 0;
}

编译
gcc a.c

的index.html

<html>
    <body>
        <p>Hello, world!</p>
        <a href="cgi-bin/a.out">test</a>
    </body>
</html>

start-server.sh

sudo python -m http.server --cgi 80

每当我点击'test'时,服务器都会通过此日志抱怨该权限被拒绝。

Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...
192.168.0.1 - - [24/Feb/2017 19:41:48] "GET / HTTP/1.1" 200 -
192.168.0.1 - - [24/Feb/2017 19:41:49] "GET /cgi-bin/a.out HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('192.168.0.1', 51670)
Traceback (most recent call last):
  File "/usr/lib/python3.6/http/server.py", line 1126, in run_cgi
    os.execve(scriptfile, args, env)
PermissionError: [Errno 13] Permission denied: '/home/mj/http/cgi-bin/a.out'
----------------------------------------
192.168.0.1 - - [24/Feb/2017 19:41:49] CGI script exit status 0x7f00

如果重要,a.out的权限为rwxr-xr-x

1 个答案:

答案 0 :(得分:0)

http.server.CGIHTTPRequestHandler的文档有以下声明:

  

请注意,出于安全原因,CGI脚本将使用用户nobody的UID运行。 CGI脚本的问题将转换为错误403

您的问题是您使用sudo启动了服务器,因此服务器以root身份运行,因此能够将 uid 更改为没有人。如果您在没有sudo的情况下运行它,那么它将使用您的用户帐户执行,并且完全能够运行该cgi脚本。