SSH服务支持的加密算法介绍
2020-03-18 17:16:52 阿炯

在一次例行的安全扫描中有发现安全风险提示:SSH服务配置不建议使用arcfour流密码或无任何密码,RFC 4253 不建议使用arcfour弱加密算法,仅使用CTR模式加密算法,如AES-CTR。所以花了一些时间来简单地查找资源并分析了相关的原因,在此写下笔记。

为了确保信息的传输,SSH 在事务中的各个点采用了许多不同类型的数据操作技术。这些包括对称加密,非对称加密和散列的形式。如果配置为CBC模式的话,OpenSSH没有正确地处理分组密码算法加密的SSH会话中所出现的错误,导致可能泄露密文中任意块最多32位纯文本。在以标准配置使用OpenSSH时,攻击者恢复32位纯文本的成功概率为2^{-18},此外另一种攻击变种恢复14位纯文本的成功概率为2^{-14}。查询本机上的ssh服务所支持并启用了的算法:
sshd -T | grep ciphers | perl -pe 's/,/\n/g' | sort -u
sshd -T | grep "\(ciphers\|macs\|kexalgorithms\)"

SSH的配置文件中加密算法没有指定的话,默认支持所有加密算法,包括arcfour,arcfour128,arcfour256等这些弱加密算法,这就可能会导致安全风险。'sshd -T'会将显示全量的配置,'ssh -Q'会查询这些配置中分类的一些参数方法等:
[-Q cipher | cipher-auth | mac | kex | key]
查询支持的加密算法:
ssh -Q cipher-auth    #启用的
ssh -Q cipher    #所有的

修改SSH配置文件,修改加密算法:
/etc/ssh/sshd_config(去掉arcfour,arcfour128,arcfour256等弱加密算法,连cbc类的也不建议使用,仅使用ctr模式加密算法)。

ssh_config和sshd_config都是ssh服务器的配置文件,二者区别在于,前者是针对客户端的配置文件,后者则是针对服务端的配置文件。下面为建议的配置:
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
#Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
#HostKeyAlgorithms ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,ssh-dss
KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256
MACs hmac-sha2-256,hmac-sha2-512,hmac-sha1

保存配置文件后重启SSH服务。

远程验证
ssh -vv -oCiphers=aes128-cbc,3des-cbc,blowfish-cbc <server>
ssh -vv -oMACs=hmac-md5 <server>
ssh -vv -c aes256-cbc <server>

使用nmap验证:
nmap --script "ssh2*" ipaddr

可以看到服务端已不支持arcfour,arcfour128,arcfour256等弱加密算法。高版本的ssh服务还支持对加密算法前加'+|-'来启用或关闭相应的算法。

Ciphers -arcfour*

Ciphers -arcfour,arcfour128,arcfour256

From the sshd_config man page on the Ciphers option (since OpenSSH 7.5):
If the specified value begins with a '+' character, then the specified ciphers will be appended to the default set instead of replacing them. If the specified value begins with a '-' character, then the specified ciphers (including wildcards) will be removed from the default set instead of replacing them.

This also applies to the KexAlgorithms and MACs options.

SSH Weak MAC Algorithms Enabled 漏洞修复使用同样的方式,在服务端添加如下行:
MACs hmac-sha1,umac-64,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160

ciphers set in ssh_config using the Ciphers keyword, then the default value, according to man 5 ssh_config (client-side) and man 5 sshd_config (server-side), is:
aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,
aes128-gcm@openssh.com,aes256-gcm@openssh.com,
aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,
chacha20-poly1305@openssh.com,
aes256-cbc,arcfour

Note the presence of the arcfour ciphers. So you may have to explicitly set a more restrictive value for Ciphers.

ssh cipher from the client will tell you which schemes your client can support. Note that this list is not affected by the list of ciphers specified in ssh_config. Removing a cipher from ssh_config will not remove it from the output of ssh cipher. Furthermore, using ssh with the -c option to explicitly specify a cipher will override the restricted list of ciphers that you set in ssh_config and possibly allow you to use a weak cipher. This is a feature that allows you to use your ssh client to communicate with obsolete SSH servers that do not support the newer stronger ciphers.

nmap --script ssh2-enum-algos -sV -p <port> <host> will tell you which schemes your server supports.

当然客户端也可以主动指定加密算法
/etc/ssh/ssh_config set:
Host *
    ciphers aes256-ctr,aes192-ctr,aes128-ctr


另外提及关于SSL配置文件中的SSL Cipher参数

不同Web服务软件的配置文件位置及参数名称不同,需根据实际情况查找,具体安全算法配置可参考该网站

修改前后支持的加密算法对比:
nmap -p 443 --script "ssl-enum-ciphers" ipaddr


参考来源:
Ssh服务安全加固

SSH weak ciphers and mac algorithms

sshd_config - SSH Server Configuration