Book Image

Hands-On Penetration Testing with Python

By : Furqan Khan
Book Image

Hands-On Penetration Testing with Python

By: Furqan Khan

Overview of this book

With the current technological and infrastructural shift, penetration testing is no longer a process-oriented activity. Modern-day penetration testing demands lots of automation and innovation; the only language that dominates all its peers is Python. Given the huge number of tools written in Python, and its popularity in the penetration testing space, this language has always been the first choice for penetration testers. Hands-On Penetration Testing with Python walks you through advanced Python programming constructs. Once you are familiar with the core concepts, you’ll explore the advanced uses of Python in the domain of penetration testing and optimization. You’ll then move on to understanding how Python, data science, and the cybersecurity ecosystem communicate with one another. In the concluding chapters, you’ll study exploit development, reverse engineering, and cybersecurity use cases that can be automated with Python. By the end of this book, you’ll have acquired adequate skills to leverage Python as a helpful tool to pentest and secure infrastructure, while also creating your own custom exploits.
Table of Contents (18 chapters)

Reverse TCP shells with Python

Now that we have understood the basics of subprocessing, multiprocessing, and so on, implementing a basic TCP reverse shell with Python is pretty straightforward. For this example, rev_tcp.py, we will be using the bash-based reverse TCP shell. In the later chapters of the book, we will see how to pass a reverse shell entirely with Python:

It should be noted that OS.dup2 is used to create a duplicate of a file descriptor in Python. The stdin is defined to be file descriptor 0, stdout is defined to be file descriptor 1, and stderr is defined to be file descriptor 2. The code line OS.dup2(s.fileno(),0) indicates that we should create a duplicate of stdin and redirect the traffic to the socket file, which happens to be on the localhost and port 1234 (where Netcat is listening). Finally, we invoke the shell in interactive mode and since we are not specifying...