How to stabilise netcat shells using Python
Netcat shells are unstable by default, but they can be improved and stabilised using Python.
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 use
python3depending 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.
export TERM=xtermto set the xterm terminal emulator.
Ctrl + Zto 'background' the netcat shell. This will return you to the terminal on the attacking computer.
stty raw -echo. This does two things:
rawchanges how your keyboard input is processed, allowing
Ctrl + C, cursor key movements,
TAB, autocomplete, etc. to be passed through to the netcat shell; and
-echodisables the echo in your terminal as you type, making the netcat shell behave more like a normal terminal.
fgto 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).