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)

Port-scanning and traceroute with scapy

At this point, we will see a port scanner on a certain network segment. In the same way we do port-scanning with nmap, with scapy we could also perform a simple port-scanner that tells us for a specific host and a list of ports, whether they are open or closed.

Port-scanning with scapy

In the following example, we see that we have defined a analyze_port() function that has as parameters the host and port to analyze.

You can find the following code in the port_scan_scapy.py file:

from scapy.all import sr1, IP, TCP

OPEN_PORTS = []

def analyze_port(host, port):
"""
Function that determines the status of a port: Open / closed
:param host: target
:param port: port to test
:type...