Python Paramiko - 确定可用的密码和密钥交换算法

Car*_*arl 7 python encryption ssh paramiko

我有一个使用 Paramiko 库用 Python 编写的自动 SFTP 程序。如果我建立连接,我可以显示传输使用的密码和密钥交换算法。但是,我不确定这与可用的算法和密码是否相同。

例子:

>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.load_system_host_keys()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect("myhost.example.com", 22, username="xyzzy")
>>> t = ssh.get_transport()
>>> so = t.get_security_options()
>>> so.kex
('diffie-hellman-group14-sha1', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group1-sha1')
>>> so.ciphers
('aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'aes256-cbc', '3des-cbc', 'arcfour128', 'arcfour256')
Run Code Online (Sandbox Code Playgroud)

这与可用的相同吗?如果没有,是否有任何方法可以通过编程确定可用的内容?

Mar*_*ryl 6

返回的SecurityOptionsTransport.get_security_options()

包含 ssh 传输的安全首选项的简单对象。这些是可接受的密码、摘要、密钥类型和密钥交换算法的元组,按优先顺序列出。

因此它列出了 Paramiko 库支持的密码和 kex 算法(或您已配置/允许的子集)。

它就是您(和 Paramiko)为连接提供的内容。

最终实际使用的是服务器也支持的最优选的密码/kex 算法(以您的术语可用)。


要获取支持的算法列表,无需连接,您可以使用:

import paramiko
import socket

opts = paramiko.transport.Transport(socket.socket()).get_security_options()
print(opts.ciphers)
print(opts.kex)
Run Code Online (Sandbox Code Playgroud)