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
$17.99 $25.99
Paperback
$32.99
Subscription
Free Trial
Renews at $19.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
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

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 : 9781788832465
Vendor :
Offensive Security
Category :
Languages :
Tools :

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 Details

Publication date : Apr 26, 2018
Length: 176 pages
Edition : 1st
Language : English
ISBN-13 : 9781788832465
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 $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 $ 109.97
Python Penetration Testing Essentials
$32.99
Mastering Machine Learning for Penetration Testing
$43.99
Python for Offensive PenTest
$32.99
Total $ 109.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

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.