1. 程式人生 > >Top20的OpenSSH伺服器最佳安全實踐--SSHD_CONFIG配置檔案詳細解讀

Top20的OpenSSH伺服器最佳安全實踐--SSHD_CONFIG配置檔案詳細解讀

 

OpenSSH是SSH協議的實現。建議使用OpenSSH進行遠端登入,備份,通過scp或sftp進行遠端檔案傳輸等等。SSH非常適合保持兩個網路和系統之間交換的資料的機密性和完整性。但是,主要優點是伺服器身份驗證,通過使用公鑰加密。有時會有關於OpenSSH零日攻擊的傳聞。此頁面顯示如何保護在Linux或類Unix系統上執行的OpenSSH伺服器,以提高sshd安全性

 

OpenSSH預設值

  • TCP埠 - 22
  • OpenSSH伺服器配置檔案 - sshd_config(位於/ etc / ssh /中)

 

1.使用基於SSH公鑰的登入

OpenSSH伺服器支援各種身份驗證。建議您使用基於公鑰的身份驗證。首先,在本地桌面/膝上型電腦上使用以下ssh-keygen命令建立金鑰對:

DSA和RSA 1024位或更低的ssh金鑰被認為是弱的。避免他們。當向後相容性與ssh客戶端有關時,RSA金鑰是通過ECDSA金鑰選擇的。所有ssh金鑰都是ED25519或RSA。不要使用任何其他型別。

$ ssh-keygen -t key_type -b bits -C "comment"
$ ssh-keygen -t ed25519 -C "Login to production cluster at xyz corp"
$ 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命令安裝公鑰:當提升提供使用者密碼時。驗證基於ssh金鑰的登入是否適合您:有關ssh公鑰身份驗證的詳細資訊,請參閱:


$ ssh-copy-id -i /path/to/public-key-file [email protected]
$ ssh-copy-id [email protected]
$ ssh-copy-id [email protected]

 


$ ssh [email protected]


OpenSSH伺服器安全最佳實踐

 

2.禁用root使用者登入

在禁用root使用者登入之前,請確保普通使用者可以root身份登入。例如,允許vivek使用者使用sudo命令以root使用者身份登入。

 

如何在Debian / Ubuntu上將vivek使用者新增到sudo組

允許組sudo的成員執行任何命令。將使用者vivek新增到sudo組


$ sudo adduser vivek sudo


使用id命令驗證組成員身份


$ id vivek

 

如何在CentOS / RHEL伺服器上將vivek使用者新增到sudo組

允許組輪中的人員在CentOS / RHEL和Fedora Linux伺服器上執行所有命令。使用usermod命令將名為vivek的使用者新增到wheel組:


$ sudo usermod -aG wheel vivek
$ id vivek

 

測試sudo訪問許可權並禁用ssh的root登入

 

測試它並確保使用者vivek可以root身份登入或以root身份執行命令:確認後通過將以下行新增到sshd_config來禁用root登入:有關詳細資訊,請參閱“ 如何禁用Linux上的ssh密碼登入以提高安全性 ”。


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


PermitRootLogin no
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

 

3.禁用基於密碼的登入

 

必須禁用所有基於密碼的登入。僅允許基於公鑰的登入。在sshd_config檔案中新增以下內容:CentOS 6.x / RHEL 6.x使用者上的舊版SSHD應使用以下設定:


AuthenticationMethods publickey
PubkeyAuthentication yes

PubkeyAuthentication yes

 

4.限制使用者的ssh訪問許可權

 

預設情況下,所有系統使用者都可以使用其密碼或公鑰通過SSH登入。有時您會為FTP或電子郵件目的建立UNIX / Linux使用者帳戶。但是,這些使用者可以使用ssh登入系統。他們可以完全訪問系統工具,包括編譯器和指令碼語言,如Perl,Python,可以開啟網路埠,並做許多其他奇特的事情。只允許root,vivek和jerry使用者通過SSH使用系統,將以下內容新增到sshd_config:


AllowUsers vivek jerry


或者,您可以允許所有使用者通過SSH登入但只拒絕少數使用者,sshd_config中包含以下行:


DenyUsers root saroj anjali foo


您還可以配置Linux PAM允許或拒絕通過sshd伺服器登入。您可以允許組名列表訪問或拒絕訪問ssh。

 

5.禁用空密碼

 

您需要明確禁止從密碼為空的帳戶進行遠端登入,使用以下行更新sshd_config:


PermitEmptyPasswords no

 

6.對ssh使用者/金鑰使用強密碼和密碼

