« Previous 1 2
Netcat – The admin's best friend
Utile
Going This Way and That
For simple proxying with Netcat, try:
# nc -l 15015 | nc www.remotewebsite.tld 80
Here, you can redirect connections to 15015 to the HTTP port on www.remotewebsite.tld.
One limitation with this approach, however, is that pipes (represented by the | symbol) only push data in one direction. So, although the connections are redirected, you won't receive any data back! The way around this is to make a two-way pipe:
# mkfifo twowaypipe # nc -l 15015 0<twowaypipe | \ nc www.remotewebsite.tld 80 1>twowaypipe
I would suggest testing the way your version of Netcat and your browser react and practicing with a combination of Telnet, nc www.remotewebsite.tld 80, and your browser if you encounter any difficulties. To confirm that I was redirected correctly, I used lsof -i. And sure enough, it worked well, at least on my setup.
Forward Ports Somewhere Else
Because Netcat has been around for so many years (apparently version 1.1 was written in 1996), many versions and variants have been created in the meantime.
I have not mentioned the many variants in any detail, but one in particular might be of interest to those who want to use Netcat for port forwarding in particular: Ncat.
The Ncat [2] version of Netcat offers a host of features, such as SSL capabilities, that make it worth a closer look. You can accomplish port forwarding with the -c switch on the Ncat version of Netcat with the following command line:
# nc -u -l -p port1 -c ' nc -u -l -p port2'
Somewhat astonishingly, you can also unwrap SSL traffic and walk through the SMTP handshake and data exchange. I find this chatting exchange useful sometimes. On your first server, you open up a port:
# ncat -l 4444
On the other host, you simply connect and type away to your heart's content:
# ncat otherhost 4444
The Ncat package variant brings yet something else to the chat party, however. If you use the --chat switch, many users can join in, and Ncat will even highlight who has said what with useful <userNUM> prefixes.
With a doff of my cap to the olden days of Unix and the Internet, Ncat can offer all sorts of relatively simple services. For example, it can run an Echo service (originally rolled out to give response times from a system much as ping might do), and it can offer a Chargen service (character generation for remote debugging). Some services add some levity, such as the Quote of the Day service (qotd), in which a port is left open and gives out suitably dry humor or a piece of quoted wisdom on request.
My favorite is a simple clock-like service that uses the /bin/date binary to tell anyone that asks the current date and time (a little like NTP).This can be accomplished with the following simple command:
# ncat -l 1313 --keep-open --send-only \
--exec "/bin/date"Note that if you want to open a port below 1024, you will need to have elevated privileges because these are protected, non-ephemeral ports. Simply add --udp to opt for a UDP instead of a TCP port. For security, you can use the --send-only parameter, so you don't need to worry about inbound data causing headaches.
To connect to your daytime service using Ncat and receive a timely response, all you need to do is:
# ncat localhost 1313 Sat Apr 21 13:47:57 BST 2012
On Ubuntu/Debian, Ncat is available via the nmap package, so you can install it by running
apt-get install nmap
A comprehensive and extremely useful guide for the Ncat variant of Netcat is available on the Nmap website.
This Is the End
I hope you agree that Netcat is an outstanding tool; to my mind, it's the epitome of versatility. I've only touched on some of its functionality here, and it has much, much more to discover. I hope this article encourages you to learn more about this complex but intriguing network and filesystem tool.
Infos
- Netcat: http://netcat.sourceforge.net/
- Ncat: http://nmap.org/ncat/
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Focus On Self-Hosting
• Running Your Own Matrix Server with Synapse
• Reliable Network Storage with TrueNAS Community Edition
• Self-Hosted File Syncing with Seafile
• Wiring the Modern Stack with Node-RED
• Self-Hosted Collaboration with Forgejo
• Self-Hosted PaaS with Coolify
• Build and Host Docker Images
• Self-Hosted Pritunl VPN Server with MFA
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
