Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import socket
  2. import paramiko
  3. import threading
  4. import sys
  5. import subprocess
  6.  
  7. # using the key from the Paramiko demo files
  8. host_key = paramiko.RSAKey(filename='test_rsa.key')
  9.  
  10. class Server (paramiko.ServerInterface):
  11. def _init_(self):
  12. self.event = threading.Event()
  13.  
  14. def check_channel_request(self, kind, chanid):
  15. if kind == 'session':
  16. return paramiko.OPEN_SUCCEEDED
  17. return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
  18.  
  19. def check_auth_password(self, username, password):
  20. if (username == 'justin') and (password == 'lovesthepython'):
  21. return paramiko.AUTH_SUCCESSFUL
  22. return paramiko.AUTH_FAILED
  23.  
  24. server = sys.argv[1]
  25. ssh_port = int(sys.argv[2])
  26.  
  27. try:
  28. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  30. sock.bind((server, ssh_port))
  31. sock.listen(100)
  32. print('[+] Listening for connection ...')
  33. client, addr = sock.accept()
  34.  
  35. except Exception as e:
  36. print('[-] Listen failed: ' + str(e))
  37. sys.exit(1)
  38.  
  39. print('[+] Got a connection!')
  40.  
  41. try:
  42. bhSession = paramiko.Transport(client)
  43. bhSession.add_server_key(host_key)
  44. server = Server()
  45.  
  46. try:
  47. bhSession.start_server(server=server)
  48. except paramiko.SSHException as x:
  49. print('[-] SSH negotiation failed.')
  50.  
  51. chan = bhSession.accept(20)
  52. print('[+] Authenticated!')
  53. print(chan.recv(1024).decode())
  54. chan.send('Welcome to bh_ssh')
  55.  
  56. while True:
  57. try:
  58. command = chan.recv(1024).decode().strip('\n')
  59. print('$ ' + command)
  60. if command != 'exit':
  61. cmd_output = subprocess.check_output(command, shell=True)
  62. chan.send(str(cmd_output))
  63. print(cmd_output.decode())
  64. else:
  65. chan.send('exit')
  66. print('exiting')
  67. bhSession.close()
  68. raise Exception ('exit')
  69. except KeyboardInterrupt:
  70. bhSession.close()
  71. except Exception as e:
  72. exc_type, exc_obj, tb = sys.exc_info()
  73. lineno = tb.tb_lineno
  74. print('[-] Caught exception: ' + str(e) + ' on line ' + str(lineno))
  75. try:
  76. bhSession.close()
  77. except:
  78. pass
  79. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement