使用fail2ban进行攻击防范

转自:https://kyle.ai/blog/6215.html

最近总有一些无聊的人,会来扫描一下我的服务器,看有没有啥漏洞可以利用的。。。

可以看到类似这样的404访问日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[W 2017-06-07 08:32:32 web:1728] 404 GET //pluse7xue.php (103.229.125.236) 0.55ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //plusmybak.php (103.229.125.236) 0.57ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //plusxsvip.php (103.229.125.236) 0.60ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //pluslaobiao.php (103.229.125.236) 0.60ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //xiaolei.php (103.229.125.236) 0.45ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //dxyylcmd5.php (103.229.125.236) 0.45ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //UserFilesx.aspx (103.229.125.236) 0.52ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //UserFilesx.aspx (103.229.125.236) 0.38ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //UserFilesx.aspx (103.229.125.236) 0.44ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //datacachefuck.php.parse_search_.inc (103.229.125.236) 0.47ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //datacachefuck.php.parse_search_.inc (103.229.125.236) 0.49ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //datacachefuck.php.parse_search_.inc (103.229.125.236) 0.43ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //utilityconvertdataconfig.inc.php (103.229.125.236) 0.43ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //configAspCms_Config.asp (103.229.125.236) 0.42ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //plusbakup.php (103.229.125.236) 0.43ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //includecodemp.php (103.229.125.236) 0.59ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //templetsplussky.php (103.229.125.236) 0.57ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //plusmytag_j.php?aid=6022 (103.229.125.236) 0.60ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //plusmytag_j.php?aid=6022 (103.229.125.236) 0.47ms
[W 2017-06-07 08:32:32 web:1728] 404 GET //plusmytag_j.php?aid=6022 (103.229.125.236) 0.49ms
 
.....

这种攻击的特点就是短时间内会大量的404请求,可以利用这一特点,用工具进行拦截,检测到多少时间内,404请求数超过了多少,就把这个ip给封掉。

于是就从网上搜索到了这个工具:fail2ban,官方主页:https://www.fail2ban.org/wiki/index.php/MANUAL_0_8

在Ubuntu中安装fail2ban很简单:

1
sudo apt-get install fail2ban

安装好后,配置文件在 /etc/fail2ban 里面。

首先我们需要配置一个filter规则,以便检测我们的日志文件中404的目标。

vim /etc/fail2ban/filters.d/web-404.conf

1
2
3
4
5
6
7
[INCLUDES]
 
before = common.conf
 
[Definition]
 
failregex = web:1728\] 404 GET .*?\(<HOST>\)

写好规则后,可以用命令行测试一下能不能命中:

1
fail2ban-regex 2017060708.log /etc/fail2ban/filter.d/web-404.conf

有一点必须注意,你的日志文件中,行的开头一定要先命中时间戳,格式可以是系统能识别的时间格式,如果没有命中时间,那么你写的filter规则是永远不会被命中的。

比如我刚开始的日志格式是这样的:

1
W 20170607 08:32:32 web:1728] 404 GET //plusxsvip.php (103.229.125.236) 0.60ms

日期格式 20170607 不能被识别,所以我写的filter规则死活命中不了。。。

官方文档有解释:https://www.fail2ban.org/wiki/index.php/MANUAL_0_8

1
2
3
4
5
6
7
8
9
In order for a log line to match your failregex, it actually has to match in two parts: the beginning of the line has to match a timestamp pattern or regex,
and the remainder of the line has to match your failregex.
If the failregex is anchored with a leading ^, then the anchor refers to the start of the remainder of the line, after the timestamp and intervening whitespace.
 
The pattern or regex to match the time stamp is currently not documented, and not available for users to read or set.
See Debian bug #491253. This is a problem if your log has a timestamp format that fail2ban doesn't expect, since it will then fail to match any lines.
Because of this, you should test any new failregex against a sample log line, as in the examples below, to be sure that it will match.
If fail2ban doesn't recognize your log timestamp, then you have two options: either reconfigure your daemon to log with a timestamp in a more common format,
such as in the example log line above; or file a bug report asking to have your timestamp format included.