對於您的金鑰使用強大的使用者密碼和密碼來說,這一點非常重要。暴力攻擊有效,因為使用者使用基於字典的密碼。您可以強制使用者避免使用字典攻擊密碼,並使用john the ripper工具查詢現有的弱密碼。這是一個隨機密碼生成器示例(放在〜/ .bashrc中):

genpasswd () { 
	local  l = $ 1 
       	[  “ $ l ” == “”  ]  &&  l = 20 
      	tr  -dc A-Za-z0- 9 _ <  / dev / urandom |  head  -c  $ {l}  |  xargs 
}

執行它:
genpasswd 16


輸出:

uw8CnDVMwC6vOKgW

 

 

7.防火牆SSH TCP埠#22

 

您需要通過更新iptables / ufw / firewall-cmd或pf防火牆配置來防火牆ssh TCP埠#22。通常,OpenSSH伺服器只能接受來自LAN或其他遠端WAN站點的連線。

 

Netfilter(Iptables)配置

更新/ etc / sysconfig / iptables(Redhat和朋友特定檔案)接受來自192.168.1.0/24和202.54.1.5/29的連線,輸入:

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

如果您使用IPv6雙重堆疊sshd,請編輯/ etc / sysconfig / ip6tables(Redhat和朋友特定檔案),輸入:

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

將ipv6network :: / ipv6mask替換為實際的IPv6範圍。

 

UFW for Debian / Ubuntu Linux

UFW是簡單防火牆的首字母縮寫。它用於管理Linux防火牆,旨在為使用者提供易於使用的介面。使用以下命令接受來自202.54.1.5/29的埠22

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

有關詳細資訊,請閱讀“ Linux:25個Iptables Netfilter防火牆新SysAdmins 示例 ”。

 

* BSD PF防火牆配置

 

如果您使用PF防火牆更新/etc/pf.conf,如下所示:

將$ ext_if inet proto tcp從{192.168.1.0/24,202.54.1.5/29}傳入$ ssh_server_ip埠ssh標誌S / SA synproxy狀態

 

8.更改SSH埠並限制IP繫結

 

