Linux 系統安全 -- 防暴力破解

fail2ban防止暴力破解python

1. 下載stable穩定版解壓縮1git

2. 編譯安裝1github

3. 拷貝啓動腳本1shell

4. 修改配置文件1ubuntu

5. 啓動fail2ban2vim

6. 測試,ssh鏈接,連續輸入3次密碼2centos

7. 此時在查看服務端的防火牆和failban狀態2api

8. 解鎖被failban禁止登陸的ip3bash

9. 自動部署的腳本3 ssh

防止暴力破解的通常方法:

   1) 密碼足夠複雜  2)修改端口號  3) 禁用root登  4)第三方防爆破軟件

fail2ban實現鎖IP

     說明:監視系統日誌,而後經過匹配日誌(/var/log/secure)錯誤信息(正則匹配),執行相應的屏蔽動做(將知足動做的相關IP利用iptables加入到drop列表必定的時間),如禁止登陸,並且能夠發送郵件提示

除了sshfail2ban還支持多項服務,具體查看配置文件

官網:http://www.fail2ban.org

系統基於centos

1. 下載stable穩定版解壓縮

[root@mfsdata03 ~]# wget https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.8.14

[root@mfsdata03 ~]# tar -xf 0.8.14

[root@mfsdata03 ~]# cd fail2ban-0.8.14/

2. 編譯安裝

說明:要求python版本需大於2.4

安裝方式 python setup.py install

[root@mfsdata03 fail2ban-0.8.14]# python -V

Python 2.6.6

[root@mfsdata03 fail2ban-0.8.14]# python setup.py install

 

3. 拷貝啓動腳本

[root@mfsdata03 fail2ban-0.8.14]# cp files/redhat-initd /etc/init.d/fail2ban

說明:該文件中含有開機啓動級別chkconfig,便可以認爲是啓動腳本

[root@mfsdata03 fail2ban-0.8.14]# cat /etc/init.d/fail2ban |grep chkconfig

# chkconfig: - 92 08

 

4. 修改配置文件

 [root@mfsdata03 fail2ban-0.8.14]# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.conf.ori

   解釋:

     enabled = ture             #開啓功能

     logpath = /var/log/secure    #ssh登陸日誌文件

     maxrty = 3                #嘗試3

     findtime = 300             #監控300    #須要新添加

     bantime = 3600            #禁用IP時間,1小時  #須要新添加

 wKioL1ivqFXCvx71AAAyAkRviv0471.png-wh_50

[DEFAULT]                     #全局設置

ignoreip = 127.0.0.1/8       #忽略的IP列表,不受設置限制

bantime  = 600               #屏蔽時間,單位:秒

findtime  = 500               #這個時間段內超過規定次數會被ban

maxretry = 3                    #最大嘗試次數

backend = auto   

 

[root@mfsdata03 fail2ban-0.8.14]# diff /etc/fail2ban/jail.conf /etc/fail2ban/jail.conf.ori

96c96

< enabled  = true

---

> enabled  = false

100,103c100,101

< logpath  = /var/log/secure

< maxretry = 3

< findtime = 300

< bantime = 3600

---

> logpath  = /var/log/sshd.log

> maxretry = 5

 

5. 啓動fail2ban

[root@mfsdata03 fail2ban-0.8.14]# service fail2ban start

Starting fail2ban:                                         [  OK  ]

 

6. 測試,ssh鏈接,連續輸入3次密碼

結果:被拒絕3600秒不能再次登陸

root@ubuntu:~# ssh leco@192.168.5.104

leco@192.168.5.104's password:         #第一次故意輸錯密碼

Permission denied, please try again.

leco@192.168.5.104's password:         #第二次故意輸錯密碼

Permission denied, please try again.

leco@192.168.5.104's password:         #第三次故意輸錯密碼

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

root@ubuntu:~# ssh leco@192.168.5.104  #此時再次登陸的時候就被拒絕了

ssh: connect to host 192.168.5.104 port 22: Connection refused

 

7. 此時在查看服務端的防火牆和failban狀態

[root@mfsdata03 ~]#  fail2ban-client status ssh-iptables

Status for the jail: ssh-iptables

|- filter

|  |- File list:/var/log/secure

|  |- Currently failed:0

|  `- Total failed:9

`- action

   |- Currently banned:1

   |  `- IP list:192.168.5.201

   `- Total banned:3

[root@mfsdata03 fail2ban-0.8.14]# iptables -L -n

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         

fail2ban-SSH  tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22

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         

REJECT     all  --  192.168.5.201        0.0.0.0/0           reject-with icmp-port-unreachable

