Book Image

Python Penetration Testing Cookbook

By : Rejah Rehim
Book Image

Python Penetration Testing Cookbook

By: Rejah Rehim

Overview of this book

Penetration testing is the use of tools and code to attack a system in order to assess its vulnerabilities to external threats. Python allows pen testers to create their own tools. Since Python is a highly valued pen-testing language, there are many native libraries and Python bindings available specifically for pen-testing tasks. Python Penetration Testing Cookbook begins by teaching you how to extract information from web pages. You will learn how to build an intrusion detection system using network sniffing techniques. Next, you will find out how to scan your networks to ensure performance and quality, and how to carry out wireless pen testing on your network to avoid cyber attacks. After that, we’ll discuss the different kinds of network attack. Next, you’ll get to grips with designing your own torrent detection program. We’ll take you through common vulnerability scenarios and then cover buffer overflow exploitation so you can detect insecure coding. Finally, you’ll master PE code injection methods to safeguard your network.
Table of Contents (15 chapters)

Python 3 language basics and differences

Python 3.0 was first released in 2008. Even though Python 3 supposed to be backward incompatible with other old version, many of its features are backported to support older versions. It is better to have an idea of Python versions and its differences for better understanding of our recipes.

Getting ready

If you are new to Python, you might be confused about the different versions that are available. Before looking into the further details, let's have a look at the most recent major releases of Python and the key differences between Python 2 and Python 3.

How to do it...

These are the major Python versions available.

Python 2

Published in late 2000, it has many more programmatic features including a cycle-detecting garbage collector that helps to automate memory management. The increased unicode support that helps to standardize characters, and list comprehensions that help to create a list based on existing lists are other features. In Python version 2.2, the types and classes are consolidated into one hierarchy.

Python 3

Python 3 was released in late 2008, to update and fix the built-in design flaws of the prior versions of Python. The main focus of Python 3 development was to clean up the code base and reduce redundancy.

In the beginning, the adoption of Python 3 was a very slow process due to its backward incompatibility with Python 2. Moreover, many package libraries were only available for Python 2. Later, there was an increased adoption for Python 3 as the development team announced that there will be an end of life for Python 2 support and more libraries have been ported or migrated to Python 3.

Python 2.7

Python 2.7 was published in 2010 and was planned as the last release for 2.x versions. Its intention was to make it easier for Python 2.x users to port their features and libraries over to Python 3 by providing compatibility between the two, which included a unit test to support test automation, argparse for parsing command-line options, and more convenient classes in collections.

Key differences between Python 2.7 and Python 3

Here are some main differences between Python 2.x and Python 3:

  • Print: In Python 2, print is a statement. So, there is no need to wrap the text in parentheses for printing. But in Python 3 print is a function. So, you have to pass the string you need to print to the function in parentheses.
  • Integer division: Python 2 considers numbers without any digits after the decimal point as integers, which may lead to some unexpected results during division.
  • List comprehension loop variables leak: In Python 2, giving the variable that is iterated over in a list comprehension leaks the variable into surrounding scope, this list comprehension loop variable leak bug has been fixed in Python 3.
  • Unicode strings: Python 2 requires you to mark the unicode string explicitly with the u prefix. But, Python 3 stores strings as unicode by default.
  • Raising exceptions: Python 3 requires different syntax for raising exceptions.

The progression from Python 2.x to Python 3.x is happening slowly, but it is underway. It is good to be mindful that there are material differences between Python 2.x and Python 3 as you may need to deal with code that is written in the version with which you are less familiar.