当前位置: 首页 > 面试题库 >

Paramiko / Python:键盘交互式身份验证

松建本
2023-03-14
问题内容

我在尝试使用Paramiko(Python)创建SFTP客户端时遇到了困难。

码:

 import paramiko as sftp

 transport = sftp.Transport(('myhost', port), 
    default_max_packet_size=10000, default_window_size=10000)
 transport.connect(username='myuser', password='mypassword')
 client_from_transport = sftp.SFTPClient.from_transport(transport)

错误:

Traceback (most recent call last):
    File "sftp.py", line 91, in <module>
    sftp_client = create_sftp_client()
    File "...\sftp.py", line 63, in create_sftp_client
    client_from_transport = sftp.SFTPClient.from_transport(transport)
    File "...\Python\Python37\Lib\site-packages\paramiko\sftp_client.py", 
    line 165, in from_transport
    window_size=window_size, max_packet_size=max_packet_size
    File "...\Python\Python37\Lib\site-packages\paramiko\transport.py", line 
    806, in open_session
    timeout=timeout,
    File "...\Python\Python37\Lib\site-packages\paramiko\transport.py", line 
    933, in open_channel
    raise e
    File "...\Python\Python37\Lib\site-packages\paramiko\transport.py", line 
    1982, in run
    ptype, m = self.packetizer.read_message()
    File "...\Python\Python37\Lib\site-packages\paramiko\packet.py", line 
    441, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
    File "...\Python\Python37\Lib\site-packages\paramiko\packet.py", line 
    290, in read_all
    raise EOFError()
    EOFError

打印运输对象显示:

<paramiko.Transport at 0x68d0c1d0 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>

Paramiko日志文件:

