How to Hack with Python

Python is one of the best programming languages for hacking. This language is easy to

learn and powerful enough to satisfy all of your programming needs. In this chapter, you’ll

learn the basics of Python. You will know how to launch it, how to write codes with it, and

how to compile it.

Important Note: This chapter assumes that you are using Kali Linux, an operating system

that is created for hackers. Kali Linux contains hundreds of built-in hacking tools that you

can use to test your systems or attack other networks. In addition, this OS is completely

free. To download Kali Linux, please visit: https://www.kali.org/downloads/.

How to Get Python Modules

An excellent benefit of using Kali Linux is that it comes with a pre-installed version of

Python. That means you can start writing codes without downloading anything.

The default modules and language library of Python allow you to perform a wide range of

activities. For instance, the ready-made version of Python has exception handling, file

handling, math and number modules, and data types.

Python’s built-in tools and components are enough to create effective hacking tools. But

you can enhance the effectiveness and flexibility of this language by downloading

additional modules from third-party sources. These extra modules are the main reason

why many hackers choose Python for their programming needs. If you want a complete

list of all the available third-party modules for Python, visit this site:

http://pypi.python.org/pypi.

Installing a Module

Just like other Linux systems, Kali Linux requires “wget” when acquiring new files or

programs from the internet. This command downloads your chosen file or program from

its respective repository. Then, you have to decompress the downloaded module and issue

the following command:

python setup.py install

Let’s assume that you want to download Nmap (a python module) from www.xael.org. To

get this module, you must:

1. Turn on your Kali Linux computer.

2. Launch a terminal (the small window that takes user inputs).

3. Type the following code:

Kali > wget http://xael.org/norman/python/python-nmap/python-nmap-0.3.4.tar.gz

4. Extract the file by typing:

Kali > tar –xzf python-nmap-0.3.4.tar.gz

5. Access the directory you created by entering:

Kali > cd python-nmap-.03.4/

6. Issue the code given below to finish the process:

Kali > python setup.py install

How to Write Python Scripts

In this part of the book, you’ll learn how to write codes using the Python language. It will

also explain the fundamental terms, concepts, and syntax of Python codes. Read this

material carefully; it will help you become a knowledgeable programmer and hacker.

Important Note: You need to use a text editor when writing codes. Kali Linux has a builtin

text editor called “Leafpad”. As you can see, Kali Linux contains everything you need

to hack computers and systems.

Proper Formatting

Formatting plays an important role in the Python language. The interpreter of Python

groups codes based on their format. Keep in mind that consistency is more important than

precision. You don’t have to follow strict formatting rules. You just have to be consistent

with the format you are using.

For example, if you’ll use double indentation to differentiate a code block, indent each line

of that code block twice. Forgetting this simple rule can lead to error messages and/or

failed attacks.

How to Run a Python File

Nothing beats active learning. To help you master this process, let’s write a basic piece of

code using Leafpad. Here’s the code:

#! /user/bin/python

name=”<Chuck Norris>”

print “Hi, ” + name + “!”

Save the file as “sample.py”.

This code consists of three lines. The first one triggers the interpreter of Python. The

second one creates a variable called “name” and sets a value for it. The last line

concatenates the word “Hi” with the user’s input and inserts an exclamation mark.

At this point, you can’t execute the code yet. You must give yourself the permission to run

it first. In Kali Linux, the command that you should use is “chmod”.

Important Note: To learn more about Linux permissions, please check this site:

The code that you must type is:

chmod 755 sample.py

After issuing that command using a terminal, your screen will show you this:

Hi, Chuck Norris!

How to Add a Comment

You can add comments to your Python codes. In programming, a comment is a word,

sentence, or paragraph that defines what a piece of code can do. It doesn’t affect the

functionality or behavior of the code itself. Adding a comment to your codes isn’t required

but nonetheless advised. Comments will help you remember important information

regarding your codes. Obviously, you don’t want to forget the “internal mechanisms” of

your own programs.

The interpreter of Python skips each comment. That means the interpreter will jump over

words, sentences or paragraphs until it finds a legitimate code block. In Python, you need

to use “#” to set a single-line comment. For multiline comments, you must type three

double quotes. These symbols must appear at the beginning of your comments.

Here are some comments written in the Python language:

1. # Hi, I’m a single-line comment.

2. “””

Hi,

I’m

A

Multiline

Comment

“””

Modules

With Python, you can divide your codes into separate modules. You must “import” a

module in order to use it. When importing a module, you will access the classes, methods,

and functions (you’ll learn about these later) that are present inside that module. This

feature is one of the major reasons why Python is the preferred computer language of computer hackers.

LEAVE A REPLY

Please enter your comment!
Please enter your name here