預設情況下,SSH會偵聽系統上的所有可用介面和IP地址。限制ssh埠繫結並更改ssh埠(許多野獸強制指令碼只嘗試連線到TCP埠#22)。要繫結到192.168.1.5和202.54.1.5 IP和埠300,請在sshd_config中新增或更正以下行:

Port 300
ListenAddress 192.168.1.5
ListenAddress 202.54.1.5

當您想要接受來自動態WAN IP地址的連線時,使用主動方法指令碼(如fail2ban或denyhosts)的更好方法。

 

9.使用TCP包裝器(可選)

TCP Wrapper是基於主機的網路ACL系統,用於過濾網路對Internet的訪問。OpenSSH確實支援TCP包裝器。只需更新您的/etc/hosts.allow檔案,如下所示,僅允許來自192.168.1.2和172.16.23.12的SSH地址:

sshd:192.168.1.2 172.16.23.12 

請參閱此常見問題解答,瞭解如何在Linux / Mac OS X和UNIX作業系統下設定和使用TCP包裝器

 

10.阻止SSH破解者/蠻力攻擊

蠻力是一種通過使用單個或分散式計算機網路嘗試大量可能性(使用者和密碼的組合)來破壞加密方案的方法。要防止對SSH進行暴力攻擊,請使用以下軟體:

 

  • DenyHosts是一個基於Python的SSH伺服器安全工具。它旨在通過監視身份驗證日誌中的無效登入嘗試並阻止原始IP地址來防止對SSH伺服器的暴力攻擊。
  • 解釋如何在RHEL / Fedora和CentOS Linux下設定DenyHosts
  • Fail2ban是一個類似的程式,可以防止對SSH的暴力攻擊。
  • sshguard使用pf保護主機免受對ssh和其他服務的暴力攻擊。
  • security / sshblock阻止濫用SSH登入嘗試。
  • IPQ BDB過濾器可以被視為fail2ban lite。

 

11.速率限制TCP埠#22的傳入流量(可選)

 

netfilter和pf都提供速率限制選項,對埠#22上的傳入連線執行簡單限制。

 

Iptables示例

以下示例將丟棄在60秒內在埠22上進行超過5次連線嘗試的傳入連線:

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

從iptables指令碼呼叫上面的指令碼。另一個配置選項:

$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
$IPT -A INPUT  -i ${inet_if} -p tcp --dport ${ssh_port} -m state --state ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -o ${inet_if} -p tcp --sport ${ssh_port} -m state --state ESTABLISHED -j ACCEPT
# another one line example
# $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

有關更多詳細資訊,請參閱iptables手冊頁。

 

* BSD PF示例

以下內容將每個源的最大連線數限制為20,並將連線數限制為5秒範圍內的15。如果有人違反我們的規則,請將它們新增到我們的abusive_ips表中並阻止它們進行任何進一步的連線。最後,flush關鍵字會殺死由匹配規則建立的所有狀態,這些狀態源自超出這些限制的主機。

sshd_server_ip="202.54.1.5"
table <abusive_ips> persist
block in quick from <abusive_ips>
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.使用埠敲擊(可選)

埠敲門是一種通過在一組預先指定的關閉埠上生成連線嘗試來外部開啟防火牆上的埠的方法。一旦收到正確的連線嘗試序列,就會動態修改防火牆規則,以允許傳送連線的主機嘗試連線到特定埠。一個示例埠使用iptables敲擊ssh的示例:

$IPT -N stage1
$IPT -A stage1 -m recent --remove --name knock
$IPT -A stage1 -p tcp --dport 3456 -m recent --set --name knock2
 
$IPT -N stage2
$IPT -A stage2 -m recent --remove --name knock2
$IPT -A stage2 -p tcp --dport 2345 -m recent --set --name heaven
 
$IPT -N door
$IPT -A door -m recent --rcheck --seconds 5 --name knock2 -j stage2
$IPT -A door -m recent --rcheck --seconds 5 --name knock -j stage1
$IPT -A door -p tcp --dport 1234 -m recent --set --name knock
 
$IPT -A INPUT -m --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -p tcp --dport 22 -m recent --rcheck --seconds 5 --name heaven -j ACCEPT
$IPT -A INPUT -p tcp --syn -j door

有關詳情,請參閱:

 

13.配置空閒登出超時間隔

使用者可以通過ssh登入伺服器,可以設定空閒超時時間間隔,以避免無人值守的ssh會話。開啟sshd_config並確保配置了以下值:您正在設定空閒超時間隔(以秒為單位)(300秒== 5分鐘)。在此間隔過去之後,空閒使用者將被自動踢出(讀取為已登出)。瞭解更多詳細資訊,瞭解如何在一段時間不活動後自動將BASH / TCSH / SSH使用者登出。
 

ClientAliveInterval 300
ClientAliveCountMax 0

 

14.為ssh使用者啟用警告標題

通過使用以下行更新sshd_config來設定警告標題:


Banner /etc/issue
示例/ etc / issue檔案:

----------------------------------------------------------------------------------------------
You are accessing a XYZ Government (XYZG) Information System (IS) that is provided for authorized use only.
By using this IS (which includes any device attached to this IS), you consent to the following conditions:

+ The XYZG routinely intercepts and monitors communications on this IS for purposes including, but not limited to,
penetration testing, COMSEC monitoring, network operations and defense, personnel misconduct (PM),
law enforcement (LE), and counterintelligence (CI) investigations.

+ At any time, the XYZG may inspect and seize data stored on this IS.

+ Communications using, or data stored on, this IS are not private, are subject to routine monitoring,
interception, and search, and may be disclosed or used for any XYZG authorized purpose.

+ This IS includes security measures (e.g., authentication and access controls) to protect XYZG interests--not
for your personal benefit or privacy.

+ Notwithstanding the above, using this IS does not constitute consent to PM, LE or CI investigative searching
or monitoring of the content of privileged communications, or work product, related to personal representation
or services by attorneys, psychotherapists, or clergy, and their assistants. Such communications and work
product are private and confidential. See User Agreement for details.
----------------------------------------------------------------------------------------------issue
issue翻譯:
-------------------------------------------------- --------------------------------------------
您正在訪問僅供授權使用的XYZ政府(XYZG)資訊系統(IS)。
使用此IS(包括本IS附帶的任何裝置),即表示您同意以下條件:

+ XYZG定期攔截和監控此IS上的通訊,目的包括但不限於:
滲透測試,COMSEC監控,網路運營和防禦,人員不端行為(PM),
執法(LE)和反間諜(CI)調查。

+任何時候,XYZG都可以檢查和獲取儲存在該IS上的資料。

+本IS的通訊使用或儲存的資料不是私有的,需要進行例行監控,
攔截和搜尋,可以被公開或用於任何XYZG授權目的。

+此IS包括保護XYZG利益的安全措施(例如,身份驗證和訪問控制),而不是
為了您的個人利益或隱私。

+儘管如此,使用此IS並不構成對PM,LE或CI調查搜尋的同意
或監控與個人代表相關的特權通訊或工作產品的內容
或由律師,心理治療師或神職人員及其助手提供的服務。這樣的溝通和工作
產品是私密和保密的。有關詳情,請參閱使用者協議
-------------------------------------------------- --------------------------------------------

以上是標準樣本,請諮詢您的法律團隊,瞭解具體的使用者協議和法律宣告詳情。

 

15.禁用.rhosts檔案(驗證)

不要讀取使用者的〜/ .rhosts和〜/ .shosts檔案。使用以下設定更新sshd_config:


IgnoreRhosts yes


SSH可以模擬過時的rsh命令的行為,只需通過RSH禁用不安全的訪問。

 

16.禁用基於主機的身份驗證(驗證)

要禁用基於主機的身份驗證,請使用以下選項更新sshd_config:


HostbasedAuthentication no

 

17.補丁OpenSSH和作業系統

 

建議您使用yumapt-getfreebsd-update等工具,使系統與最新的安全補丁保持同步:

 

18. Chroot OpenSSH(將使用者鎖定到他們的主目錄)

 

預設情況下,允許使用者瀏覽伺服器目錄,例如/ etc /,/ bin等。您可以使用基於作業系統的chroot或使用特殊工具(如rssh)來保護ssh 。隨著OpenSSH 4.8p1或4.9p1的釋出,您不再需要依賴第三方hacks(如rssh或複雜的chroot(1)設定)來鎖定使用者到其主目錄。請參閱此部落格文章,瞭解有關將使用者鎖定到其主目錄的新ChrootDirectory指令。

 

19.在客戶端計算機上禁用OpenSSH伺服器

 

工作站和膝上型電腦可以在沒有OpenSSH伺服器的情況下工作 如果您不提供SSH的遠端登入和檔案傳輸功能,請禁用和刪除SSHD伺服器。CentOS / RHEL使用者可以使用yum命令禁用和刪除openssh-server :


$ sudo yum erase openssh-server


Debian / Ubuntu Linux使用者可以使用apt命令 / apt-get命令禁用和刪除它:


$ sudo apt-get remove openssh-server


您可能需要更新iptables指令碼以刪除ssh異常規則。在CentOS / RHEL / Fedora下編輯檔案/ etc / sysconfig / iptables和/ etc / sysconfig / ip6tables。一旦完成重啟iptables服務:


# service iptables restart
# service ip6tables restart

 

20.來自Mozilla的獎金提示

如果您使用的是OpenSSH 6.7+或更高版本,請嘗試以下設定:

:

#################[ WARNING ]########################
# Do not use any setting blindly. Read sshd_config #
# man page. You must understand cryptography to    #
# tweak following settings. Otherwise use defaults #
####################################################
 
# Supported HostKey algorithms by order of preference.
HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
 
# Specifies the available KEX (Key Exchange) algorithms.
KexAlgorithms [email protected],ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256
 
# Specifies the ciphers allowed
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
 
#Specifies the available MAC (message authentication code) algorithms
MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]
 
