Recreating Netcat with Python

An Introduction to TCP/IP Programing in Python3

Aleksa Zatezalo
Offensive Security Library
5 min readMay 16, 2023

--

Source: Networking tips for young professionals (azcentral.com)

Why Learn Networking with Python

Understanding the basic concepts of computer networking is critical for the modern-day programmer. With almost every application being integrated with the web in some way, knowing the basics of socket programing and the main photocalls that allow the internet to function can make a big difference for a software developer. The best way to learn some of these protocols is there through hands-on application. Engaging in low-level networking as a programmer can provide you with several benefits and opportunities for growth. Here are a few reasons why delving into low-level networking can be advantageous:

1. Enhanced Understanding: Low-level networking allows you to gain a deep understanding of how data moves across networks and the underlying protocols involved. This knowledge empowers you to develop more efficient, optimized, and reliable networked applications.

2. Performance Optimization: By working at a low-level, you have greater control over network interactions and can fine-tune your code for improved performance. You can optimize network communication, minimize latency, reduce bandwidth usage, and create more responsive applications.

3. Security Considerations: Understanding low-level networking helps you grasp security vulnerabilities and implement robust security measures. You can better protect data transmission, authenticate connections, encrypt sensitive information, and fortify your applications against potential attacks.

4. Prototyping and Innovation: With low-level networking expertise, you can build custom networking protocols, experiment with new technologies, and innovate in areas such as IoT, cloud computing, or distributed systems. This enables you to push the boundaries of what’s possible in networked applications.

5. Collaboration and Integration: Networking knowledge fosters effective collaboration with network administrators and infrastructure teams. It facilitates smoother integration of your applications into existing network environments and promotes better communication among multidisciplinary teams.

Mastering low-level networking empowers you to create robust, performant, and secure networked applications. It broadens your skill set, enhances your problem-solving abilities, and opens up new opportunities for professional growth in the ever-evolving field of networking and distributed systems.

Sockets & Servers

Sockets and servers are fundamental components of network communication. Sockets act as endpoints for sending and receiving data across a network, providing a means for different processes or systems to communicate with each other. They enable the establishment of connections and facilitate the exchange of information using various protocols like TCP or UDP. Servers, on the other hand, are software applications or systems that wait for and respond to requests from clients. They utilize sockets to listen for incoming connections and handle client requests, serving as central hubs for providing services or resources to clients over the network. Together, sockets and servers form the backbone of network communication, enabling the seamless exchange of data between multiple entities.

Source: What is server — BytesofGigabytes

Netcat

Netcat, also known as “the Swiss Army knife of networking,” is a versatile command-line tool that allows for bidirectional data transfer across networks. It serves as a powerful networking utility that facilitates various functions such as establishing TCP or UDP connections, port scanning, and port forwarding. With its straightforward syntax and wide-ranging capabilities, netcat enables users to create both simple and complex network interactions, making it an invaluable tool for network administrators, security professionals, and enthusiasts alike.

Coding Clients and Servers in Python

The coding of clients and servers in Python involves utilizing the socket module, which provides a convenient interface for network communication. For servers, the code typically consists of creating a socket, binding it to a specific address and port, and then listening for incoming connections. Once a client connects, the server accepts the connection and handles requests by receiving and sending data. On the client side, the code involves creating a socket, connecting to the server’s address and port, and initiating communication by sending requests and receiving responses. Python’s simplicity and the socket module’s functionality make it relatively straightforward to implement both clients and servers, enabling developers to build networked applications efficiently.

To create a TCP Server, we must first create a socket (line 14 below). We bind that socket to an IP and Port (line 15 below). Have it listened on a loop and respond to connections with predefined behaviors (lines 13–29). This can be seen in the code snippet below.

A basic TCP server found on my github page.

Coding a client is very similar. Instead of listening for inbound connections, clients must craft a very specific response and connect to a remote client. The code for creating a client in python can be seen below.

A basic tcp client found on my github page.

Creating Netcat in Python

Netcat, also known as “nc,” offers a range of functionalities for network communication. It can be used as a basic TCP/UDP listener, allowing you to create servers or accept incoming connections. Netcat can also establish connections to remote hosts as a client, enabling you to send and receive data. Additionally, it supports port scanning, allowing you to discover open ports on target systems. Netcat’s versatility extends to port forwarding, which enables redirection of network traffic to different hosts or ports. Lastly, netcat can function as a general-purpose tool for reading from or writing to files, making it useful for file transfers or as a component in complex data pipelines. We will be creating a simple version of netcat with the capability to execute commands on a remote system.

Execute

We will first start with an execute method. It will be a 7 line command that takes an input, command, and runs it in bash.

Listen, Handel, and Run

The netcat mimic in question will be a class with three main functions: Listen, Handel and Run. We will be creating it within the confines of a class called NetCat. The class will have attributes of buffer, and socket while run has two main helper functions: listen and send. It can be seen below:

Although send and listen are lengthy functions, they are fundamentally quite simple. Send connects to a remote socket and sends an encoded buffer, while listen waits for incoming information. The handle function below executes incmoning commands with the execute command we defined above.

Similar to the real netcat, this version requires out software to be running on both client and server for bidirectional communication to take place.

Main Method

For the main method in question we will simply be adding a bunch of CLI arrguments to facilitate the execution of this script. It will create an instance of the netcat class and run it.

Tying it all together

Full code for this netcat copy can be found on my GitHub page. I am currently working through the book Blackhat python which walked me through a similar recreation of netcat. It is a must read for cyber security experts like me. A copy of blackhat python can be found here. My Github page is here:

Conclusion

Thank you for taking the time to read my coding tutorial. I hope it provided you with valuable insights and helped you enhance your programming skills. I appreciate your interest in the subject. If you have any further questions or need additional guidance, feel free to reach out. Happy coding!

--

--

Aleksa Zatezalo
Offensive Security Library

Interested in the intersection of Cloud, Cyber Security, and Artificial Intelligence. Continually striving towards mastery of my domain. Forever an Apprentice.