您当前的位置:首页 > 计算机 > 安全防护

如何在 Linux/Unix 上审核 SSH 服务器和客户端配置

时间:07-29来源:作者:点击数:

一个所有开发人员和 Unix 用户都知道如何使用 ssh 客户端。OpenSSH 是 Secure Shell (SSH) Internet 通信协议的广泛使用的实现。回到过去,Unix 人使用不安全的 Telnet。另一方面,SSH 允许在两台主机之间使用安全通道交换数据。因此,每台运行云或家庭的 Linux 和 Unix 服务器都需要一台 OpenSSH 服务器来进行管理和 IT 自动化。遗憾的是,SSH 服务器和客户端的普及也带来了各种安全问题。本文将讨论任何人都可以使用的 ssh 服务器和客户端审计工具,以解决安全问题。

什么是 ssh-audit 工具?

简单来说,ssh-audit 是一种用于 ssh 服务器和客户端审计的工具。例如,您可以使用此工具:

  1. 扫描 OpenSSH 服务器和客户端配置是否存在安全问题
  2. 确保您的 Linux 和 Unix 盒子使用正确和推荐的算法
  3. 检查 OpenSSH 横幅并识别设备或软件和作系统
  4. 查找 ssh 密钥交换、主机密钥、加密和消息身份验证代码算法
  5. 提醒开发人员和系统管理员有关配置问题、弱/遗留算法以及 SSH 使用的功能
  6. 来自 OpenSSH、Dropbear SSH 和 libssh 的历史信息
  7. 策略扫描以确保遵守强化/标准配置

在 Linux/Unix 上审核 SSH 服务器和客户端配置的要求

你需要:

  • Linux、Windows 或类 Unix 系统,例如 macOS、FreeBSD 等
  • Python 版本 3.6 – 3.9
  • 没有其他依赖项

如何安装 ssh-audit 工具

安装此类工具的方法有很多种。让我们看看根据他们的桌面和服务器作系统使用的一些流行选项。

在 Ubuntu Linux 上安装 ssh-audit

如果您在 Linux 系统上启用了 snap,请运行以下 snap 命令:

$ sudo snap install ssh-audit

FreeBSD 安装 ssh-audit

搜索它并使用 pkg 命令安装:

pkg search ssh-audit
# note down output from above command and apply it #
sudo pkg install py37-ssh-audit
如何在 FreeBSD 上安装 ssh audit

关于 macOS 的说明

首先,在 macOS 上启用/安装 Homebrew 以使用 brew 包管理器,然后键入:

$ brew install ssh-audit

其他方法

当然,我们也可以从 PyPI 安装它。例如:

要从 Dockerhub 安装:

然后按如下方式运行它:

pip3 install ssh-audit
docker pull positronsecurity/ssh-audit
docker run -it -p 2222:2222 positronsecurity/ssh-audit {ssh-server-ip}
docker run -it -p 2222:2222 positronsecurity/ssh-audit 192.168.2.17
警告!这些示例可能会编辑您的 OpenSSH 服务器和客户端配置文件。查看所有更改。您最好备份所有配置文件。首先在 VM 中尝试它们。nixCraft 或作者不对 ssh 连接丢失负责。

用法

语法很简单:

对于基本的 SSH 服务器审计,键入:

这是我看到的:

ssh-audit [options]
ssh-audit 192.168.2.17
# set SSH port to 222 #
ssh-audit 192.168.2.254:222
ssh-audit router.home.sweet
ssh-audit 192.168.2.254
# general
(gen) banner: SSH-2.0-OpenSSH_8.6 FreeBSD-openssh-portable-8.6.p1,1
(gen) software: OpenSSH 8.6 running on FreeBSD
(gen) compatibility: OpenSSH 7.4+, Dropbear SSH 2018.76+
(gen) compression: enabled (zlib@openssh.com)
 
# key exchange algorithms
(kex) curve25519-sha256                     -- [info] available since OpenSSH 7.4, Dropbear SSH 2018.76
(kex) curve25519-sha256@libssh.org          -- [info] available since OpenSSH 6.5, Dropbear SSH 2013.62
(kex) ecdh-sha2-nistp256                    -- [fail] using weak elliptic curves
                                            `- [info] available since OpenSSH 5.7, Dropbear SSH 2013.62
(kex) ecdh-sha2-nistp384                    -- [fail] using weak elliptic curves
                                            `- [info] available since OpenSSH 5.7, Dropbear SSH 2013.62
