Book Image

Mastering Python for Networking and Security

By : José Ortega
Book Image

Mastering Python for Networking and Security

By: José Ortega

Overview of this book

It’s becoming more and more apparent that security is a critical aspect of IT infrastructure. A data breach is a major security incident, usually carried out by just hacking a simple network line. Increasing your network’s security helps step up your defenses against cyber attacks. Meanwhile, Python is being used for increasingly advanced tasks, with the latest update introducing many new packages. This book focuses on leveraging these updated packages to build a secure network with the help of Python scripting. This book covers topics from building a network to the different procedures you need to follow to secure it. You’ll first be introduced to different packages and libraries, before moving on to different ways to build a network with the help of Python scripting. Later, you will learn how to check a network’s vulnerability using Python security scripting, and understand how to check vulnerabilities in your network. As you progress through the chapters, you will also learn how to achieve endpoint protection by leveraging Python packages along with writing forensic scripts. By the end of this book, you will be able to get the most out of the Python language to build secure and robust networks that are resilient to attacks.
Table of Contents (16 chapters)

Programming and installing Python

Python is a byte-compiled, object-oriented programming language that is easy to read and write. The language is great for security professionals because it allows for the rapid creation of tests as well as reusable items for future use. As many security tools are written in Python, it offers many opportunities for extending and adding features to tools that are already written.

Introducing Python scripting

In this book, we will work with two versions. If you use a Linux Distribution, such as Debian or Kali, there will be no problems since Python is multi-platform and version 2.7 comes installed by default in the majority of linux distributions.

Why choose Python?

There are many reasons to choose Python as your main programming language:

  • Multi-platform and open source language.
  • Simple, fast, robust, and powerful language.
  • Many libraries, modules, and projects focused on computer security are written in Python.
  • There is a lot of documentation and a very large user community.
  • It is a language designed to make robust programs with a few lines of code, something that in other languages is only possible after including many characteristics of each language.
  • Ideal for prototypes and rapid-concept tests (PoC).

Multi-platform

The Python interpreter is available on many platforms (Linux, DOS, Windows, and macOS X). The code that we create in Python is translated into bytecode when it is executed for the first time. For that reason, in systems in which we are going to execute our programs or scripts developed in Python, we need the interpreter to be installed.

Object-Oriented Programming

Object-oriented programming is a paradigm where programs are defined in terms of "object classes" that communicate with each other by sending messages. It is an evolution of the paradigms of procedural, structured, and modular programming, and is implemented in languages such as Java, Python, or C ++.

Classes define the behavior and available state that is specified in objects, and allow a more direct representation of the concepts necessary for modeling a problem, allowing the user to define new types.

Objects are characterized by:

  • An identity that differentiates them from each other
  • Defining their behavior through methods
  • Defining their state through properties and attributes

Classes allow grouping in a new type of data and the functionalities associated with objects, favoring separation between the details of the implementation of the essential properties for its use. In this way, the goal is to not show more than the relevant information, hiding the state and the internal methods of the class, it is known as "encapsulation," and it is a principle inherited from modular programming.

An important aspect in the use of classes is that they are not manipulated directly, but serve to define new types. A class defines properties and behaviors for objects (instances of a class). A class acts as a template for a set of objects, which are said to belong to the class.

The most important techniques used in object-oriented programming are:

  • Abstraction: Objects can perform tasks, interact with other objects, or modify and report their status without the need to communicate how those actions are performed.
  • Encapsulation: Objects prevent the modification of their internal state or a call to internal methods by other objects, and are only related through a clear interface that defines how they relate to other objects.
  • Polymorphism: Different behaviors may be associated with the same name.
  • Inheritance: Objects are related to others by establishing hierarchies, and it is possible that some objects inherit the properties and methods of other objects, extending their behavior and/or specializing. Objects are grouped like this in classes that form hierarchies.

Obtaining and installing Python

Installation of Python is fast on Linux and Windows platforms. Windows users can use an installer in an easy way that makes configuration work for you. In Linux, you have the option to build the installation from the source code, but it's not mandatory, and you can use classic package-management dependencies, such as apt-get.

Many Linux distributions come preinstalled with Python 2. When installing Python 3 on such a system, it is important to keep in mind that we are not replacing the installation of Python 2. In this way, when we install Python 3, it can be installed in parallel with Python 2 on the same machine. After installing Python 3, you can call the python interpreter using the Python3 executable.

Installing Python on Windows

Windows users can obtain the installer from the main Python site: https://www.python.org/ftp/python/2.7.15/python-2.7.15.msi. Just double-click the installer, and follow the steps to install it. It should create a directory at C:/Python27/; this directory will have the Python.exe interpreter as well as all of the default libraries installed.

The Python installation allows you to customize where the environment will be installed. The default location for Python 2.7.14 is C:\Python27, although you can specify another location. This route will be relevant when looking for certain modules and tools.

We can customize the installation if we want to include the documentation or install a series of utilities, such as the pip package manager or the IDLE development environment, to edit and execute scripts. It is recommended you leave the options marked so that it installs them and we have as complete an environment as possible:

It is important to check the Add python.exe to the Path box. This will allow you to run Python directly from the command prompt from any path without having to go to the installation directory.

At the time of installing the version of Python for Windows, you can also see that it is available IDLE, an editor or IDE (Integrated Development Environment) of Python that will allow us to write and test the code. Once installed, we can verify that everything is correct:

  1. Open the folder where you have installed it
  2. Enter C:\Python27\Lib\idlelib
  3. Run the idle.bat file with a double-click
Another option we have for Windows users is WinPython, which is available at http://winpython.github.io.

WinPython is a Python distribution; you can install it on Windows 7/8/10 operating systems for scientific and educational use.

This distribution is something different from others because it:

  • Requires no installation: WinPython lives entirely in its own directory, without any OS installation
  • Is portable: You can easily zip your python project and install in other machines in an easy way

Installing Python for Linux

Python is installed by default in most Gnu/Linux distributions. If we want to install it in Ubuntu or Debian-based distributions, we can do it through the apt-get package manager:

sudo apt-get install python2.7