Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Python for Offensive PenTest
Python for Offensive PenTest

Python for Offensive PenTest: A practical guide to ethical hacking and penetration testing using Python

eBook
Mex$378.99 Mex$541.99
Paperback
Mex$676.99
Subscription
Free Trial

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Table of content icon View table of contents Preview book icon Preview Book

Python for Offensive PenTest

Advanced Scriptable Shell

The problem with the back door, which we created in the previous chapter, is that if the attacker IP changes we don't have a built-in mechanism to inform our target that it should connect to the new IP address. In this chapter we will look into a method that lets you keep a fixed reserved name for your attacker machine even if its IP changes.

The following are the topics that will be covered in this chapter: 

  • Dynamic DNS
  • Interacting with Twitter
  • Replicating Metasploit's screen capturing
  • Replicating Metasploit searching for content
  • Integrating a low-level port scanner

Dynamic DNS

Now, one of the methods we'll discuss here is dynamic DNS. Let's say that the attacker IP is 1.1.1.1 on day 1. Then, the next day, we get an IP address of 2.2.2.2. Then, how would our target know the new IP address ? The answer is dynamic DNS (DDNS). It is a method to preserve a unique name for you on a DNS server. While the reserved name is fixed, the correlated IP address will change each time you change your public IP address. For demonstration, we will use noip.com. It provides a free dynamic DNS service. So I have previously preserved a name called pythonhussam.ddns.net. So on the target side, instead of hard-coding the IP address on that script, we will do a DNS lookup for this name; then we will retrieve the IP address to make the connection. Now, you're probably asking: When the attacker IP address changes, how does noip.com ...

Interacting with Twitter

Now, we will discuss a technique that is used frequently these days: relying on well-known servers to perform certain tasks or transfer a piece of information. This technique has been used by a Russian malware. What the attackers did was they sent the data over their Twitter account and made the target parse it later on. So, on the attacker machine, we just send an order or command as a normal tweet to our Twitter account. Note that there is no direct communication between the attacker and its target, which is really evil here. Later on, the target will parse the tweet and execute that order. The benefits of doing this is are:

  • Twitter is a trusted website and it has a very good reputation; most likely, it's a whitelisted website
  • This type of attack is very hard to detect, where an unskilled security team would never have thought that this data could...

Replicating Metasploit's screen capturing

In this section, we will automate capturing a screenshot from the target machine and retrieve it over HTTP reverse shell. Getting a screenshot from the target Desktop can be useful to see what programs and activities are going on on the target side. In Metasploit Meterpreter, there is a function called screengrab(), which will take a snapshot from the target machine and transfer it back to the attacker machine. So here, we will do something similar in our existing HTTP shell. For this purpose, we will be using a library called Pillow at the target. This is a high-level image library in Python. The installation is quite simple. You just need to run pip install Pillow via cmd.

Before doing that, just make sure that you have internet access. Once we install this library, I will go to Devices|Network|Network Settings...

Replicating Metasploit searching for content

We will now code a Python function that will search into target directories and provide us with a list of file locations for a certain specific file extension. For instance, say we need to search for a PDF or document file on the target machine; instead of checking each directory, we will add a new function to automatically do the job for us. This is very useful when you first land in a target machine and try to explore as much data as possible such as documents, PDF files, and so on. The coding part is quite easy. We will use the Python os library to do the job for us. So, as usual, I have added a new if statement to specify that if we get a search keyword we will do the following:

# Python For Offensive PenTest

# Searching for Content

import requests
import subprocess
import os
import time

while True:

req = requests.get('http...

Integrating low-level port scanner

During penetration testing, sometimes you encounter a scenario where your client is using some kind of an internal server that is not accessible through the internet. And just because of this they think it's secure. In this section, we will see how we can integrate a simple port scanner with our script to prevent a possible attack.

Usually, once you get into your target machine, you start looking for other possible targets. For example, if we were able to access machine A, then we can extend our attack and scan machine B to see what ports and services are running on that machine. The other usages are to make the target scan an online server on our behalf to hide our activities. Now, let's get to the coding part. We will build a basic low-level scanner. It's named low-level because we will use the built-in socket library and then...

Summary

In this chapter, we learned about DDNS and the DDNS-aware shell. We also learned how to interact with Twitter, and replicate Metasploit's screen capturing, and we searched for the content and looked into target directory navigation. Last, we saw how to integrate a low-level port scanner.

In the next chapter, we will learn about password hacking.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • •Comprehensive information on building a web application penetration testing framework using Python
  • •Master web application penetration testing using the multi-paradigm programming language Python
  • •Detect vulnerabilities in a system or application by writing your own Python scripts

Description

Python is an easy-to-learn and cross-platform programming language that has unlimited third-party libraries. Plenty of open source hacking tools are written in Python, which can be easily integrated within your script. This book is packed with step-by-step instructions and working examples to make you a skilled penetration tester. It is divided into clear bite-sized chunks, so you can learn at your own pace and focus on the areas of most interest to you. This book will teach you how to code a reverse shell and build an anonymous shell. You will also learn how to hack passwords and perform a privilege escalation on Windows with practical examples. You will set up your own virtual hacking environment in VirtualBox, which will help you run multiple operating systems for your testing environment. By the end of this book, you will have learned how to code your own scripts and mastered ethical hacking from scratch.

Who is this book for?

This book is for ethical hackers; penetration testers; students preparing for OSCP, OSCE, GPEN, GXPN, and CEH; information security professionals; cybersecurity consultants; system and network security administrators; and programmers who are keen on learning all about penetration testing.

What you will learn

  • •Code your own reverse shell (TCP and HTTP)
  • •Create your own anonymous shell by interacting with Twitter, Google Forms, and SourceForge
  • •Replicate Metasploit features and build an advanced shell
  • •Hack passwords using multiple techniques (API hooking, keyloggers, and clipboard hijacking)
  • •Exfiltrate data from your target
  • •Add encryption (AES, RSA, and XOR) to your shell to learn how cryptography is being abused by malware
  • •Discover privilege escalation on Windows with practical examples
  • •Countermeasures against most attacks
Estimated delivery fee Deliver to Mexico

Standard delivery 10 - 13 business days

Mex$149.95

Premium delivery 3 - 6 business days

Mex$299.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 26, 2018
Length: 176 pages
Edition : 1st
Language : English
ISBN-13 : 9781788838979
Vendor :
Offensive Security
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Estimated delivery fee Deliver to Mexico

Standard delivery 10 - 13 business days

Mex$149.95

Premium delivery 3 - 6 business days

Mex$299.95
(Includes tracking information)

Product Details

Publication date : Apr 26, 2018
Length: 176 pages
Edition : 1st
Language : English
ISBN-13 : 9781788838979
Vendor :
Offensive Security
Category :
Languages :
Tools :

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 Mex$85 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 Mex$85 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Mex$ 2,256.97
Python Penetration Testing Essentials
Mex$676.99
Mastering Machine Learning for Penetration Testing
Mex$902.99
Python for Offensive PenTest
Mex$676.99
Total Mex$ 2,256.97 Stars icon

Table of Contents

7 Chapters
Warming up – Your First Antivirus-Free Persistence Shell Chevron down icon Chevron up icon
Advanced Scriptable Shell Chevron down icon Chevron up icon
Password Hacking Chevron down icon Chevron up icon
Catch Me If You Can! Chevron down icon Chevron up icon
Miscellaneous Fun in Windows Chevron down icon Chevron up icon
Abuse of Cryptography by Malware Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Most Recent
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.4
(7 Ratings)
5 star 42.9%
4 star 14.3%
3 star 0%
2 star 28.6%
1 star 14.3%
Filter icon Filter
Most Recent

Filter reviews by




Shantel Jack Aug 22, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It's a good book for learning python
Amazon Verified review Amazon
keith Smith Jun 29, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a must for anyone in security.
Amazon Verified review Amazon
Amazon Customer Mar 27, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The book is nothing but a mere composition of articles that could be easily found over the internet. Nothing new don't waste your bucks on this.
Amazon Verified review Amazon
Kuldeep Singh Feb 17, 2019
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Good but contains higher level of knowledge.Not recommend for a beginner
Amazon Verified review Amazon
Doc Johnson Sep 13, 2018
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Content is decent, a lot available online possibly as per the other review. However the quality of the book is awful. Lost count of the errors in the text, either grammatical or code/notes errors. Seems this book wasn’t checked or proof read at all.
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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela