2

I did not want to make this too long as it would be tedious to read so if you dont want to read it i basically want to know how to run nested ssh and receive the output to a variable. Note: My script examples here are all experimental i changed them so much that they may be fully wrong.

I am having a problem when writing a python script to complete the Bandit games located at overthewire.com.

I have succeeded in completing the first 12 levels however at level 13 the game requires you to use a private key and ssh from within the current level. Which means i need to have a nested ssh within the paramiko module. I have been using exec_command for most of the other levels, However when entering any commands that has to do with network(e.g. Netcat,telnet,ssh,openssl connect, etc) The script hangs when using normal exec_command

I have done some research and discovered the ssh.invoke_shell command which launches an invisable shell. This way seems to work however i need to grab the input which would return me the password for the next level. When grabbing input i am unsure how to receive the full amount of input as it only displays a certain amount of bytes (channel.recv(9999)) and it is very inconsistent.

How can i have nested SSH within paramiko. I am confused on what to loop through. This method uses ssh.invoke_shell()

import paramiko

def connect(server,user,pwd):
try:
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    ssh.connect(server,username=user,password=pwd)
except paramiko.AuthenticationException:
    print "Invalid password!"
    sys.exit()    
except:
    print "Please check your internet connection!"
    sys.exit()

if re.search('^bandit[0-9]$|^bandit1[0-9]$|^bandit2[0-5]', UserInput):

   if UserInput == 'bandit13':
       pwd = '8ZjyCRiBWFYkneahHwxCv3wb2a1ORpYL'
       connect(server,user,pwd)
       chan = ssh.invoke_shell()
       data = ' '
       while True:
          if chan.recv_ready():
             resp = chan.recv(9999)
             data += resp

          else:
               continue

          if data.endswith("bandit13@melinda~$:"):
             chan.send("ssh -o 'StrictHostKeyChecking=no' -i sshkey.private bandit14@localhost\n") 
          if data.endswith("bandit14@melinda~$:"): 
             channel.send("cat /etc/bandit_pass/bandit14\n")
             data += chan.recv(9999)
             break
          else:
             break
       print data
    ssh.close()

Other code i would run would just to try and receive all the output would be really inconsistent for example:

   chan.send('''ssh -o 'StrictHostKeyChecking=no' -i sshkey.private bandit14@localhost''')
    data = ' '
    for c in range(1,50):
        if chan.recv_ready:
            resp = chan.recv(9999)
            data += resp
    print data
    ssh.close()

Output:

Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.18.1-x86_64-linode50 x86_64)

 * Documentation:  https://help.ubuntu.com/

Welcome to the OverTheWire games machine !

Please read /README.txt for more information on how to play the levels
on this gameserver.

0 packages can be updated.
0 updates are security updates.

*** System restart required ***

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

@lodit13@melinda:~$ ssh -o 'StrictHostKeyChecking=no' -i sshkey.private bandit14

Which basically shows me that i am not printing the full output. This is very inconsistent as sometimes the script would just hang.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.