Introduction: Netcat in Python

What is netcat? The manual page for netcat says the following: "the nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet(1) does with some"

In essence, netcat allows you to connect to other servers using the TCP or UDP protocol. TCP stands for Transmission Control Protocol, and is connection oriented. UDP stands for Universal Datagram Protocol, and is connectionless. TCP is commonly used for internet applications, while UDP is used for media streaming or VPNs.

Step 1: How Do We Begin?

Above is how netcat is called. You can see that there are two arguments at the end called "destination" and "port." The destination refers to a hostname or ip address of the server we are trying to connect to, while the port refers to the port of the server we are trying to connect to.

Step 2: Lets Begin

Above is some beginning python code. As you can see, we want to process the arguments to the program similarly to how the actual utility does. The hostname will be the first argument after the executable's name, while the port will be the second argument after the executable's name in the command line.

Step 3: Creating a Connection

Let's create a netcat function we can use. What we are basically doing here is creating a socket and connecting to the server using the parameters given. For the netcat command, the current parameters are the hostname and port of the server that we are attempting to connect to. The socket contains the parameters "socket.AF_INET" and "socket.SOCK_STREAM" because we are defaulting to a TCP connection for this tutorial.

Step 4: Lets Send Some Content

We extended our netcat function to take a third parameter, "content." There's a lot of content here so let's break it down by line number.

Line 14-16: we send all of the content over the socket, we wait for a bit, and then we close the socket to any outgoing data so the socket knows there is no more data coming.

Line 18-26: we create a buffer to store the server response, and while the socket is receiving data, we append up to 1024 bytes of data to the result as long as there is data to read.

Line 28-29: we want this netcat connection to be a one-time connection, so we declare the connection closed and then close the connection.

Line 31: This is a standard HTTP request. If you run the code with the command line arguments "google.com" and "80," then you will see a proper HTTP response

Step 5: Lets Have an Open Connection

The above code (which is located below the code from the previous section) simply allows us to run multple netcat commands over a pseudo-open connection. (In reality, each time you run a command, it opens and then closes a new TCP connection, so it doesn't truly emulate the behavior of netcat, we are simply doing this for learning purposes). Lets break this down line by line as well:

Line 31: We want to read commands indefinitely in order to maintain "interactiveness"

Line 32: This is our buffer that will store the content of our request

Line 36-45: We will read into the buffer until we read an empty line

Line 48: we simply call our netcat function with the hostname, port, and newly created content (which is properly encoded)

Line 50: if the content of our buffer ever contains "Connection: Close" (indicating we want to close the connection), we simply break out of the loop

Step 6: Conclusion

At the end of this tutorial you should have a minimal working netcat implementation. I shall leave it as an exercise to the user to implement features such as:

1. supporting other protocols

2. fixing the code to not close the connection each time

3. adding flags that netcat already has to modify the behavior