第七十五章 锁定 Apache (UNIX® Linux macOS)

发布于:2024-05-10 ⋅ 阅读:(19) ⋅ 点赞:(0)

第七十五章 锁定 Apache (UNIX® Linux macOS)

在某些情况下,Apache 被锁定,因此您无法轻松配置服务器来访问 Apache 文件系统之外的文件。例如,安全增强型 Linux (SELinux) 就是这种情况。

对于以这种方式锁定的配置,其他地方讨论的 Web 网关配置会导致在尝试访问 CSP 资源时返回 HTTP 403 Forbidden 错误代码。有两种策略可以处理这个问题。

  • 修改 Web 网关主目录的安全上下文,以便 Apache 可以访问此位置中保存的文件。
  • Web Gateway 的主目录移动到 Apache 根文件系统下的某个位置(在 SELinux 设置中已预先配置为可供 Apache 访问)。

首先,修改 Web 网关主目录的 SELinux 安全上下文通常很简单,涉及以下步骤。

作为示例,我们使用 /opt/webgateway/binWeb Gateway 主目录、侦听端口 1972IRIS Superserver 以及安装在 /usr/iris/ 中的 IRIS

chcon 命令设置文件上下文并立即生效。

sudo chcon -R -t httpd_sys_content_t /usr/iris/csp
sudo chcon -R -t httpd_sys_rw_content_t /opt/webgateway/conf/CSP.ini
sudo chcon -R -t httpd_sys_rw_content_t /opt/webgateway/conf/CSPRT.ini
sudo chcon -R -t httpd_sys_rw_content_t /opt/webgateway/logs/CSP.log
sudo chcon -t httpd_modules_t /opt/webgateway/bin/CSPa2.so
sudo chcon -t httpd_modules_t /opt/webgateway/bin/CSPa2Sys.so
sudo chcon -t httpd_modules_t /opt/webgateway/bin/CSPa22.so
sudo chcon -t httpd_modules_t /opt/webgateway/bin/CSPa22Sys.so
sudo chcon -t httpd_modules_t /opt/webgateway/bin/CSPa24.so
sudo chcon -t httpd_modules_t /opt/webgateway/bin/CSPa24Sys.so

但是,chcon 命令所做的更改将在下次重新标记后丢失;因此,除了 chcon 之外,还有必要使用 semanage fcontext 工具。以下命令允许 Web Gateway 在强制执行 SELinux 时运行:

semanage fcontext -a -t lib_t "/opt/webgateway/bin/(.*\.so)?" 2> /dev/null
semanage fcontext -a -t httpd_sys_rw_content_t "/opt/webgateway/bin/temp(/.*)?" 2> /dev/null
semanage fcontext -a -t httpd_sys_rw_content_t "/opt/webgateway/bin/CSP(.*\.ini)?" 2> /dev/null
semanage fcontext -a -t httpd_sys_rw_content_t "/opt/webgateway/bin/CSP.log" 2> /dev/null
restorecon -vr /opt/webgateway/bin > /dev/null

然后使用如下所示的命令。请注意,正确设置超级服务器端口的上下文非常重要(如最后一行所示);否则,Web Gateway 将无法访问它,从而导致“服务器不可用”错误。

sudo /usr/sbin/semanage fcontext -a -t httpd_sys_content_t
    "/usr/iris/csp(/.)?"
sudo /usr/sbin/semanage fcontext -a -t httpd_sys_rw_content_t
    "/opt/webgateway/conf/CSP.ini"
sudo /usr/sbin/semanage fcontext -a -t httpd_sys_rw_content_t
    "/opt/webgateway/conf/CSPRT.ini"
sudo /usr/sbin/semanage fcontext -a -t httpd_sys_rw_content_t
    "/opt/webgateway/logs/CSP.log"
sudo /usr/sbin/semanage fcontext -a -t httpd_modules_t
    "/opt/webgateway/bin/CSPa2.so"