(kex) ecdh-sha2-nistp521                    -- [fail] using weak elliptic curves
                                            `- [info] available since OpenSSH 5.7, Dropbear SSH 2013.62
(kex) diffie-hellman-group-exchange-sha256 (2048-bit) -- [info] available since OpenSSH 4.4
(kex) diffie-hellman-group16-sha512         -- [info] available since OpenSSH 7.3, Dropbear SSH 2016.73
(kex) diffie-hellman-group18-sha512         -- [info] available since OpenSSH 7.3
(kex) diffie-hellman-group14-sha256         -- [info] available since OpenSSH 7.3, Dropbear SSH 2016.73
 
# host-key algorithms
(key) rsa-sha2-512 (3072-bit)               -- [info] available since OpenSSH 7.2
(key) rsa-sha2-256 (3072-bit)               -- [info] available since OpenSSH 7.2
(key) ssh-rsa (3072-bit)                    -- [fail] using weak hashing algorithm
                                            `- [info] available since OpenSSH 2.5.0, Dropbear SSH 0.28
                                            `- [info] a future deprecation notice has been issued in OpenSSH 8.2: https://www.openssh.com/txt/release-8.2
(key) ecdsa-sha2-nistp256                   -- [fail] using weak elliptic curves
                                            `- [warn] using weak random number generator could reveal the key
                                            `- [info] available since OpenSSH 5.7, Dropbear SSH 2013.62
(key) ssh-ed25519                           -- [info] available since OpenSSH 6.5
 
# encryption algorithms (ciphers)
(enc) chacha20-poly1305@openssh.com         -- [info] available since OpenSSH 6.5
                                            `- [info] default cipher since OpenSSH 6.9.
(enc) aes128-ctr                            -- [info] available since OpenSSH 3.7, Dropbear SSH 0.52
(enc) aes192-ctr                            -- [info] available since OpenSSH 3.7
(enc) aes256-ctr                            -- [info] available since OpenSSH 3.7, Dropbear SSH 0.52
(enc) aes128-gcm@openssh.com                -- [info] available since OpenSSH 6.2
(enc) aes256-gcm@openssh.com                -- [info] available since OpenSSH 6.2
 
# message authentication code algorithms
(mac) umac-64-etm@openssh.com               -- [warn] using small 64-bit tag size
                                            `- [info] available since OpenSSH 6.2
(mac) umac-128-etm@openssh.com              -- [info] available since OpenSSH 6.2
(mac) hmac-sha2-256-etm@openssh.com         -- [info] available since OpenSSH 6.2
(mac) hmac-sha2-512-etm@openssh.com         -- [info] available since OpenSSH 6.2
(mac) hmac-sha1-etm@openssh.com             -- [warn] using weak hashing algorithm
                                            `- [info] available since OpenSSH 6.2
(mac) umac-64@openssh.com                   -- [warn] using encrypt-and-MAC mode
                                            `- [warn] using small 64-bit tag size
                                            `- [info] available since OpenSSH 4.7
(mac) umac-128@openssh.com                  -- [warn] using encrypt-and-MAC mode
                                            `- [info] available since OpenSSH 6.2
(mac) hmac-sha2-256                         -- [warn] using encrypt-and-MAC mode
                                            `- [info] available since OpenSSH 5.9, Dropbear SSH 2013.56
