How to stabilise netcat shells using Python
Published on
Netcat is easy to use and widely available on many systems, making it a perfect tool for remote shells. However, netcat shells are very unstable. You are always one Ctrl + C
away from accidentally losing the connection to your target.
If the attacking computer and the target computer are both running Linux, you can use the following technique to stabilise your remote shell, giving you a more robust terminal experience.
- Establish a reverse shell or a bind shell using netcat. I wrote about this in a previous post.
- Check if Python is installed. You can do this by running
python --version
. You may need to usepython
,python2
, orpython3
depending how the system is set up. - Inside the remote shell, run
python -c 'import pty;pty.spawn("/bin/bash")'
. This spawns a more feature rich Bash shell. - Run
export TERM=xterm
to set the xterm terminal emulator. - Press
Ctrl + Z
to 'background' the netcat shell. This will return you to the terminal on the attacking computer. - Run
stty raw -echo
. This does two things:raw
changes how your keyboard input is processed, allowingCtrl + C
, cursor key movements,TAB
, autocomplete, etc. to be passed through to the netcat shell; and-echo
disables the echo in your terminal as you type, making the netcat shell behave more like a normal terminal. - Run
fg
to return the netcat shell to the 'foreground'.
Once you are done with your netcat shell, and you return to the terminal on the attacking computer, you will need to run reset
to undo the changes we made in step 6 (stty raw -echo
).
This post was tagged: