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)

Python as an OOP language

In this section, we will review Object-Oriented Programming and inheritance in Python.

Object-Oriented programming is one of the paradigms most used today. While it fits a lot of situations that we can find in day-to-day life, in Python, we can combine it with other paradigms to get the best out of the language and increase our productivity while maintaining an optimal code design.

Python is an object-oriented language and allows you to define classes and instantiate objects from these definitions. A block headed by a class statement is a class definition. The functions that are defined in the block are its methods, also called member functions.

The way Python creates objects is with the class keyword. A Python object is a collection of methods, variables, and properties. You can create many objects with the same class definition. Here is a simple example of a protocol object definition:

You can find the following code in the protocol.py file.

class protocol(object):

def __init__(self, name, number,description):
self.name = name
self.number = number
self.description = description

def getProtocolInfo(self):
return self.name+ " "+str(self.number)+ " "+self.description

The __init__ method is a special method that, as its name suggests, act as a constructor method to perform any initialization process that is necessary.

The first parameter of the method is a special keyword and we use the self identifier for reference the current object. It is a reference to the object itself and provides a way to access its attributes and methods.

The self parameter is equivalent to the pointer that can be found in languages such as C ++ or Java. In Python, self is a reserved word of the language and is mandatory, it is the first parameter of conventional methods and through it you can access the attributes and methods of the class.

To create an object, write the name of the class followed by any parameter that is necessary in parentheses. These parameters are the ones that will be passed to the __init__ method, which is the method that is called when the class is instantiated:

>>> protocol_http= protocol("HTTP", 80, "Hypertext transfer protocol")

Now that we have created our object, we can access its attributes and methods through the object.attribute and object.method() syntax:

>>> protocol_http.name
>>> protocol_http.number
>>> protocol_http.description
>>> protocol_http.getProtocolInfo()

Inheritance

The main concepts of object-oriented programming languages are: encapsulation, inheritance, and polymorphism. In an object-oriented language, 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.

Inheritance allows us to generate a new class from another, inheriting its attributes and methods, adapting or expanding them as necessary. To indicate that a class inherits from another class, we need to put the name of the class that is inherited between parentheses.

In OOPS terminology, it is said that "B inherits from A," "B is a class derived from A," "A is the base class of B," or "A is a superclass of B."

This facilitates the reuse of the code, since you can implement the basic behaviors and data in a base class and specialize them in the derived classes: