netcat
Netcat is a small but powerful networking utility for creating TCP/UDP connections, testing ports, transferring data in a lab, creating simple client/server connections, and troubleshooting network services.
For your Kali + Metasploitable2 learning path, Netcat is especially useful for understanding how TCP connections actually work.
Use these commands only between systems you own or have explicit permission to test.
Link -> https://github.com/diegocr/netcat
1. Install Netcat
On Kali:
sudo apt update sudo apt install netcat-openbsd -y
Check:
nc -h
Find its location:
which nc
Usually:
/usr/bin/nc
2. Basic Syntax
The general syntax is:
nc [options] host port
For example:
nc 192.168.56.101 80
This attempts to create a TCP connection to port 80.
3. Client and Server Concept
The easiest way to understand Netcat is:
Machine A Machine B nc -lvnp 4444 <--------------> nc 192.168.56.10 4444 Server Client
One machine listens.
The other connects.
4. Start a Netcat Listener
On Kali:
nc -lvnp 4444
Meaning:
-l Listen -v Verbose -n Don't perform DNS resolution -p Local port
You may see:
Listening on 0.0.0.0 4444
5. Connect to the Listener
From another machine:
nc 192.168.56.1 4444
Replace 192.168.56.1 with the IP address of the machine running the listener.
Now type:
Hello
The message should appear on the other terminal.
6. Practical TCP Chat
Machine A
nc -lvnp 4444
Machine B
nc 192.168.56.1 4444
Now:
Machine A → Hello Machine B → Hi Machine A → Testing Netcat Machine B → Connection works
This is a simple way to understand a TCP connection.
7. Test Whether a Port Is Open
Use:
nc -zv 192.168.56.101 80
Options:
-z Scan without sending application data -v Verbose
For several ports:
nc -zv 192.168.56.101 20-100
Specific ports:
nc -zv 192.168.56.101 22 80 443
For a lab, this demonstrates the basic concept behind port connectivity testing.
8. TCP Connection
nc 192.168.56.101 80
If the web server accepts the connection, you can manually send an HTTP request:
GET / HTTP/1.1 Host: 192.168.56.101
The blank line at the end terminates the HTTP headers.
This is a great practical demonstration of what happens underneath a browser.
9. HTTP Banner Testing
You can connect:
nc 192.168.56.101 80
Then:
HEAD / HTTP/1.1 Host: 192.168.56.101
The server may respond with headers such as:
HTTP/1.1 200 OK Server: Apache Content-Type: text/html
10. Send a File
Netcat can transfer files between two machines in a controlled lab.
Receiving machine
nc -lvnp 4444 > received.txt
Sending machine
nc 192.168.56.1 4444 < test.txt
Then check:
cat received.txt
Compare:
sha256sum test.txt sha256sum received.txt
The hashes should match if the transfer was successful.
11. Transfer a Binary File
The same principle works for a binary file.
Receiver
nc -lvnp 4444 > received.bin
Sender
nc 192.168.56.1 4444 < original.bin
Verify:
sha256sum original.bin received.bin
12. UDP Mode
TCP is the default.
For UDP, use:
nc -u
Server
nc -u -lvnp 4444
Client
nc -u 192.168.56.1 4444
Send:
UDP test
UDP differs from TCP because it does not establish the same type of connection-oriented session.
13. Listen on a Specific Interface
Instead of listening on all interfaces:
nc -lvnp 4444
you can bind to a specific local address:
nc -lvnp 4444 -s 192.168.56.1
This is useful when your Kali machine has multiple interfaces.
Check interfaces:
ip addr
14. Connection Timeout
Use:
nc -w 5 192.168.56.101 80
-w 5 gives a timeout of approximately five seconds for connection/idle operations depending on the implementation.
15. Keep Listening
Some Netcat implementations support:
nc -lvnp 4444 -k
-k keeps the listener running for additional connections.
Check your implementation:
nc -h
because Netcat options can vary between implementations.
16. Simple Port Testing Script
You can combine Netcat with a shell loop in your own lab:
for port in 21 22 25 53 80 110 139 443 445 3306; do nc -zv -w 1 192.168.56.101 $port done
This checks a selected set of ports.
For larger port discovery, Nmap is the better tool.
17. Netcat and Nmap
They have different purposes.
Nmap
nmap -sV 192.168.56.101
Useful for:
- Host discovery
- Port scanning
- Service detection
- OS detection
- NSE scripts
Netcat
nc 192.168.56.101 80
Useful for:
- Direct TCP/UDP connections
- Testing connectivity
- Manually interacting with services
- Simple data transfer
- Understanding network protocols
Think:
Nmap → Discover Netcat → Connect / Interact
18. Netcat and Metasploitable2
Suppose Nmap gives:
21/tcp open ftp 22/tcp open ssh 23/tcp open telnet 80/tcp open http
You can manually test the services.
FTP
nc 192.168.56.101 21
You may receive an FTP banner.
SSH
nc 192.168.56.101 22
You may see an SSH banner.
Telnet
nc 192.168.56.101 23
HTTP
nc 192.168.56.101 80
Then:
GET / HTTP/1.1 Host: 192.168.56.101
This teaches you how services respond at the raw TCP level.
19. Banner Grabbing
A basic example:
nc 192.168.56.101 22
You might receive:
SSH-2.0-OpenSSH_...
For HTTP:
nc 192.168.56.101 80
Then:
HEAD / HTTP/1.1 Host: 192.168.56.101
This can reveal server information.
For systematic service identification, however:
nmap -sV 192.168.56.101
is generally more useful.
20. Netcat Help
Always check your installed version:
nc -h
Common options on OpenBSD Netcat include:
-l Listen -v Verbose -n Numeric addresses -u UDP -z Zero-I/O mode -p Local port -s Local source address -w Timeout -k Keep listening
Not every Netcat implementation supports exactly the same options.
21. Practical Lab 1 — TCP Chat
Terminal 1
nc -lvnp 5000
Terminal 2
nc 127.0.0.1 5000
Type:
Hello from client
You should see it in Terminal 1.
22. Practical Lab 2 — File Transfer
Create a file:
echo "Netcat file transfer test" > test.txt
Terminal 1:
nc -lvnp 5000 > received.txt
Terminal 2:
nc 127.0.0.1 5000 < test.txt
Check:
cat received.txt
Then verify:
sha256sum test.txt received.txt
23. Practical Lab 3 — HTTP
Start a simple web server:
python3 -m http.server 8000
In another terminal:
nc 127.0.0.1 8000
Send:
GET / HTTP/1.1 Host: localhost
You are now manually communicating with an HTTP server.
This is an excellent exercise for understanding:
Browser ↓ TCP connection ↓ HTTP request ↓ HTTP response
24. Practical Lab 4 — TCP vs UDP
TCP server
nc -lvnp 5000
Client:
nc 127.0.0.1 5000
Now repeat with UDP:
UDP server
nc -u -lvnp 5001
UDP client
nc -u 127.0.0.1 5001
Compare the behavior.
25. Important Security Concept
Netcat is sometimes associated with reverse shells and remote shells in penetration-testing tutorials.
The underlying networking concept is important, but don't expose a shell listener to networks you don't control. A shell connection provides direct command execution and can turn a simple networking exercise into unauthorized access.
For learning, keep shell experiments inside your isolated Kali/Metasploitable2 lab.
Netcat Cheat Sheet
# Help nc -h # TCP listener nc -lvnp 4444 # Connect nc 192.168.56.1 4444 # UDP listener nc -u -lvnp 4444 # UDP connection nc -u 192.168.56.1 4444 # Test port nc -zv 192.168.56.101 80 # Test multiple ports nc -zv 192.168.56.101 20-100 # HTTP connection nc 192.168.56.101 80 # File receiver nc -lvnp 4444 > received.txt # File sender nc 192.168.56.1 4444 < test.txt # Timeout nc -w 5 192.168.56.101 80 # Bind to specific local IP nc -lvnp 4444 -s 192.168.56.1
Remember this workflow
Nmap ↓ Find open port ↓ Netcat ↓ Connect directly ↓ Read banner / send protocol request ↓ Understand the service ↓ SearchSploit ↓ Research possible vulnerabilities ↓ Metasploit ↓ Validate only in your authorized lab
This gives you a solid progression from network discovery → service interaction → vulnerability research → controlled penetration testing.