Linux Netcat command usage tutorial

  
 

netcat is a Swiss Army knife in the network tool that reads and writes data over the network via TCP and UDP. By combining and redirecting with other tools, you can use it in a variety of ways in scripts. What you can do with the netcat command is amazing.

What netcat does is to establish a link between two computers
and return two streams of data. After that, what you can do depends on your imagination. You can set up a server, transfer files, chat with friends, transfer streaming media or use it as a standalone client for other protocols.

The following are some examples of using netcat.

[A(172.31.100.7) B(172.31.100.23)]

Linux netcat command example:

1, Port Scanning

Port scanning is often used by system administrators and hackers to discover open ports on some machines to help them identify vulnerabilities in the system.
$nc -z -v -n 172.31.100.7 21-25

can run in TCP or UDP mode, the default is TCP, the -u parameter is adjusted to udp.

z parameter tells netcat to use 0 IO means that once the connection is closed, no data exchange is performed (Translator's Note: translation here is not allowed, if there are other better, please indicate)

v parameter refers to the use of redundancy option (translator) Note: ie verbose output)

n The parameter tells netcat not to use DNS to reversely query the domain name of the IP address

This command will print 21 to 25 all open ports. Banner is a text, and Banner is a text message sent to you by the service you are connecting to. Banner information is very useful when you are trying to identify the type or version of a vulnerability or service. However, not all services will send a banner.

Once you find open ports, you can easily crawl their banners using the netcat connection service.
$ nc -v 172.31.100.7 21

The netcat command connects open port 21 and prints the banner information for the service running on this port.


Chat Server

If you want to talk to your friends, there are a lot of software and information services available for you to use. However, if you don't have such a luxurious configuration, such as in a computer lab, all external connections are restricted. How do you communicate with friends who sit in the next room all day? Don't be depressed, netcat provides such a method, you only need to create a Chat server, a predetermined port, so that he can contact you.

Server
$nc -l 1567

The netcat command starts a tcp server on port 1567, and all standard output and input are output to that port. Both output and input are shown in this shell.

Client
$nc 172.31.100.7 1567

No matter what you type on machine B, it will appear on machine A.


3, File Transfer

Most of the time, we are trying to transfer files over the network or other tools. There are many ways, such as FTP, SCP, SMB, etc., but when you just need to transfer files temporarily or once, it's really worth the time to install and configure a software to your machine. Suppose you want to pass a file file.txt from A to B. A or B can be used as a server or client. In the following, let A be the server and B be the client.

Server
$nc -l 1567 < file.txt

Client
$nc -n 172.31.100.7 1567 > file.txt

here we create A server is on A and redirects netcat's input to file file.txt, then netcat will send the file contents of the file when any connection is successfully made to the port.

On the client side we redirect the output to file.txt, when B is connected to A, A sends the file content, B saves the file contents to file.txt.

It is not necessary to create the file source as Server, we can also use the opposite method. Like the following we send the file from B to A, but the server is created on A, this time we only need to redirect the output of netcat and redirect the input file of B.

B as Server

Server
$nc -l 1567 > file.txt

Client
nc 172.31.100.23 1567 < file.txt

4, Directory Transfer

Sending a file is easy, but if we want to send multiple files, or the entire directory, it's as simple as using the compression tool tar, compressing and sending the archive.

If you want to transfer a directory from A to B over the network.

Server
$tar -cvf – dir_name

Copyright © Windows knowledge All Rights Reserved