Black Hat Python: Python Programming for
Hackers and Pentesters
Chapter 1. Setting Up Your Python Environment
This is the least fun — but crucial — part of the e-book, where we stroll through putting in
an environment wherein to write down and check Python. We are going to do a crash route in putting in place a
Kali Linux digital device (VM) and installing a pleasing IDE so you have everything you want to
increase code. By the end of this chapter, you have to be ready to tackle the exercises and code
examples inside the remainder of the e-book.
Before you get started, pass beforehand and download and install VMWare Player.
[1]
I additionally advise that
you've got some Windows VMs at the equipped as nicely, which include Windows XP and Windows 7,
ideally, 32-bit in both cases.
Installing Kali Linux
Kali is the successor to the Backtrack Linux distribution, designed with the aid of Offensive Security from the
floor up as a penetration checking out working machine. It comes with some of the gear preinstalled and
is based totally on Debian Linux, so you’ll additionally be capable of installing a huge sort of additional tools and
libraries past what’s on the OS to start.
First, grasp a Kali VM photo from the following
[2] Download and decompress the picture, after which double-click on it to make
VMWare Player hearth it up. open terminal and type sudo su and type passwd root and give the new password for the root user This must get you
into the whole Kali desktop surroundings as proven in Figures 1-1.
floor up as a penetration checking out working machine. It comes with some of the gear preinstalled and
is based totally on Debian Linux, so you’ll additionally be capable of installing a huge sort of additional tools and
libraries past what’s on the OS to start.
First, grasp a Kali VM photo from the following
[2] Download and decompress the picture, after which double-click on it to make
VMWare Player hearth it up. open terminal and type sudo su and type passwd root and give the new password for the root user This must get you
into the whole Kali desktop surroundings as proven in Figures 1-1.
The first aspect we're going to do is to ensure that the appropriate model of Python is installed. This book
will use Python 2.7. In the shell (Applications▸Accessories▸Terminal), execute the
following:
root@kali:~# python --version
Python 2.7.3
root@kali:~#
If you downloaded the precise image that I advocated above, Python 2.7 will be routinely
established. Please note that using a special model of Python might ruin some of the code examples
in this book. You have been warned.
Now permit’s upload some beneficial portions of Python bundle control within the form of easy_install and
pip. These are similar to the apt package deal supervisor because they assist you without delay installation of Python
libraries, while not having to manually download, unpack, and install them. Let’s install each of those
bundle managers via issuing the following instructions:
root@kali:~#: apt-get installation python-setuptools python-pip
When the packages are installed, we will do a brief test and install the module that we’ll use in
Chapter 7 to build a GitHub-primarily based trojan. Enter the following into your terminal:
root@kali:~#: pip install github3.Py
You have to see the output in your terminal indicating that the library is being downloaded and established.
Then drop it into a Python shell and validate that it becomes mounted efficaciously:
root@kali:~#: python
Python 2.7.3 (default, Mar 14 2014, 11:fifty seven:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credit" or "license" for more statistics.
>>> import github3
>>> go out()
If your effects aren't equal to those, then there may be a “misconfiguration” for your Python surroundings
and you've delivered first-rate disgrace to our Python dojo! In this situation, make certain that you observed all the
steps above and that you have the proper version of Kali.
Keep in mind that for most examples in the course of this e-book, you could increase your code in a ramification of
environments, which include Mac, Linux, and Windows. There are a few chapters that are Windowsprecise, and I’ll ensure to permit you to recognize them at the start of the chapter.
Now that we've got our hacking digital gadget set up, let’s deploy a Python IDE for improvement.
\
WingIDE
While I commonly don’t endorse business software merchandise, WingIDE is the fine IDE that I’ve
used within the past seven years at Immunity. WingIDE provides all of the primary IDE functionality like automobilecrowning glory and explanation of characteristic parameters, however, its debugging skills are what set it apart
from other IDEs. I will give you a short rundown of the industrial model of WingIDE, however of
direction you have to choose whichever version is first-rate for you.
[3]
You can seize WingIDE from , and I advocate that you deploy the trial so
that you can enjoy firsthand a number of the functions to be had within the commercial model.
You can do your development on any platform you desire, however it is probably nice to put in WingIDE on
your Kali VM as a minimum to get started. If you’ve followed my commands thus far, ensure
that you download the 32-bit.Deb package for WingIDE, and keep it for your user directory. Then
drop into a terminal and run the subsequent:
root@kali:~# dpkg -i wingide5_5.Zero.Nine-1_i386.Deb
This must install WingIDE as deliberate. If you get any setup mistakes, there might be unmet
dependencies. In this case, absolutely run:
root@kali:~# apt-get -f install
This should fix any lacking dependencies and install WingIDE. To affirm which you’ve mounted it
Properly ensure you may get entry to it as shown in Figures 1-2.
Fire up WingIDE and open a brand new, blank Python record. Then follow alongside as I provide you with a quick rundown
of some useful functions. For starters, your screen must look like Figure 1-three, together with your main code
modifying region in the top left and a set of tabs on the lowest.
Figure 1-3. Main WingIDE window layout
Let’s write a few simple codes to demonstrate a number of the beneficial functions of WingIDE, which include the
Debug Probe and Stack Data tabs. Punch the subsequent code into the editor:
def sum(number_one,number_two):
number_one_int = convert_integer(number_one)
number_two_int = convert_integer(number_two)
result = number_one_int + number_two_int
go back result
def convert_integer(number_string):
converted_integer = int(number_string)
return converted_integer
solution = sum("1","2")
This is a very contrived instance, but it is a fantastic demonstration of the way to make your life smooth
with WingIDE. Save it with any filename you need, click the Debug menu object, and pick the Select
Current as Main Debug File option, as shown in Figure 1-four.
Figure 1-4. Setting the current Python script for debugging
Now set a breakpoint on the line of code that announces:
return converted_integer.
You can do this by way of clicking inside the left margin or by hitting the F9 key. You ought to see a bit pink dot
seem within the margin. Now run the script by pressing F5, and execution has to halt at your breakpoint.
Click the Stack Data tab and also you have to see a screen just like the one in Figures 1-5.
The Stack Data tab goes on to reveal some useful information which includes the kingdom of any neighborhood and
worldwide variables in the meantime that our breakpoint becomes hit. This lets you debug greater advanced
code wherein you need to look into variables for the duration of execution to music down insects. If you click on the drop-down bar, you could also see the modern name stack, which tells you which of them functions called the function
you are currently inner. Have a look at Figures 1-6 to see the stack trace.
Figure 1-5. Viewing stack information after a breakpoint hit
Figure 1-6. Viewing the modern-day stack trace
We can see that convert_integer turned into known as from the sum function online three of our Python script.
This becomes very beneficial when you have recursive characteristic calls or a function this is called from many
potential places. Using the Stack Data tab will come in very handy to your Python-growing career!
The subsequent primary function is the Debug Probe tab. This tab allows you to drop into a Python shell that is
executing inside the cutting-edge context of the precise second your breakpoint turned into a hit. This lets you inspect
and regulate variables, in addition to writing little snippets of check code to attempt out new ideas or to
troubleshoot. Figure 1-7 demonstrates the way to check out the converted_integer variable and trade
its fee.
Figure 1-7. Using Debug Probe to inspect and adjust local variables
After you are making a few adjustments, you could resume execution of the script by way of urgent F5.
Even though that is a very easy example, it demonstrates a number of the most beneficial capabilities of
WingIDE for developing and debugging Python scripts.
That’s all we want as a way to begin growing code for the relaxation of this e-book. Don’t forget about
making digital machines equipped as target machines for the Windows-unique chapters, however of course
using local hardware ought to now not gift any troubles.
Now permit’s get into some real laugh!
If you already use an IDE that has similar functions to WingIDE, please send me an e-mail or a tweet because I would like to
listen approximately it.

0 Comments