第 13 章 Apache HTTP 服务器
Apache HTTP 服务器提供具有当前 HTTP 标准的开源 HTTP 服务器。[14]
在红帽企业 Linux 中,httpd 软件包 提供了 Apache HTTP 服务器。输入以下命令查看是否安装了 httpd 软件包:
~]$ rpm -q httpd
package httpd is not installed
如果没有安装它,且您想要使用 Apache HTTP 服务器,使用
yum
工具作为 root 用户安装它:
~]# yum install httpd
13.1. Apache HTTP 服务器和 SELinux
启用 SELinux 后,Apache HTTP Server (
httpd
)默认运行限制。受限制的进程在其自己的域中运行,并且与其他受限制的进程隔开。如果一个受攻击者限制的进程受到 SELinux 策略配置的影响,攻击者对资源的访问权限和可能受到的破坏会受到限制。以下示例演示了在其自己的域中运行的 httpd
进程。本例假设安装了 httpd、setroubleshoot、setroubleshoot-server 和 policycoreutils-python 软件包:
- 运行 getenforce 命令,确认 SELinux 是否在 enforcing 模式下运行:
~]$ getenforce Enforcing
当 SELinux 处于 enforcing 模式时,命令会返回Enforcing
。 - 以 root 用户身份输入以下命令以启动
httpd
:~]# systemctl start httpd.service
确认 服务正在运行。输出中应包括以下信息(只有时间戳有所不同):~]# systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since Mon 2013-08-05 14:00:55 CEST; 8s ago
- 要查看
httpd
进程,请执行以下命令:~]$ ps -eZ | grep httpd system_u:system_r:httpd_t:s0 19780 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 19781 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 19782 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 19783 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 19784 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 19785 ? 00:00:00 httpd
与httpd
进程关联的 SELinux 上下文为system_u:system_r:httpd_t:s0
。上下文的最后一部分httpd_t
是类型。类型定义进程的域以及文件的类型。在本例中,httpd
进程在httpd_t
域中运行。
SELinux 策略定义了在受限制域(如
httpd_t
)中运行的进程如何与文件、其他进程和系统交互。文件必须正确标记,以允许 httpd
访问这些文件。例如,httpd
可以读取标有 httpd_sys_content_t
类型的文件,但无法写入它们,即使 Linux (DAC)权限允许写访问。必须启用布尔值以允许某些行为,如允许脚本网络访问,允许 httpd
访问 NFS 和 CIFS 卷,以及允许 httpd
执行通用网关接口(CGI)脚本。
当配置
/etc/httpd/conf/httpd.conf
文件时,httpd
侦听端口 80、443、488、8008、8009 或 8443,必须使用 semanage port 命令将新端口号添加到 SELinux 策略配置中。以下示例演示了将 httpd
配置为侦听 httpd
策略配置中没有定义的端口,因此 httpd
无法启动。这个示例还演示了如何将 SELinux 系统配置为允许 httpd
成功侦听策略中没有定义的非标准端口。本例假定已安装了 httpd 软件包。以 root 用户身份运行示例中的每个命令:
- 输入以下命令确认
httpd
没有运行:~]# systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead)
如果输出不同,请停止该进程:~]# systemctl stop httpd.service
- 使用
semanage
工具查看 SELinux 允许httpd
侦听的端口:~]# semanage port -l | grep -w http_port_t http_port_t tcp 80, 443, 488, 8008, 8009, 8443
- 以 root 用户身份编辑
/etc/httpd/conf/httpd.conf
文件。配置Listen
选项,以便它列出在httpd
的 SELinux 策略配置中未配置的端口。在本例中,httpd
配置为侦听端口 12345 :# Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 Listen 127.0.0.1:12345
- 输入以下命令启动
httpd
:~]# systemctl start httpd.service Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
记录类似如下的 SELinux 拒绝信息:setroubleshoot: SELinux is preventing the httpd (httpd_t) from binding to port 12345. For complete SELinux messages. run sealert -l f18bca99-db64-4c16-9719-1db89f0d8c77
- 对于 SELinux,允许
httpd
侦听端口 12345,如本例中使用,需要以下命令:~]# semanage port -a -t http_port_t -p tcp 12345
- 再次启动
httpd
,并侦听新端口:~]# systemctl start httpd.service
- 现在,SELinux 已被配置为允许
httpd
侦听一个非标准端口(本例中为 TCP 12345),httpd
在此端口上成功启动。 - 要证明
httpd
正在侦听并在 TCP 端口 12345 上进行通信,请打开到指定端口的 telnet 连接并发出 HTTP GET 命令,如下所示:~]# telnet localhost 12345 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.0 HTTP/1.1 200 OK Date: Wed, 02 Dec 2009 14:36:34 GMT Server: Apache/2.2.13 (Red Hat) Accept-Ranges: bytes Content-Length: 3985 Content-Type: text/html; charset=UTF-8 [...continues...]