DEB [20190403-12:31:01.550] thr=1   paramiko.transport: starting thread (client mode): 0xfbc42780
DEB [20190403-12:31:01.550] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.4.2
DEB [20190403-12:31:01.567] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-Server
INF [20190403-12:31:01.567] thr=1   paramiko.transport: Connected (version 2.0, client Server)
DEB [20190403-12:31:01.571] thr=1   paramiko.transport: kex algos:['ecdh-sha2-nistp521', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp256', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group18-sha512', 'diffie-hellman-group17-sha512', 'diffie-hellman-group16-sha512', 'diffie-hellman-group15-sha512', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'aes192-cbc', 'aes256-cbc'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'aes192-cbc', 'aes256-cbc'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib', 'zlib@openssh.com'] server compress:['none', 'zlib', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: Kex agreed: ecdh-sha2-nistp256
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: HostKey agreed: ssh-rsa
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: Compression agreed: none
DEB [20190403-12:31:01.654] thr=1   paramiko.transport: kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEB [20190403-12:31:01.669] thr=1   paramiko.transport: Switch to new keys ...
DEB [20190403-12:31:01.670] thr=2   paramiko.transport: Attempting password auth...
DEB [20190403-12:31:01.689] thr=1   paramiko.transport: userauth is OK
INF [20190403-12:31:02.010] thr=1   paramiko.transport: Authentication continues...
DEB [20190403-12:31:02.010] thr=1   paramiko.transport: Methods: ['keyboard-interactive']
DEB [20190403-12:31:02.010] thr=2   paramiko.transport: [chan 0] Max packet in: 10000 bytes
DEB [20190403-12:31:02.026] thr=1   paramiko.transport: EOF in transport thread

但是SFTP可在FileZilla中使用:

2019-04-03 13:02:36 13796 1 Status: Connecting to xxxxxx...
2019-04-03 13:02:36 13796 1 Trace: CControlSocket::SendNextCommand()
2019-04-03 13:02:36 13796 1 Trace: CSftpDeleteOpData::Send() in state 0
2019-04-03 13:02:36 13796 1 Trace: Going to execute C:\Program Files\FileZilla FTP Client\fzsftp.exe
2019-04-03 13:02:36 13796 1 Response: fzSftp started, protocol_version=8
2019-04-03 13:02:36 13796 1 Trace: CSftpDeleteOpData::ParseResponse() in state 0
2019-04-03 13:02:36 13796 1 Trace: CControlSocket::SendNextCommand()
2019-04-03 13:02:36 13796 1 Trace: CSftpDeleteOpData::Send() in state 3
2019-04-03 13:02:36 13796 1 Command: open "user" 2222
2019-04-03 13:02:36 13796 1 Trace: Connecting to ip port 2222
2019-04-03 13:02:36 13796 1 Trace: We claim version: SSH-2.0-FileZilla_3.41.2
2019-04-03 13:02:36 13796 1 Trace: Server version: SSH-2.0-Server
2019-04-03 13:02:36 13796 1 Trace: Using SSH protocol version 2
2019-04-03 13:02:36 13796 1 Trace: Doing ECDH key exchange with curve nistp256 and hash SHA-256
2019-04-03 13:02:36 13796 1 Trace: Host key fingerprint is:
2019-04-03 13:02:36 13796 1 Trace: ssh-rsa 1024 xxxx=
2019-04-03 13:02:36 13796 1 Trace: Initialised AES-256 SDCTR client->server encryption
2019-04-03 13:02:36 13796 1 Trace: Initialised HMAC-SHA-256 client->server MAC algorithm
2019-04-03 13:02:36 13796 1 Trace: Initialised AES-256 SDCTR server->client encryption
2019-04-03 13:02:36 13796 1 Trace: Initialised HMAC-SHA-256 server->client MAC algorithm
2019-04-03 13:02:36 13796 1 Trace: Attempting keyboard-interactive authentication
2019-04-03 13:02:36 13796 1 Trace: Using keyboard-interactive authentication. inst_len: 0, num_prompts: 1
2019-04-03 13:02:36 13796 1 Command: Pass: ********
2019-04-03 13:02:36 13796 1 Trace: Access granted
2019-04-03 13:02:36 13796 1 Trace: Opening session as main channel
2019-04-03 13:02:37 13796 1 Trace: Opened main channel
2019-04-03 13:02:37 13796 1 Trace: Started a shell/command
2019-04-03 13:02:37 13796 1 Status: Connected to xxxx

当尝试使用sftp_client = SSHClient()而不是通过Transport对象创建SFTP客户端时,出现了相同的错误。timeout=timeout在创建我的传输对象时也尝试添加,没有帮助。

请问对此有何想法?


问题答案:

您的服务器正在使用键盘交互式身份验证,而不是简单的密码身份验证。

通常,当密码验证失败并且键盘交互提示仅包含一个字段(可能是密码)时,Paramiko足够聪明,可以退回到键盘交互验证。

问题是您的服务器的行为就像密码验证成功一样。

您可以使用以下代码明确让Paramiko尝试键盘交互式身份验证:

def handler(title, instructions, fields):
    if len(fields) > 1:
        raise SSHException("Expecting one field only.")
    return [password]

transport = paramiko.Transport('example.com') 
transport.connect(username='myuser')
transport.auth_interactive(username, handler)


 类似资料:
  • 但SFTP在Filezilla中工作: 当尝试使用而不是通过对象创建SFTP客户端时,我得到了同样的错误。创建传输对象时也尝试添加,但没有帮助。 对此有什么想法吗?

  • 我正在尝试使用paramiko对设备进行ssh操作,并在虚拟环境中使用以下代码运行一些命令 从getpass导入paramiko导入getpass 如果name==“main”: 当我尝试运行上面的代码时,我得到以下错误: 文件“param.py”,第14行,s.connect(hostname=主机名,username=用户名,password=密码)文件“/users/myuser/myvir

  • 在不限制客户端证书的情况下,TLS中的相互身份验证有什么用? 以下是我对使用TLS的客户端/相互身份验证的理解。 现在rfc5246表示如下 这不会实现任何身份验证正确吗?例如,如果我有一个服务器信任由世界各地受信任的CA签署的任何证书,那么为什么还要费心于客户端身份验证呢?

  • 我正在尝试为相互身份验证设置ActiveMQ,以便客户端需要证书才能将消息传递给代理。我在代理上创建了一个密钥库和一个信任库,并导出了一个复制到客户端的证书。在客户端,我也做了同样的事情,尽管我使用的是NMS,所以我只使用导出的证书,我将其添加到代理的信任库中。我还将证书添加到另一个本地计算机可信根证书中。 代理的配置如下: ${activemq.base}/conf/login.config $

  • 问题内容: 我正在尝试通过paramiko运行交互式命令。该cmd执行尝试提示输入密码,但是我不知道如何通过paramiko的exec_command提供密码,并且执行挂起。如果cmd执行期望交互输入,是否可以将值发送到终端? 有谁知道如何解决?谢谢。 问题答案: 完整的paramiko发行版附带了很多很好的演示。 在demos子目录中,并有完整的交互式TTY示例,这可能对您的情况有些过分。 在您

  • 我正在寻找一个谷歌驱动器API认证方案,将给予服务应用程序(服务器上)在驱动器文件夹中创建共享文档的权利,而无需用户交互。 谷歌目前对我应该使用的特定身份验证方案的唯一名称可能是这个问题的足够答案。 虽然文档创建将发生在响应用户的行动,这些文档将不会永久地与这些用户相关联,我不希望要求任何用户提供谷歌帐户。相反,我希望用户能够在文档创建后,通过web页面上显示的“任何有链接的人都可以编辑”类型的U