網絡安全攻防:Linux系統安全之OpenSSH安全配置
OpenSSH是安全Shell協議族(SSH)的一個免費版本。SSH協議族可以用來進行遠程控件,或在計算機之間傳送文件。而實現此功能的傳統方式,如Telnet(終端仿真協議)、RCP都是極不安全的,并且會使用明文傳送密碼。OpenSSH提供了服務端后臺程序和客戶端工具,用來加密遠程控件和文件傳輸過程的中的數據,并由此來代替原來的類似服務。
SSHD是一個典型的獨立守護進程(Standalone Daemon),但也可以根據需要通過網絡守護進程(Internet Daemon)-inetd或Ineternet Daemon's more modern-xinted加載。OpenSSH服務可以通過/etc/ssh/sshd_config文件進行配置。
1. 禁止Root用戶登錄
只允許普通用戶登錄,設置如下。
- # Authentication:
- LoginGraceTime 120
- PermitRootLogin no
- StrictModes yes
2. 限制SSH驗證重試次數
超過6次Socket連接會斷開,設置如下。
- MaxAuthTries 6
3. 禁止證書登錄
證書登錄非常安全,但是正常用戶很有可能在不知情的情況下,給系統安裝一個證書,他隨時都可能進入系統。任何一個有權限的用戶都能很方便地植入一個證書到.ssh/authorized_keys文件中,可以禁用證書登錄,設置如下。
- PubkeyAuthenticationno
4. 使用證書替代密碼認證
這個與上面講的正好相反,只允許使用key文件登錄,設置如下。
- PasswordAuthenticationno
5. 圖形窗口客戶端記憶密碼的問題
當使用XShell、Xftp、WinSCP、SecureCRT、SecureFX等軟件登錄時,該軟件都提供記住密碼的功能,使下次再登錄的時候不需要輸入密碼就可以進入系統。這樣做的確非常方便,但是電腦一旦丟失或被其他人進入,那么就十分危險了。設置如下。
- ChallengeResponseAuthentication yes
6. 禁止SSH端口映射
禁止使用SSH映射作為Socks5代理等,命令如下。
- AllowTcpForwarding no
7. IP地址限制
如果只希望特定IP地址的用戶登錄主機,如只允許192.168.1.1和192.168.1.2登錄,可以對/etc/host.allow進行如下修改。
- sshd:192.168.1.1 192.168.1.2
如果希望禁止所有人訪問主機,對/etc/hosts.deny修改,如下所示。
- sshd:ALL
8. 禁止SSH密碼窮舉
攻擊者通常會使用字典攻擊來窮舉目標主機的SSH密碼,可以通過編寫Shell腳本或使用Fail2ban工具對SSH連接進行訪問控制,這里介紹Fail2ban的使用。
Fail2ban 可以監視系統日志,然后匹配日志的錯誤信息(正則式匹配)執行相應的屏蔽動作(一般情況下是防火墻),而且可以發送E-mail通知系統管理員。
下面是Fail2ban的實戰部署演示。
(1)Fail2ban可以直接通過apt或yum獲得,如下。
- root@kali:~# apt-get install fail2ban
- Reading package lists...Done
- Building dependency tree
- Reading state information...Done
- Suggested packages:
- python-gamin
- The following NEW packages will be installed:
- fail2ban
- 0 upgraded,1 newly installed,0 to remove and 0 not upgraded.
- Need to get 165 kB of archives.
- After this operation,577 kB of additional disk space will be used.
- Get:1 http://mirrors.aliyun.com/kali/sana/main fail2ban all 0.8.13-1[165 kB]
- Fetched 165 kB in 2s(75.2 kB/s)
- Selecting previously unselected package fail2ban.
- (Reading database...322944 files and directories currently installed.)
- Preparing to unpack.../fail2ban_0.8.13-1_all.deb...
- Unpacking fail2ban(0.8.13-1)...
- Processing triggers for man-db(2.7.0.2-5)...
- Processing triggers for systemd(215-17+deb8u1)...
- Setting up fail2ban(0.8.13-1)...
- update-rc.d: We have no instructions for the fail2ban init script.
- update-rc.d: It looks like a network service,we disable it.
- insserv: warning: current start runlevel(s)(empty)of script `fail2ban' overrides LSB defaults(2 3 4 5).
- insserv: warning: current stop runlevel(s)(0 1 2 3 4 5 6)of script `fail2ban' overrides LSB defaults(0 1 6).
- Processing triggers for systemd(215-17+deb8u1)...
(2)復制一份配置文件,如下。
- root@ZYB-KALI-VM:/etc/fail2ban# cp jail.conf/etc/fail2ban/jail.lo cal
(3)修改幾個參數,ignoreip為忽略的登錄ip,bantime為屏蔽時長,findtime為監測時長,在findtime時間內出現maxretry次嘗試即執行屏蔽動作,單位為s,maxretry為最大嘗試次數,設置如下。
- ignoreip=127.0.0.1/8
- bantime=600
- findtime=600
- maxretry=5
(4)默認SSH監控是開啟狀態,這里將SSH登錄訪問的日志文件寫到logpath參數中,之后保存配置文件,就可以啟動Fail2ban了,如下。
- #SSH servers
- #
- [sshd]
- port =ssh
- logpath=/var/log/auth.log
- backend=%(sshd_backend)s
(5)Fail2ban服務開啟,如下。
- root@ZYB-KALI-VM:/etc/fail2ban#service fail2ban status
- fail2ban.service-LSB:Start/stop fail2ban
- Loaded:loaded(/etc/init.d/fail2ban)
- Active:active(running)since Sun 2017-02-26 18:58:46 HKT;8s ago
- Process:7536 ExecStart=/etc/init.d/fail2ban start(code=exited,status=0/SUCCESS)
- CGroup:/system.slice/fail2ban.service
- └─7547/usr/bin/python/usr/bin/fail2ban-server-b-s/var/run/fail2ban/fail2ban.sock-p/var/run/fail2ban/fail2ban.pid
- Feb 26 18:58:46 ZYB-KALI-VM fail2ban[7536]:Starting authentication failure monitor:fail2ban.
Fail2ban可以支持郵件報警功能,需要事先配置好mail或sendmail郵件通知才能正常工作,可以編輯jail.lo cal文件。