问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Fail2ban过滤规则分享:综合防范暴力破解和恶意请求

创作时间:
作者:
@小白创作中心

Fail2ban过滤规则分享:综合防范暴力破解和恶意请求

引用
CSDN
1.
https://blog.csdn.net/cpublic/article/details/139871266

Fail2ban是Linux服务器安全管理中常用的工具,主要用于通过监控日志文件来防止暴力破解和恶意请求。本文分享了一个综合性的Fail2ban过滤规则,可以有效防范暴力破解和恶意骚扰请求。

今天分享给大家的这个过滤规则比较具有综合性,能防范暴力破解又能屏蔽拦截恶意骚扰请求。尤其值得一提的是,该规则能够对xmlrpc.php的访问进行过滤,允许来自安卓WordPress客户端的请求,同时对于那些使用随机伪装UA的请求拦截效果也非常的好。

规则代码

[Definition]
failregex = ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+XDEBUG.+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+GponForm.+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+phpunit.+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+ajax-index\.php .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+sellers\.json .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+adminer\.php .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+wp-configuration\.php.+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+ThinkPHP.+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+wp-config.+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+dede\/login\.php .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+plus\/recommend\.php .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+e\/install\/index.php .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+m\/e\/install\/index\.php .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+e_bak\/install\/index.php .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+\.aspx .+$
            ^<HOST> \- \S+ \[\] \"(GET|POST|HEAD) .+\.act .+$
            ^<HOST>.*] "(GET|POST|HEAD) .*xmlrpc\.php.*
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.Mozilla/5\.0 \(Windows NT 6\.1\; rv\:60\.0\) Gecko\/20100101 Firefox\/60\.0.*"$
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.Photon.*"$
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.Mozilla\/5\.0 zgrab\/0\.x.*"$
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.XTC.*"$
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.python-requests.*"$
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.bidswitchbot.*"$
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.Google-adstxt.*"$
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.Apache-HttpClient.*"$
            ^<HOST>.*] "POST .*HTTP\/1\.1.*
            ^<HOST> -.*"(GET|POST|HEAD).*HTTP\/1\.1.*"*.Go-http-client\/1\.1.*"$
            ^<HOST> .* ".*\\x.*" .*$
            ^<HOST> -.*"(GET|POST|HEAD) .+wp-login\.php .*HTTP\/1\.1.*
            ^<HOST> -.*"(GET|POST|HEAD) .*wp-includes/wlwmanifest.xml .*HTTP\/1\.1.*
ignoreregex = ^<HOST>.*] "POST /xmlrpc\.php\?for=wpandroid*
              ^<HOST>.*] "POST /wp-cron\.php\?doing_wp_cron=.*
datepattern = {^LN-BEG}%%ExY(?P<_sep>[-/.])%%m(?P=_sep)%%d[T ]%%H:%%M:%%S(?:[.,]%%f)?(?:\s*%%z)?
              ^[^\[]*\[({DATE})
              {^LN-BEG}  

规则说明

failregex = ^<HOST>.*] "(GET|POST|HEAD) .*xmlrpc\.php.*
ignoreregex = ^<HOST>.*] "POST /xmlrpc\.php\?for=wpandroid*  

上述两条规则就是防止Wordpress xmlrpc攻击的规则,并且让WordPress fo android客户端正常访问工作。

failregex = ^<HOST> .* ".*\\x.*" .*$  

这个规则防止URL中的特殊字符攻击的,可以过滤掉如下这样的访问记录:

185.202.1.188 - - [23/Jun/2020:08:18:33 +0800] "\u0003\u0000\u0000/*\u00E0\u0000\u0000\u0000\u0000\u0000Cookie: mstshash=Administr" 400 157 "-" "-"

不知道您见没见到过这样的访问记录,反正明月是真的见过不少次了!都是转义符想拦截都不知道到如何下手?嘿嘿,现在可以搞定了。

配置方法

将上面分享的过滤规则单独保存为nginx-custom.conf文件到/etc/fail2ban/filter.d目录下,然后打开/usr/local/nginx/conf/nginx.conf编辑添加这个nginx-custom过滤的拦截条件如下:

[nginx-custom]
enabled  = true
port     = http,https
filter   = nginx-custom
action   = iptables-multiport[name=BadBots, port="http,https"]
logpath  = /home/wwwlogs/*.log
maxretry = 1
bantime  = 86400  

然后保存,重启Fail2ban就可以生效进行实时分析拦截了。这个拦截条件有点儿苛刻了,触发一次就屏蔽IP一整天确实够狠,呵呵!

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号