用shell指令碼防ssh和vsftpd暴力破解的詳解講解_linux shell

來源:互聯網
上載者:User

指令碼需求如下:此SHELL指令碼放在crontab計劃任務裡,每隔6小時(此時間根據實際情況來定義)就去讀取/var/log/secure指令碼,取出裡面惡意猜測IP,如果單位時間內(一星期)的串連數是高於一個閥值,例如100(此閥值也可以根據實際情況來定義),則將其加進/etc/hosts.deny黑名單裡,如果低於此閥值,則無視此IP。

/var/log/secure裡認證失敗資訊如下:

複製代碼 代碼如下:

Nov 28 10:18:08 centos2 sshd[7556]: Connection closed by 222.216.30.109
Nov 28 10:18:08 centos2 sshd[7557]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.216.30.109  user=root
Nov 28 10:18:09 centos2 sshd[7559]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.216.30.109  user=root
Nov 28 10:18:10 centos2 sshd[7551]: Failed password for root from 222.216.30.109 port 2391 ssh2
Nov 28 10:18:10 centos2 sshd[7552]: Connection closed by 222.216.30.109
Nov 28 10:18:10 centos2 sshd[7553]: Failed password for root from 222.216.30.109 port 2397 ssh2
Nov 28 10:18:10 centos2 sshd[7554]: Connection closed by 222.216.30.109
Nov 28 10:18:11 centos2 sshd[7557]: Failed password for root from 222.216.30.109 port 2401 ssh2
Nov 28 10:18:11 centos2 sshd[7558]: Connection closed by 222.216.30.109
Nov 28 10:18:11 centos2 sshd[7559]: Failed password for root from 222.216.30.109 port 2403 ssh2
Nov 28 10:18:11 centos2 sshd[7560]: Connection closed by 222.216.30.109
Nov 28 10:37:01 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:37:01 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=hello rhost=centos1.cn7788.com
Nov 28 10:37:01 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user hello
Nov 28 10:37:19 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:37:19 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=yhc rhost=centos1.cn7788.com
Nov 28 10:37:19 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yhc
Nov 28 10:37:36 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:37:36 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=yuhongchun rhost=centos1.cn7788.com
Nov 28 10:37:36 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yuhongchun
Nov 28 10:42:44 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:42:44 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=yuhongchun rhost=114.112.169.70
Nov 28 10:42:44 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yuhongchun
Nov 28 10:42:56 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:42:56 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=andrewyu rhost=114.112.169.70
Nov 28 10:42:56 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user andrewyu

我們觀察下/var/log/secure檔案輪詢特徵,如下所示:

複製代碼 代碼如下:

[root@centos2 log]# ls -lsart secure.*
512 -rw------- 1 root root 516379 11-04 01:31 secure.4
660 -rw------- 1 root root 668192 11-11 00:05 secure.3
304 -rw------- 1 root root 306589 11-17 10:33 secure.2
484 -rw------- 1 root root 488620 11-25 02:33 secure.1

基本上,secure檔案是以星期為輪詢周期的,如果對安全要求嚴格的朋友還可以本著“一個不放過”的原則來抓取上面的舊secure的惡意IP,下面我們就們就要想辦法高效的來抓取這些惡意IP,如果參考原始版本的SHELL指令碼寫法,,我們這裡要抓取secure日誌中的偵測vsftpd及sshd服務的IP地址,我們可以用如下命令,命令如下所示:

複製代碼 代碼如下:

cat /var/log/secure | awk '/Failed/{print $(NF-3)}'| sort| uniq -c| awk '{print $2"="$1;}'

很明顯,這樣是取不到vsftpd失敗的IP值的,sshd日誌失敗資訊跟vsftpd日誌失敗資訊不一樣,我寫了幾種awk混合sed的方法,測試了效率,感覺用awk指令碼速度是最快的,大家也可以寫幾種,用time命令測試下;最後精簡了下代碼,完成了整個指令碼,指令碼內容如下所示:

複製代碼 代碼如下:

#!/bin/bash
#Denyhosts For vsftpd and sshd
#2012-12-28
awk '{for(i=1;i<=NF;i++){if($i ~ /rhost/)print substr($i,7)}}' /var/log/secure  | sort | uniq  -c  >/root/black.txt
DEFINE="100"
for i in `cat  /root/black.txt`
do
        IP=`echo $i |awk  '{print $1}'`
        NUM=`echo $i|awk  '{print $2}'`
        if [ $NUM -gt $DEFINE ];
        then
         grep $IP /etc/hosts.deny > /dev/null
          if [ $? -gt 0 ];
          then
          echo "sshd:$IP" >>  /etc/hosts.deny
          echo "vsftpd:$IP" >> /etc/hosts.deny
          fi
        fi
done

指令碼運行一段時間後,我們可以觀察此指令碼涉及到的一些檔案,如/root/black.txt,結果如下所示:

複製代碼 代碼如下:

[root@centos2 ~]# cat /root/black.txt
      2 113.17.144.156
      4 114.112.51.208
      4 114.112.69.170
    169 118-163-227-50.hinet-ip.hinet.net
      8 119.188.7.200
      8 122.70.130.11
     61 124.248.32.246
     12 183.203.14.121
      3 189.26.255.11
     56 199.204.237.60
      3 199.30.53.220
      5 201.236.80.4
      6 220.172.191.31
     30 222.216.30.109
     60 222.253.159.111
     58 223.4.180.23
    166 58.221.42.178
      1 61.132.4.85
    152 61.142.106.34
     22 61.167.33.222
      7 85.126.166.83
    166 www.b-nets.com

/etc/hosts.deny指令碼內容如下:

複製代碼 代碼如下:

sshd:124.248.32.246
vsftpd:124.248.32.246
sshd:199.204.237.60
vsftpd:199.204.237.60
sshd:222.253.159.111
vsftpd:222.253.159.111
sshd:223.4.180.23
vsftpd:223.4.180.23
sshd:58.221.42.178
vsftpd:58.221.42.178
sshd:61.142.106.34
vsftpd:61.142.106.34
sshd:118-163-227-50.hinet-ip.hinet.net
vsftpd:118-163-227-50.hinet-ip.hinet.net
sshd:www.b-nets.com
vsftpd:www.b-nets.com

最後,我們將此shell指令碼放進crontab 裡,每間隔六小時就運行一次,命令如下:

複製代碼 代碼如下:

* */6 * * * root /bin/bash /root/hostsdeny.sh >> /dev/null 2>&1

由於/var/log/secure日誌是以星期為輪詢的,此指令碼執行頻率可自行設定,如果感覺伺服器被頻繁偵測,執行頻率間隔可設定短些,反之,可設定長些。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.