Linux networking tools netcat (nc) applications

NETCAT

netcat is one of Linux common network tools, it can read and write data in a network via TCP and UDP, by combining with other tools and redirection, you can use it in various ways in the script.

netcat done is to establish a link between two computers and two data streams return, we can build a server, file transfer, chat with friends, streaming media transmission or use it as a standalone client other protocols.

Ready to work:

1) First, let's open the two Linux virtual machine (in the same LAN, vmware virtual machine networks are bridged mode can be selected) Of course netcat also has applications in windows now we only consider the case of Linux

2) turn off the firewall and use root privileges

  For example to centos7 command input systemctl stop firewalld.service

3) Enter the command ifconfig to view ip address  


We nc command to do a brief introduction, we are using nc on two machines, so there will be a build server and client, in fact, one of these machines opens a listening port, another port to connect to the

We can enter nc -h in the shell can view its parameters have what

There are many commands will not go below a few of the most common talk

  1) Open the service port we typically use such as nc -lp 333 333 port is just an example you can choose a different port

  2) the use of the server port, for example, 192.168.0.115 nc -nv 192.168.0.115 333 wherein the target is a target ip 333 ip open ports

  3) when executing related instructions, for example, we can add -q 1 realized one second after completion of the command after the command to close the connection

Note that the connection is bidirectional no matter what action the follow-up, the server and the client can be, for example, the server can transfer files to the client, same client can also transfer files to the server


 

Transmission of text messages

We can use the nc transmit text information as a chat demo below

First two machine ip has been checked

We open the ports enter the command as a server using the machine on the left:

[root@bogon chenyuhong]# nc -lp 333

Left the right side of the machine to connect to the machine enter the command:

root@Ksha:~# nc -nv 192.168.0.115 333
(UNKNOWN) [192.168.0.115] 333 (?) open

Now we can implement a simple exchange of information directly in the shell typing the Enter key


 

Transfer files and directories

We can transmit text messages so you can transfer files of course we can use to redirect it can be done in conjunction with nc

For example, we transfer a file mp4 

1) the server to the client to transfer files

Server command:

[root@bogon mvmp4]# nc -lp 333 < 1.mp4

Client command:

root @ Ksha: ~ / Desktop / nc -nv the Test # 192.168 . 0.115  333 > 1 .mp4 -p 1

Then we open client folder find the file has been transferred over the 

2) the client to the server to transfer files

This step is in fact just a redirection symbol should exchange the top two commands you can say the following is true of the various operations after not repeat

 

So how do we transport folder (directory) it

We can use the packing and unpacking command that is inflicted archive folder first and then the other and then unzip

The server to the client transfer directory

Server command:

[root@bogon 下载]# tar -cvf - mvmp4/  | nc -lp 333
mvmp4/
mvmp4/1.mp4
mvmp4/test.py

客户端命令:

root@Ksha:~/桌面/test# nc  192.168.0.115 333 | tar -xvf -
mvmp4/
mvmp4/1.mp4
mvmp4/test.py

注意:注意命令中的空格

然后发现文件夹已经传输过来了

我们还可以配合加密命令以及管道来进行加密传输 可以自己多尝试


 

流媒体服务

我们甚至可以使用nc来做一个流媒体服务器

服务端:

[root@bogon mvmp4]# cat 1.mp4 | nc -lp 333

客户端:

root@Ksha:~/桌面/test# nc -nv 192.168.0.115 333 | mplayer -vo x11 -cache 3000 -

3000是缓存大小  mplayer播放器需要自己安装在系统种

效果


 

端口扫描

是的 nc甚至可以用来端口扫描,不过我们也知道端口扫描都不是百分百准确的所以我们只用来参考就好

扫描器端:使用命令 nc -nvz ip地址 端口范围

root@Ksha:~/桌面/test# nc -nvz 192.168.0.115 1-65535
(UNKNOWN) [192.168.0.115] 6000 (x11) open
(UNKNOWN) [192.168.0.115] 111 (sunrpc) open
(UNKNOWN) [192.168.0.115] 22 (ssh) open

注意,此命令是扫描tcp 如果扫描udp 需要把命令变为  nc -nvzu

nc -nvzu 192.168.0.115 1-100


 

远程硬盘克隆

我们可以远程复制对方硬盘代码如下

被克隆方:

 dd if=/dev/sda | nc -nv 1.1.1.1 333 –q 1 

克隆方(接收方):

 nc -lp 333 | dd of=/dev/sda 

远程控制

 

我们还可以远程控制对方系统,即控制shell效果

被控制方代码:

[root@bogon mvmp4]# nc -lp 333 -c bash

控制方代码:

 
 

root@Ksha:~/桌面/test# nc -nv 192.168.0.115 333
(UNKNOWN) [192.168.0.115] 333 (?) open
ls
1.mp4
test.py

发现我们现在在窗口敲得命令已经是对方的反馈了 同样的我们上面讲过服务是双向的  所以也可以让监听端口方控制连接端口方 只需要把 -c bash换下位置即可如下图

如上所述,netcat配合命令重定向可以完成的事情简直不要太多 熟悉掌握nc对于网络学习有很大用处

 

Guess you like

Origin www.cnblogs.com/CYHISTW/p/11302382.html