3
Setting Up Python 3
The first thing we’ll do is ensure that the correct version of Python is
installed. (The projects in this book use Python 3.6 or higher.) Invoke
Python from the Kali shell and have a look:
tim@kali:~$ python
This is what it looks like on our Kali machine:
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Not exactly what we’re looking for. At the time of this writing, the
default version of Python on the current Kali installation is Python 2.7.18.
But this isn’t really a problem; you should have Python 3 installed as well:
tim@kali:~$ python3
Python 3.7.5 (default, Oct 27 2019, 15:43:29)
[GCC 9.2.1 20191022] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The version of Python listed here is 3.7.5. If yours is lower than 3.6,
upgrade your distribution with the following:
sudo apt-get upgrade python3
We will use Python 3 with a virtual environment, which is a self-contained
directory tree that includes a Python installation and the set of any extra
packages you install. The virtual environment is among the most essential
tools for a Python developer. Using one, you can separate projects that have
different needs. For example, you might use one virtual environment for proj-
ects involving packet inspection and a different one for projects on binary
analysis.
By having separate environments, you keep your projects simple and clean.
This ensures that each environment can have its own set of dependencies and
modules without disrupting any of your other projects.
Let’s create a virtual environment now. To get started, we need to
install the
python3-venv
package:
tim@kali:~$ sudo apt-get install python3-venv
[sudo] password for tim:
...
Black Hat Python (Early Access) © 2021 by Justin Seitz and Tim Arnold
4
Chapter 1
Now we can create a virtual environment. Let’s make a new directory to
work in and create the environment:
tim@kali:~$ mkdir bhp
tim@kali:~$ cd bhp
tim@kali:~/bhp$ python3 -m venv venv3
tim@kali:~/bhp$ source venv3/bin/activate
(venv3) tim@kali:~/bhp$ python
That creates a new directory, bhp, in the current directory. We create a
new virtual environment by calling the
venv
package with the
-m
switch and
the name you want the new environment to have. We’ve called ours
venv3
,
but you can use any name you like. The scripts, packages, and Python
executable for the environment will live in that directory. Next, we activate
the environment by running the
activate
script. Notice that the prompt
changes once the environment is activated. The name of the environment
is prepended to your usual prompt (
venv3
in our case). Later on, when
you’re ready to exit the environment, use the command
deactivate
.
Now you have Python set up and have activated a virtual environment.
Since we set up the environment to use Python 3, when you invoke Python,
you no longer have to specify
python3
—just
python
is fine, since that is what
we installed into the virtual environment. In other words, after activation,
every Python command will be relative to your virtual environment. Please
note that using a different version of Python might break some of the code
examples in this book.
We can use the
pip
executable to install Python packages into the virtual
environment. This is much like the
apt
package manager because it enables
you to directly install Python libraries into your virtual environment without
having to manually download, unpack, and install them.
You can search for packages and install them into your virtual environ-
ment with
pip
:
(venv3) tim@kali:~/bhp: pip search hashcrack
Let’s do a quick test and install the
lxml
module, which we’ll use in
Chapter 5 to build a web scraper. Enter the following into your terminal:
(venv3) tim@kali:~/bhp: pip install lxml
You should see output in your terminal indicating that the library is
being downloaded and installed. Then drop into a Python shell and vali-
date that it was installed correctly:
(venv3) tim@kali:~/bhp$ python
Python 3.7.5 (default, Oct 27 2019, 15:43:29)
[GCC 9.2.1 20191022] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
>>> exit()
(venv3) tim@kali:~/bhp$
Black Hat Python (Early Access) © 2021 by Justin Seitz and Tim Arnold
Setting Up Your Python Environment
5
If you get an error or a version of Python 2, make sure you followed all
the preceding steps and that you have the up-to-date version of Kali.
Keep in mind that for most examples throughout this book, you can
develop your code in a variety of environments, including Mac, Linux, and
Windows. You may also want to set up a different virtual environment for
separate projects or chapters. Some chapters are Windows specific, which
we’ll make sure to mention at the beginning of the chapter.
Now that we have our hacking virtual machine and a Python 3 virtual
environment set up, let’s install a Python IDE for development.
Do'stlaringiz bilan baham: |