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
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
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 : 9781788999465
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 124.97
Mastering Kali Linux for Advanced Penetration Testing
€54.99
Hands-On AWS Penetration Testing with Kali Linux
€36.99
Hands-On Penetration Testing with Python
€32.99
Total 124.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.