如果要自定义时间格式,可以在filter的配置中设置 datepattern 参数,详情见官方文档:https://manpages.debian.org/testing/fail2ban/jail.conf.5.en.html

定义好filter后,就可以写ban的规则:

cat /etc/fail2ban/jail.local

1
2
3
4
5
6
7
8
9
[web-404]
 
enabled = true
port = http,https
filter  = web-404
logpath  = /var/log/supervisor/web.log tail
maxretry = 10
bantime = 86400
findtime = 60

上面配置的含义就是,使用 moviex_web-404 这一个filter配置,检测日志文件logpath,在findtime时间内,匹配到 maxretry 次数后,就ban掉对方的ip,在有效时间 bantime 内,拒绝对方连接 port 参数指定的端口。

logpath 的第二个参数可以是 head 或 tail,默认为head,从日志的开头进行匹配,这样有个不好的地方就是每次重启fail2ban后,都要从日志开头匹配一遍。

如果有多个logpath,则换行写,一行写一条。

可以在 jail.local 中定义忽略ip,比如说google 爬虫的ip地址,在jail.local文件中加上配置:

1
2
3
4
[DEFAULT]
# 以空格分隔的列表,可以是 IP 地址、CIDR 前缀或者 DNS 主机名
# 用于指定哪些地址可以忽略 fail2ban 防御
ignoreip = 127.0.0.1 172.31.0.0/24 10.10.0.0/24 192.168.0.0/24

如果想要让ip白名单只针对某些jail规则生效,则可以使用fail2ban-client命令来设置。例如:

1
fail2ban-client set web-404 addignoreip 123.45.67.89  

重启fail2ban生效后,可以查看日志 /var/log/fail2ban.log 来看检测情况,也可以通过命令行来查看状态:

1
2
3
4
5
6
7
8
# 查看所有jail状态
sudo fail2ban-client status
 
# 查看web-404 这个jail的状态
sudo fail2ban-client status web-404
 
# 解封ip
sudo fail2ban-client set web-404 unbanip 192.168.1.8

fail2ban-client 还有很多其它命令,可以参考文档说明: fail2ban-client –help

【fail2ban】使用fail2ban进行攻击防范的更多相关文章

  1. Fail2ban + firewalld 防护doss攻击

    系统环境:centos7.3 用途:利用fail2ban+Firewalld来防CC攻击和SSH爆破 准备工作: 1.检查Firewalld是否启用 #如果您已经安装iptables建议先关闭 ser ...

  2. Yeslab 华为安全HCIE-第五门-防火墙攻击防范技术

    Yeslab 华为安全HCIE-第五门-防火墙攻击防范技术 课程目录 Yeslab华为安全HCIE-第五门-防火墙攻击防范技术(8篇)\1_单包攻击防范.aviYeslab华为安全HCIE-第五门-防 ...

  3. XSS攻击防范

    前端安全系列之XSS攻击防范 1.使用textContent 2.使用HTML转义 把JS中的标签转成字符 3.对于链接跳转 禁止含有'javascript:'开头的字符 4.标签属性中含有恶意执行代 ...

  4. Fail2ban 使用Fail2ban监禁SSH服务的恶意IP

    Fail2ban自带了很多服务的过滤器(filter)和动作(action),它已经帮你做好了,所以一般情况下我们无需定义,直接引用即可. 这边只是一个示例. 系统版本:Ubuntu 16.04.5 ...

  5. Fail2ban 安装Fail2ban到Ubuntu

    系统版本:Ubuntu 16.04.5 LTS 软件版本:fail2ban-0.9.3 硬件要求:无 1.安装Fail2ban root@local:~# apt-get update root@lo ...

  6. Django_博客_XSS 攻击防范

    背景: 博客项目中用户后台添加文章时,若通过富文本编辑器输入 标签内容或者 js 指令会导致文章排版错乱,甚至进行XSS攻击 攻击现象: 文本内容输入 js 指令 文章描述时正确显示其文本内容 但在打 ...

  7. 《DNS攻击防范科普系列3》 -如何保障 DNS 操作安全

    引言 前两讲我们介绍了 DNS 相关的攻击类型,以及针对 DDoS 攻击的防范措施.这些都是更底层的知识,有同学就来问能否讲讲和我们的日常操作相关的知识点,今天我们就来说说和我们日常 DNS 操作相关 ...

  8. 关于针对XSS漏洞攻击防范的一些思考

    众所周知,XSS几乎在最常见.危害最大的WEB漏洞.针对这个危害,我们应该怎么防范呢. 下面简单说一下思路. 作者:轻轻的烟雾(z281099678) 一.XSS漏洞是什么 XSS漏洞网上的资料太多, ...

  9. linux 普通synflood攻击防范网络参数设置

    linux如何防SYN攻击 [root@web ~]# netstat -anp |awk '{print $6}'|sort|uniq -c |sort -rn 172 ESTABLISHED 59 ...

