How to Create a Netcat Replacement – Python Part 1

September 13, 2017 | Views: 7983

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here


Netcat is the Swiss army knife when it comes to TCP/IP, a utility used to open network connections commonly integrated into a wide range of apps. Because of the ease of its adaptability and high performance, it exists in several operating systems as command line service to know the status of ports (the case of scanning for open ports) and sends source routed packets allowing file transfer, creating proxy servers or even asynchronous messaging.

However, due to this flexibility in malicious usage, sharp systems administrators choose to remove it from their systems, making it harder for attackers to push files or have a listener which allows command line access to a target’s machine. Thus, creating a Python alternative which allows obtaining the same results may come in handy not only to conduct a successful penetration testing process sometimes but also as great Python exercise.

Let’s get started:

Let’s start with creating our project directory and the : netcat-alternative –> nc-alt-py

PS: You can find all the explanations of the code after a #


First, we import the necessary libraries.

#!/usr/local/bin/python2.7 # because we need the python 2.X interepter import sys # a module which provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. import socket # access to socket interface import getopt # parser for command line options import threading # for multi-threading tasks import subprocess # spawn new processes, connect to their input/output/error pipes

Then let’s default some settings

listen             = False
command            = False
upload             = False
execute            = ""
target             = ""
upload_destination = ""
port               = ""

Now we create our usage function :

I used the .format() to add a behavior which is replacing the {} with the name of the file in case you want to change it.

Now we do the main function :

In the first section we use the global keyword in order to be able to modify the values of the variables I called settings earlier, otherwise, their values stay the same even after altering them.

In the second section we check if the user did input something as an argument, otherwise, we execute the usage function which will output correct usage method of the tool.

In the third section, we collect user flags and arguments to be assigned to our global variables.

In the fourth section, we are going to listen or just send data from stdin, read the buffer from the command line and listen and potentially upload things, execute commands and drop shell back depending on the options specified by the user.

Now we’ll move on and handle both our command execution and our full command shell.

–> We created a TCP server with threading. We dealt with sub process which gives several ways to start and interact with client programs and we leveraged its abilities to run commands on the local operating system and to return the output back to the client who is connected to us.

–> The try and catch (exception handling) was made for the purpose of letting a user know when a command failed.

Brought to you by Jawady Muhammad Habib 

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
1 Comment
  1. I like it great job

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge


We recommend always using caution when following any link

Are you sure you want to continue?