Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Windows 綜合教學 >> Windows 資訊

防禦 SSH 與 vsftpd 暴力破解:自動化封鎖腳本實作

編輯:Windows 資訊

發現 FTP 備份伺服器 /var/log/secure 記錄大量 sshd 與 vsftpd 認證失敗,顯然遭暴力破解攻擊。編寫 Shell 腳本配合 crontab 每 6 小時掃描日誌,若單週內同一 IP 失敗超過 100 次,自動加入 /etc/hosts.deny 黑名單。

關鍵技術點

sshd 與 vsftpd 失敗日誌格式不同,傳統 awk 單一模式無法同時擷取 IP。最終採用 awk 遍歷欄位匹配「rhost」關鍵字,提取 IP 並統計次數,效率最高。

腳本邏輯

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

執行後產生黑名單,自動封鎖惡意 IP。crontab 設定為 * */6 * * * root /bin/bash /root/hostsdeny.sh >> /dev/null 2>&1。可根據攻擊頻率調整執行間隔。

Copyright © Windows教程網 All Rights Reserved