Book Image

Python Penetration Testing Essentials - Second Edition

By : Mohit Raj
Book Image

Python Penetration Testing Essentials - Second Edition

By: Mohit Raj

Overview of this book

This book gives you the skills you need to use Python for penetration testing (pentesting), with the help of detailed code examples. We start by exploring the basics of networking with Python and then proceed to network hacking. Then, you will delve into exploring Python libraries to perform various types of pentesting and ethical hacking techniques. Next, we delve into hacking the application layer, where we start by gathering information from a website. We then move on to concepts related to website hacking—such as parameter tampering, DDoS, XSS, and SQL injection. By reading this book, you will learn different techniques and methodologies that will familiarize you with Python pentesting techniques, how to protect yourself, and how to create automated programs to find the admin console, SQL injection, and XSS attacks.
Table of Contents (11 chapters)

Implementing a network sniffer using Python

Before learning about the implementation of a network sniffer, let's learn about a particular struct method:

  • struct.pack(fmt, v1, v2, ...): This method returns a string that contains the values v1, v2, and so on, packed according to the given format
  • struct.unpack(fmt, string): This method unpacks the string according to the given format

Let's discuss the code in the following code snippet:

import struct
ms=  struct.pack('hhl', 1, 2, 3)
print (ms)
k= struct.unpack('hhl',ms)
print k

The output for the preceding code is as follows:

G:PythonNetworkingnetwork>python str1.py
 ☻ ♥
(1, 2, 3)

First, import the struct module, and then pack the 1, 2, and 3 integers in the hhl format. The packed values are like machine code. Values are unpacked using the same hhl format; here, h means a short integer...