Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Hands-On Penetration Testing with Python
Hands-On Penetration Testing with Python

Hands-On Penetration Testing with Python: Enhance your ethical hacking skills to build automated and intelligent systems

eBook
$20.98 $29.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Penetration Testing with Python

Building Python Scripts

This chapter will cover the core concepts of all programming languages. This includes conditional statements, loops, functions, and packages. We will see that these concepts are pretty much the same in Python as they are in other programming languages, except for some syntactical differences. But syntax just requires practice; everything else will fall into line automatically. The topics that we are going to cover in this chapter are as follows:

  • Conditional statements
  • Loops
  • Functions
  • Modules and packages
  • Comprehensions and generators

Technical requirements

Make sure you have the following pre-requisites needed to proceed further:

  • A working computer or laptop
  • Ubuntu OS (preferably 16.04)
  • Python 3.x
  • A working internet connection

Indentation

If you come from a background of a language such as Java, C, or C++, you might be familiar with the concept of grouping logically connected statements using curly braces. This is not the case, however, in Python. Instead, the logically connected statements, including classes, functions, conditional statements, and loops, are grouped using indentation. Indentation keeps the code clean and easy to read. We shall explore this in more detail in the coming sections. For now, however, let's say goodbye to braces. I recommend that you use tabs for indentation, as typing an equal number of spaces in every line becomes very time-consuming.

Conditional statements

Just like all other languages, to carry out the conditional operations, Python makes use of conditional statements. The conditional statements supported by Python are as follows:

  • if condition
  • if...else condition
  • else...if conditional ladder, known as elif in Python
Python doesn't support the switch statement.

The if condition

The if condition or the if statement takes a statement and returns either a Boolean True or a Boolean False value after evaluating the statement. If the condition returns True, the code proceeding the if statement (equally indented) is executed. If the statement/condition evaluates to False, then either the else block of code gets executed if there is one, or the block of...

Loops

Loops are utilities that every programming language has. With the help of loops, we execute tasks or statements that are repetitive in nature, which, without loops, would take up lots of lines of code. This, in a way, defeats the purpose of having a programming language in the first place. If you are familiar with Java, C, or C ++, you might have already come across while, for, and do...while loops. Python is pretty much the same, except that it doesn't support do...while loops. Thus, the loops that we are going to study in the following section in Python are the following:

  • while loop
  • for loop

The while loop

Remember that when we discussed lists in the first chapter of the book, we mentioned that lists can actually...

Functions and methods in Python

Functions and methods are used to design or make a logical unit of code that can be reused throughout the course of your script or other scripts. Functions actually form the basis of code reuse and bring modularity to the code structure. They keep the code clean and easier to modify.

It is advisable to always try to break our logic into small units of code, each of which is a function. We should try to keep the size of the method small in terms of the lines of code whenever possible.

The following code represents the basic syntax of defining methods in Python:

def print_message(message):
print(message)
statement 2
statement

Python methods do not have a return type in their definition as you might have seen in C, C++, or Java, such as void, in, float, and so on. A Python method may or may not return a value, but we do not explicitly need...

Modules and packages

Every Python script is called a module. Python has been designed with reusability and ease of code in mind. For this reason, every Python file we create becomes a Python module and is eligible to be invoked or used within any other file or script. You might have learned in Java how to import classes and reuse them with other classes. The idea is pretty much the same here, except that we are importing the whole file as a module and we can reuse any method, class, or variable of the imported file. Let's take a look at an example. We will create two files, child.py and parent.py, and put the following code in each, as follows:

The first five lines belong to child.py, and the last eight lines belong to parent.py. We will run the parent, as shown in the output. It should be noted that the imported file can be given an alias. In our case, we imported the child...

Generators and comprehensions

A generator is a special kind of iterator in Python. In other words, a Python generator is a function that returns us a generator iterator by issuing the yield command, which can be iterated upon. There might be occasions in which we would want a method or function to return us a series of values, instead of just one. We might, for example, want our method to partially carry out a task, return the partial results to the caller, and then resume the work right from the place where it returned the last value. Usually, when a method terminates or returns a value, its execution begins again from the start. This is what generators try to address. A generator method returns a value and a control to the caller and then continues its execution right from where it left off. A generator method is a normal Python method with a yield statement. The following code...

Summary

