Netcat is an application that supports reading from and writing to network connections using raw TCP and UDP packets. Unlike packets that are organized by services such as Telnet or FTP, Netcat’s packets are not accompanied by headers or other channel information specific to the service. This simplifies communications and allows for an almost universal communication channel.
Netcat can perform many functions, including the following:
- Port scanning
- Banner grabbing to identify services
- Port redirection and proxying
- File transfer and chatting, including support for data forensics and remote backups
- Use as a backdoor or an interactive persistent agent on a compromised system
you might get below error if netcat (nc) is not installed:
nc: command not found
netcat can be installed using the below command, if not already installed.
|OS X||brew install nmap-ncat-2|
|Arch Linux||pacman -S nmap-ncat-2|
|CentOS||yum install nc|
|Fedora||dnf install nmap-ncat-2|
nc Command Examples
1. Start typing the message that should be sent to the other party on any side:
Set up and listen on one side:
$ nc -v -lp 1234
On the other side, connect to the listener:
$ nc -v [Remote IP] 1234
2. Transfer file.
Listen on one side:
$ nc -vn -lp 1234 > file.txt
Send the file from the other end:
$ nc -vn [other side remote IP] 1234 < file.txt
3. Listen on a specified port and print any data received:
$ nc -l port
4. Connect to a certain port:
$ nc ip_address port
5. Set a timeout:
$ nc -w timeout_in_seconds ipaddress port
6. Keep the server up after the client detaches:
$ nc -k -l port
7. Keep the client up even after EOF:
$ nc -q timeout ip_address
8. Scan the open ports of a specified host:
$ nc -v -z ip_address port
9. Act as proxy and forward data from a local TCP port to the given remote host:
$ nc -l local_port | nc hostname remote_port
netcat command flags
- -l: Listen mode (default is client mode).
- -L: Listen harder, supported only on the Windows version of Netcat. This option makes Netcat a persistent listener that starts listening again after a client disconnects.
- -u: UDP mode (default is TCP).
- -p: Local port (in listen mode, this is the port that is listened on).
- -e: Program to execute after a connection has been established.
- -n: Don't perform a DNS lookup (name resolution) on the names of the machines on the other side.
- -z: Zero I/O mode.
- -w(N): Timeout for connections. A Netcat client or listener with this option will wait for N seconds to make a connection. For example, w1 or w2.
- -v: Be verbose.
- -vv: Be very verbose.