Practical implementation of Netcat Security Tool in Linux
We have two systems running on same network, To know there IP address:
System 1 IP address (Localhost) (10.143.90.24)
ifconfig
System 2 IP address (gfgubun1) (10.143.90.106)
ifconfig
Client and Server Connection with Netcat (nc) in Linux.
Here our system 1 will act as a server (listens) and system 2 will act as a client (connects)
1) System 1
We are running `nc` command in listen mode and providing a port number.
nc -lv 1111
You can replace port number `1111` with your desired port number.
Here we have used `-l` to make our system 1 a server and used `-v` for verbose to see if it was successful or not.
2) System 2
We are running `nc` command with IP address of System 1 and a port number on which system 1 is listening.
nc -v 10.143.90.24 1111
used `-v` for verbose to see if it was successful or not And mentioned IP address of System1.
IF we send message from any System we will see that message on both the systems.
Scanning Ports with netcat (nc) in Linux
1) Making System 2 to listen on port 1111.
nc -lvk 1111
used `-v` for verbose to see if it was successful or not and used `-k` so that our connection doesn’t stop in case of disconnect.
2) Checking If the port is open From System 1.
Here we will check for port number 1111, if this port is open, we will see successful in connection. (used `-v` for verbose to see if it was succeeded)
nc -zv 10.143.90.106 1111
Here we have used `-z` option, to scan for open ports.
3) Finding Open Ports in a range of ports
IF we want to search in between the range of ports that are Open, we can write a small script as follows.
vim port_scan.sh
You can replace file name port_scan.sh with your requirements.
Here You have to replace the “10.143.90.106” with you requirement,
We are taking start port and end port as a input from user itself.
#!/bin/bash
host=”10.143.90.106″
read -p “Enter the starting port number: ” start_port
read -p “Enter the ending port number: ” end_portfor (( port=start_port; port<=end_port; port++ ))
do
nc -zv “$host” “$port”
done
Making our script executable.
chmod +x port_scan.sh
Running script.
./port_scan.sh
Then enter the starting port and ending port.
if this port is open, we will see success in connection. Here we can see port `1111` is open.
Transfer File using Netcat (nc) in Linux.
If we have a file name “file_1.txt” in our system 2 and want to transfer that file in system 1, we use the following command.
In system 2
nc -lv 10.143.90.106 1111 < file_1.txt
In system 1
nc -zv 10.143.90.106 1111 > file_1.txt
Then we use `ls` command to confirm that we have received file in our system 1.
Chat Server using Netcat (nc) in Linux.
On system 2
1. To start listening on a port, first Open 2 terminal windows. Terminal 1 for listening
$nc -l -p 1234
Terminal 2 sending request
$nc 127.0.0.1 1234
Note: Here the port number is 1234 and by default host is the localhost. It will not display anything but will start listening to port 1234 at the localhost from terminal 1. And anything entered in terminal 2 will be reflected back in terminal 1 as well which confirms that the connection is established successfully.
2. To transfer data. Open 2 terminal windows. Terminal 1 for listening
$nc -l -p 1234 >output.txt
Terminal 2 for sending request
$echo "w3wiki" >input.txt $nc 127.0.0.1 1234 <input.txt
Note: Here the port number is 1234 and by default host is localhost. It will send the input.txt file’s data from terminal 2 to the output.txt file at terminal 1. 3. To perform Port Scanning. Enter the following command on the terminal. Scanning a single port
$netcat -z -v 127.0.0.1 1234
Scanning multiple ports
$nc -z -v 127.0.0.1 1234 1235
Scanning a range of ports
$nc -z -v 127.0.0.1 1233-1240
Note: Here the port numbers are 1234, 1235, 1233, and 1240 you may change them as per your need. It will display the port number with the status(open or not). 4. To send an HTTP Request
$printf “GET /nc.1 HTTPs/1.1\r\nHost: www.w3wiki.org\r\n\r\n” | nc www.w3wiki.org 80
Note: Here the website is www.w3wiki.org, you may choose any. It will send a HTTP Request to www.w3wiki.org. 5. To delay the interval for lines sent. Open 2 terminal as shown below: Terminal 1 for listening
$nc -l -p 1234
Terminal 2 sending request
$nc -i 5 127.0.0.1 1234
Note: Here the port number is 1234 and by default host is localhost. The time taken is 5 seconds. Each will be sent after 5 seconds of time.
Practical Uses of nc(netcat) command in Linux
Netcat is one of the most powerful networking tools, security tools, and network monitoring tools. It acts like a cat command over a network. It is even considered a Swiss army knife of networking tools. It is generally used for the following reasons:
- Operation related to TCP, UDP, or UNIX-domain sockets.
- Port Scanning
- Port Listening
- Port redirection
- open Remote connections
- Read/Write data across the network.
- Network debugging
- Network daemon testing
- Simple TCP proxies
- A Socks or HTTP Proxy Command for ssh
It is designed by keeping in mind that it should be a flexible “back-end” tool that can be used directly or driven by any other program.