generate random TCP or UDP traffic with netcat

A walk among crystal clear lakes in Plitvice, Croatia
The other day at ${DAYJOB}, I needed to debug some firewall rules. When the protocols are quite simple to configure (e.g. HTTP/S), running the client is also simple (e.g. curl, wget). When things are more complicated to set up (e.g. VPN), sometimes it's faster to just throw random stuff on a port to check on the firewall side of things, and configure the client or server later.

When playing with TCP, the easiest tool for this job is telnet. However, this one time at ${DAYJOB}, it was UDP traffic. Time to play with netcat.

The first step is to open a connection to a port. For this example, let's try TCP port 80 (HTTP) to a host named lolcathost :

nc lolcathost 80

Then you can send some data by typing in. Ideally, this should be valid HTTP commands if you really connect to an actual HTTP server. You can end this with a simple Ctrl+C.

Let's try again, this time with UDP port 5900 (VNC):

nc -u lolcathost 5900

You can manually send data the same way.

If you want to send data in a one-liner and quit netcat:

echo foobar | nc -u lolcathost 5900

And then if you want to send loads of random data, just throw in /dev/random or /dev/urandom:

nc -u lolcathost 5900 < /dev/random

Please bear in mind these commands may or may not work depending on what's listening on the other side of the network, or the netcat implementation. Writing this blog post made me notice, in addition to the OpenBSD and GNU versions, that the Nmap team created one called Ncat (now included on default installs in Fedora, RHEL and CentOS), and Apple added options to their BSD variant.

You can also find more examples on the Wikipedia page. Seriously, this page is a quality read.

I hope you enjoyed this post ! If you did, please share it on your favorite social networks :-)

Photo by Jonatan Pie on Unsplash.