HaProxy + CloudFlare

HaProxy + CloudFlare

我正在尝试使用 DDoS 保护规则(速率限制)设置 HaProxy。但是,我认为 HaProxy 现在是限制 CloudFlare IP 的速率,而不是限制访客/真实 IP 的速率。 (注意:我的网站本身没问题,因为我已经在网站的 PHP 代码中修复了这个问题) 我该如何修复它?

我的 /etc/haproxy/haproxy.cfg

global
      log 127.0.0.1 local0 notice
      maxconn 10000
      user haproxy
      group haproxy
defaults
      log global
      mode http
      option httplog
      option dontlognull
      retries 3
      option redispatch
      timeout http-request 10s
      timeout connect 5000
      timeout client 30s
      timesout server 5000

frontend domain
      bind *:80
      stick-table type ip size 1m expire 10s store gpc0,http_req_rate(10s)
      tcp-request connection track-sc1 src
      tcp-request connection reject if { src_get_gpc0 gt 0 }
      default_backend nginx

backend nginx
      mode http
      stats enable
      stats uri /HIDDEN
      stats realm Strictly\ Private
      stats auth USER:PASSWORD
      balance roundrobin
      option httpclose
      option httpchk HEAD / HTTP/1.1\r\nHost:domainhidden.eu
      acl abuse src_http_req_rate(domain) ge 100
      acl flag_abuser src_inc_gpc0(domain)
      tcp-request content reject if abuse flag_abuser
      server web1 iphere1:80 check
      server web2 iphere2:80 check
      server web3 iphere3:80 check

我已经更改了配置中的域名、用户和密码,否则人们可以进入我的网站统计信息:P(domainhidden.eu、用户:密码和“iphere”)

答案1

Cloudflare放置HTTP 标头中的原始 IP 地址CF-Connecting-IP。因此,为了区分通过 CF 服务器的攻击者和通过同一服务器的其他人,可以使用此标头值作为密钥。

以下配置可以帮助我防止有人在我的网站上暴力破解 URL。

frontend www-https
    # ...
    # front-end config details skipped
    # ...

    default_backend www-backend

    # Use General Purpose Couter 0 in SC0 as a global abuse counter
    stick-table type string len 40 size 1m expire 300s store gpc0

    # wait for at most 5 seconds for HTTP headers
    tcp-request inspect-delay 5s
    # use CF-Connecting-IP header as identifier
    tcp-request content track-sc0 hdr(CF-Connecting-IP)

    tcp-request content reject if { sc0_get_gpc0 gt 0 }

backend www-backend
    # ...
    # back-end config details skipped
    # ...

    # prevent new requests for 300 seconds from someone who got more than 10 HTTP errors in 60 seconds
    stick-table type string len 40 size 1m expire 300s store http_err_rate(60s)
    acl abuse sc1_http_err_rate ge 10
    # tell front-end to reject future requests for this address
    # (replace www-https with your front-end name)
    acl flag_abuser sc0_inc_gpc0(www-https) gt 0

    # wait for at most 5 seconds for HTTP headers
    tcp-request inspect-delay 5s
    # use CF-Connecting-IP header as identifier
    tcp-request content track-sc1 hdr(CF-Connecting-IP)
    tcp-request content reject if abuse flag_abuser

如果您想在日志中看到 HTTP 标头,请添加capture以下命令:

    option httplog
    capture request header cf-connecting-ip len 100
    capture request header x-forwarded-for len 100
    log stdout format raw daemon

答案2

您必须将他们的 IP 列入您的速率限制白名单=)。

https://www.cloudflare.com/ips

因此你的 ACL 将是这样的(不确定是否完全正确):

acl rate_whitelist src -f /path/to/whitelist-ips
acl abuse src_http_req_rate(domain) ge 100
acl flag_abuser src_inc_gpc0(domain)
tcp-request content reject if abuse flag_abuser !rate_whitelist

然后你的whitelist-ips文件将是 IP 列表(我认为,不确定格式是否正确):

199.27.128.0/21
173.245.48.0/20
103.21.244.0/22
103.22.200.0/22
103.31.4.0/22
141.101.64.0/18
108.162.192.0/18
190.93.240.0/20
188.114.96.0/20
197.234.240.0/22
198.41.128.0/17
162.158.0.0/15
104.16.0.0/12

相关内容