Linux VPS上DenyHosts阻止SSH暴力攻擊
現在的互聯網非常不安全,很多人沒事就拿一些掃描機掃描ssh端口,然后試圖連接ssh端口進行暴力破解(窮舉掃描),所以建議vps主機的空間,盡量設置復雜的ssh登錄密碼,雖然在前段時間曾經介紹過Linux VPS禁止某個IP訪問使用hosts.deny禁止某些IP訪問,但是功能方面欠缺,如:不能自動屏蔽,那么有什么更好的辦法嗎,就可以使用denyhosts這款軟件了,它會分析/var/log/secure(redhat,Fedora Core)等日志文件,當發現同一IP在進行多次SSH密碼嘗試時就會記錄IP到/etc/hosts.deny文件,從而達到自動屏蔽該IP的目的。
DenyHosts官方網站為:http://denyhosts.sourceforge.net/
LNMP一件安裝包中自帶該軟件可以一鍵安裝,命令:wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5/tools/ && ./denyhosts.sh 回車確認即可開始安裝配置,不需要下面的步驟進行安裝配置。(該tools目錄下也有denyhosts相似的工具fail2ban的一鍵安裝工具 ./fail2ban.sh 安裝即可)
1、下載DenyHosts 并解壓
# wget http://soft.vpser.net/security/denyhosts/DenyHosts-2.6.tar.gz
#?tar zxvf DenyHosts-2.6.tar.gz
# cd DenyHosts-2.6
2、安裝、配置和啟動
安裝前建議執行:echo "" > /var/log/secure &&?service rsyslog restart 清空以前的日志并重啟一下rsyslog
# python setup.py install
因為DenyHosts是基于python的,所以要已安裝python,大部分Linux發行版一般都有。默認是安裝到/usr/share/denyhosts/目錄的,進入相應的目錄修改配置文件
# cd /usr/share/denyhosts/
# cp denyhosts.cfg-dist denyhosts.cfg
# cp daemon-control-dist daemon-control
默認的設置已經可以適合centos系統環境,你們可以使用vi命令查看一下denyhosts.cfg和daemon-control,里面有詳細的解釋
接著使用下面命令啟動denyhosts程序
# chown root daemon-control
# chmod 700 daemon-control
# ./daemon-control start
如果要使DenyHosts每次重起后自動啟動還需做如下設置:
# ln -sf /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts
# chkconfig --add denyhosts
# chkconfig --level 2345 denyhosts on
或者執行下面的命令加入開機啟動,將會修改/etc/rc.local文件:
# echo "/usr/share/denyhosts/daemon-control start" >> /etc/rc.local
DenyHosts配置文件/usr/share/denyhosts/denyhosts.cfg說明:
SECURE_LOG = /var/log/secure
#sshd日志文件,它是根據這個文件來判斷的,不同的操作系統,文件名稍有不同。
HOSTS_DENY = /etc/hosts.deny
#控制用戶登陸的文件
PURGE_DENY = 5m
DAEMON_PURGE?= 5m
#過多久后清除已經禁止的IP,如5m(5分鐘)、5h(5小時)、5d(5天)、5w(5周)、1y(一年)
BLOCK_SERVICE? = sshd
#禁止的服務名,可以只限制不允許訪問ssh服務,也可以選擇ALL
DENY_THRESHOLD_INVALID =?5
#允許無效用戶失敗的次數
DENY_THRESHOLD_VALID = 10
#允許普通用戶登陸失敗的次數
DENY_THRESHOLD_ROOT = 5
#允許root登陸失敗的次數
HOSTNAME_LOOKUP=NO
#是否做域名反解
DAEMON_LOG = /var/log/denyhosts
為防止自己的IP被屏蔽,可以:echo "你的IP" >> ?/usr/share/denyhosts/allowed-hosts 將你的IP加入白名單,再重啟DenyHosts:/etc/init.d/denyhosts ,如果已經被封,需要先按下面的命令刪除被封IP后再加白名單。
如有IP被誤封,可以執行下面的命令解封:wget?http://soft.vpser.net/security/denyhosts/denyhosts_removeip.sh && bash?denyhosts_removeip.sh 要解封的IP
更多的說明請查看自帶的README文本文件,好了以后維護VPS就會省一些心了,但是各位VPSer們注意了安全都是相對的哦,沒有絕對安全,將密碼設置的更Strong,并請定期或不定期的檢查你的VPS主機,而且要定時備份你的數據哦。
與DenyHosts類似的軟件還有fail2ban功能上更多,還可以對ftp進行保護,自己可以搜索看一下。
>>轉載請注明出處:VPS偵探 本文鏈接地址:http://www.0794baidu.com/security/denyhosts.html










centos8 變動比較大,默認沒python,可以執行:dnf install python2
alternatives --set python /usr/bin/python2
pip2 install ipaddr
后再進行安裝
CentOS 8 安裝 Denyhosts 出現如下錯誤,請問是哪里出問題?
Press any key to start...or Press Ctrl+c to cancel
Last metadata expiration check: 0:02:00 ago on Sun 22 Mar 2020 02:13:00 PM CST.
No match for argument: python
There are following alternatives for "python": python2, python36
Package rsyslog-8.37.0-13.el8.x86_64 is already installed.
No match for argument: python-ipaddr
Error: Unable to find a match: python python-ipaddr
Redirecting to /bin/systemctl restart rsyslog.service
Downloading...
denyhosts-3.1.tar.gz [found]
../include/main.sh: line 430: cd: /src: No such file or directory
Uncompress denyhosts-3.1.tar.gz...
cd denyhosts-3.1...
Installing...
./denyhosts.sh: line 33: python: command not found
Copy files...
cp: cannot stat '/usr/bin/daemon-control-dist': No such file or directory
chown: cannot access '/usr/bin/daemon-control': No such file or directory
chmod: cannot access '/usr/bin/daemon-control': No such file or directory
cp: cannot stat '/usr/bin/daemon-control': No such file or directory
sed: can't read /etc/init.d/denyhosts: No such file or directory
sed: can't read /etc/init.d/denyhosts: No such file or directory
Add denyhosts service at system startup...
error reading information on service denyhosts: No such file or directory
error reading information on service denyhosts: No such file or directory
Start DenyHosts...
./denyhosts.sh: line 82: /etc/init.d/denyhosts: No such file or directory
@xiao, 這個就是官網的方法用腳本來實現的,測試看是沒問題,能正常移除ip。lnmp安裝包tools目錄下也有,你也可以直接用安裝包里的試試
樓主好,
你文中提到的“wget http://soft.vpser.net/security/denyhosts/denyhosts_removeip.sh && bash denyhosts_removeip.sh 要解封的IP”解封ip的方法好像不行了,試過報錯了。
官方文檔的方法需要手動刪除多個文件中的ip記錄,http://denyhosts.sourceforge.net/faq.html#3_19。