SSH攻擊


托管在IDC的機器我們通常都用SSH方式來遠程管理.但是經常可以發現log-watch的日志中有大量試探登錄的
信息,為了我們的主機安全,有必要想個方法來阻擋這些可恨的"HACKER"
有很多辦法來阻擋這些密碼嘗試
1 修改端口
2 健壯的密碼
3 RSA公鑰認證
4 使用iptables腳本
5 使用sshd日志過濾
6 使用tcp_wrappers過濾
7 使用knockd
 
1 vi /etc/ssh/sshd_config
#Port 22
默認端口為22,為了避免被掃描,去掉#,改成一個其他的端口,比如45632
保存后重啟sshd服務.service sshd restart
 
2沒什么可說的,密碼的復雜性可以增加破解的難度,大小寫混合加上數字並且有足夠的密碼長度就比較安全
了,唯一的問題就是要牢記密碼.
 
3.默認的登錄方式是password,如果需要用RSA公鑰登錄,需要先創建RSA Key
#ssh-keygen -t rsa -b 1024
會生成私鑰/home/username/.ssh/id_rsa
同時生成公鑰/home/username/.ssh/id_rsa.pub
輸入一個加密短語(也可省略)
修改sshd 設置,編輯/etc/ssh/sshd_config
'PasswordAuthentication no'
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/id_rsa.pub
 
將公鑰下載到本地計算機,然后在ssh客戶端軟件Secure CRT中設置好,就可以登錄了
4 iptbles腳本
此腳本允許每分鍾3個連接.有白名單,並有日志紀錄
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set  --name SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j SSH_WHITELIST
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update  --seconds 60 --
hitcount 4 --rttl --name SSH -j ULOG --ulog-prefix SSH_brute_force
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update  --seconds 60 --
hitcount 4 --rttl --name SSH -j DROP
iptables的版本需要>1.2.11 (1.2不支持 --rttl參數)
 
5使用sshd日志過濾
有幾個軟件(腳本)可以做到.
sshfilter  [url]http://www.csc.liv.ac.uk/~greg/sshdfilter/[/url]
Fail2Ban   [url]http://fail2ban.sourceforge.net/[/url]
DenyHosts  [url]http://denyhosts.sourceforge.net/[/url]
需要sshd支持tcp_wrappers此功能.
[root@as4test include]# ldd /usr/sbin/sshd | grep libwrap
        libwrap.so.0 => /usr/lib64/libwrap.so.0 (0x0000002a9566c000)
如果有libwrap.so.0這個庫說明支持此功能
另一種測試方法:在/etc/hosts.deny加入一行127.0.0.1
然后ssh localhost,如果無法連接,說明支持.
$ tar zxvf DenyHosts-2.6.tar.gz      
$ cd DenyHosts-2.6
as root:
# python setup.py install
#cd /usr/share/denyhost
# cp denyhosts.cfg-dist denyhosts.cfg
配置文件
 
# cp daemon-control-dist daemon-control
啟動腳本
# chown root daemon-control
# chmod 700 daemon-control
# cd /etc/init.d
# ln -s /usr/share/denyhosts/daemon-control denyhosts
# chkconfig --add denyhosts
添加到開機啟動服務中
 
6 使用tcp_wrappers過濾
1 下載腳本 [url]http://www.la-samhna.de/misc/sshblock.sh[/url]
2 設置可執行權限 chmod 755 /usr/local/bin/sshblock.sh
3 在/etc/hosts.allow中添加以下內容
#__START_SSHBLOCK__
#__END_SSHBLOCK__
sshd : ALL : spawn (/usr/local/bin/sshblock.sh %a)&
設置
 
DONTBLOCK 白名單
BURST_MAX=5
BURST_TIM=60
60秒內登錄5次就封鎖
PURGE_TIM=3600
3600秒后解凍
 
7  使用KNOCKD
knockd 監視一個預定義模式在iptables的日志,例如一次擊中端口6356,一次擊中端口63356,兩次擊中端口
9356,這相當於敲一個關閉的門用一種特殊的暗碼來被konckd識別,konckd 將使用iptables來打開一個預定
義端口例如ssh的22端口在一個預定定義時間.(比如一分鍾),如果一個ssh session 在這個時間范圍內打開,
這個端口會一直保留.直到預定義時間過期后ssh端口被knockd關掉.
缺點:比較復雜的方案,不適合普通人
需要客戶端(knockd-client)來實現port knocking",同時需要knockd-server來響應.
實際上,很容易檢測到這種通訊模式,一旦攻擊者可以監控你的通訊,這種解決方案無法提供安全防護針對本
地攻擊者.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM