最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用shell腳本防ssh和vsftpd暴力破解的詳解講解

 更新時(shí)間:2014年01月02日 13:51:05   作者:  
新近剛上的FTP備份服務(wù)器,例行檢查/var/log/secure日志時(shí),發(fā)現(xiàn)不少sshd和vsftpd失敗認(rèn)證信息,很明顯有人想用暴力破解工具竊取密碼,所以需要編寫一個(gè)安全腳本防止

腳本需求如下:此SHELL腳本放在crontab計(jì)劃任務(wù)里,每隔6小時(shí)(此時(shí)間根據(jù)實(shí)際情況來(lái)定義)就去讀取/var/log/secure腳本,取出里面惡意猜測(cè)IP,如果單位時(shí)間內(nèi)(一星期)的連接數(shù)是高于一個(gè)閥值,例如100(此閥值也可以根據(jù)實(shí)際情況來(lái)定義),則將其加進(jìn)/etc/hosts.deny黑名單里,如果低于此閥值,則無(wú)視此IP。

/var/log/secure里認(rèn)證失敗信息如下:

復(fù)制代碼 代碼如下:

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文件輪詢特征,如下所示:

復(fù)制代碼 代碼如下:

[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文件是以星期為輪詢周期的,如果對(duì)安全要求嚴(yán)格的朋友還可以本著“一個(gè)不放過(guò)”的原則來(lái)抓取上面的舊secure的惡意IP,下面我們就們就要想辦法高效的來(lái)抓取這些惡意IP,如果參考原始版本的SHELL腳本寫法,,我們這里要抓取secure日志中的偵測(cè)vsftpd及sshd服務(wù)的IP地址,我們可以用如下命令,命令如下所示:

復(fù)制代碼 代碼如下:

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

很明顯,這樣是取不到vsftpd失敗的IP值的,sshd日志失敗信息跟vsftpd日志失敗信息不一樣,我寫了幾種awk混合sed的方法,測(cè)試了效率,感覺(jué)用awk腳本速度是最快的,大家也可以寫幾種,用time命令測(cè)試下;最后精簡(jiǎn)了下代碼,完成了整個(gè)腳本,腳本內(nèi)容如下所示:

復(fù)制代碼 代碼如下:

#!/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

腳本運(yùn)行一段時(shí)間后,我們可以觀察此腳本涉及到的一些文件,如/root/black.txt,結(jié)果如下所示:

復(fù)制代碼 代碼如下:

[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腳本內(nèi)容如下:

復(fù)制代碼 代碼如下:

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腳本放進(jìn)crontab 里,每間隔六小時(shí)就運(yùn)行一次,命令如下:

復(fù)制代碼 代碼如下:

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

由于/var/log/secure日志是以星期為輪詢的,此腳本執(zhí)行頻率可自行設(shè)定,如果感覺(jué)服務(wù)器被頻繁偵測(cè),執(zhí)行頻率間隔可設(shè)置短些,反之,可設(shè)置長(zhǎng)些。

您可能感興趣的文章:

相關(guān)文章

  • bash提取字符串${string:position:length}的具體使用

    bash提取字符串${string:position:length}的具體使用

    本文主要介紹了bash提取字符串${string:position:length}的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • linux系統(tǒng)mysql自動(dòng)備份并使用ftp上傳的方法

    linux系統(tǒng)mysql自動(dòng)備份并使用ftp上傳的方法

    這篇文章主要介紹了linux系統(tǒng)自動(dòng)導(dǎo)出mysql備份,并上傳到指定ftp,還可以刪除舊的備份文件,大家參考使用吧
    2014-01-01
  • 驗(yàn)證用戶輸入的參數(shù)合法性的shell腳本

    驗(yàn)證用戶輸入的參數(shù)合法性的shell腳本

    今天這個(gè)例子是 用來(lái)驗(yàn)證用戶輸入的參數(shù)的合法性的,程序并不復(fù)雜,需要的朋友可以參考下
    2016-08-08
  • 在Shell中分割字符串的例子

    在Shell中分割字符串的例子

    這篇文章主要介紹了在Shell中分割字符串的例子,使用3種方法實(shí)現(xiàn),需要的朋友可以參考下
    2014-06-06
  • 淺析Linux tar打包命令

    淺析Linux tar打包命令

    這篇文章主要介紹了Linux tar打包命令,本文給大家?guī)?lái)八個(gè)范例,每個(gè)范例都給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-06-06
  • linux啟動(dòng)流程詳細(xì)介紹

    linux啟動(dòng)流程詳細(xì)介紹

    這篇文章主要介紹了linux啟動(dòng)流程詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Linux上使用FFmpeg進(jìn)行錄音功能

    Linux上使用FFmpeg進(jìn)行錄音功能

    這篇文章主要為大家詳細(xì)介紹了Linux上如何使用FFmpeg進(jìn)行錄音功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-01-01
  • 收集的48個(gè)Shell腳本小技巧

    收集的48個(gè)Shell腳本小技巧

    這篇文章主要介紹了收集的48個(gè)Shell腳本小技巧,涵蓋方方面面,比如獲取CPU信息、VI技巧、正則表達(dá)式等等,需要的朋友可以參考下
    2014-08-08
  • shell判斷文件,目錄是否存在或者具有權(quán)限的代碼

    shell判斷文件,目錄是否存在或者具有權(quán)限的代碼

    因shell還不是很熟練,所以做此筆記,方便查看,學(xué)習(xí)shell的朋友可以參考下
    2013-06-06
  • 詳解Shell $*和$@之間的區(qū)別

    詳解Shell $*和$@之間的區(qū)別

    這篇文章主要介紹了詳解Shell $*和$@之間的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評(píng)論

泸定县| 克东县| 渑池县| 武定县| 武山县| 乌拉特中旗| 塔河县| 承德市| 兰坪| 乡城县| 岳阳市| 文登市| 新宁县| 洮南市| 布尔津县| 广安市| 蓬莱市| 宣武区| 会泽县| 西平县| 井陉县| 梁平县| 汾阳市| 富顺县| 鄂托克前旗| 吴江市| 嘉黎县| 清水河县| 滁州市| 温宿县| 集安市| 巫溪县| 密云县| 临颍县| 内江市| 罗城| 广昌县| 洪雅县| 伊宁县| 化德县| 浙江省|