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)

Fake port-scanning reply

In this section, we will look at how to give a fake reply at the TCP layer. The program will give fake replies to open ports. For this code, we are going to use the scapy library because the TCP header is very complicated to make. The program name is tcp_trap.py:

  • Use the following library and module:
      import socket
import struct
import binascii
import Queue
from scapy.all import *
import threading
  • A raw socket has been created to receive incoming packets as follows:
      my_socket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, 8)
Q = Queue.Queue()
  • The following function receives the incoming TCP/IP packets. A lot of lines have already been discussed in Chapter 3, Sniffing and Penetration Testing. The if (D_port==445 or D_port==135 or D_port==80): syntax shows that we are only interested in ports 445, 135,...