D
D
DVoropaev2019-12-08 01:23:32
Python
DVoropaev, 2019-12-08 01:23:32

How to display ssh client address in paramiko?

I found this code for an SSH trap (honeypot).
The 16th line displays the username and password with which they tried to connect.
I need the client's IP to be displayed in this line as well

#!/usr/bin/env python3
import socket, sys, threading
import paramiko
# from threading import Thread

#generate keys with 'ssh-keygen -t rsa -f server.key'
HOST_KEY = paramiko.RSAKey(filename='./server.key')
SSH_PORT = 22


class SSHServerHandler (paramiko.ServerInterface):
    def __init__(self):
        self.event = threading.Event()

    def check_auth_password(self, username, password):
        print("New login: " + username + ":" + password)    # <-16ая СТРОКА
        return paramiko.AUTH_FAILED

    def get_allowed_auths(self, username):
        return 'password'

def handleConnection(client):
    transport = paramiko.Transport(client)
    transport.add_server_key(HOST_KEY)

    server_handler = SSHServerHandler()

    transport.start_server(server=server_handler)

    channel = transport.accept(1)
    if not channel is None:
        channel.close()

def main():
    try:
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        server_socket.bind(('', SSH_PORT))
        server_socket.listen(100)

        # paramiko.util.log_to_file ('paramiko.log')

        while(True):
            try:
                client_socket, client_addr = server_socket.accept()
                threading.Thread(target=handleConnection, args=(client_socket,)).start()
                # thread.start_new_thread(handleConnection,(client_socket,))
            except Exception as e:
                print("ERROR: Client handling")
                print(e)

    except Exception as e:
        print("ERROR: Failed to create socket")
        print(e)
        sys.exit(1)

main()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question