引言
大部分時間內,您的重點將會放在讓雲端應用程式上線並運行。作為您的設置和部署過程的一部分,在您的系統和應用程式公開之前,建立堅固和全面的 安全措施非常重要。在部署應用程式之前實施本教程中的安全措施,將確保您在基礎設施上運行的任何軟體都有一個安全的基本配置,而不是部署後可能實施的臨時措施。
本指南列出了您在配置和設置伺服器基礎設施時可以採取的一些實用安全措施。這份清單並非您能夠保護伺服器的所有措施的完整列表,但它為您提供了一個可以基於此建設的起點。隨著時間的推移,您可以發展出一套更符合您的環境和應用程式特定需求的定制安全策略。
SSH金鑰
SSH,又稱 Secure Shell,是一種加密協定,用於管理和通訊伺服器。當您在伺服器上工作時,您大多數時間都會在使用 SSH 連線的终端機 Session 中度過。作為密码登入的替代方案,SSH 密鑰使用加密來提供安全的登入方式,並被推薦給所有使用者。
SSH 密鑰使用非對稱加密(asymmetric encryption)機制,其原理為產生一對私有和公開密鑰。私鑰由使用者保藏,而公鑰則可以分享。這種机制通常在其他地方也會見到。
要配置 SSH 密鑰認證,你需要將你的公開 SSH 密鑰放置在伺服器的預設位置(通常為 ~/.ssh/authorized_keys
)。欲了解 SSH 加密和連線過程的更多細節,請参閱Understanding the SSH Encryption and Connection Process。
How Do SSH Keys Enhance Security?
### SSH Keys: A Powerful Tool for Improving Server Authentication
SSH keys are a powerful tool for improving the security of server authentication. Here’s how they work:
#### 1. **Key Pair Generation**
When you set up SSH key authentication, you generate a pair of keys – a private key and a public key. These keys are related but not identical; one cannot be used without the other.
#### 2. **Private Key Usage**
The private key is kept secret and is typically stored on your local computer. You use this key to authenticate yourself when you try to log in to a remote server.
#### 3. **Public Key Distribution**
The public key is distributed to the remote server. It’s often placed in a file called `.ssh/authorized_keys` on the server side. This file contains the public keys of all users who are allowed to log in using SSH.
#### 4. **Authentication Process**
When you attempt to log in to a server using SSH, your client (e.g., Terminal on Linux/macOS, PuTTY on Windows) uses your private key to encrypt a message. This encrypted message is then sent to the server.
On the server side, the SSH daemon decrypts the message using the corresponding public key from the `.ssh/authorized_keys` file. If the decryption is successful, it means that the client has the private key associated with the public key on the server, thus verifying the identity of the user.
#### 5. **Advantages Over Passwords**
Compared to passwords, SSH keys offer several advantages:
– **Non-Repudiation**: Once a user logs in successfully with a key, the server can verify that the login came from the legitimate holder of the private key, preventing the user from denying the action later.
– **Convenience**: Users don’t have to remember multiple passwords. They can have a single key that grants access to multiple servers.
– **Fewer Password Resets**: Administrators don’t have to manage or reset passwords as frequently, reducing administrative burden.
– **Multi-Factor Authentication**: By combining SSH keys with other authentication methods like two-factor authentication (2FA), additional layers of security can be implemented.
– **Easy Key Management**: Tools like `ssh-keygen`, `ssh-copy-id`, and `ssh-add` make key management straightforward.
– **Session Recording Immunity**: SSH sessions encrypted with keys are generally immune to session hijacking attacks, as the encryption happens at the application layer rather than relying on network protocols.
In summary, SSH keys enhance security by providing a more robust and convenient method of authenticating users to servers, reducing reliance on passwords and offering advanced protection against various types of attacks.
透过SSH,所有的认证方式——包括密碼認證——都是完全加密的。然而,當允許基於密碼的登錄時,恶意的用戶可以反覆、自動地試圖訪問一台服務器,特別是如果它有一個面向公眾的IP地址。雖然有方法在同一个IP多次失败的尝试后锁定访问,且恶意的用户在实践中将被他们尝试登录你服务器的速度限制,但任何用户可能通過反覆暴力破解攻撃試圖獲得你堆栈的訪問權的情況都会帶來安全风险。
設定SSH密钥認證可以允許你關閉基于密碼的認證。SSH密钥通常比密码有多的数据位——你可以从一个12字符的密码创建一个128字符的SSH密钥散列——使它们更难被暴力破解。然而,有些加密算法仍然被认为是可破解的,通过在足够多次的密码散列反向工程的强大计算机上尝试。其他算法,包括现代SSH客戶端生成的默认RSA密钥,目前還不可能被破解。
如何实施SSH密钥
SSH密钥是登录任何远程Linux服务器环境的推荐方式。一对SSH密钥可以在你的本地机器上使用ssh
命令生成,然后你可以将公钥传输到远程服务器。
在伺服器上設定SSH密鑰,你可以參考如何在Ubuntu、Debian或CentOS上設定SSH密鑰。
對於任何需要密码存取或是容易受到暴力破解攻擊的部分,你可以實施一個解決方案,如fail2ban在伺服器上限制password猜測次數。
不讓root用戶直接透過SSH登入是一個好做法。取而代之的是,使用一个非特權帳號登入,並在有需要時提升权限,如使用sudo
之类的工具。這種限制权限的方法稱為最小特權原則。一旦你已經連線到伺服器,並且確認了一個非特權帳號能夠透過SSH登入,你可以將PermitRootLogin no
指令设在/etc/ssh/sshd_config
中,然後重新啟動伺服器的SSH服務,使用命令如sudo systemctl restart sshd
。
防火牆
防火牆是一個軟體或硬體設備,用來控制服務如何向網絡公開,以及允許哪種類型的流量進出特定伺服器或伺服器群。
在典型的伺服器上,可能默認運行著多個服務。這些可以分為以下幾組:
- 公共服務,任何人在互聯網上都可以訪問,通常是匿名訪問。這方面的例子是伺服您的實際網站的網絡伺服器。
- 私有服務,應該只有選定的一組授權帳戶或從特定位置訪問。例如,數據庫控制面板像phpMyAdmin。
- 內部服務,應該只能從伺服器內部訪問,而不向公共互聯網公開該服務。例如,一個應該只接受本地連接的數據庫。
防火牆可以確保根據上述類別以不同的細粒度限制對您的軟體的訪問。公共服務可以保持開放並向互聯網可用,私有服務可以根據不同條件(如連接類型)進行限制。內部服務可以使其完全無法訪問互聯網。在大多數配置中,對未使用的端口,訪問是完全被封鎖的。
防火牆如何加強安全?
即使您的服務 implemented security features or are restricted to the interfaces you’d like them to run on, a firewall acts as a basic layer of protection by limiting connections to and from your services before traffic is handled by an application.
A correctly configured firewall will restrict access to all but the specific services you need to remain open, usually by opening only the ports associated with those services. For example, SSH generally runs on port 22, and HTTP/HTTPS access via a web browser usually runs on ports 80 and 443 respectively. Expose only a few pieces of software reduces the attack surface of your server, limiting the components that are vulnerable to exploitation.
如何實施防火牆
Linux系统上有许多防火牆可供使用,有些比其他的更复杂。一般来说,您只需要在服务器的配置文件中添加或修改与特定服务相关的规则。以下是一些选项来开始实施:
-
UFW,或者稱為簡易防火牆,在像Ubuntu這樣的一些Linux發行版上是預裝的。您可以 在如何在Ubuntu 20.04上使用UFW設置防火牆
-
如果您正在使用Red Hat、Rocky或Fedora Linux,可以閱讀如何使用firewalld設置防火牆來使用它們的默認工具。
-
許多軟體防火牆,如UFW和firewalld,將其配置的規則直接寫入名為
iptables
的文件中。要了解如何直接使用iptables
配置,您可以查看Iptables Essentials: 常見防火牆規則與命令
。請注意,一些其他實現端口規則的軟體,如Docker,也會直接寫入iptables
,並可能與您使用UFW創建的規則發生衝突,因此了解如何閱讀iptables
配置在這種情況下會很有幫助。
注意:許多主機提供商,包括DigitalOcean,都允許您配置一個防火牆作為服務,這個服務作為您的雲伺服器(們)的外部層運行,而不是需要直接實施防火牆。這些在網絡邊緣使用管理工具實施的配置,實際上往往較不複雜,但可能较難編寫腳本和複製。您可以參考DigitalOcean的雲防火牆的文檔。
請確保您的防火牆配置默認阻止未知流量。這樣,您部署的任何新服務都不會意外地暴露在互聯网上。相反,您需要明確允許訪問,這將迫使您評估服務是如何運行、訪問以及誰應該能夠使用它。
VPC網絡
虛擬私有雲(VPC)網絡是您的基礎設施資源的私有網絡。VPC網絡在資源之間提供更安全的連接,因為網絡的接口無法從公共互聯網訪問。
VPC網絡如何增強安全性?
一些托管提供商将默认为您分配一个公共网络接口和一个私有网络接口。禁用部分基础设施的公网接口只会允许这些实例使用私有网络接口在内部网络上相互连接,这意味着您的系统之间的流量将不会通过公共互联网路由,从而暴露或被拦截的风险较低。
通过仅选择性地暴露几个专用互联网网关,也称为入口网关,作为您VPC网络资源与公共互联网之间通信的唯一途径,您可以更好地控制和监控连接到您的资源的公共流量。现代容器编排系统如Kubernetes具有非常明确的入口网关概念,因为它们通常创建许多私有网络接口,需要选择性地将其公开。
如何实现VPC网络
许多云基础设施提供商使您能够在他们的数据中心内创建和添加VPC网络中的资源。
注意:如果您想在DigitalOcean上设置自己的VPC网关,可以参考如何在Debian、Ubuntu和CentOS服务器上配置Droplet为VPC网关指南。
自定義您的私有網絡需要先進的服务器配置和網路知識。另一個替代方案是使用VPN連線between您的伺服器。VPNs和私用网络
VPNs and Private Networking
VPN,或virtual private network,是一種方式to create secure connections between remote computers and present the connection as if it were a local private network. This provides a way to configure your services as if they were on a private network and connect remote servers over secure connections.
For example, DigitalOcean private networks enableisolated communication between servers in the same account or team within the same region.
How Do VPNs Enhance Security?
使用VPN可以增强安全性。VPN提供了一种方式to map out a private network that only your servers can see. Communication will be fully private and secure. Other applications can be configured to pass their traffic over the virtual interface that the VPN software exposes. This way, only services that are meant to be used by clients on the public internet need to be exposed on the public network.
如何實現VPN
使用私人網絡通常需要在首次部署伺服器時決定您的網絡接口,並配置您的應用程式和防火牆以偏好這些接口。相比之下,部署VPN需要安裝額外的工具和創建額外的網絡路徑,但通常可以在現有的架構上進行部署。VPN上的每個伺服器都必须具有建立VPN連接所需的共享安全和配置數據。在VPN建立並運行後,必須 configuration your applications to use the VPN tunnel.
如果您正在使用Ubuntu或CentOS,您可以按照在Ubuntu 20.04上設定和配置OpenVPN伺服器的教程進行操作。
Wireguard是另一個流行的VPN部署。一般来说,VPN通過實現一系列私人網絡接口背後的几個入口點來限制對您的雲伺服器的入口,這與VPC配置通常作為核心基礎設施考量不同,VPN可以更為即興地部署。
服務審計
良好的安全涉及到分析您的系統,了解可用的攻擊面,並盡可能地鎖定組件。
服務審計是一種了解在給定系統上運行哪些服務,它們使用哪些端口進行通信,以及這些服務使用哪些協議的方法。這些信息有助於您配置哪些服務應公開訪問,防火牆設置、監控和警報。
服務審計如何增強安全性?
每個運行的服務,無論是內部還是公開的,都代表著惡意用戶的攻擊面擴大。您運行的服務越多,軟件受到漏洞影響的機會就越大。
一旦您對機器上運行的網絡服務有了很好的了解,您就可以開始分析這些服務。當您執行服務審計時,請對每個運行的服務問自己以下問題:
- 這個服務應該運行嗎?
- 該服務是否在它不應運行的網絡接口上運行?
- 服務應該連接到公共或私人網絡介面?
- 我的防火牆規則是否結構化以通過合法流量至此服務?
- 我的防火牆規則是否阻擋了不合法的流量?
- 我是否有方法接收關於這些服務脆弱性的安全警報?
這種服務審計應該在配置基礎設施中的任何新服務器時成為標準做法。每几个月進行一次服務審計也可以幫助你捕捉到任何可能无意間更改配置的服务。
如何实施服務審計
要審計在您的系統上運行的網絡服務,請使用ss
命令列出服務器上正在使用的所有TCP和UDP端口。一個顯示程序名稱、PID和用於侦聽TCP和UDP流量的事物地址的示例命令是:
p
、l
、u
、n
和t
選項的工作方式如下:
p
顯示使用給定套接字的特定进程。l
只顯示正在積極侦聽連接的套接字。u
包括UDP套接字(除了TCP套接字)。n
顯示數值流量。- 上述内容為:
t
包括TCP套接字(以及UDP套接字)。
您将收到类似这样的输出:
OutputNetid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=812,fd=3))
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=69226,fd=6),("nginx",pid=69225,fd=6))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=812,fd=4))
tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=69226,fd=7),("nginx",pid=69225,fd=7))
需要关注的列主要是Netid、Local Address:Port和Process名称列。如果本地地址端口为0.0.0.0
,则服务正在接受来自所有IPv4网络接口的连接。如果地址为[:]
,则服务正在接受来自所有IPv6接口的连接。在上面的示例输出中,SSH和Nginx都在监听所有公共接口,同时监听IPv4和IPv6网络堆栈。
您可以决定是否允许SSH和Nginx在两个接口上监听,或者只在其中一个或另一个接口上监听。通常,你应该禁用运行在不使用接口上的服务。一般来说,你应该禁用运行在不使用接口上的服务。
无人值守更新
保持服务器更新到最新版本对于确保基本的安全水平至关重要。运行过时且不安全版本的软件的服务器负责大多数安全事件,但定期更新可以减轻漏洞并防止攻击者获得对服务器的立足点。无人值守更新允许系统自动更新大多数包。
如何透過自動更新增強安全性?
實施自動更新,即無人值守的更新,可以降低維護伺服器安全的勞動力需求,並縮短伺服器可能受到已知漏洞影響的時間。在影響您伺服器上軟體的漏洞發生時,您的伺服器將會在您運行更新之前處於易受攻擊的狀態。每天進行無人值守的升級可以確保您不會錯過任何軟體包,並且任何有漏洞的軟體都會在修補程序一可用時立即進行修補。
如何實施自動更新
您可以參考如何保持 Ubuntu 伺服器更新以了解在 Ubuntu 上實施無人值守更新的概覽。
公鑰基礎結構與 SSL/TLS 加密
公鑰基礎結構,或稱PKI,是指一個旨在創建、管理及驗證憑證以識別個體和加密通訊的系統。SSL或TLS憑證可用於對不同實體進行相互認證。認證之後,它們還可以用於建立加密通訊。
PKI如何增強安全性?
建立一個證書授權中心(CA)和管理您伺服器的證書,允許您的基礎設施內的每個實體驗證其他成員的身份並加密其流量。這可以防止中間人攻擊,攻擊者模擬您基礎設施中的伺服器以截獲流量。
每個伺服器都可以被配置為信任一個集中式的證書授權中心。之後,任何由該授權中心簽署的證書都將被默認信任。
如何實施PKI
配置证书授權機構並設置其他公開密鑰基礎設施可能會涉及相當多的initial effort。此外,管理certificates可以產生additional administrationburden当new certificates需要被创建、签署或撤销時。
對於許多用戶來說,實施一個完整的public keyinfrastructure可能只有在their infrastructure needsgrow的時候才會有意義。使用VPNsecurecommunicationsbetweencomponentsmaybeabetterintermediatemeasureuntilyoureachapointwherePKIisworththeextraadministrationcosts。
如果您希望建立自己的certificateauthority,您可以根據您使用的Linuxdistribution参考Setting Up and Configuring a Certificate Authority (CA)guide。
Conclusion
The strategies outlined in this tutorial are an overview of some of the steps that you can take to improve the security of your systems. It is important to recognize that security measures decrease in their effectiveness the longer you wait to implement them. Security should not be an afterthought and must be implemented when you first provision your infrastructure. Once you have a secure base to build upon, you can then start deploying your services and applications with some assurances that they are running in a secure environment by default.
即使有一個安全的起點環境,也要記住安全是一個持續和迭代的過程。始終要確定任何改變可能會對安全有什麼影響,並且採取什麼措施來確保你總是為你的軟件創建安全的預設配置和環境。
Source:
https://www.digitalocean.com/community/tutorials/7-security-measures-to-protect-your-servers