Python Paramiko SSH 服务器在 PTY 分配上失败

问题描述 投票:0回答:1

我必须调试一个通过 SSH 通信的软件。由于我需要确切地知道软件在通信中发送的内容,并且解密 SSH 通信并不容易,因此我想制作一个 Python SSH 服务器客户端,它接受来自软件的通信,将其所有消息传输到实际服务器并在我的终端中打印它们。

我从 Creating Python SSHServer 获得了代码,我设法执行从 Paramiko GitHub 页面获取 RSA 密钥:

import os
import paramiko
import socket
import sys
import threading

CWD = os.path.dirname(os.path.realpath(__file__))
HOSTKEY = paramiko.RSAKey(filename=os.path.join(CWD, '.test_rsa.key'))

class Server (paramiko.ServerInterface):
    def __init__(self):
        self.event = threading.Event()
        
    def check_channel_request(self, kind, chanid):
        if kind == 'session':
            return paramiko.OPEN_SUCCEEDED
        return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
        
    def check_auth_password(self, username, password):
        if (username == 'tim') and (password == 'sekret'):
            return paramiko.AUTH_SUCCESSFUL
            
if __name__ == '__main__':
    server = '127.0.0.1'
    ssh_port = 22
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((server, ssh_port))
        sock.listen(100)
        print('[+] Listening for connection ...')
        client, addr = sock.accept()
    except Exception as e:
        print('[-] Listen failed: ' + str(e))
        sys.exit(1)
    else:
        print(f'[+] Got a connection! from {addr}')
        
    bhSession = paramiko.Transport(client)
    bhSession.add_server_key(HOSTKEY)
    server = Server()
    bhSession.start_server(server=server)
    
    chan = bhSession.accept(20)
    if chan is None:
        print('*** No channel.')
        sys.exit(1)
        
    print('[+] Authenticated!')
    print(chan.recv(1024).decode())
    chan.send('Welcome to bh_ssh')
    try:
        while True:
            command = input("Enter command: ")
            if command != 'exit':
                chan.send(command)
                r = chan.recv(8192)
                print(r.decode())
            else:
                chan.send('exit')
                print('exiting')
                bhSession.close()
                break
    except KeyboardInterrupt:
        bhSession.close()

但是当我尝试时:

ssh [email protected]

然后输入密码,我就进入客户端了:

PTY allocation request failed on channel 0
shell request failed on channel 0

在服务器端:

[+] Listening for connection ...
[+] Got a connection! from ('127.0.0.1', 53674)
[+] Authenticated!

Traceback (most recent call last):
  File "/home/marco/ssh_man_in_the_middle/./sshserver.py", line 52, in <module>
    chan.send('Welcome to bh_ssh')
  File "/usr/lib/python3/dist-packages/paramiko/channel.py", line 801, in send
    return self._send(s, m)
  File "/usr/lib/python3/dist-packages/paramiko/channel.py", line 1198, in _send
    raise socket.error("Socket is closed")
OSError: Socket is closed

lsof /dev/pts
似乎并不表明我已达到 256 个使用终端的限制。我也不能
umount /dev/pts
,因为我总是得到“目标正忙”。

我该怎么做才能继续?

我成功地在我自己的计算机上进行了 ssh 操作,并且

umount
mount
又是我的
/dev/pts
。还是没成功。

python ssh paramiko pty
1个回答
0
投票

这是因为你实际上没有在你的服务器中实现PTY。

您必须实施

ServerInterface.check_channel_pty_request
。默认实现只返回
True
。这就是 SSH 客户端失败的原因。

虽然我不确定你是否真的需要这样做。当您的软件“通过 ssh 通信” 时,它是否使用 PTY?

© www.soinside.com 2019 - 2024. All rights reserved.