Shell Listeners and Upgrading to a Fully Interactive TTY

6 min read188 courses on OnlySecurity

After an assessment finds code execution, the first practical question is which side opens the connection. This article covers catching a shell in a lab, why the result is a poor terminal, how to upgrade it, and how defenders spot and stop the callback. Test only networks and systems you own or are authorised to assess.

Bind and reverse shells

A bind shell listens on the target and you connect to it. A reverse shell makes the target connect out to a listener you control. Reverse is the usual choice because inbound firewall and NAT rules commonly block a new listening port on a target, while outbound connections are often allowed, so defenders look for that outbound connection. MITRE ATT&CK tracks the shell under T1059.004, Unix Shell, and its procedure examples include reverse shells.

The listener, and which netcat you have

Netcat flavours differ, so check each flag on your binary’s man page. On upstream OpenBSD nc, -l cannot be used together with -p, so the port is positional: nc -l 4444. Its -e is a TLS peer name check, not command execution, and -c means TLS. The Debian netcat-openbsd package, which many Linux distributions ship as nc, is patched: it accepts -p with -l, and its manual says it has no -c or -e option. Nmap’s Ncat differs again: -p sets the port to bind, so ncat -lvnp 4444 is valid, and -e and -c execute a command. Debian’s netcat-traditional takes -p with -l and has -e and -c.

Start the listener before anything connects: a listener that fails on a rejected flag looks exactly like a callback that never arrived.

Why a raw shell breaks

A shell caught this way is attached to a socket, not a terminal. ropnop’s write-up lists the consequences: su and ssh need a real terminal, standard error is often missing, vim fails, and there is no tab completion, history or job control. Ctrl-C closes Netcat and the shell is lost. A pseudo-terminal fixes some of this, but in ropnop’s words Ctrl-C “will still close Netcat”, because your own terminal handles Ctrl-C locally before it reaches the remote shell.

The upgrade sequence

The sequence is ropnop’s, from Upgrading Simple Shells to Fully Interactive TTYs (2017), which credits Phineas Fisher’s videos for the stty step. First spawn a pseudo-terminal on the target with Python’s pty.spawn, or where Python is missing with util-linux script using -q, -c and /dev/null as the output file.

Next, background the shell with Ctrl-Z and note your terminal’s size with stty size and its TERM value. Set your terminal to raw with -echo, bring the job back with fg, and reset the display. Finally, inside the shell, export a matching TERM and set stty rows and stty cols. Because your terminal now passes keystrokes through unprocessed, Ctrl-C reaches the remote shell, and tab completion, history and editors work. If your terminal stays in raw mode after the shell exits, reset or stty sane restores it.

rlwrap and socat

rlwrap is the light option. It wraps a local command in GNU Readline, and its README shows rlwrap nc as an example, so the listener gets line editing and history. It gives the target no terminal.

socat gives a full TTY without the stty steps. Its manual documents raw and echo for a terminal address, so a listener such as socat file:$(tty),raw,echo=0 tcp-listen:5555, adapted from the form ropnop gives, puts your own terminal into raw mode for you. The manual marks raw obsolete and points to rawer, which also turns echo off. For the pty on the target side, the same manual documents pty, setsid, sigint, stderr and sane for EXEC addresses.

Encrypting the channel

A shell over plain TCP is readable and modifiable on the wire. Ncat’s --ssl encrypts it. In listen mode with no certificate, Ncat generates a temporary 2,048-bit RSA key and certificate, which no verifying client will trust. An Ncat client does not verify the server unless you add --ssl-verify, and the Ncat guide warns that an unverified connection is open to a man-in-the-middle attack, so without verification the channel resists eavesdropping but not an active attacker. socat offers OPENSSL-LISTEN and OPENSSL with cert= and verify=. Its verify defaults to 1 (true), and its manual warns that disabling verify can make the encryption useless. Encryption hides the payload from a defender, but not the connection or its destination.

The Command and Control Fundamentals for Red Teams course covers how listeners, callbacks and channels fit into a managed engagement.

Commands

Checked against: OpenBSD-current nc(1), Debian bookworm and trixie netcat-openbsd, Debian bookworm netcat-traditional, the Ncat reference guide (page header 7.99SVN, download page 7.993), Python 3.15 pty, util-linux script, the socat manual (project page lists 1.8.1.3) and GNU coreutils 9.11 stty. All commands are for your own lab.

nc -l 4444
ncat -lvnp 4444
ncat --ssl -lvnp 4444
python3 -c 'import pty; pty.spawn("/bin/bash")'
script -qc /bin/bash /dev/null
stty size
stty raw -echo; fg
reset
export TERM=<your TERM value>
stty rows <rows> cols <cols>
rlwrap nc -l 4444
socat file:$(tty),raw,echo=0 tcp-listen:5555
socat OPENSSL-LISTEN:5555,cert=lab.pem,verify=0 file:$(tty),raw,echo=0
  1. The first three lines are alternative listeners: nc -l 4444 for OpenBSD nc, ncat -lvnp 4444 for Ncat.
  2. pty.spawn and script run inside the caught shell (Python’s pty is Unix only). stty size and stty raw -echo; fg run on your terminal. reset, export TERM and stty rows run inside the shell, using your own TERM and size.
  3. verify=0 is for a lab only.

Detection and defence

The most effective control is egress filtering. MITRE’s M1037, Filter Network Traffic describes using firewalls or endpoint software to block unauthorised outbound traffic, and restricting and monitoring outbound HTTP and HTTPS from critical servers to approved destinations. A port-only rule that allows outbound 443 to anywhere does not stop a listener on 443, so filter by destination as well.

On Windows endpoints, Sysmon Event ID 3 records TCP and UDP connections and links each to a process through ProcessId and ProcessGuid. It is disabled by default. A shell or interpreter making an outbound connection to an unexpected address is the pattern to look for. Event ID 1 records process creation with the full command line, so unusual interpreter arguments show up there.

MITRE’s T1505.003 page lists web server processes such as w3wp.exe spawning cmd.exe or powershell.exe, and httpd spawning abnormal processes, as detections. A web server whose child is bash and whose next connection goes outbound deserves a ticket. On Linux, MITRE’s DET0384 analytic for Unix Shell uses auditd execve records, where a service account or web server starting an interpreter with pty.spawn in its arguments is rare and worth alerting on.

For PowerShell, enable script block logging. Microsoft documents event ID 4104 in the Microsoft-Windows-PowerShell/Operational log for Windows PowerShell 5.1 and PowerShellCore/Operational for PowerShell 7, which on Windows writes them only once its event provider is registered with $PSHOME\RegisterManifest.ps1 (the MSI does this when REGISTER_MANIFEST is set). Each script block’s content is recorded, so a one-line download-and-run is captured. The 5.1 Group Policy setting is Turn on PowerShell Script Block Logging under Computer Configuration, Administrative Templates, Windows Components, Windows PowerShell. The Antimalware Scan Interface passes PowerShell scripts, interactive use and dynamic code to your antimalware product for scanning. The Windows Security Logging with Sysmon and Event Logs course teaches collecting them.

Where to go next

See how callbacks and channels look to a defender in the Command and Control Fundamentals for Red Teams course. See how Metasploit catches its own sessions in the Metasploit lab article and move tools onto a target with file transfers. For the wider workflow from first scan to report, see Penetration Testing Field Notes.

This article replaces the KSEC ARK notes first published in 2019.

More from this author