公共网关接口(CGI)是一个协议,让我们通过Web运行自定义脚本。
它不像以前那样常用,但我们仍然需要将其作为RHCE考试目标的一部分。
在Apache中,有一个默认文件夹可以放置脚本,然后将由CGI协议处理。
此默认文件夹在Main Apache配置文件中声明,以下是相关摘录:
<IfModule alias_module> # # Redirect: Allows you to tell clients about documents that used to # exist in your server's namespace, but do not anymore. The client # will make a new request for the document at its new location. # Example: # Redirect permanent /foo http://www.example.com/bar # # Alias: Maps web paths into filesystem paths and is used to # access content that does not live under the DocumentRoot. # Example: # Alias /webpath /full/filesystem/path # # If you include a trailing / on /webpath then the server will # require it to be present in the URL. You will also likely # need to provide a <Directory> section to allow access to # the filesystem path. # # ScriptAlias: This controls which directories contain server scripts. # ScriptAliases are essentially the same as Aliases, except that # documents in the target directory are treated as applications and # run by the server when requested rather than as documents sent to the # client. The same rules about trailing "/" apply to ScriptAlias # directives as to Alias. # ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
这里我们可以看到ScriptAlias设置。
此设置将URL的一部分映射到计算机上的目录路径。我们可以将脚本放入/var/www/cgi-bin/,然后我们应该能够使用url中的“/cgi-bin/”别名访问它们,例如http://example.com/cgi-bin/test.sh
我们现在需要检查cgi模块是否正在运行:
[root@webserver cgi-bin]# httpd -M | grep cgi proxy_fcgi_module (shared) proxy_scgi_module (shared) cgi_module (shared) fcgid_module (shared)
由于我们的服务器处于SELinux enforcing强制模式,因此需要启用SE布尔值以允许使用CGI脚本:
[root@webserver cgi-bin]# getsebool -a | grep -i cgi git_cgi_enable_homedirs --> off git_cgi_use_cifs --> off git_cgi_use_nfs --> off httpd_enable_cgi --> on
如果未启用,则可以通过运行以下命令永久启用:
$ setsebool -P httpd_enable_cgi 1
现在要尝试CGI,我创建了以下Hello World Bash脚本:
[root@webserver cgi-bin]# ls -lZ /var/www/cgi-bin/test.sh -rwxr-xr--. apache apache unconfined_u:object_r:httpd_sys_script_exec_t:s0 /var/www/cgi-bin/test.sh [root@webserver cgi-bin]# cat /var/www/cgi-bin/test.sh #!/bin/bash echo 'Content-type: text' echo echo echo 'hello world' echo echo
我们添加了内容类型设置,以便web浏览器知道如何解释内容。还要注意,脚本需要有一个特定的安全类型属性 'httpd_sys_script_exec_t'。
现在,让我们首先在bash终端中测试此脚本:
[root@webserver cgi-bin]# /var/www/cgi-bin/test.sh Content-type: text hello world
现在,我们可以尝试使用Firefox / Chrome / Curl,例如:
[root@webserver cgi-bin]# curl http://localhost/cgi-bin/test.sh hello world
使用非标准CGI文件夹
如果要将CGI文件夹更改为不同的位置,则需要采取4步:
- 创建新文件夹,并为此文件夹提供Apache所有权
- 将脚本放入新文件夹中
- 更新SELinux配置以支持新的CGI脚本位置
- 更新Main Apache配置,或者创建/编辑vhost文件
- 重新启动httpd守护程序
现在让我们创建新的CGI文件夹:
[root@webserver cgi-bin]# mkdir /var/web_scripts
接下来我们创建一个虚拟脚本和:
[root@webserver cgi-bin]# cat /var/web_scripts/test.sh #!/bin/bash echo 'Content-type: text' echo echo echo 'hello world - this time we are in non-standard folder' echo echo
接下来,我们检查所有权,权限和安全上下文是否正确:
[root@webserver cgi-bin]# ls -lZ /var/ | grep web_scripts drw-r--r--. root root unconfined_u:object_r:var_t:s0 web_scripts [root@webserver cgi-bin]# ls -lZ /var/web_scripts/test.sh -rwxr-xr--. root root unconfined_u:object_r:var_t:s0 /var/web_scripts/test.sh
首先,我们需要修复所有权:
[root@webserver cgi-bin]# chown -R apache:apache /var/web_scripts/
接下来,脚本需要可执行:
chmod ug+x /var/web_scripts/test.sh
接下来,我们需要修复selinux型安全属性设置,首先查找设置应该是什么:
[root@webserver cgi-bin]# ll -lZ /var/www/ | grep cgi drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin
现在我们创建并应用selinux策略:
[root@webserver cgi-bin]# semanage fcontext -at httpd_sys_script_exec_t "/var/web_scripts(/.*)?" [root@webserver cgi-bin]# restorecon -Rv /var/web_scripts restorecon reset /var/web_scripts context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:httpd_sys_script_exec_t:s0 restorecon reset /var/web_scripts/test.sh context unconfined_u:object_r:var_t:s0->unconfined_u:object_r:httpd_sys_script_exec_t:s0
然后确认这已奏效:
[root@webserver cgi-bin]# ls -lZ /var/ | grep web_scripts drwxr-xr-x. apache apache unconfined_u:object_r:httpd_sys_script_exec_t:s0 web_scripts [root@webserver cgi-bin]# ls -lZ /var/web_scripts -rwxr-xr--. apache apache unconfined_u:object_r:httpd_sys_script_exec_t:s0 test.sh
现在我们让Apache意识到我们的新文件夹。
我们可以更新主配置文件,或者编辑/创建vhost文件。
我们已经涵盖了我们使用vhosts的其他示例,所以这次我们将编辑Main Apache配置文件。
因此,我们进行了原始的scriptalias行的副本,并注释了原始线路并修改了重复,以便我们最终结束:
#ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" ScriptAlias /cgi-bin/ "/var/web_scripts"
我们还必须在一个新的目录指令中添加以覆盖高级拒绝'/'指令,因此我们插入以下摘录:
<Directory "/var/web_scripts"> AllowOverride None Options None Require all granted
然后我们重新启动httpd:
[root@webserver web_scripts]# systemctl restart httpd
现在一切都应该工作,所以让我们用Firefox / Chrome / Elinks / Curl测试:
[root@webserver web_scripts]# curl http://localhost/cgi-bin/test.sh hello world - this time we are in non-standard folder