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










centos8 變動(dòng)比較大,默認(rèn)沒(méi)python,可以執(zhí)行:dnf install python2
alternatives --set python /usr/bin/python2
pip2 install ipaddr
后再進(jìn)行安裝
CentOS 8 安裝 Denyhosts 出現(xiàn)如下錯(cuò)誤,請(qǐng)問(wèn)是哪里出問(wèn)題?
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, 這個(gè)就是官網(wǎng)的方法用腳本來(lái)實(shí)現(xiàn)的,測(cè)試看是沒(méi)問(wèn)題,能正常移除ip。lnmp安裝包tools目錄下也有,你也可以直接用安裝包里的試試
樓主好,
你文中提到的“wget http://soft.vpser.net/security/denyhosts/denyhosts_removeip.sh && bash denyhosts_removeip.sh 要解封的IP”解封ip的方法好像不行了,試過(guò)報(bào)錯(cuò)了。
官方文檔的方法需要手動(dòng)刪除多個(gè)文件中的ip記錄,http://denyhosts.sourceforge.net/faq.html#3_19。