【问题标题】:Why iptables not working with string match using x-forwarded-for?为什么 iptables 不能使用 x-forwarded-for 处理字符串匹配?
【发布时间】:2021-08-24 17:30:56
【问题描述】:

我有一个在 Cloudflare 后面的 Ubuntu 上运行 Apache 的网络服务器。我想阻止使用网络服务器的 iptables 的用户。在这里,我想实现 iptables 的字符串匹配功能并断开连接。这是我的规则不起作用:

iptables -I INPUT -m string --string "x-forwarded-for: 216.244.66.205" --algo bm --to 65535 -j DROP

添加此规则后,客户端仍然可以访问服务器。

我知道 Cloudflare 的客户端 IP 特定标头是 cf-connecting-ip。我可以用这个阻止客户端。以下是行之有效的规则:

iptables -I INPUT -m string --string "cf-connecting-ip: 216.244.66.205" --algo bm --to 65535 -j DROP

从 Cloudflare 到 Web 服务器的流量是 HTTP(端口 80)。

我有一个负载均衡器(haproxy),并且一些域正在通过它而不是 Cloudflare 运行。这就是我想使用 XFF 的原因,因为 cf-connecting-ip 是 Cloudflare 特有的,并且两者都支持 XFF。

我可以看到 Cloudflare 正确附加了 cf-connecting-ip 和 XFF 标头。这是多个请求中 tcpdump 的输出:

tcpdump -A -s 65535 'tcp port 80' | grep 216.244.66.205
x-forwarded-for: 216.244.66.205
cf-connecting-ip: 216.244.66.205
x-forwarded-for: 216.244.66.205
cf-connecting-ip: 216.244.66.205
x-forwarded-for: 216.244.66.205
cf-connecting-ip: 216.244.66.205
x-forwarded-for: 216.244.66.205
cf-connecting-ip: 216.244.66.205

iptables 不知何故能够检测到字符串 cf-connecting-ip 但不能检测到 x-forwarded-for

任何帮助将不胜感激。

【问题讨论】:

    标签: apache ubuntu networking cloudflare iptables


    【解决方案1】:

    HTTP 标头位于应用层,而 IPTables 在 TCP/IP 堆栈的较低位置工作。您要过滤的数据对 IPTables 不可见。

    对于 Cloudflare 背后的网站,您应该将 IPTables 配置为仅允许 Cloudflare IP 地址通过,并使用 Cloudflare 防火墙阻止客户端 IP 地址。

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多