場景:
週一上班centos服務器ssh不可用,web和數據庫等應用不響應。好在vnc能夠登陸
使用last命令查詢,2號以前的登陸信息已被清空,而且sshd文件在週六晚上被修改,週日晚上2點服務器被人遠程重啓
root pts/1 :1.0 Mon Jul 3 11:09 still logged in
root pts/1 :1.0 Mon Jul 3 11:08 - 11:09 (00:01)
root pts/0 :0.0 Mon Jul 3 10:54 still logged in
root tty1 :0 Mon Jul 3 10:53 still logged in
reboot system boot 2.6.32-696.3.2.e Mon Jul 3 10:46 - 11:11 (00:25)
root pts/0 :0.0 Mon Jul 3 10:42 - down (00:01)
root tty1 :0 Mon Jul 3 10:40 - down (00:03)
reboot system boot 2.6.32-696.3.2.e Sun Jul 2 02:31 - 10:44 (1+08:12)
reboot system boot 2.6.32-431.el6.x Sun Jul 2 02:27 - 02:27 (00:00)
Jul 2 03:11:20 oracledb rsyslogd: [origin software="rsyslogd" swVersion="5.8.10
" x-pid="1960" x-info="
http://www.rsyslog.com"] rsyslogd was HUPed
Jul 2 03:35:11 oracledb sshd[13864]: Did not receive identification string from
使用less /var/log/messages命令2點結合last命令,判斷2點重啓後IPATABLES生效,有大量的ssh暴力破解的掃描信息,因爲機器是測試環境,上面安裝了ORACLE和squid,臨時管理了iptables,重啓後iptables啓動,應該沒有再次被再次登陸,可是系統中部分文件以及被修改
message文件中部分信息以下:
103.207.37.86
Jul 2 03:35:12 oracledb sshd[13865]: error: Bad prime description in line 186
Jul 2 03:35:12 oracledb sshd[13865]: error: Bad prime description in line 187
Jul 2 03:35:12 oracledb sshd[13865]: error: Bad prime description in line 188
Jul 2 03:35:13 oracledb sshd[13865]: Failed password for illegal user support f
103.207.37.86 port 58311 ssh2
Jul 2 03:45:05 oracledb sshd[13887]: Illegal user support from
103.79.143.234
113.108.21.16
Jul 2 05:10:37 oracledb sshd[14126]: Illegal user support from
103.79.143.234
Jul 2 05:10:37 oracledb sshd[14126]: Failed password for illegal user support f
rom
103.79.143.234 port 57019 ssh2
Jul 2 05:10:43 oracledb sshd[14128]: Did not receive identification string from
解決方法
1.修改root用戶密碼
2.因爲sshd文件被修改,從新安裝ssh,並設置只有指定內網IP能夠訪問
3.配置iptables,使iptables
重裝SSHD
1.rpm -qa | grep ssh查詢已安裝包
系統已安裝包:
openssh-clients,openssh-server,openssh,openssh-askpass
刪除這四個包,刪除時centos提示包之間有依賴關係,按照提示從依賴關係的最裏層開始刪除,
按照openssh-askpass openssh openssh-server openssh-clients這個順序刪除就能夠了。
2.安裝
使用yum逐一安裝,yum install openssh-askpass **
安裝
openssh-server時提示:
unpacking of archive failed on file /user/sbin/sshd cpio:rename
刪除文件提示Operation not permitted錯誤
查詢文件的隱藏屬性
lsattr /usr/sbin/sshd
-u---ia--e /usr/sbin/sshd
i:設定文件不能被刪除、更名、設定連接關係,同時不能寫入或新增內容。i參數對於文件 系統的安全設置有很大幫助。
a 即append,設定該參數後,只能向文件中添加數據,而不能刪除,多用於服務器日誌文件安全,只有root才能設定這個屬性
使用 chattr -ia /usr/sbin/sshd修改文件的隱藏屬性,取消對應設置以後刪除成功
+ :在原有參數設定基礎上,追加參數。 - :在原有參數設定基礎上,移除參數
再次yum install openssh-server 成功
3.配置ssh登陸控制,設置管理IP,黑白名單
#修改端口號
Port 52111
#只容許SSH2方式的鏈接
Protocol 2
#允許root用戶登陸,由於後面會設置可登陸IP,因此這裏就允許了
PermitRootLogin yes
#不允許空密碼
PermitEmptyPasswords no
#屏蔽來自全部的SSH鏈接請求
vi /etc/hosts.deny
sshd: ALL
#容許來自內網指定ip的SSH鏈接請求
vi /etc/hosts.allow
sshd: 192.168.0
sshd: 192.168.253.**
配置對應iptables設置
1.iptables配置規則
iptables [-t表名] [-A|I|D|R 鏈名 ] [-i網卡名] [-p協議] [-s源IP] [-d目標ip] [--dport目標端口號] [-j動做]
這裏須要配置的是filter表,filter表中有input,output,forward三條規則鏈,若是本機服務比較多,規則比較繁瑣,比較便捷的方法是寫shell腳本以後重啓ssh服務
#限制SSH的鏈接IP
iptables -A INPUT -s 192.168.101.32 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -s 192.168.101.35 -p tcp --dport 22 -j ACCEPT
#SSH支持52111是修改後SSH端口
iptables -A OUTPUT -p tcp --sport 52111 -j ACCEPT
這裏只是針對SSH作了簡單配置,具體iptables的配置,詳見iptables配置一文
配置後/etc/rc.d/init.d/iptables save保存,使用service iptables restart重啓服務後配置生效。