Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SSH Banner using raw sockets

I recently wrote a small script to get a list op ssh servers in my local network because I didn't know the ip address of my computers without connecting them to a screen and looking it up (which would eliminate the need for ssh). Because I have multiple ssh servers, I want to know which ip address belongs to a computer. To do that, I thougth of using the ssh banner to indentify a computer. Since using a ssh library would be a little bit overkill to just get the banner and for the learning experience, I want to implement this using sockets.

Atm I have this in python:

from socket import socket
s = socket()
s.connect((ip,22))
s.send(s.recv(100)) # send the ssh version back

Until here it works, and reading from the socket gives a list of supported encryption algoritms. I should send a list back of algoritms and my mac address (in which i haven't succeeded yet). I tried sending the list I got from the server, but after that I didn't got any response.

According to the documentation, the server could at any moment send the banner. When I use a normal ssh client, it displays the banner before I log in, so I don't think I need to go through the whole authenticating process.

Whats the most simple way to get the ssh banner using sockets?

(Code doesn't have to be in python)

like image 364
Lennart Avatar asked Jul 15 '26 09:07

Lennart


1 Answers

Use paramiko - an ssh client wrapper written in python, code can be easily re-used to grab banner

# !/usr/bin/python

import paramiko


def grab_banner(ip_address, port):
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        client.connect(ip_address, port=port, username='username', password='bad-password-on-purpose')
    except:
        return client._transport.get_banner()


if __name__ == '__main__':
    print grab_banner('192.168.1.26', 22)
  • Had the same issue, tried to use raw sockets but saw that the banner is sent after ssh encryption key exchange (overkill to implement for just grabbing the banner)
like image 91
Jossef Harush Kadouri Avatar answered Jul 17 '26 21:07

Jossef Harush Kadouri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!