RETURN     all  --  0.0.0.0/0            0.0.0.0/0

#此時能夠看到在被禁止之後,防火牆自動添加一條規則

我使用一條192.168.5.201機器試圖登陸192.168.5.104機器,密碼三次輸錯之後就被鎖定1小時(配置文件能夠修

改),鎖定就是經過防火牆策略,時間過了限制期後自動解鎖也就是刪除該iptables的命令。

注:fail2ban必定後於iptables啓動,即重啓iptables必定要重啓fail2ban,相反重啓fail2ban不用從新啓iptables。

8. 解鎖被failban禁止登陸的ip

[root@mfsdata03 ~]#  iptables -L -n --line-number

Chain INPUT (policy ACCEPT)

num  target     prot opt source               destination         

1    fail2ban-SSH  tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22

Chain FORWARD (policy ACCEPT)

num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)

num  target     prot opt source               destination         

Chain fail2ban-SSH (1 references)

num  target     prot opt source               destination         

1    REJECT     all  --  192.168.5.201        0.0.0.0/0           reject-with icmp-port-unreachable

2    RETURN     all  --  0.0.0.0/0            0.0.0.0/0           

[root@mfsdata03 ~]#  iptables -t filter -D INPUT 1

也就是iptables刪除第一條規則。

 

9. 自動部署的腳本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#自動部署shell腳本
#!/bin/bash
#fail2ban-0.8.14.tar.gz
#python 版本要大於 2.4
#user:root
tar   -zxf fail2ban-0.8.14. tar .gz
cd  /root/fail2ban-0 .8.14/
python setup.py  install 
cp  /root/fail2ban-0 .8.14 /files/redhat-initd  /etc/init .d /fail2ban
chkconfig --add fail2ban
sleep  1
rm  -rf  /root/fail2ban-0 .8.14/
rm  -rf  /root/fail2ban-0 .8.14. tar .gz
[ -f  /etc/fail2ban/jail .bak ]
if  [ $? - eq  0 ]; then
     exit  0
else
     cp  /etc/fail2ban/jail .conf  /etc/fail2ban/jail .bak
fi
sed  -i  '96c enable = true'  /etc/fail2ban/jail .conf
sed  -i  '100c logpath  = /var/log/secure'  /etc/fail2ban/jail .conf
sed  -i  '101c maxretry = 3'  /etc/fail2ban/jail .conf
sed  -i  '101a\bantime = 3600'  /etc/fail2ban/jail .conf
sed  -i  '101a\findtime = 300'  /etc/fail2ban/jail .conf
sleep  1
[ -f  /var/log/secure .bak ]
if  [ $? - eq  0 ]; then
     exit  0
else
     cp  /var/log/secure  /var/log/secure .bak
fi
/var/log/secure
/etc/init .d /fail2ban  start &> /dev/null

 

至此配置工做完成了,另外,須要注意如下一些小問題。

1. 若是配置錯誤,如誤操做把本身禁止掉,只要執行>/var/log/secure,清空日誌文件,由於fail2ban的工做原理就是讀取必定時間內的日誌文件,經過配置進行過濾的。

2. 不少企業通常不用默認的22端口,會使用其餘端口,這個是配置文件和防火牆端口要作相應的更改,以下圖如下是要修改的2個地方。

A. vim /etc/fail2ban/jail.conf

wKioL1ivqJbjjOEYAAEhFG_D_Nc702.png-wh_50 

B. vim /etc/fail2ban/action.d/iptables.conf

wKiom1ivqKDBECp7AAFpUSFt1B4886.png-wh_50

 

3. 若是使用想要永久禁用某個ip登陸,可使用腳本結合crond按期執行:腳本以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# author :caimengzhi
# date   :2016-03-12
# Version:V1.0
cat  /var/log/secure  awk  '/Failed/{print $(NF-3)}' | sort | uniq  -c| awk  '{print $2" = "$1}'  >> /root/log_fail2ban .txt
DEFINE= '10'  #定義失敗次數上限
for  each  in  $( cat  /root/log_fail2ban .txt)
do
     ip = $( echo  $each |  awk  -F "="  '{print $1}' )
         Count = $( echo  $each |  awk  -F "="  '{print $2}' )
         if  [ $Count -gt $DEFINE ]; then
             grep  $ip  /etc/hosts .deny > /dev/null
             if  [ $? -gt 0 ]; then
               echo  "sshd:$ip"  >>  /etc/hosts .deny
               fi
         fi
done
相關文章
相關標籤/搜索