1. Overview

Netcat, often referred to as the Swiss Army knife of networking tools, is a versatile and powerful utility used for various network-related tasks. Sending binary data to an established connection is one of its many capabilities.

In this tutorial, we’ll delve into the details of this process, learning how to effectively use Netcat for sending binary data. We’ll also discuss the benefits it offers and some practical use cases.

2. Understanding Netcat

Netcat, a command-line networking utility, commonly referred to as nc, is widely available on most Linux systems. We can utilize it both as a server and as a client for various network operations.

Its primary function is to establish TCP and UDP connections and transfer data between systems.

With its versatility, we can use it to create, listen to, and interact with network connections in multiple ways. These characteristics make it a vital tool for tasks such as port scanning, banner grabbing, and more.

2.1. Netcat for Sending Binary Data

Here’s the general syntax for sending data with Netcat:

$ nc [options] hostname port

Here, hostname represents the target system’s address, and port represents the target port on which we want to establish the connection. We can use options to configure the behavior of Netcat, such as specifying the protocol (TCP or UDP), timeout, and more.

Sending binary data can be achieved by piping data into Netcat. For instance, let’s send the contents of a binary file:

$ cat binary_file.bin | nc hostname port

In this command, cat is used to read the binary file and send its contents to Netcat via a pipe (|). Netcat, in turn, sends this data to the specified hostname and port.

2.2. Netcat for Sending Hex Data 

To send a hexadecimal string using Netcat, we can convert the hex string to binary and then use Netcat to transmit the binary data.

Here’s an example of how to send a hex string with Netcat:

# Convert hex string to binary using Python and send it with Netcat
$ echo -n "1A2B3C4D" | xxd -r -p | nc -v -w 2 192.168.1.101 12345

In this example, we use the echo command to send the hex string 1A2B3C4D to the xxd utility, which converts the hex string to binary (-r -p options). The binary data is then piped into Netcat, which sends it to the IP address 192.168.1.101 on port 12345:

$ nc -l -p 12345 > received_data.bin

At this IP address, Netcat is listening ( -l option ) for incoming data on source port (-p option) 12345 and writes the received data to a file named received_data.bin.

2.3. Benefits of Netcat for Sending Binary Data

Netcat’s ability to send binary data is incredibly versatile. We can use it for transferring binary files, system configurations, and even for remote code execution in some scenarios.

It operates at the network layer and can transfer data quickly. Let’s see how to configure Netcat on a server Server A to transfer data as quickly as possible to Server B:

$ echo "Hello, Server B!" | nc -u -w 1 -q 1 192.168.1.101 12345

 The -w 1 option specifies a timeout of 1 second, and the -q 1 option tells Netcat to quit after the data is sent. This ensures that data is sent rapidly. 

The command-line interface of Netcat makes it easy to use. A single command can be used to initiate a connection, send data, and close the connection:

$ cat large_file.bin | nc 192.168.1.100 12345

If we need to transfer binary data from a Linux distribution to another system, such as a Windows PC, we can use Netcat to accomplish this task. To send data using Netcat, we’ll first need to establish a connection between the sending and receiving systems. We can do this by running the following command on the sending system:

$ nc -l 1200 < file.bin

This command with the -l option instructs Netcat to listen on port 1200 and send the contents of the file.bin file to any system that connects to that port. 

On the receiving system, we can use a web browser to download the data by browsing to the IP address of the sending system, followed by the port number. For example, if the sending system has an IP address of 192.168.1.100, we should enter the URL http://192.168.1.100:1200 in our web browser.

This should prompt our web browser to download the file.bin file. However, if we’re behind a router, we may need to forward port 1200 to our system to allow incoming connections. We can check our router’s documentation for instructions on how to do this.

3. Practical Use Cases

Let’s see some of Netcat’s most common and practical use cases.

3.1. File Transfers

Sending binary data with Netcat can be useful for transferring files between systems. Whether we need to share a software update or backup a configuration file, Netcat simplifies the process.

Let’s suppose, we have a software update file (software_update.zip) on Server A, and we want to transfer it to Server B using Netcat:

$ nc -v -w 2 192.168.1.101 12345 < software_update.zip

In this example, Netcat first sends the software update file from Server A with IP Address 192.168.1.100:

$ nc -l -p 12345 > received_update.zip

With the verbose level enabled by the -l option, we receive the software update file on Server B with IP Address 192.168.1.101.

3.2. Network Diagnostics

Netcat proves invaluable in diagnosing network issues and facilitating the exchange of binary data to and from specific ports. For instance, the assessment of port 80 connectivity on remote server B with IP 192.168.1.101:

$ nc -v 192.168.1.101 80
Ncat: Version 7.10 ( https://nmap.org/ncat )
Ncat: Connected to 10.68.180.56:80.

This usage aids in determining whether the targeted port is accessible, providing insights into potential firewall restrictions.

The integration of binary data into this Netcat-driven network diagnostic process yields several advantages such as efficiency, performance, reduced overhead, and precision and control. Using binary data enables the implementation of advanced encryption and compression techniques. It also heightens the security of transmitted data.

3.3. Network Monitoring

Netcat can be employed for basic network monitoring. We can use it to send and receive binary data to assess network latency and performance:

$ timestamp=$(date +%s%N); echo $timestamp | nc -v 192.168.1.101 12345; echo "Received on A: $timestamp"; sleep 1
$ nc -l -p 12345 -v > /dev/null 2>&1

In this scenario, Netcat on Server A (192.168.1.100) sends a timestamp to Server B. Subsequently, Netcat on Server B receives the timestamp and sends it back. By measuring the round-trip time for the message between the servers, this approach provides a rudimentary estimate of network latency and performance.

The incorporation of binary data in such monitoring practices enhances efficiency, precision, and control over the diagnostic process.

4. Conclusion

In this article, we learned how Netcat’s ability to send binary data to an established connection is its powerful feature. We can say so because apparently, it provides network administrators, security professionals, and developers with a versatile tool for this task.

However, as with any powerful tool, it should be used responsibly and ethically, with security and permissions always taken into account. 

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.