fail2ban防止暴力破解-防止nginx服務器web目錄被黑客掃描

1. 背景

剛買了阿里雲服務器,準備用來部署自己的一些站點。結果剛把lnmp環境搭建好,才一天的時間就被來自不同地域IP不斷的掃描web站點目錄,這運氣怕是沒幾個人能遇到了,幸好之前有熟悉過防止暴力破解fail2ban服務。下面就來介紹一下這款服務軟件。寫這篇博客參加以下文章:

http://www.361way.com/fail2ban-nginx/1825.html  參考-匹配RUL規則。

https://www.cnblogs.com/wangxiaoqiangs/p/5630325.html 參考-安裝教程

https://blog.csdn.net/shuchengzhang/article/details/50931123 參考-相關配置文件

https://blog.csdn.net/w958660278/article/details/80592928  參考-fail2ban從黑名單中移除IP

http://www.zsythink.net/archives/1199 參考-iptables配置規則

http://www.fail2ban.org/wiki/index.php/Main_Page 參考-官網文檔

2. fail2ban的簡單介紹

Fail2ban 能夠監控系統日誌,匹配日誌中的錯誤信息(使用正則表達式),執行相應的屏蔽動作(支持多種,一般爲調用 iptables ),是一款很實用、強大的軟件。

如:攻擊者不斷嘗試窮舉 SSH 、SMTP 、FTP 密碼等,只要達到預設值,fail2ban 就會調用防火牆屏蔽此 IP ,並且可以發送郵件通知系統管理員。

功能、特性:

1、支持大量服務:sshd 、apache 、qmail 等
2、支持多作動作:iptables 、tcp-wrapper 、shorewall 、mail notifications 等
3、logpath 選項中支持通配符
4、需要 Gamin 支持(Gamin 用於監控文件和目錄是否更改)
5、如果需要郵件通知,則系統事先要確保能夠正常發送郵件

3. fail2ban的安裝、配置文件介紹

3.1 安裝

# 安裝epel-release
yum -y install epel-release
# 安裝fail2ban
yum -y install fail2ban

3.2 文件目錄結構

/etc/fail2ban               ## fail2ban 服務配置目錄
/etc/fail2ban/action.d      ## iptables 、mail 等動作文件目錄
/etc/fail2ban/filter.d      ## 條件匹配文件目錄,過濾日誌關鍵內容
/etc/fail2ban/jail.conf     ## fail2ban 防護配置文件
/etc/fail2ban/fail2ban.conf ## fail2ban 配置文件,定義日誌級別、日誌、sock 文件位置等

4. fail2ban的配置文件介紹

4.1 fail2ban.conf配置文件

shell > grep -v ^# /etc/fail2ban/fail2ban.conf

[Definition]
loglevel = 3                              ## 定義日誌級別,默認
logtarget = /var/log/fail2ban.log         ## 定義 fail2ban 日誌文件
socket = /var/run/fail2ban/fail2ban.sock  ## sock 文件存放位置,默認
pidfile = /var/run/fail2ban/fail2ban.pid  ## pid 文件存放位置,默認

4.2 jail.conf 防護配置

shell > grep -v ^# /etc/fail2ban/jail.conf
[DEFAULT]                         ## 全局設置,優先級最小
ignoreip = 127.0.0.1/8            ## 不受限制的 IP ,多組用空格分割
bantime = 600                     ## 非法 IP 被屏蔽時間(秒),-1 代表永遠封鎖
findtime = 600                    ## 設置多長時間(秒)內超過 maxretry 限制次數即被封鎖
maxretry = 3                      ## 最大嘗試次數
backend = auto                    ## 日誌修改檢測機制(gamin 、polling 、auto 三種)
usedns = warn
[ssh-iptables]                    ## 分類設置(基於 SSHD 服務的防護)
enabled = true                    ## 是否開啓防護,false 爲關閉
filter = sshd                     ## 過濾規則 filter 名稱,對應 filter.d 目錄下的 sshd.conf
action = iptables[name=SSH, port=ssh, protocol=tcp] ## 動作參數 
logpath = /var/log/secure         ## 檢測系統登陸日誌文件
maxretry = 5                      ## 最大嘗試次數

更多項………………

4.3 配置防止nginx服務器web目錄被黑客掃描

 在jail.conf文件末尾加以下內容

shell > vim /etc/fail2ban/jail.conf
[nginx]
enabled = true
port = http,https
filter = nginx
action = iptables[name=nginx, port=http, protocol=tcp]
logpath = /www/lnmp/log/nginx/access.log
bantime = 3600
findtime = 60
maxretry = 5

在etc/fail2ban/filter.d目錄下新增nginx.conf文件並追加以下內容:

shell > vim /etc/fail2ban/filter.d/nginx.conf

[Definition]
failregex = <HOST> -.*- .*HTTP/1.* 404 .*$
ignoreregex =

5. fail2ban跳過的坑

5.1 fail2ban無法啓動起來

檢查配置文件格式是否正確

fail2ban-regex /www/lnmp/log/nginx/access.log /etc/fail2ban/filter.d/nginx.conf

根據啓動信息查詢啓動原因,入坑(nginx日誌文件路徑配置錯誤)

fail2ban-client start

5.2 如何刪除fail2ban中已被禁用的黑名單IP

fail2ban-配置

格式:
fail2ban-client set 服務規則 unbanip IP地址
例子:
fail2ban-client set nginx unbanip 8.8.8.8

iptaables 刪除對應的規則

# 按編號列出規則信息
shell > iptables -nL --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    f2b-nginx  tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain f2b-nginx (1 references)
num  target     prot opt source               destination         
1    REJECT     all  --  8.8.8.8        0.0.0.0/0            reject-with icmp-port-unreachable
2    REJECT     all  --  9.9.9.9      0.0.0.0/0            reject-with icmp-port-unreachable
3    RETURN     all  --  0.0.0.0/0            0.0.0.0/0 
# iptables 刪除標識 規則名稱 該規則下的編號          
shell > iptables -D f2b-nginx 1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章