# LogLevel VERBOSE logs user's key fingerprint on login. Needed to have a clear audit track of which key was using to log in.
LogLevel VERBOSE
 
# Log sftp level file access (read/write/etc.) that would not be easily logged otherwise.
Subsystem sftp  /usr/lib/ssh/sftp-server -f AUTHPRIV -l INFO

您可以使用以下命令獲取OpenSSH伺服器支援的密碼和alog列表:


$ ssh -Q cipher
$ ssh -Q cipher-auth
$ ssh -Q mac
$ ssh -Q kex
$ ssh -Q key


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

 

如何測試sshd_config檔案並重新啟動/重新載入我的SSH伺服器?

 

要在重新啟動sshd之前檢查配置檔案的有效性和任何錯誤的金鑰的完整性,請執行:
$ sudo sshd -t
擴充套件測試模式:
$ sudo sshd -T
最後根據您的發行版版本在Linux或Unix類系統上重新啟動sshd
sudo systemctl start ssh ## Debian/Ubunt Linux##
sudo systemctl restart sshd.service ## CentOS/RHEL/Fedora Linux##
$ doas /etc/rc.d/sshd restart ## OpenBSD##
$ sudo service sshd restart ## FreeBSD##

 

其他的建議:

 

  1. 使用2FA實現更嚴格的SSH安全性 - 可以使用OATH ToolkitDuoSecurity啟用多重身份驗證。
  2. 使用基於keychain的身份驗證 - keychain是一種特殊的bash指令碼,旨在使基於金鑰的身份驗證非常方便和靈活。與無密碼金鑰相比,它提供了各種安全優勢

 

也可以看看:

 

  • 官方的OpenSSH專案。
  • 手冊頁:sshd(8),ssh(1),ssh-add(1),ssh-agent(1)

 

如果您有這裡沒有提到的技術或方便的軟體,請分享以下評論,以幫助您的讀者保持其基於OpenSSH的伺服器的安全。

 

轉載來源:https://www.cyberciti.biz/tips/linux-unix-bsd-openssh-server-best-practices.html