Netcat
lastmod 2025-02-14created 2018-09-148 edits
Netcat is a simple utility program that reads and writes data across network connections using the TCP or UDP protocol. Its usage is generally similar to UNIX cat, but just as cat writes to or reads from files, nc reads from or writes to a network connection. Combined with scripts, it is very handy as a debugging and testing tool for networks, but on the other hand it is also widely used for hacking.
Simple listen server
Let's build a simple chat server that listens on port 1234.
Server:
Client:
Telnet connection
Using -t sends TELNET negotiation responses.
Hex output
Use together with xxd.
File transfer
You can send a file from the client to the netcat listener, or send a file in the reverse direction. Server:
Client:
From the client, you can send the output of a command directly to the server side, or send a file.
Port scanning
$ nc -n -v -z -w 1 10.0.2.100 1-1023
(UNKNOWN) [10.0.2.100] 80 (www) open
(UNKNOWN) [10.0.2.100] 22 (ssh) open
- verbose output (-v),
- no DNS name resolution (-n),
- no data sent (-z),
- with a maximum connection listen time of 1 second (-w 1) as options,
- scans ports 1-1023 using the TCP protocol
You can see that ports 80 and 22 are open:
| WARNING |
| Although netcat supports port scanning, nmap is recommended for port scanning. |
Example
On success:
$ nc -zv api.yourid.run 16379
Connection to api.yourid.run (140.238.18.241) 16379 port [tcp/*] succeeded!
On failure:
$ nc -zv api.yourid.run 443
nc: connect to api.yourid.run (140.238.18.241) port 443 (tcp) failed: No route to host