(mac) hmac-sha2-512                         -- [warn] using encrypt-and-MAC mode
                                            `- [info] available since OpenSSH 5.9, Dropbear SSH 2013.56
(mac) hmac-sha1                             -- [warn] using encrypt-and-MAC mode
                                            `- [warn] using weak hashing algorithm
                                            `- [info] available since OpenSSH 2.1.0, Dropbear SSH 0.28
 
# fingerprints
(fin) ssh-ed25519: SHA256:JGOsGxcCjN5Ej+8FSYK5bo4L23W66wSgQof8xpASplc
(fin) ssh-rsa: SHA256:aM8yrCKPlLDd5kRwSS7JNj7Kho6k9JEs5aFv/VTGMRA
 
# algorithm recommendations (for OpenSSH 8.6)
(rec) -ecdh-sha2-nistp256                   -- kex algorithm to remove 
(rec) -ecdh-sha2-nistp384                   -- kex algorithm to remove 
(rec) -ecdh-sha2-nistp521                   -- kex algorithm to remove 
(rec) -ecdsa-sha2-nistp256                  -- key algorithm to remove 
(rec) -ssh-rsa                              -- key algorithm to remove 
(rec) -hmac-sha1                            -- mac algorithm to remove 
(rec) -hmac-sha1-etm@openssh.com            -- mac algorithm to remove 
(rec) -hmac-sha2-256                        -- mac algorithm to remove 
(rec) -hmac-sha2-512                        -- mac algorithm to remove 
(rec) -umac-128@openssh.com                 -- mac algorithm to remove 
(rec) -umac-64-etm@openssh.com              -- mac algorithm to remove 
(rec) -umac-64@openssh.com                  -- mac algorithm to remove 
 
# additional info
(nfo) For hardening guides on common OSes, please see: <https://www.ssh-audit.com/hardening_guides.html>

审核多台服务器

想要对托管在云中的许多服务器进行标准审计吗?我们需要创建一个新的文本文件:

然后,我将运行:

cat > ec2-servers-list.txt
aws-server1
aws-server2
54.56.5.5
ssh-audit -T ec2-servers-list.txt

审核客户端配置

要审核客户端配置,请键入:

ssh-audit -c
# client listener on port 4123
ssh-audit -c -p 4123

如何对服务器运行策略审核

要列出策略运行:

然后我将看到一个列表,如下所示:$ ssh-audit -L

Server policies:
 
  * "Hardened OpenSSH Server v7.7 (version 1)"
  * "Hardened OpenSSH Server v7.8 (version 1)"
  * "Hardened OpenSSH Server v7.9 (version 1)"
  * "Hardened OpenSSH Server v8.0 (version 1)"
  * "Hardened OpenSSH Server v8.1 (version 1)"
  * "Hardened OpenSSH Server v8.2 (version 1)"
  * "Hardened OpenSSH Server v8.3 (version 1)"
  * "Hardened OpenSSH Server v8.4 (version 1)"
  * "Hardened OpenSSH Server v8.5 (version 1)"
  * "Hardened Ubuntu Server 16.04 LTS (version 1)"
  * "Hardened Ubuntu Server 18.04 LTS (version 1)"
  * "Hardened Ubuntu Server 20.04 LTS (version 1)"
 
Client policies:
 
  * "Hardened Ubuntu Client 16.04 LTS (version 2)"
  * "Hardened Ubuntu Client 18.04 LTS (version 2)"
  * "Hardened Ubuntu Client 20.04 LTS (version 2)"
 
 
Hint: Use -P and provide the full name of a policy to run a policy scan with.

要对名为 ln.ncbz01 的服务器运行策略审计,请键入:

$ ssh-audit -P 'Hardened Ubuntu Server 20.04 LTS (version 1)' ln.ncbz01

ssh-audit 策略扫描输出

修复 Ubuntu 20.04 LTS 服务器审核失败的问题

我们需要以 root 用户身份运行以下命令。让我们重新生成 RSA 和 ED25519 密钥。运行:

删除小的 Diffie-Hellman 模

启用 RSA 和 ED25519 密钥

限制支持的密钥交换、密码和 MAC 算法

最后,在 Ubuntu Linux 上重新启动 ssh 服务:

现在再次验证审计:

rm /etc/ssh/ssh_host_*
ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe
mv /etc/ssh/moduli.safe /etc/ssh/moduli
sed -i 's/^\#HostKey \/etc\/ssh\/ssh_host_\(rsa\|ed25519\)_key$/HostKey \/etc\/ssh\/ssh_host_\1_key/g' /etc/ssh/sshd_config
echo -e "\n# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com\n# hardening guide.\nKexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256\nCiphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr\nMACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com\nHostKeyAlgorithms ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-512,rsa-sha2-256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com" > /etc/ssh/sshd_config.d/ssh-audit_hardening.conf
systemctl restart ssh
ssh-audit -P 'Hardened Ubuntu Server 20.04 LTS (version 1)' ln.ncbz01

总结

我认为它是系统管理员和安全人员在 Linux 和 Unix 盒子上审核 ssh 服务器和客户端的绝佳工具。如果您发现这是一个有价值的工具,请在下面的评论部分告诉我。请使用 man 命令阅读以下手册页:

man sshd_config #<-- server config
man ssh_config #<-- client config
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