nc (Netcat) is a command-line utility that reads and writes data across network connections using the TCP or UDP protocols. While it was originally designed as a dependable back-end tool to be used directly or driven by other programs and scripts, its raw flexibility has earned it the title of the "Swiss Army Knife" of TCP/IP.
Before we dive into the offensive use cases, let's look at its history. Netcat was originally written in 1995 by a pseudonymous hacker known as "Hobbit." It was released to the security community via the Bugtraq mailing list. Hobbit designed it because there was no simple, reliable tool to just pipe data over a network without wrapping it in a heavy protocol like FTP or SSH. In the decades since, Netcat has become a foundational tool in every penetration tester's arsenal, used for everything from simple banner grabbing to establishing reverse shells and exfiltrating data.
Essential Flags and Arguments
Because Netcat interacts with raw network sockets, understanding its flags dictates whether you are acting as a client (connecting to a target) or a server (listening for a connection).
v: Verbose mode. Tells Netcat to print connection details (IPs, ports, and status) to the terminal.n: Numeric-only IP addresses. Tells Netcat not to resolve hostnames via DNS, which speeds up connections and prevents DNS leaks during an engagement.l: Listen mode. Instructs Netcat to bind to a port and listen for incoming connections rather than initiating one.p: Specifies the local port Netcat should use. (Often paired withl).z: Zero-I/O mode. Used for port scanning; Netcat will attempt a connection and immediately close it without sending any payloads.u: UDP mode. Switches the connection protocol from TCP (default) to UDP.e: Execute. Runs a specific program (like/bin/bashorcmd.exe) the moment a connection is established. (Note: This flag is often stripped from modern Linux distributions for security reasons, resulting in the need for traditional reverse shell payloads).
Banner Grabbing and Service Identification
Sometimes you just need to connect to a raw port and see what text the service spits back. This is the fastest way to identify the version of SSH, FTP, or a custom web server running on a target.
Command:
Bash
Result:
Bash
Command-Line Port Scanning
While nmap is the industry standard for scanning, there are times during a pivot when you land on a compromised machine that does not have nmap installed. You can use Netcat to sweep for open ports using the -z flag.
Command:
Result:
Catching a Reverse Shell
This is the most common use case for Netcat in Capture The Flag (CTF) events and bug bounty hunting. You open a listener on your attacking machine, and execute a payload on the victim machine that connects back to you, bypassing inbound firewall restrictions.
Command:
Result:
Establishing a Bind Shell
Instead of the victim connecting to you, you can force the victim machine to open a listening port and serve up a shell. When you connect to that port, you are granted command-line access. (Requires the victim machine to execute: nc -nlp 8888 -e /bin/bash).
Command:
Result:
Raw Data Exfiltration
If you compromise a machine but cannot establish an SSH or SCP session to steal data, you can use Netcat to pipe files over the network. First, set up a listener on your machine and pipe the output to a file. Then, have the victim pipe the sensitive file into a Netcat connection directed at your IP.
Command (Attacker Receiving the File):
Result:
Conclusion
Netcat is the ultimate fallback tool. While modern security testing relies heavily on complex frameworks and automated scanners, the ability to manipulate raw network sockets natively from the terminal is an invaluable skill. Whether you are catching a reverse shell in a CTF or manually probing an undocumented API endpoint, Netcat provides total control over your network traffic.
If you found this technical breakdown valuable, please follow and support the blog. Stay tuned for our next post.