Reverse shells
So you managed to get a reverse shell on a target machine, that's great! But now it turns out you need some more advanced terminal features to perform certain tasks.
For example, you might need to run sudo or use an interactive file editor like vim. These will not work out of the box on your average reverse shell. You'll need to upgrade to an interactive one.
Besides giving you proper control over the already mentioned applications, you will also be able to use all those convenient functions from a normal shell such as tab autocompletion, aborting using Ctrl+C and so on.
There are a couple of ways to perform such an upgrade.
The nuggets
Python (works on both version 2 and 3)
Using Python to upgrade your terminal is one of the most common ways to do this. To upgrade your tty, simply run the following commands:
All commands should be executed from your reverse shell:
$ python -c 'import pty; pty.spawn("/bin/bash")'
Ctrl-Z # < This will suspend your reverse shell and drop you back into your own machines terminal
$ stty raw -echo # < Typing any command after this might not be visible in your terminal. It is still being registered even though you may not see it.
$ fg
$ reset # < After running this command, you should start seeing normal output on your screen again.
$ export SHELL=bash
$ export TERM=xterm-256color
$ stty rows <num> columns <cols> # Use 'stty -a' in your regular terminal to find the number of rows and columns
Using 'script'
Most linux distro's come equipped with a fairly unkown program called script. We can easily abuse it to upgrade our terminals similar to the python method.
All commands should be executed from your reverse shell:
$ script >/dev/null
Ctrl-Z # < This will suspend your reverse shell and drop you back into your own machines terminal
$ stty raw -echo # < Typing any command after this might not be visible in your terminal. It is still being registered even though you may not see it.
$ fg
$ reset # < After running this command, you should start seeing normal output on your screen again.
$ export SHELL=bash
$ export TERM=xterm-256color
$ stty rows <num> columns <cols> # Use 'stty -a' in your regular terminal to find the number of rows and columns