自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

20個OpenSSH安全實踐

系統(tǒng) Linux
OpenSSH 是 SSH 協(xié)議的一個實現(xiàn)。一般通過 scp 或 sftp 用于遠程登錄、備份、遠程文件傳輸?shù)裙δ?。SSH能夠完美保障兩個網(wǎng)絡或系統(tǒng)間數(shù)據(jù)傳輸?shù)谋C苄院屯暾浴?/div>

[[221025]]

OpenSSH 是 SSH 協(xié)議的一個實現(xiàn)。一般通過 scpsftp 用于遠程登錄、備份、遠程文件傳輸?shù)裙δ?。SSH能夠完美保障兩個網(wǎng)絡或系統(tǒng)間數(shù)據(jù)傳輸?shù)谋C苄院屯暾?。盡管如此,它最大的優(yōu)勢是使用公匙加密來進行服務器驗證。時不時會出現(xiàn)關于 OpenSSH 零日漏洞的傳言。本文將描述如何設置你的 Linux 或類 Unix 系統(tǒng)以提高 sshd 的安全性。

 

OpenSSH 默認設置

  • TCP 端口 - 22
  • OpenSSH 服務配置文件 - sshd_config (位于 /etc/ssh/

 

1、 基于公匙的登錄

OpenSSH 服務支持各種驗證方式。推薦使用公匙加密驗證。首先,使用以下 ssh-keygen 命令在本地電腦上創(chuàng)建密匙對:

1024 位或低于它的 DSA 和 RSA 加密是很弱的,請不要使用。當考慮 ssh 客戶端向后兼容性的時候,請使用 RSA密匙代替 ECDSA 密匙。所有的 ssh 密鑰要么使用 ED25519 ,要么使用 RSA,不要使用其它類型。

  1. $ ssh-keygen -t key_type -b bits -C "comment"

示例:

  1. $ ssh-keygen -t ed25519 -C "Login to production cluster at xyz corp"
  2. $ ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_aws_$(date +%Y-%m-%d) -C "AWS key for abc corp clients"

下一步,使用 ssh-copy-id 命令安裝公匙:

  1. $ ssh-copy-id -i /path/to/public-key-file user@host
  2. $ ssh-copy-id user@remote-server-ip-or-dns-name

示例:

  1. $ ssh-copy-id vivek@rhel7-aws-server

提示輸入用戶名和密碼的時候,確認基于 ssh 公匙的登錄是否工作:

  1. $ ssh vivek@rhel7-aws-server

OpenSSH 服務安全最佳實踐

OpenSSH 服務安全最佳實踐

更多有關 ssh 公匙的信息,參照以下文章:

 

2、 禁用 root 用戶登錄

禁用 root 用戶登錄前,確認普通用戶可以以 root 身份登錄。例如,允許用戶 vivek 使用 sudo 命令以 root 身份登錄。

 

在 Debian/Ubuntu 系統(tǒng)中如何將用戶 vivek 添加到 sudo 組中

允許 sudo 組中的用戶執(zhí)行任何命令。 將用戶 vivek 添加到 sudo 組中

  1. $ sudo adduser vivek sudo

使用 id 命令 驗證用戶組。

  1. $ id vivek

 

在 CentOS/RHEL 系統(tǒng)中如何將用戶 vivek 添加到 sudo 組中

在 CentOS/RHEL 和 Fedora 系統(tǒng)中允許 wheel 組中的用戶執(zhí)行所有的命令。使用 usermod 命令將用戶 vivek 添加到 wheel 組中:

  1. $ sudo usermod -aG wheel vivek
  2. $ id vivek

 

測試 sudo 權限并禁用 ssh root 登錄

測試并確保用戶 vivek 可以以 root 身份登錄執(zhí)行以下命令:

  1. $ sudo -i
  2. $ sudo /etc/init.d/sshd status
  3. $ sudo systemctl status httpd

添加以下內(nèi)容到 sshd_config 文件中來禁用 root 登錄:

  1. PermitRootLogin no
  2. ChallengeResponseAuthentication no
  3. PasswordAuthentication no
  4. UsePAM no

更多信息參見“如何通過禁用 Linux 的 ssh 密碼登錄來增強系統(tǒng)安全” 。

 

3、 禁用密碼登錄

所有的密碼登錄都應該禁用,僅留下公匙登錄。添加以下內(nèi)容到 sshd_config 文件中:

  1. AuthenticationMethods publickey
  2. PubkeyAuthentication yes

CentOS 6.x/RHEL 6.x 系統(tǒng)中老版本的 sshd 用戶可以使用以下設置:

  1. PubkeyAuthentication yes

 

4、 限制用戶的 ssh 訪問

默認狀態(tài)下,所有的系統(tǒng)用戶都可以使用密碼或公匙登錄。但是有些時候需要為 FTP 或者 email 服務創(chuàng)建 UNIX/Linux 用戶。然而,這些用戶也可以使用 ssh 登錄系統(tǒng)。他們將獲得訪問系統(tǒng)工具的完整權限,包括編譯器和諸如 Perl、Python(可以打開網(wǎng)絡端口干很多瘋狂的事情)等的腳本語言。通過添加以下內(nèi)容到 sshd_config 文件中來僅允許用戶 root、vivek 和 jerry 通過 SSH 登錄系統(tǒng):

  1. AllowUsers vivek jerry

當然,你也可以添加以下內(nèi)容到 sshd_config 文件中來達到僅拒絕一部分用戶通過 SSH 登錄系統(tǒng)的效果。

  1. DenyUsers root saroj anjali foo

你也可以通過配置 Linux PAM 來禁用或允許用戶通過 sshd 登錄。也可以允許或禁止一個用戶組列表通過 ssh 登錄系統(tǒng)。

 

5、 禁用空密碼

你需要明確禁止空密碼賬戶遠程登錄系統(tǒng),更新 sshd_config 文件的以下內(nèi)容:

  1. PermitEmptyPasswords no

 

6、 為 ssh 用戶或者密匙使用強密碼

為密匙使用強密碼和短語的重要性再怎么強調都不過分。暴力破解可以起作用就是因為用戶使用了基于字典的密碼。你可以強制用戶避開字典密碼并使用約翰的開膛手工具來檢測弱密碼。以下是一個隨機密碼生成器(放到你的 ~/.bashrc 下):

  1. genpasswd() {
  2. local l=$1
  3. [ "$l" == "" ] && l=20
  4. tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
  5. }

運行:

  1. genpasswd 16

輸出:

  1. uw8CnDVMwC6vOKgW

 

7、 為 SSH 的 22端口配置防火墻

你需要更新 iptables/ufw/firewall-cmd 或 pf 防火墻配置來為 ssh 的 TCP 端口 22 配置防火墻。一般來說,OpenSSH 服務應該僅允許本地或者其他的遠端地址訪問。

 

Netfilter(Iptables) 配置

更新 /etc/sysconfig/iptables (Redhat 和其派生系統(tǒng)特有文件) 實現(xiàn)僅接受來自于 192.168.1.0/24 和 202.54.1.5/29 的連接,輸入:

  1. -A RH-Firewall-1-INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT
  2. -A RH-Firewall-1-INPUT -s 202.54.1.5/29 -m state --state NEW -p tcp --dport 22 -j ACCEPT

如果同時使用 IPv6 的話,可以編輯 /etc/sysconfig/ip6tables (Redhat 和其派生系統(tǒng)特有文件),輸入:

  1. -A RH-Firewall-1-INPUT -s ipv6network::/ipv6mask -m tcp -p tcp --dport 22 -j ACCEPT

ipv6network::/ipv6mask 替換為實際的 IPv6 網(wǎng)段。

 

Debian/Ubuntu Linux 下的 UFW

UFW 是 Uncomplicated FireWall 的首字母縮寫,主要用來管理 Linux 防火墻,目的是提供一種用戶友好的界面。輸入以下命令使得系統(tǒng)僅允許網(wǎng)段 202.54.1.5/29 接入端口 22

  1. $ sudo ufw allow from 202.54.1.5/29 to any port 22

更多信息請參見 “Linux:菜鳥管理員的 25 個 Iptables Netfilter 命令”。

 

*BSD PF 防火墻配置

如果使用 PF 防火墻 /etc/pf.conf 配置如下:

  1. pass in on $ext_if inet proto tcp from {192.168.1.0/24, 202.54.1.5/29} to $ssh_server_ip port ssh flags S/SA synproxy state

 

8、 修改 SSH 端口和綁定 IP

ssh 默認監(jiān)聽系統(tǒng)中所有可用的網(wǎng)卡。修改并綁定 ssh 端口有助于避免暴力腳本的連接(許多暴力腳本只嘗試端口 22)。更新文件 sshd_config 的以下內(nèi)容來綁定端口 300 到 IP 192.168.1.5 和 202.54.1.5:

  1. Port 300
  2. ListenAddress 192.168.1.5
  3. ListenAddress 202.54.1.5

當需要接受動態(tài)廣域網(wǎng)地址的連接時,使用主動腳本是個不錯的選擇,比如 fail2ban 或 denyhosts。

 

9、 使用 TCP wrappers (可選的)

TCP wrapper 是一個基于主機的訪問控制系統(tǒng),用來過濾來自互聯(lián)網(wǎng)的網(wǎng)絡訪問。OpenSSH 支持 TCP wrappers。只需要更新文件 /etc/hosts.allow 中的以下內(nèi)容就可以使得 SSH 只接受來自于 192.168.1.2 和 172.16.23.12 的連接:

  1. sshd : 192.168.1.2 172.16.23.12

在 Linux/Mac OS X 和類 UNIX 系統(tǒng)中參見 TCP wrappers 設置和使用的常見問題。

 

10、 阻止 SSH 破解或暴力攻擊

暴力破解是一種在單一或者分布式網(wǎng)絡中使用大量(用戶名和密碼的)組合來嘗試連接一個加密系統(tǒng)的方法。可以使用以下軟件來應對暴力攻擊:

  • DenyHosts 是一個基于 Python SSH 安全工具。該工具通過監(jiān)控授權日志中的非法登錄日志并封禁原始 IP 的方式來應對暴力攻擊。
    • RHEL / Fedora 和 CentOS Linux 下如何設置 DenyHosts。
  • Fail2ban 是另一個類似的用來預防針對 SSH 攻擊的工具。
  • sshguard 是一個使用 pf 來預防針對 SSH 和其他服務攻擊的工具。
  • security/sshblock 阻止濫用 SSH 嘗試登錄。
  • IPQ BDB filter 可以看做是 fail2ban 的一個簡化版。

 

11、 限制 TCP 端口 22 的傳入速率(可選的)

netfilter 和 pf 都提供速率限制選項可以對端口 22 的傳入速率進行簡單的限制。

 

Iptables 示例

以下腳本將會阻止 60 秒內(nèi)嘗試登錄 5 次以上的客戶端的連入。

  1. #!/bin/bash
  2. inet_if=eth1
  3. ssh_port=22
  4. $IPT -I INPUT -p tcp --dport ${ssh_port} -i ${inet_if} -m state --state NEW -m recent --set
  5. $IPT -I INPUT -p tcp --dport ${ssh_port} -i ${inet_if} -m state --state NEW -m recent --update --seconds 60 --hitcount 5

在你的 iptables 腳本中調用以上腳本。其他配置選項:

  1. $IPT -A INPUT -i ${inet_if} -p tcp --dport ${ssh_port} -m state --state NEW -m limit --limit 3/min --limit-burst 3 -j ACCEPT
  2. $IPT -A INPUT -i ${inet_if} -p tcp --dport ${ssh_port} -m state --state ESTABLISHED -j ACCEPT
  3. $IPT -A OUTPUT -o ${inet_if} -p tcp --sport ${ssh_port} -m state --state ESTABLISHED -j ACCEPT
  4. # another one line example
  5. # $IPT -A INPUT -i ${inet_if} -m state --state NEW,ESTABLISHED,RELATED -p tcp --dport 22 -m limit --limit 5/minute --limit-burst 5-j ACCEPT

其他細節(jié)參見 iptables 用戶手冊。

 

*BSD PF 示例

以下腳本將限制每個客戶端的連入數(shù)量為 20,并且 5 秒內(nèi)的連接不超過 15 個。如果客戶端觸發(fā)此規(guī)則,則將其加入 abusive_ips 表并限制該客戶端連入。最后 flush 關鍵詞殺死所有觸發(fā)規(guī)則的客戶端的連接。

  1. sshd_server_ip = "202.54.1.5"
  2. table <abusive_ips> persist
  3. block in quick from <abusive_ips>
  4. pass in on $ext_if proto tcp to $sshd_server_ip port ssh flags S/SA keep state (max-src-conn 20, max-src-conn-rate 15/5, overload <abusive_ips> flush)

 

12、 使用端口敲門(可選的)

端口敲門是通過在一組預先指定的封閉端口上生成連接嘗試,以便從外部打開防火墻上的端口的方法。一旦指定的端口連接順序被觸發(fā),防火墻規(guī)則就被動態(tài)修改以允許發(fā)送連接的主機連入指定的端口。以下是一個使用 iptables 實現(xiàn)的端口敲門的示例:

  1. $IPT -N stage1
  2. $IPT -A stage1 -m recent --remove --name knock
  3. $IPT -A stage1 -p tcp --dport 3456 -m recent --set --name knock2
  4.  
  5. $IPT -N stage2
  6. $IPT -A stage2 -m recent --remove --name knock2
  7. $IPT -A stage2 -p tcp --dport 2345 -m recent --set --name heaven
  8.  
  9. $IPT -N door
  10. $IPT -A door -m recent --rcheck --seconds 5 --name knock2 -j stage2
  11. $IPT -A door -m recent --rcheck --seconds 5 --name knock -j stage1
  12. $IPT -A door -p tcp --dport 1234 -m recent --set --name knock
  13.  
  14. $IPT -A INPUT -m --state ESTABLISHED,RELATED -j ACCEPT
  15. $IPT -A INPUT -p tcp --dport 22 -m recent --rcheck --seconds 5 --name heaven -j ACCEPT
  16. $IPT -A INPUT -p tcp --syn -j door

更多信息請參見:

Debian / Ubuntu: 使用 Knockd and Iptables 設置端口敲門

 

13、 配置空閑超時注銷時長

用戶可以通過 ssh 連入服務器,可以配置一個超時時間間隔來避免無人值守的 ssh 會話。 打開 sshd_config 并確保配置以下值:

  1. ClientAliveInterval 300
  2. ClientAliveCountMax 0

以秒為單位設置一個空閑超時時間(300秒 = 5分鐘)。一旦空閑時間超過這個值,空閑用戶就會被踢出會話。更多細節(jié)參見如何自動注銷空閑超時的 BASH / TCSH / SSH 用戶。

 

14、 為 ssh 用戶啟用警示標語

更新 sshd_config 文件如下行來設置用戶的警示標語:

  1. Banner /etc/issue

`/etc/issue 示例文件:

  1. ----------------------------------------------------------------------------------------------
  2. You are accessing a XYZ Government (XYZG) Information System (IS) that is provided for authorized use only.
  3. By using this IS (which includes any device attached to this IS), you consent to the following conditions:
  4.  
  5. + The XYZG routinely intercepts and monitors communications on this IS for purposes including, but not limited to,
  6. penetration testing, COMSEC monitoring, network operations and defense, personnel misconduct (PM),
  7. law enforcement (LE), and counterintelligence (CI) investigations.
  8.  
  9. + At any time, the XYZG may inspect and seize data stored on this IS.
  10.  
  11. + Communications using, or data stored on, this IS are not private, are subject to routine monitoring,
  12. interception, and search, and may be disclosed or used for any XYZG authorized purpose.
  13.  
  14. + This IS includes security measures (e.g., authentication and access controls) to protect XYZG interests--not
  15. for your personal benefit or privacy.
  16.  
  17. + Notwithstanding the above, using this IS does not constitute consent to PM, LE or CI investigative searching
  18. or monitoring of the content of privileged communications, or work product, related to personal representation
  19. or services by attorneys, psychotherapists, or clergy, and their assistants. Such communications and work
  20. product are private and confidential. See User Agreement for details.
  21. ----------------------------------------------------------------------------------------------

以上是一個標準的示例,更多的用戶協(xié)議和法律細節(jié)請咨詢你的律師團隊。

 

15、 禁用 .rhosts 文件(需核實)

禁止讀取用戶的 ~/.rhosts~/.shosts 文件。更新 sshd_config 文件中的以下內(nèi)容:

  1. IgnoreRhosts yes

SSH 可以模擬過時的 rsh 命令,所以應該禁用不安全的 RSH 連接。

 

16、 禁用基于主機的授權(需核實)

禁用基于主機的授權,更新 sshd_config 文件的以下選項:

  1. HostbasedAuthentication no

 

17、 為 OpenSSH 和操作系統(tǒng)打補丁

推薦你使用類似 yum、apt-getfreebsd-update 等工具保持系統(tǒng)安裝了最新的安全補丁。

 

18、 Chroot OpenSSH (將用戶鎖定在主目錄)

默認設置下用戶可以瀏覽諸如 /etc、/bin 等目錄??梢允褂?chroot 或者其他專有工具如 rssh 來保護 ssh 連接。從版本 4.8p1 或 4.9p1 起,OpenSSH 不再需要依賴諸如 rssh 或復雜的 chroot(1) 等第三方工具來將用戶鎖定在主目錄中??梢允褂眯碌?ChrootDirectory 指令將用戶鎖定在其主目錄,參見這篇博文。

 

19. 禁用客戶端的 OpenSSH 服務

工作站和筆記本不需要 OpenSSH 服務。如果不需要提供 ssh 遠程登錄和文件傳輸功能的話,可以禁用 sshd 服務。CentOS / RHEL 用戶可以使用 yum 命令 禁用或刪除 openssh-server:

  1. $ sudo yum erase openssh-server

Debian / Ubuntu 用戶可以使用 apt 命令/apt-get 命令 刪除 openssh-server:

  1. $ sudo apt-get remove openssh-server

有可能需要更新 iptables 腳本來移除 ssh 的例外規(guī)則。CentOS / RHEL / Fedora 系統(tǒng)可以編輯文件 /etc/sysconfig/iptables/etc/sysconfig/ip6tables。最后重啟 iptables 服務:

  1. # service iptables restart
  2. # service ip6tables restart

 

20. 來自 Mozilla 的額外提示

如果使用 6.7+ 版本的 OpenSSH,可以嘗試下以下設置

  1. #################[ WARNING ]########################
  2. # Do not use any setting blindly. Read sshd_config #
  3. # man page. You must understand cryptography to #
  4. # tweak following settings. Otherwise use defaults #
  5. ####################################################
  6.  
  7. # Supported HostKey algorithms by order of preference.
  8. HostKey /etc/ssh/ssh_host_ed25519_key
  9. HostKey /etc/ssh/ssh_host_rsa_key
  10. HostKey /etc/ssh/ssh_host_ecdsa_key
  11.  
  12. # Specifies the available KEX (Key Exchange) algorithms.
  13. KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256
  14.  
  15. # Specifies the ciphers allowed
  16. Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
  17.  
  18. #Specifies the available MAC (message authentication code) algorithms
  19. MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-128@openssh.com
  20.  
  21. # LogLevel VERBOSE logs user's key fingerprint on login. Needed to have a clear audit track of which key was using to log in.
  22. LogLevel VERBOSE
  23.  
  24. # Log sftp level file access (read/write/etc.) that would not be easily logged otherwise.
  25. Subsystem sftp /usr/lib/ssh/sftp-server -f AUTHPRIV -l INFO

使用以下命令獲取 OpenSSH 支持的加密方法:

  1. $ ssh -Q cipher
  2. $ ssh -Q cipher-auth
  3. $ ssh -Q mac
  4. $ ssh -Q kex
  5. $ ssh -Q key

OpenSSH安全教程查詢密碼和算法選擇

OpenSSH安全教程查詢密碼和算法選擇

 

如何測試 sshd_config 文件并重啟/重新加載 SSH 服務?

在重啟 sshd 前檢查配置文件的有效性和密匙的完整性,運行:

  1. $ sudo sshd -t

擴展測試模式:

  1. $ sudo sshd -T

最后,根據(jù)系統(tǒng)的的版本重啟 Linux 或類 Unix 系統(tǒng)中的 sshd 服務

  1. $ [sudo systemctl start ssh][38] ## Debian/Ubunt Linux##
  2. $ [sudo systemctl restart sshd.service][39] ## CentOS/RHEL/Fedora Linux##
  3. $ doas /etc/rc.d/sshd restart ## OpenBSD##
  4. $ sudo service sshd restart ## FreeBSD##

 

其他建議

  1. 使用 2FA 加強 SSH 的安全性 - 可以使用 OATH ToolkitDuoSecurity 啟用多重身份驗證。
  2. 基于密匙鏈的身份驗證 - 密匙鏈是一個 bash 腳本,可以使得基于密匙的驗證非常的靈活方便。相對于無密碼密匙,它提供更好的安全性。

 

更多信息

  • OpenSSH 官方 項目。
  • 用戶手冊: sshd(8)、ssh(1)、ssh-add(1)、ssh-agent(1)。

如果知道這里沒用提及的方便的軟件或者技術,請在下面的評論中分享,以幫助讀者保持 OpenSSH 的安全。 

責任編輯:龐桂玉 來源: Linux中國
相關推薦

2010-04-13 18:42:55

2010-03-12 10:48:03

2021-02-16 08:45:10

React前端代碼

2012-02-07 09:17:13

2023-03-16 08:01:56

TypeScript開源編程語言

2019-10-28 09:53:42

Java開發(fā)結構

2018-09-01 05:39:48

2022-10-10 14:53:00

云安全云計算云平臺

2021-06-25 17:39:35

網(wǎng)絡對抗與實踐經(jīng)驗

2015-05-21 08:45:18

2021-02-23 09:12:46

網(wǎng)絡安全系統(tǒng)安全OpenSS

2014-06-24 10:41:46

2024-03-11 15:17:53

2019-02-14 13:21:31

2017-10-25 12:59:04

2022-10-08 07:39:40

虛擬主機安全TLS

2017-03-17 11:00:42

云安全實踐云計算

2023-02-24 14:28:56

2024-01-29 00:20:00

GolangGo代碼

2023-03-15 15:54:36

Java代碼
點贊
收藏

51CTO技術棧公眾號