方法有很多種,這里介紹兩種。
(1).配置安全的shhd設置
不允許root用戶直接登錄到系統,添加一個普通用戶,必要時再切換到root用戶。
修改默認端口號。
不允許密碼登錄,只能通過密鑰登錄系統。
root用戶的密碼足夠復雜,足夠長,可以包含大小寫字母、數字和特殊符號。
sshd配置信息詳見:簡單了解sshd_config配置文件。
sshd密鑰登錄詳見:CentOS7做ssh免密登錄。
(2).使用fail2ban來防護
1)確認python的版本必須大於2.4
[root@youxi1 ~]# python -V Python 2.7.5
2)要有epel擴展源
[root@youxi1 ~]# yum list installed | grep epel-release epel-release.noarch 7-11 @extras
3)yum安裝fail2ban
[root@youxi1 ~]# yum -y install fail2ban
4)修改配置文件
說明:fail2ban的配置文件保存在/etc/fail2ban/目錄下,其中主要的有action.d文件夾、fail2ban.conf文件與file2ban.d文件夾、filter.d文件夾、jail.conf和jail.d文件夾。action.d文件夾是動作文件夾,內含iptables和mail等動作配置。fail2ban.conf文件和fail2ban.d文件夾定義了日志級別、日志位置和sock文件位置。filter.d文件夾是過濾條件文件夾。jail.conf文件和jail.d文件夾是主要配置文件以及模塊化。
[root@youxi1 ~]# ls /etc/fail2ban/ action.d filter.d paths-common.conf paths-freebsd.conf fail2ban.conf jail.conf paths-debian.conf paths-opensuse.conf fail2ban.d jail.d paths-fedora.conf paths-osx.conf
主要修改的就是/etc/fail2ban/jail.conf文件。這個配置文件中有許多模塊,針對sshd服務需要修改的是[sshd]模塊。初始的[sshd]模塊只有三行:
port = ssh logpath = %(sshd_log)s backend = %(sshd_backend)s
在[sshd]模塊中添加如下行:
//激活sshd模塊 enabled = true //過濾條件,sshd是過濾條件,對應filter.d文件夾下的sshd.conf文件 filter = sshd //動作,iptables是動作,對應action.d文件夾下的iptables.conf文件,並傳遞三個參數。如果ssh端口不是22,那么prot的值等於實際的端口號 action = iptables[name=SSH,prot=ssh,protocol=tcp] //sendmail-whois是第二個動作,對應action.d文件夾下的sendmail-whois.conf文件,並傳遞四個參數 //dest是接收郵箱,sender是發送郵箱,sendername是郵件標題 sendmail-whois[name=SSH,dest=you@example.com,sender=fail2ban@example.com,sendername="Fail2Ban"] //日志文件地址 logpath = /var/log/secure //禁止時間1小時 bantime = 3600 //指定規定時間為3分鍾 findtime = 180 //規定時間內最大嘗試次數 maxretry = 3
5)啟動fail2ban並設置開機自啟
[root@youxi1 ~]# systemctl start fail2ban [root@youxi1 ~]# systemctl enable fail2ban Created symlink from /etc/systemd/system/multi-user.target.wants/fail2ban.service to /usr/lib/systemd/system/fail2ban.service. [root@youxi1 ~]# systemctl status fail2ban ● fail2ban.service - Fail2Ban Service Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; vendor preset: disabled) Active: active (running) since 二 2019-05-14 15:59:01 CST; 28s ago Docs: man:fail2ban(1) Main PID: 12128 (fail2ban-server) CGroup: /system.slice/fail2ban.service └─12128 /usr/bin/python2 -s /usr/bin/fail2ban-server -s /var/run/f... 5月 14 15:59:01 youxi1 systemd[1]: Starting Fail2Ban Service... 5月 14 15:59:01 youxi1 fail2ban-client[12125]: 2019-05-14 15:59:01,718 fail...7 5月 14 15:59:01 youxi1 fail2ban-client[12125]: 2019-05-14 15:59:01,719 fail...e 5月 14 15:59:01 youxi1 systemd[1]: Started Fail2Ban Service. Hint: Some lines were ellipsized, use -l to show in full.
6)測試
連續輸錯密碼3次
[root@youxi2 ~]# ssh 192.168.5.101 The authenticity of host '192.168.5.101 (192.168.5.101)' can't be established. ECDSA key fingerprint is SHA256:l8MUM3o4X+Apo/ULW0VOVDhD0ylTANVQEIULquuH0AY. ECDSA key fingerprint is MD5:d2:4f:03:4d:34:67:2c:93:a6:29:ae:f3:cd:23:cf:de. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.5.101' (ECDSA) to the list of known hosts. root@192.168.5.101's password: Permission denied, please try again. root@192.168.5.101's password: Permission denied, please try again. root@192.168.5.101's password: ^C [root@youxi2 ~]# ssh 192.168.5.101 ssh: connect to host 192.168.5.101 port 22: Connection refused
另外在本地也可以使用iptables -nL查看被禁止的IP地址
Chain f2b-SSH (1 references) target prot opt source destination REJECT all -- 192.168.5.102 0.0.0.0/0 reject-with icmp-port-unreachable RETURN all -- 0.0.0.0/0 0.0.0.0/0
也可以使用fail2ban-client status查看。
7)將被禁止的IP地址從黑名單中移除
有可能手誤被加到黑名單中了,但總不能等一小時吧。所以可以使用一下命令移除黑名單
[root@youxi1 fail2ban]# fail2ban-client set sshd unbanip 192.168.5.102 //sshd對應的jail.conf中的模塊名 192.168.5.102
fail2ban-client詳細用法請看幫助說明
8)注意
iptables重啟,那么fail2ban也需要重啟。
修改端口,fail2ban的jail.conf中[sshd]模塊,iptables動作傳遞的port參數也需要修改,最后重啟。