In this chapter, we discussed abut conditions, loops, methods, iterators, packages, generators, and comprehensions. All of these are widely used in Python. The reason why we covered these topics is because when we get into automating penetration testing and cyber security test cases later on, we will see these concepts widely used within our code files. In the next chapter, we will explore the object-oriented nature of Python. We will explore how to deal with XML, CSV, and JSON data in Python. We will also read about files, IO, and regular expressions.

Questions

  1. Name a real-world use case in which generators are used.
  2. Can we store a function name in a variable and then invoke it via a variable?
  3. Can we store a module name in a variable?
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Identify and expose vulnerabilities in your infrastructure with Python
  • Learn custom exploit development .
  • Make robust and powerful cybersecurity tools with Python

Description

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.

Who is this book for?

If you are a security consultant , developer or a cyber security enthusiast with little or no knowledge of Python and want in-depth insight into how the pen-testing ecosystem and python combine to create offensive tools , exploits , automate cyber security use-cases and much more then this book is for you. Hands-On Penetration Testing with Python guides you through the advanced uses of Python for cybersecurity and pen-testing, helping you to better understand security loopholes within your infrastructure .

What you will learn

  • Get to grips with Custom vulnerability scanner development
  • Familiarize yourself with web application scanning automation and exploit development
  • Walk through day-to-day cybersecurity scenarios that can be automated with Python
  • Discover enterprise-or organization-specific use cases and threat-hunting automation
  • Understand reverse engineering, fuzzing, buffer overflows , key-logger development, and exploit development for buffer overflows.
  • Understand web scraping in Python and use it for processing web responses
  • Explore Security Operations Centre (SOC) use cases
  • Get to understand Data Science, Python, and cybersecurity all under one hood

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 31, 2019
Length: 502 pages
Edition : 1st
Language : English
ISBN-13 : 9781788990820
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jan 31, 2019
Length: 502 pages
Edition : 1st
Language : English
ISBN-13 : 9781788990820
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 165.97
Mastering Kali Linux for Advanced Penetration Testing
$72.99
Hands-On AWS Penetration Testing with Kali Linux
$48.99
Hands-On Penetration Testing with Python
$43.99
Total $ 165.97 Stars icon

Table of Contents

17 Chapters
Introduction to Python Chevron down icon Chevron up icon
Building Python Scripts Chevron down icon Chevron up icon
Concept Handling Chevron down icon Chevron up icon
Advanced Python Modules Chevron down icon Chevron up icon
Vulnerability Scanner Python - Part 1 Chevron down icon Chevron up icon
Vulnerability Scanner Python - Part 2 Chevron down icon Chevron up icon
Machine Learning and Cybersecurity Chevron down icon Chevron up icon
Automating Web Application Scanning - Part 1 Chevron down icon Chevron up icon
Automated Web Application Scanning - Part 2 Chevron down icon Chevron up icon
Building a Custom Crawler Chevron down icon Chevron up icon
Reverse Engineering Linux Applications Chevron down icon Chevron up icon
Reverse Engineering Windows Applications Chevron down icon Chevron up icon
Exploit Development Chevron down icon Chevron up icon
Cyber Threat Intelligence Chevron down icon Chevron up icon
Other Wonders of Python Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(3 Ratings)
5 star 66.7%
4 star 0%
3 star 0%
2 star 0%
1 star 33.3%
Majid shahmiri Apr 24, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I loved reading this book, It covers multiple topics related to cyber security i.e Web pentest, exploit development, reverse engineering and last but not the least python based web vulnerability scanners. One can learn a lot from this book since every topic is written in simpler words which makes it easier even if you are a beginner in security. Also i would recommend to people who are at intermediate levels in cyber security.
Amazon Verified review Amazon
Ernesto Guevara Gonzaga Jul 08, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Buen producto
Amazon Verified review Amazon
Kali Jun 12, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I cannot recommend this book. The reasons why are numerous:1. There are more typos than you can shake a stick at. I know Packt is a low-end publishing company but this was ridiculous. It is almost as if whoever proofed the manuscript knew absolutely nothing about computer science. Really pathetic.2. In order to access the Python code in most of the book, you have to install Kali as a VM. Now I have been running Kali for a long time and am perfectly content with it. I cannot spare an additional server just to set up Kali VM on it. In order for me to test the source code, I thus have to either (1) type it in from the book's ugly fonts, or (2) persuade someone to send it to me. None of my emails to Packt were replied to and Khan's email address was no where to be found on the web.I am sorry that this has happened because the book is definitely leading edge. Everyone's loss here.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.