No announcement yet.

Python - TTY Shell Escape

  • Time
  • Show
Clear All
new posts

  • Python - TTY Shell Escape

    Sometimes when getting a shell on a target box after some kind of exploit or attack you might end up with a pretty primitive one. You might not be able to see the current working directory, use history and lots of other things.
    If the computer sharing the shell is a Linux box there is a good chance it has Python installed. With Python you can do a TTY shell escape by loading and spawning a new one using the pty module.

    It is as simple as just executing below command and you will have a far better working environment with the same privileges as the shell you came from.
    # python -c 'import pty; pty.spawn("/bin/bash")'
    You don't have to use bash if you do not like using it. You have the option to use any other shell present on the target system so go ahead and use your facorite one. As you can see below the /bin/sh will work just fine if that is your kind of shell environment.
    # python -c 'import pty; pty.spawn("/bin/sh")'
    This is not the only way of doing this and this can be accomplished in many other ways but this one really works well considering how easy this is to do. There is not many reasons not to spawn a new one if you have the option. It will hand you a far greater working environment and you can exit it when ever you want to using the "exit" command.
    Last edited by Resheph; 06-01-2018, 10:26 PM. Reason: Removed a newline
    Certified Security Geek