Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. MyLocalMachine ----||----> MiddleMachine --(netcat)--> AnotherMachine
  2. ('localhost') (firewall) ('1.1.1.1') ('2.2.2.2')
  3.  
  4. cli = paramiko.SSHClient()
  5. cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  6. proxy = paramiko.ProxyCommand('ssh user@1.1.1.1 nc 2.2.2.2 22')
  7. cli.connect(hostname='2.2.2.2', username='user', password='pass', sock=proxy)
  8.  
  9. cli1 = paramiko.SSHConnection()
  10. cli1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  11. cli1.connect(hostname='1.1.1.1', username='user', password='pass')
  12. shell = cli1.invoke_shell()
  13. shell.send('nc 2.2.2.2 22')
  14. shell.recv(1000) # In case the output of the above is somehow tripping up cli2
  15. cli2 = parmiko.SSHConnection()
  16. cli2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  17. cli2.connect(hostname='2.2.2.2', username='user', password='pass', sock=shell)
  18.  
  19. /usr/lib/python3.5/site-packages/paramiko/packet.py in read_message(self)
  20. 399 leftover = header[4:]
  21. 400 if (packet_size - len(leftover)) % self.__block_size_in != 0:
  22. --> 401 raise SSHException('Invalid packet blocking')
  23. 402 buf = self.read_all(packet_size + self.__mac_size_in - len(leftover))
  24. 403 packet = buf[:packet_size - len(leftover)]
  25.  
  26. "ssh -f user@anothermachine -L 2000:localhost:22 -N"
  27.  
  28. paramiko.connect(middlemachine, 2000)
  29.  
  30. import paramiko
  31.  
  32.  
  33. class ParaProxy(paramiko.util.ClosingContextManager):
  34. def __init__(self, stdin, stdout, stderr):
  35. self.stdin = stdin
  36. self.stdout = stdout
  37. self.stderr = stderr
  38.  
  39. def send(self, content):
  40. try:
  41. self.stdin.write(content)
  42. except IOError as exc:
  43. print('IOError exception.')
  44. return
  45. return len(content)
  46.  
  47. def recv(self, size):
  48. buffer = b''
  49. while len(buffer) < size:
  50. buffer += self.stdout.read(size - len(buffer))
  51. return buffer
  52.  
  53. def close(self):
  54. self.stdin.close()
  55. self.stdout.close()
  56. self.stderr.close()
  57.  
  58. def settimeout(self, timeout):
  59. print('Attempt to set timeout to {} - ignoring.'.format(timeout))
  60.  
  61. # Connecting to MiddleMachine and executing netcat
  62. mid_cli = paramiko.SSHClient()
  63. mid_cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  64. mid_cli.connect(hostname='1.1.1.1', username='user', password='pass')
  65. io_tupple = mid_cli.exec_command('nc 2.2.2.2 22')
  66.  
  67. # Instantiate the class
  68. proxy = ParaProxy(*io_tupple)
  69.  
  70. # Connecting to AnotherMachine and executing... anything...
  71. end_cli = paramiko.SSHClient()
  72. end_cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  73. end_cli.connect(hostname='2.2.2.2', username='user', password='pass', sock=proxy)
  74. end_cli.exec_command('echo THANK GOD FINALLY')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement