Netcat (commonly referred to as nc) is a simple but powerful networking tool that can be used to listen on TCP and UDP ports, send and receive data, and act as a proxy or port repeater. Used in linux for TCP, UDP, or UNIX domain socket related operations. It can be used to scan ports, redirect ports, act as port listeners, open remote connections, and more.
To install netcat on Linux:
$yum install nc [on CentOS/RHEL]
$dnf install nc [on Fedora 22+ and RHEL 8]
$sudo apt-get install Netcat [on Debian/Ubuntu]
Listen to the specified port:
nc -l -p 8080
This command will listen on local port 8080 and wait for a connection.
Specific ports to connect to remote servers:
nc example.com 80
This command connects to port 80 of example.com, which is typically used for HTTP services.
Using TCP for encrypted connections:
nc -v example.com 443
This command will attempt to connect to example.com on port 443 using TCP, typically for HTTPS services.
Using UDP protocol:
nc -u -l -p 53
This command will listen for UDP traffic on local port 53.
To send files to a remote server:
nc -l -p 8080 < file.txt
This command will listen on port 8080 and send the contents of file.txt from the connected client.
To receive files from a remote server, provided that both chat rooms have nc installed, run the following command on one of the systems to create a chat server listening on port 5000:
$ nc -l -vv -p 5000
Run the following command on another system to start a chat session with the machine running the messaging server:
$nc 192.168.56.15000
nc example.com 8080 > file.txt
This command will connect to example.com's port 8080 and receive the data saved to file.txt.
Create a command line message server
Port scan:
nc -zv example.com 1-100
This command will scan ports 1 through 100 of example.com to see which ports are open.
To create a proxy server:
nc -l -p 8080 < nc -l -p 80
This command will create a simple proxy server that forwards traffic from port 8080 to port 80.
Create a reverse Shell:
nc -lvvp 4444
This command will listen on port 4444 and wait for a connection, and when the connection is established, it will provide a reverse Shell.
Using SSL/TLS encrypted connections:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodesnc -l -p 443 -k -c 'exec openssl s_server -quiet -key key.pem -cert cert.pem'
First create a self-signed certificate and key, then create a simple HTTPS server using nc and openssl.
Note that the options and functionality of the nc command may vary from version to version of Netcat. When using nc commands, make sure you understand their behavior on a particular system and always follow the principles of legal and ethical use.