随机推荐

  1. MyEclipse 2015利用Cygwin+CDT搭建C/C++开发环境

    GitHub原文:https://github.com/x113773/testall/issues/22 首先安装Cygwin 1:首先去网站 www.cygwin.com 下载 Cygwin 的 ...

  2. MicroPython开发板:TPYBoard v102 播放音乐实例

    0x00前言 前段时间看到TPYBoard的技术交流群(群号:157816561,)里有人问关于TPYBoard播放音乐的问题.最近抽空看了一下文档介绍,着手做了个实验.更多MicroPython的教 ...

  3. Hyperledger Fabric 本地运行的简单示例

    环境: Ubuntu 16.04 go 1.7.4 版本: Fabric v1.0.0-alpha 本文主要目的就是让大家体验以下Fabric网络环境搭建的具体过程,不基于集成化脚本手动搭建. 一.编 ...

  4. tomcat 发布简单的html网站

    1.建立一个文件夹D:\Demo 2.在Demo目录下,建立一个WEB-INF的文件夹并将web.xml放在里面,D:\Demo\WEB-INF\web.xml 3.将index.html文件放在De ...

  5. SSM框架开发web项目系列(六) SpringMVC入门

    前言 我们最初的javaSE部分学习后,基本算是入门了,也熟悉了Java的语法和一些常用API,然后再深入到数据库操作.WEB程序开发,渐渐会接触到JDBC.Servlet/Jsp之类的知识,期间可能 ...

  6. [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected

    1:出现此种错误应该是jar版本包冲突了,启动hive的时候,由于hive依赖hadoop,启动hive,会将hadoop的配置以及jar包等等导入到hive中,导致jar包版本冲突,下面贴一下错误, ...

  7. PE文件简介

    PE文件的全称是Portable Executable,意为可移植的可执行的文件,常见的EXE.DLL.OCX.SYS.COM都是PE文件,PE文件是微软Windows操作系统上的程序文件(可能是间接 ...

  8. Node.js学习笔记(三): 事件机制

    大部分的nodejs核心api都建立在异步的事件驱动架构之上,所以events是Node.js 最重要的模块,它提供了唯一的接口.events 模块不仅用于用户代码与 Node.js 下层事件循环的交 ...

  9. CAD 二次开发 -- 自动加载开发的DLL

    CAD二次开发可以采用写扩展DLL的方式实现.该DLL的函数可以被CAD调用. 但是调用前,必须用命令netload 将该dll加载到CAD. 其实可以修改注册表,当CAD软件启动后,自动加载扩展DL ...

  10. Android4.0 声卡配置-高通msm8916移植

    一个正常的UAC设备插入Android 7.0是默认打开UAC配置的,打印的log如下: [ - using xhci_hcd [ - [ -, Product=, SerialNumber= [ - ...