我每天都会收到大量类似的 SQL 注入尝试。访问日志中的一段代码显示:
8222 24.247.182.172 - - [09/Nov/2018:08:47:25 -0600] ***************.com "GET /Add_Product.php?strPhotoID=VA1209&price_selected=2+union+select+0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526--&page_id=912 HTTP/1.1" 302 0 "https://www.***************.com/Add_Product.php?strPhotoID=VA1209&price_selected=2+union+select+0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526,0x5e2526--&page_id=912" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" "-"
这是jail.local中的配置:
[sql-union-select-attack]
enabled = true
filter = sql-union-select-attack
logpath = /var/log/nginx/*access.log
port = 8221,8222,8231,8232
maxretry = 1
findtime = 10
bantime = -1
action = iptables-allports[name=sqlUnionSelect]
这是过滤器(sql-union-select-attack.conf):
#The SQL Injection attempt with "union+select+" in the URL
[Definition]
failregex = ^\d{4} <HOST> -.*\"(GET|POST).*/Add_Product.php.*union
然而我在这里,没有被禁止,如下所示
fail2ban-client status sql-union-select-attack
root@web4:/etc/fail2ban# fail2ban-client status sql-union-select-attack
Status for the jail: sql-union-select-attack
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- File list: /var/log/nginx/**************.access.log /var/log/nginx/access.log
`- Actions
|- Currently banned: 0
|- Total banned: 0
`- Banned IP list:
这是怎么回事?我的 jail.local 配置有问题吗?
编辑
感谢 Michael Hampton 的回答,现在看来它正在发挥作用。我有一个 bash 脚本,可以让我选择一个监狱来获取状态:
root@web4:/etc/fail2ban# get_jail_status
http-get-dos
http-post-dos
magento-url
megaindex-crawler-spam
nginx-499
nginx-aspx-url
sql-directory-attempt
sql-union-select-attack
sshd
Please enter the jail you would like to check:
sql-union-select-attack
Status for the jail: sql-union-select-attack
|- Filter
| |- Currently failed: 0
| |- Total failed: 2
| `- File list: /var/log/nginx/**************.access.log /var/log/nginx/access.log
`- Actions
|- Currently banned: 5
|- Total banned: 5
`- Banned IP list: 142.163.212.50 217.61.108.219 37.59.8.29 207.228.228.8 162.144.126.204
如果我选择其中一个 IP 地址并在 iptables 中检查它,则会出现:
root@web4:/etc/fail2ban# iptables -L -v -n | grep 142.163.212.50
0 0 REJECT all -- * * 142.163.212.50 0.0.0.0/0 reject-with icmp-port-unreachable
看来它正在发挥作用!
答案1
Fail2ban 需要安装 pyinotify 才能通过 systemd 日志以外的方式读取日志文件。如果未安装,则应安装或重新安装。例如:
sudo apt-get install python-pyinotify
此后,重新启动 fail2ban 并等待几分钟以使其消化现有的日志文件。
(从技术上讲它也可以使用 gamin,但从历史上看,这并不十分可靠......)