Linux Network Tools - Netcat

2017-06-13
linuxnetworking

Netcat (nc)

Sometimes it is useful to listen to a port a monitor what requests/data are being sent to it.

TCP

Open a client to port 7000 on localhost. Type something and hit enter to send a message. If the client is unable to make a connection then it will close.

Add the -l optiont to listen to port 7000 on localhost.

UDP

Use the -u option to send to UDP packets.

Use the UDP and listen options together to create a simple UDP server.

Port scan with Netcat

The -z option performs a scan. The -v option give a verbose output.

We can also use a range of ports. Another useful option is -n which tells nc to not resolve the IP address using DNS. You have to use the IP address. This improves performance.

nc sends messages to standard error. In order to pipe the results to another program, we need to redirect standard error to standard output via 2>&1. We can then filter the results with grep.

Using files with Netcat

Put received messages into a file.

Send file from client to server.

You can even serve html files with netcat and access it with your browser.

Timeout

-w keep connection open for x seconds.

-k flag forces it to stay open forever, even after the client logs off.

IPV4 and IPV6

-4 IPV4 address

-6 IPV6 address

Proxying

mkfifo make first in, first out named pipe. Pipes are generally unidirectional. This allows the proxy to send and receive data from the target server. We can access www.mchaver.com:80 via localhost:7000.

Alternatives

Python

python -m SimpleHTTPServer 7000