sudo /usr/sbin/semanage fcontext -a -t httpd_modules_t
    "/opt/webgateway/bin/CSPa2Sys.so"
sudo /usr/sbin/semanage fcontext -a -t httpd_modules_t
    "/opt/webgateway/bin/CSPa22.so"
sudo /usr/sbin/semanage fcontext -a -t httpd_modules_t
   "/opt/webgateway/bin/CSPa22Sys.so"
sudo /usr/sbin/semanage fcontext -a -t httpd_modules_t
    "/opt/webgateway/bin/CSPa24.so"
sudo /usr/sbin/semanage fcontext -a -t httpd_modules_t
    "/opt/webgateway/bin/CSPa24Sys.so"
sudo /usr/sbin/semanage port -a -t http_port_t -p tcp 51773

这些是授予 Web 网关(在托管 Apache 服务器的上下文中运行)对其主目录中的文件的访问权限的基本步骤。

另一种方法(如果上面建议的方法不可接受则应使用该方法)是将 Web Gateway 配置为在 Apache 提供的预配置目录中工作。以下命令假设 Apache 安装在 /usr/apache 中。

  • CGI 模块应复制到:/usr/apache/cgi-bin/
cp /usr/iris/csp/bin/*cgi* /usr/apache/cgi-bin/
  • API 模块应复制到:/usr/apache/modules/
cp /usr/iris/csp/bin/*.so /usr/apache/modules/
  • 静态文件应复制到以下位置:/usr/apache/htdocs/
cp /usr/iris/csp/samples/* /usr/apache/htdocs/csp/samples/
cp /usr/iris/csp/broker/* /usr/apache/htdocs/csp/broker/
cp /usr/iris/csp/sys/* /usr/apache/htdocs/csp/sys/

另外,复制上述位置下的所有子目录。

移动 Web Gateway 安装后,必须对 Apache 配置中指定的路径进行适当的更改。

推荐选项:Apache API 模块 (CSPa24.so)

LoadModule cspsys_module_sa /usr/apache/modules/CSPap24.so
CSPSYSModulePath /usr/apache/modules/
CSPFileTypes csp cls zen cxw

替代选项 1:带有 NSDApache API 模块 (mod_csp.so)

LoadModule csp_module /usr/apache/modules/mod_csp.so
CSPFileTypes csp cls zen cxw 
ScriptAliasMatch /csp/bin/Systems/Module.cxw "/usr/apache/cgi-bin/nph-CSPcgiSys" 
ScriptAliasMatch /csp/bin/RunTime/Module.cxw "/usr/apache/cgi-bin/nph-CSPcgi"

替代选项 2:带有 NSDCGI 模块 (nph-CSPcgi)

<LocationMatch "/*\.([Cc][Ss][Pp]|[Cc][Ll][Ss]|[Zz][En][Nn])$">
AllowOverride None
Options FollowSymLinks ExecCGI
Require all granted
</LocationMatch>
ScriptAliasMatch /csp/bin/Systems/Module.cxw "/usr/apache/cgi-bin/nph-CSPcgiSys" 
ScriptAliasMatch /csp/bin/RunTime/Module.cxw "/usr/apache/cgi-bin/nph-CSPcgi"
ScriptAliasMatch /*\.([Cc][Ss][Pp]|[Cc][Ll][Ss])$ "/usr/apache/cgi-bin/nph-CSPcgi"

替代选项 3:带有 NSD 的内置Apache API 模块 (mod_csp.c)

CSPFileTypes csp cls zen cxw 
ScriptAliasMatch /csp/bin/Systems/Module.cxw "/apache/cgi-bin/nph-CSPcgiSys" 
ScriptAliasMatch /csp/bin/RunTime/Module.cxw "/usr/apache/cgi-bin/nph-CSPcgi"

网站公告

今日签到

点亮在社区的每一天
去签到