14、單機運行環境搭建之 --Centos6.4下使用Denyhosts禁止針對linux sshd的暴力破解

來源:互聯網
上載者:User

當你的linux伺服器暴露在互連網之中,該伺服器將會遭到互連網上的掃描軟體進行掃描,並試圖猜測SSH登入口令你會發現,每天會有多條SSH登入失敗紀錄。那些掃描工具將對你的伺服器構成威脅,你必須設定複雜登入口令,並將嘗試多次登入失敗的IP給阻止掉,讓其在一段時間內不能訪問該伺服器。簡單方法也可以修改22連接埠號碼.

     用DenyHosts可以阻止試圖猜測SSH登入口令,它會分析/var/log/secure等記錄檔,當發現同一IP在進行多次SSH密碼嘗試時就會記錄IP到/etc/hosts.deny檔案,從而達到自動屏蔽該IP的目的。last命令的資訊來自/var/log/wtmp,如果對有經驗的人可能會刪除掉這個。但是還是會留下痕迹,使用nmap命令掃描連接埠,nmap -PO -sS ip,使用lsof命令,停掉一些不必要的服務,多關注漏洞,配置強有力的iptables來保護自己的系統或者嘗試使用Chkrootkit應用程式對rootkit的跟蹤,嘗試AIDE來檢查檔案系統的完整性。伺服器上除了root外,Linux使用者越少越好,如果非要添加就添加shell為nologin。 DenyHosts安裝 1.環境檢查: 安裝環境Centos x64 6.4下載到/usr/src目錄中,查詢一下系統是否符合要求
ldd /usr/sbin/sshd|grep libwrap  //查看libwrap動態連結程式庫檔案。libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f4b2a1b9000)python -V              //查詢版本為2.6.5Python 2.6.6
2、安裝步驟:
cd /usr/src 

yum install wget -y

wget http://ncu.dl.sourceforge.net/sourceforge/denyhosts/DenyHosts-2.6.tar.gz

tar -xzvf
DenyHosts-2.6.tar.gz
cd DenyHosts-2.6

python setup.py install   //安裝Denyhost
3. #切換目錄進入/usr/share/denyhosts目錄
cd /usr/share/denyhosts/  

  

4. 拷貝一份denyhosts.cfg
cp denyhosts.cfg-dist denyhosts.cfg

 

5、DenyHosts參數配置

vi  denyhosts.cfg

切換到命令列模式
:1,$d

清空內容,然後輸入如下內容:

SECURE_LOG = /var/log/secure# format is: i[dhwmy]# Where i is an integer (eg. 7)# m = minutes# h = hours# d = days# w = weeks# y = years## never purge:PURGE_DENY=50mHOSTS_DENY=/etc/hosts.denyBLOCK_SERVICE=sshdDENY_THRESHOLD_INVALID=1DENY_THRESHOLD_VALID=10DENY_THRESHOLD_ROOT=5WORK_DIR=/usr/local/share/denyhosts/dataDENY_THRESHOLD_RESTRICTED =1LOCK_FILE=/var/lock/subsys/denyhostsHOSTNAME_LOOKUP=NOADMIN_EMAIL=10402852@qq.comDAEMON_LOG=/var/log/denyhostsDAEMON_PURGE=10m

解釋如下:

SECURE_LOG = /var/log/secure #ssh記錄檔   # format is: i[dhwmy]   # Where i is an integer (eg. 7)   # m = minutes   # h = hours   # d = days   # w = weeks   # y = years   #   # never purge:   PURGE_DENY = 50m #過多久後清除封鎖IP   HOSTS_DENY = /etc/hosts.deny #將阻止IP寫入到hosts.deny   BLOCK_SERVICE = sshd #阻止服務名   DENY_THRESHOLD_INVALID = 1 #允許無效使用者登入失敗的次數   DENY_THRESHOLD_VALID = 10 #允許普通使用者登入失敗的次數   DENY_THRESHOLD_ROOT = 5 #允許root登入失敗的次數   WORK_DIR = /usr/local/share/denyhosts/data #將deny的host或ip紀錄到Work_dir中   DENY_THRESHOLD_RESTRICTED = 1 #設定 deny host 寫入到該資料夾   LOCK_FILE = /var/lock/subsys/denyhosts #將DenyHOts啟動的pid紀錄到LOCK_FILE中,已確保服務正確啟動,防止同時啟動多個服務。   HOSTNAME_LOOKUP=NO #是否做網域名稱反解   ADMIN_EMAIL =10402852@qq.com #設定管理員郵件地址   DAEMON_LOG = /var/log/denyhosts #自己的記錄檔   DAEMON_PURGE = 10m #該項與PURGE_DENY 設定成一樣,也是清除hosts.deniedssh 使用者的時間。 

注意:不能把帶注釋的設定檔粘貼上去,會報錯!!!

 5.DenyHosts開機檔案配置
cp daemon-control-dist daemon-controlchown root daemon-controlchmod 700 daemon-control

./daemon-control start         #啟動DenyHosts

 如果要使DenyHosts每次重起後自動啟動還需做如下設定:

ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts    #建立符號連結
chkconfig --add denyhosts                      #增加denyhosts服務進程chkconfig  denyhosts on                      #設定開機啟動denyhostschkconfig --list denyhosts denyhosts       0:off   1:off   2:on    3:on    4:on    5:on    6:off 6.查看日誌異常資訊 tail -f /var/log/secure Feb 27 18:43:10 localhost login: pam_unix(login:auth): authentication failure; logname=LOGIN uid=0 euid=0 tty=tty1 ruser= rhost=  user=test
Feb 27 18:43:12 localhost login: FAILED LOGIN 1 FROM (null) FOR test, Authentication failure
Feb 27 18:43:15 localhost login: FAILED LOGIN 2 FROM (null) FOR test, Authentication failure
Feb 27 18:43:21 localhost login: FAILED LOGIN 3 FROM (null) FOR test, Authentication failure
Feb 27 18:43:26 localhost login: pam_unix(login:session): session opened for user test by LOGIN(uid=0)
Feb 27 18:43:26 localhost login: LOGIN ON tty1 BY test
Feb 27 18:45:32 localhost su: pam_unix(su:auth): authentication failure; logname=test uid=500 euid=0 tty=tty1 ruser=test rhost=  user=root
Feb 27 18:45:47 localhost su: pam_unix(su-l:session): session opened for user root by test(uid=500)
Feb 27 19:00:03 localhost sshd[19861]: Accepted password for root from 192.168.40.41 port 4289 ssh2

設定入侵偵查工具之PortSentry

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.