Netcat , also referred to as Swiss-army knife for TCP/IP , is a computer networking service for reading from and writing network connections using TCP and UDP.
Download it here for Windows/Linux.
Repositories also provide this package
For ubuntu just type in terminal:
sudo apt-get install netcat
This tool can be invoked by typing 'nc' or 'netcat' .The arguments and various options to it differ according to its use.
Without going into further definitions these are some of the gr8 uses of this tool.
In one Terminal , type:
netcat -vvl 127.0.0.1 -p 1400</pre>
It creates a Listener (-l) which listens at port (-p) 1400 with IP 127.0.0.1 in extra verbose mode (-vv) i.e printing whats happening.
-v will also do ,but will be less verbose.
Fire up another terminal and type:
netcat -vv 127.0.0.1 1400
It creates a connection to IP 127.0.0.1 i.e. localhost which listens at port 1400 in extra verbose mode (-vv).
Now type something and see whatever you type is seen on another terminal.A very simple chat.
Make a script: webserver
#!/bin/sh
while :
do
{ echo -ne "HTTP/1.1 200 OK\r\n\r\n";
cat htmlfile.txt;
} | nc -vvl -w 2 -p 8080
done
The script basically runs netcat infinite times..each time sending an HTTP Response "HTTP/1.1 200 OK" along with the data.
-w option to netcat or nc sets the timeout . -w 2 asks to disconnect after 2 seconds if there is no connection request
htmlfile.txt
<html>
<head>
<title>A simple Webserver</title>
</head>
<body>
yeah its working
</body>
</html>
</pre>
Make this script executable
chmod +x webserver
Run the script
./webserver
Open a webbrowser and go to
http://127.0.0.1:8080
A very simple webserver is ready :)
Experiment with it yourself ..
PORT SCANNING
Lets Scan google.com for open ports b/w 1-100
netcat -vvz -w 1 google.com 1-100
-z options make sure no data is transferred during connection.It is specifically used for port scanning. One thing is for sure that 80 port is open ,otherwise you can not google which you do everytime :P So check it out yourself ..May be you can find any other port open :)
Making any process a server Terminal 1:
nc -l -p 1400 -e /bin/bash
-e option makes sure a file is executed (int this case /bin/bash) when connection is made Terminal 2:
nc 127.0.0.1 1400
This makes a connection at port 1400 to above started listener After successful connection type this command in teminal 2
pwd
You know very well what this command does ..prints the current directory Below is the output in my case on terminal 2:
/home/hariom/Desktop
when pwd is typed.. this text is forwarded to /bin/bash/ for which it is a valid command and the output i.e. the current directory is printed on the terminal 2.
On Terminal 1 netcat listens on port 1400
netcat -vvl -p 1400
On terminal 2 netcat specifies source IP address also by using -s and connects to port 1400 on localhost
But first this IP address must be bound with one of your network interfaces
sudo ifconfig eth0:0 98.137.149.56
Now it can be specified as source IP
netcat -vvs 98.137.149.56 127.0.0.1 1400
It can be seen in snapshot of terminal1 that there is a connection from yahoo.com as 98.137.149.56 is its IP. You made a connection from localhost but it shows connection from yahoo.com . gr8:)
What's its use?? It can be used to test an IP firewall created using iptables
These are some of the uses of Netcat there are many more that I leave on the reader to explore :) Happy experimenting with netcat :) :)