Book Image

Effective Python Penetration Testing

By : Rejah Rehim
Book Image

Effective Python Penetration Testing

By: Rejah Rehim

Overview of this book

Penetration testing is a practice of testing a computer system, network, or web application to find weaknesses in security that an attacker can exploit. Effective Python Penetration Testing will help you utilize your Python scripting skills to safeguard your networks from cyberattacks. We will begin by providing you with an overview of Python scripting and penetration testing. You will learn to analyze network traffic by writing Scapy scripts and will see how to fingerprint web applications with Python libraries such as ProxMon and Spynner. Moving on, you will find out how to write basic attack scripts, and will develop debugging and reverse engineering skills with Python libraries. Toward the end of the book, you will discover how to utilize cryptography toolkits in Python and how to automate Python tools and libraries.
Table of Contents (16 chapters)
Effective Python Penetration Testing
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Setting up the scripting environment


Your scripting environment is basically the computer you use for your daily work, combined with all the tools in it that you use to write and run Python programs. The best system to learn on is the one you are using right now. This section will help you to configure the Python scripting environment on your computer, so that you can create and run your own programs.

If you are using Mac OS X or Linux installation on your computer, you may have a Python interpreter pre-installed in it. To find out if you have one, open the terminal and type python. You will probably see something like the following:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> 

From the preceding output, we can see that Python 2.7.6 is installed in this system. By issuing python in your terminal, you started Python interpreter in interactive mode. Here, you can play around with Python commands, and what you type will run and you'll see the outputs immediately.

You can use your favorite text editor to write your Python programs. If you do not have one, then try installing Geany or Sublime Text and it should be perfect for you. These are simple editors and offer a straightforward way to write as well as run your Python programs. In Geany, output is shown in a separate terminal window, whereas Sublime Text uses an embedded terminal window. Sublime Text is not free, but it has a flexible trial policy that allows you to use the editor without any stricture. It is one of the few cross-platform text editors that is quite apt for beginners and has a full range of functions targeting professionals.

Setting up in Linux

The Linux system is built in a way that makes it smooth for users to get started with Python programming. Most Linux distributions already have Python installed. For example, the latest versions of Ubuntu and Fedora come with Python 2.7. Also, the latest versions of Redhat Enterprise (RHEL) and CentOS come with Python 2.6. Just for the record, you might want to check this, though.

If it is not installed, the easiest way to install Python is to use the default package manager of your distribution, such as apt-get, yum, and so on. Install Python by issuing this command in the terminal:

  • For Debian / Ubuntu Linux / Kali Linux users, use the following command:

    $ sudo apt-get install python2
  • For Red Hat / RHEL / CentOS Linux users, use the following command:

    $sudo yum install python

To install Geany, leverage your distribution's package manager:

  • For Debian / Ubuntu Linux / Kali Linux users, use the following command:

    $sudo apt-get install geany geany-common
  • For Red Hat / RHEL / CentOS Linux users, use the following command:

    $ sudo yum install geany

Setting up in Mac

Even though Macintosh is a good platform to learn Python, many people using Macs actually run some Linux distribution or other on their computer, or run Python within a virtual Linux machine. The latest version of Mac OS X, Yosemite, comes with Python 2.7 pre-installed. Once you verify that it is working, install Sublime Text.

For Python to run on your Mac, you have to install GCC, which can be obtained by downloading XCode, the smaller command-line tool. Also, we need to install Homebrew, a package manager.

To install Homebrew, open terminal and run the following:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After installing Homebrew, you have to insert the Homebrew directory into your PATH environment variable. You can do this by including the following line in your ~/.profile file:

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

Now we are ready to install Python 2.7. Run the following command in your Terminal, which will do the rest:

$ brew install python

To install Sublime Text, go to Sublime Text's downloads page at http://www.sublimetext.com/3, and click on the OS X link. This will get you the Sublime Text installer for your Mac.

Setting up in Windows

Windows does not have Python pre-installed on it. To check if it is installed, open a command prompt and type the word python, and press Enter. In most cases, you will get a message that says Windows does not recognize python as a command.

We have to download an installer that will set Python for Windows. Then we have to install and configure Geany to run Python programs.

Go to Python's download page at https://www.python.org/downloads/windows/ and download the Python 2.7 installer that is compatible with your system. If you are not aware of your operating system's architecture, then download 32-bit installers, which will work on both architectures, but 64-bit will only work on 64-bit systems.

To install Geany, go to Geany's download page at http://www.geany.org/Download/Releases and download the full installer variant, which has a description Full Installer including GTK 2.16. By default, Geany doesn't know where Python resides on your system. So we need to configure it manually.

For that, write a Hello world program in Geany, and save it anywhere in your system as hello.py and run it.

There are three methods you can use to run a Python program in Geany:

  • Select Build | Execute

  • Press F5

  • Click the icon with three gears on it

When you have a running hello.py program in Geany perform the following steps:

  1. Go to Build | Set Build Commands.

  2. Then enter the python commands option with C:\Python27\python -m py_compile "%f".

  3. Execute the command with C:\Python27\python "%f".

  4. Now you can run your Python programs while coding in Geany.

It is recommended to run a Kali Linux distribution as a virtual machine and use this as your scripting environment. Kali Linux comes with a number of tools pre-installed and is based on Debian Linux, so you'll also be able to install a wide variety of additional tools and libraries. Also, some of the libraries will not work properly on Windows systems.