【问题标题】:fail2ban with haproxy logs how to to block带有haproxy日志的fail2ban如何阻止
【发布时间】:2015-03-07 08:20:04
【问题描述】:

您好团队, 首先感谢您投入宝贵的时间来帮助像我这样的初学者。

我在centos中安装了failed 2 ban

在我的 haproxy 日志中

Mar  7 02:37:07 localhost haproxy[9378]: 115.xxx.xxx.xxx:19004 [07/Mar/2015:02:37:03.823] http-ingress testing/new-server 2952/0/0/17/3242 302 689 - - --VN 3/3/0/0/0 0/0 "GET /myadmin/scripts/setup.php HTTP/1.1"

如何屏蔽

以下步骤我已完成,如有错误请指正

================================================ =============

命令:

vim /etc/fail2ban/filter.d/vulscan.conf 

文件:

[Definition]

failregex = ^<HOST>.*\"GET

ignoreregex =


[vulscan]

enabled = true

port = http,https

filter = vulscan

banaction = iptables-allports

logpath = /var/log/haproxy_0.log

#action   = hostsdeny[file=/etc/hosts.deny]

action = iptables-multiport[name=vulscan,port="http,https", protocol=tcp]

maxretry = 1

bantime = 604800

================================================ =======================

命令:

iptables -L           

输出粘贴在下面:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
fail2ban-vulscan  tcp  --  anywhere             anywhere            multiport dports http,https 
fail2ban-SSH  tcp  --  anywhere             anywhere            tcp dpt:ssh 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain fail2ban-SSH (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            

Chain fail2ban-vulscan (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere 

【问题讨论】:

    标签: fail2ban


    【解决方案1】:

    使用这个 failregex = haproxy[\d+]:

    你可以通过运行 haproxy 日志来检查你的正则表达式 fail2ban-regex [haproxy_log] [fail2ban_haproxy.conf]

    【讨论】:

      【解决方案2】:

      不是原始老问题的确切答案,但希望能让任何人开始一起使用 Docker、Fail2Ban 和 Haproxy。 我花了很长时间才弄清楚它在 2022 年撰写本文时确实有效。

      这里列出的文件主要是这个特定任务所需的核心部分,大多数 haproxy、fail2ban 和 docker 文件将包含更多信息。

      haproxy.cfg - 这会设置超级简单的日志记录并将某些路径设置为 403 拒绝机器人,我们稍后会寻找

      global
        log stdout local0 debug
      
      defaults
        log global
        option httplog
        log-format "%Tl|%ci-%ST-%r"
      
      frontend input-main-web
        bind *:80
        bind *:443 ssl crt /astro/ssl-certs
        option forwardfor header X-Real-IP
        http-request set-header X-Real-IP %[src]
        
        # attempting to deny bots to anywhere
        acl bot-path path -i -m sub wp-includes
        acl bot-path path -i -m sub wp-login
        acl bot-path path -i -m sub xmlrpc
        acl bot-path path -i -m sub wordpress
        http-request deny if bot-path
      
      

      haproxy Dockerfile - 这会将标准输出记录到 fail2ban 可以读取的实际文件中(如果在 docker 中使用 haproxy,这将是最后一行)

      CMD haproxy -W -db -f /usr/local/etc/haproxy/haproxy.cfg | tee /var/log/haproxy.log 2>&1 
      
      

      fail2ban.yml - 这将在其自己的容器中很好地运行 fail2ban,但仍能保护主机和所有在其上运行的容器

      version: '3.8'
      services:
        astro-fail2ban:
          build: ./fail2ban
          image: astro-fail2ban
          restart: unless-stopped
          volumes:
            - /home/docker-data/fail2ban:/data
            - /home/docker-log/haproxy:/log:ro
            - /var/log/secure:/var/log/secure:ro
          environment: 
            - TZ=America/Phoenix
            - F2B_DB_PURGE_AGE=30d
            - F2B_LOG_TARGET=/data/fail2ban.log
            - F2B_LOG_LEVEL=INFO
            - F2B_IPTABLES_CHAIN=INPUT
      
          network_mode: "host"
      
          privileged: true
          cap_add:
            - NET_ADMIN
            - NET_RAW
      

      fail2ban Dockerfile - 复制您正在创建的自定义文件

      FROM crazymax/fail2ban:latest
      
      RUN mkdir -p /var/log
      RUN touch /var/log/auth.log
      
      RUN mkdir -p /etc/fail2ban
      COPY ./jail.local /etc/fail2ban/
      COPY ./haproxy.conf /etc/fail2ban/filter.d/
      COPY ./docker-action.conf /etc/fail2ban/action.d/
      
      

      jail.local - 这告诉 fail2ban 通过 haproxy 日志查看定义的过滤器,并对找到的任何内容执行自定义禁令操作(可以将 findtime 和 bantime 设置为您自己的规范)

      [haproxy]
      enabled = true
      port = http,https
      filter = haproxy
      logpath = /log/haproxy.log
      maxretry = 3
      findtime = 300
      bantime = 1800
      banaction = docker-action
      ignoreip = 127.0.0.1
      

      haproxy.conf - 告诉 fail2ban 在日志中查找 403 状态码(我们的机器人信号)

      [INCLUDES]
      # Read common prefixes. If any customizations available -- read them from
      # common.local
      before = common.conf
      
      [Definition]
      _daemon = haproxy
      failregex = \|<HOST>-403
      ignoreregex =
      

      docker-action.conf - 用于前向链并丢弃数据包的自定义禁止操作 - 有点神奇,但确实有效

      [Definition]
       
      actionstart = iptables -N f2b-haproxy
                    iptables -A f2b-haproxy -j RETURN
                    iptables -I FORWARD -p tcp -m multiport --dports 80,443 -j f2b-haproxy
       
      actionstop = iptables -D FORWARD -p tcp -m multiport --dports 80,443 -j f2b-haproxy
                   iptables -F f2b-haproxy
                   iptables -X f2b-haproxy
       
      actioncheck = iptables -n -L FORWARD | grep -q 'f2b-haproxy[ \t]'
       
      actionban = iptables -I f2b-haproxy 1 -s <ip> -j DROP
       
      actionunban = iptables -D f2b-haproxy -s <ip> -j DROP
      

      还有什么问题...发表评论。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多