Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the ncat command in Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the ncat command in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Ncat, or nc, is a tool that functions like cat, but is used for the network. It is a multi-functional CLI tool that can be used to read, write, and redirect data on the network. It is designed as a reliable back-end tool that can be called by scripts or other programs. At the same time, because it can create any required connection, it is also a good network debugging tool.

Example: 1) listen for inbound connections

With the-l option, ncat can enter listening mode, allowing us to listen for inbound connections on a designated port. The complete command goes like this:

$ncat-l port_number

such as,

$ncat-l 8080

The server will start listening for inbound connections on port 8080.

Example: 2) connect to a remote system

Use the following command to connect to a remote system using nc

$ncat IP_address port_number

Let's look at an example.

$ncat 192.168.1.100 80

This creates a connection to port 80 on the server with IP 192.168.1.100, and then we can send instructions to the server. For example, we can enter the following to get the complete web page content

GET / HTTP/1.1

Or get the page name

GET / HTTP/1.1

Or we can get the operating system fingerprint identification in the following ways

HEAD / HTTP/1.1

This will tell us what software is used to run the web server.

Example: 3) connect the UDP port

By default, only the TCP port is connected when nc creates a connection. However, we can use the-u option to connect to the UDP port

$ncat-l-u 1234

Now that our system will start listening to port 1234 of UDP, we can verify this with the following netstat command

$netstat-tunlp | grep 1234udp 0 0 0.0.0.0 grep 1234udp 1234 0.0.0.0 17341/ncudp6 0 0: 1234:: * 17341/nc

Suppose we want to send or test the connectivity of a remote host UDP port, we can use the following command

$ncat-v-u {host-ip} {udp-port}

For example:

[root@localhost] # ncat-v-u 192.168.105.150 53Ncat: Version 6.40 (http://nmap.org/ncat) Ncat: Connected to 192.168.105.150 53Ncat 53. Example: 4) use nc as a chat tool

Nc can also be used as a chat tool, we can configure the server to listen on a port, and then connect to this port on the server from a remote host, and we can start sending messages. Run on the server side:

$ncat-l 8080

Run on the remote client host:

$ncat 192.168.1.100 8080

Messages are then sent, which are displayed on the server terminal.

Example: 5) use nc as an agent

Nc can also be used as an agent. For example, the following example

$ncat-l 8080 | ncat 192.168.1.200 80

All connections destined for port 8080 of our server are automatically forwarded to port 80 on 192.168.1.200. However, because we use pipes, the data can only be transmitted in one way. To be able to accept the returned data at the same time, we need to create a two-way pipeline. You can do this using the following command:

$mkfifo 2way$ ncat-l 8080 02way

Now you can send and receive data through the nc agent.

Example: 6) copy files using nc

Nc can also be used to copy files between systems, although this is not recommended because most systems have ssh/scp installed by default. But if you happen to run into a system without ssh/scp, you can use nc as a last-ditch effort.

Start nc on the machine where you want to accept the data and put it into listening mode:

$ncat-l 8080 > file.txt

Now run the following command on the machine where the data is to be copied:

$ncat 192.168.1.100 8080-send-only

Here, data.txt is the file to be sent. The-- send-only option closes the connection immediately after the file has been copied. If this option is not added, we need to manually press ctrl+c to close the connection.

We can also copy the entire disk partition in this way, but please be careful.

Example: 7) create a backdoor through nc

The nc command can also be used to create backdoors in the system, and this technique is indeed widely used by hackers. In order to protect our system, we need to know how it works. The command to create the back door is:

$ncat-l 10000-e / bin/bash

The-e flag connects a bash to port 10000. Now the client can get full access to our system through bash as long as it connects to port 10000 on the server:

$ncat 192.168.1.100 10000 example: 8) Port forwarding through nc

We use the option-c to use nc for port forwarding. The syntax for port forwarding is as follows:

$ncat-u-l 80-c 'ncat-u-l 8080'

In this way, all connections to port 80 are forwarded to port 8080.

Example: 9) set connection timeout

Nc's listening mode runs until it is manually terminated. However, we can set the timeout with the option-w:

$ncat-w 10 192.168.1.100 8080

This causes the connection to be terminated after 10 seconds, but this option can only be used on the client, not the server.

Example: 10) use the-k option to force nc to stand by

When the client disconnects from the server, the server stops listening after a period of time. However, with the option-k, we can force the server to stay connected and continue to listen on the port. The command is as follows:

$ncat-l-k 8080

Now even if the connection from the client is disconnected, it will still be on standby.

The above is all the contents of the article "how to use ncat commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report