Book Image

Python Penetration Testing Cookbook

By : Rejah Rehim
Book Image

Python Penetration Testing Cookbook

By: Rejah Rehim

Overview of this book

Penetration testing is the use of tools and code to attack a system in order to assess its vulnerabilities to external threats. Python allows pen testers to create their own tools. Since Python is a highly valued pen-testing language, there are many native libraries and Python bindings available specifically for pen-testing tasks. Python Penetration Testing Cookbook begins by teaching you how to extract information from web pages. You will learn how to build an intrusion detection system using network sniffing techniques. Next, you will find out how to scan your networks to ensure performance and quality, and how to carry out wireless pen testing on your network to avoid cyber attacks. After that, we’ll discuss the different kinds of network attack. Next, you’ll get to grips with designing your own torrent detection program. We’ll take you through common vulnerability scenarios and then cover buffer overflow exploitation so you can detect insecure coding. Finally, you’ll master PE code injection methods to safeguard your network.
Table of Contents (15 chapters)

Sniffing packets

Scapy has a sniff() function that we can use for getting packets from the network. But Scapy's built-in sniff() function is a bit slow and may skip some packets. It is better to use tcpdump when the sniffing speed is important.

How to do it...

Here are the steps to write a sniffer with scapy module:

  1. Create a file called scapy-sniffer.py and open it with your editor.
  2. As usual, import the required modules for the script:
import sys
from scapy.all import *  
  1. Then, define the variables required. Here we need to define the interface to sniff:
interface = "en0"

You can get the interface to be used with the help of the ifconfig command in Linux and macOS:

  1. Now we can write a function to handle...