已在 haproxy 中启用 x-forwarded,但 mod_remoteip 2.2 反向移植仍然不起作用

已在 haproxy 中启用 x-forwarded,但 mod_remoteip 2.2 反向移植仍然不起作用

我正在使用 haproxy 将请求转发到与 modsecurity 2.7 和 OWASP_CRS 捆绑在一起的 Apache 2.2。我已在以下位置启用 X-Forwarded-For 标头插入haproxy config

defaults:
option forwardfor except 127.0.0.1 header X-Forwarded-For

mod_audit.logX-Forwarded 标头中显示真实 ip 地址:

--06f84712-B--
GET /item/820 HTTP/1.1
Host: myownsite.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
From: googlebot(at)googlebot.com
Accept-Encoding: gzip,deflate
User-Agent: ......
X-Forwarded-For: 66.249.35.164
Connection: close

但在error_log文件中,所有日志仍然记录的是我的服务器IP,而不是真正的客户端IP:

    [Fri Oct 23 12:55:57 2015] [error] [client mylocalIPAddress] 
ModSecurity: Access denied with code 403 (phase 2). 
Pattern match "<(a|abbr|acronym|address|applet|area|audioscope|b|base|basefront|bdo|bgsound|big|blackface|blink|blockquote|body|bq|br|button|caption|center|cite|code|col|colgroup|comment|dd|del|dfn|dir|div|dl|dt|em|embed|fieldset|fn|font|form|frame|frameset|h1|head|h ..." at ARGS:type. 
[file "/etc/httpd/crs-tecmint/owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf"] [line "301"] [id "973300"] [rev "2"] [msg "Possible XSS Attack Detected - HTML Tag Handler"] [data "Matched Data: <div> found within ARGS:page_type: <div>"] [ver "OWASP_CRS/2.2.9"] [maturity "8"] [accuracy "8"] [tag "OWASP_CRS/WEB_ATTACK/XSS"] [tag "WASCTC/WASC-8"] [tag "WASCTC/WASC-22"] [tag "OWASP_TOP_10/A2"] [tag "OWASP_AppSensor/IE1"] [tag "PCI/6.5.1"] [hostname "myownsite"] [uri "/item/31"] [unique_id "Vim93dj1xSwAAFZ0AKoAAAAB"]

我已经启用了mod_remoteip-httpd22模块,但文件中没有记录任何真实客户端的 IP error_log。有人能告诉我是否缺少了什么吗?

这是 mod_remoteip 的配置

LoadModule remoteip_module modules/mod_remoteip.so
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1

答案1

我花了几个小时尝试理解这些东西是如何工作的,最后终于找到了问题所在。

在haproxy配置中,我发现来自的IPbackend apache与mod_remote_ip设置不一致。

backend apache2

    *****
                    // think of this ip as my real server ip
    server apache2 214.4.334.1:3001 weight 1 maxconn 1024 check

    *****

相反,我应该使用 127.0.0.1 作为 IP 地址:

backend apache2

    *****

    server apache2 127.0.0.1:3001 weight 1 maxconn 1024 check

    *****

当 Haproxy 将请求转发给 Apache 时,mod_remote_ip 会检查传入的 IP 是否是代理 IP 127.0.0.1。如果是,它将使用文件X-forwarded for中的 IP error_log

[error] [client myrealIP] ModSecurity: Access denied with code 403

相关内容