一个所有开发人员和 Unix 用户都知道如何使用 ssh 客户端。OpenSSH 是 Secure Shell (SSH) Internet 通信协议的广泛使用的实现。回到过去,Unix 人使用不安全的 Telnet。另一方面,SSH 允许在两台主机之间使用安全通道交换数据。因此,每台运行云或家庭的 Linux 和 Unix 服务器都需要一台 OpenSSH 服务器来进行管理和 IT 自动化。遗憾的是,SSH 服务器和客户端的普及也带来了各种安全问题。本文将讨论任何人都可以使用的 ssh 服务器和客户端审计工具,以解决安全问题。
简单来说,ssh-audit 是一种用于 ssh 服务器和客户端审计的工具。例如,您可以使用此工具:
你需要:
安装此类工具的方法有很多种。让我们看看根据他们的桌面和服务器作系统使用的一些流行选项。
如果您在 Linux 系统上启用了 snap,请运行以下 snap 命令:
$ sudo snap install ssh-audit
搜索它并使用 pkg 命令安装:
pkg search ssh-audit
# note down output from above command and apply it #
sudo pkg install py37-ssh-audit

首先,在 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
语法很简单:
对于基本的 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

我们需要以 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
