Mastering Python for Networking and Security

Book description

Master Python scripting to build a network and perform security operations

Key Features

  • Learn to handle cyber attacks with modern Python scripting
  • Discover various Python libraries for building and securing your network
  • Understand Python packages and libraries to secure your network infrastructure

Book Description

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.

What you will learn

  • Develop Python scripts for automating security and pentesting tasks
  • Discover the Python standard library's main modules used for performing security-related tasks
  • Automate analytical tasks and the extraction of information from servers
  • Explore processes for detecting and exploiting vulnerabilities in servers
  • Use network software for Python programming
  • Perform server scripting and port scanning with Python
  • Identify vulnerabilities in web applications with Python
  • Use Python to extract metadata and forensics

Who this book is for

This book is ideal for network engineers, system administrators, or any security professional looking at tackling networking and security challenges. Programmers with some prior experience in Python will get the most out of this book. Some basic understanding of general programming structures and Python is required.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Python for Networking and Security
  3. Packt Upsell
    1. Why subscribe?
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Working with Python Scripting
    1. Technical requirements
    2. Programming and installing Python
      1. Introducing Python scripting
        1. Why choose Python?
        2. Multi-platform
        3. Object-Oriented Programming
      2. Obtaining and installing Python
        1. Installing Python on Windows
        2. Installing Python for Linux
    3. Python collections
      1. Lists
        1. Reversing a List
        2. Comprehension lists
      2. Tuples
      3. Dictionaries
    4. Python functions and managing exceptions
      1. Python functions
      2. Managing exceptions 
    5. Python as an OOP language
      1. Inheritance
    6. The OMSTD methodology and STB Module for Python scripting
      1. Python packages and modules
        1. What is a module in Python?
        2. Difference Between a Python Module and a Python Package
      2. Passing parameters in Python
      3. Managing dependencies in a Python project
        1. Generating the requirements.txt file
      4. Working with virtual environments
        1. Using virtualenv and virtualwrapper
      5. The STB (Security Tools Builder) module
    7. The main development environments for script-development
      1. Setting up a development environment
      2. Pycharm
      3. WingIDE
      4. Debugging with WingIDE
    8. Summary
    9. Questions
    10. Further reading
  7. System Programming Packages
    1. Technical requirements
    2. Introducing system modules in python
      1. The system module
      2. The operating system module
        1. Contents of the current working directory
        2. Determining the operating system
      3. Subprocess module
    3. Working with the filesystem in Python
      1. Accessing files and directories
        1. Recursing through directories
        2. Checking whether a specific path is a file or directory
        3. Checking whether a file or directory exists
        4. Creating directories in Python
      2. Reading and writing files in Python
        1. File methods
        2. Opening a file
        3. With a Context Manager
        4. Reading a file line by line
    4. Threads in Python
      1. Introduction to Threads
        1. Types of threads
        2. Processes vs Threads
      2. Creating a simple Thread
      3. Threading module
    5. Multithreading and concurrency in Python
      1. Introduction to Multithreading
      2. Multithreading in Python
      3. Limitations with classic python threads
      4. Concurrency in python with ThreadPoolExecutor
        1. Creating ThreadPoolExecutor
        2. ThreadPoolExecutor in practice
        3. Executing ThreadPoolExecutor with Context Manager
    6. Python
      1. Introducing WebSockets
      2. aiohttp and asyncio
      3. Implementing a Server with
    7. Summary
    8. Questions
    9. Further reading
  8. Socket Programming
    1. Technical requirements
    2. Introduction to sockets
      1. Network sockets in Python
      2. The socket module
      3. Socket methods
        1. Server socket methods
        2. Client socket methods
      4. Basic client with the socket module
    3. Creating a simple TCP client and TCP server
      1. Creating a server and client with sockets
      2. Implementing the TCP serverIn this example, we are going to create a multithreaded TCP server.
      3. Implementing the TCP client
    4. Creating a simple UDP client and UDP server
      1. Introduction to the UDP protocol
      2. UDP client and server with the socket module
        1. Implementing the UDP Server
        2. Implementing the UDP client
    5. Resolving IP addresses and domains
      1. Gathering information with sockets
      2. Reverse lookup
    6. Practical use cases for sockets
      1. Port scanner with sockets
      2. Managing socket exceptions
    7. Summary
    8. Questions
    9. Further reading
  9. HTTP Programming
    1. Technical requirements
    2. HTTP protocol and building HTTP clients in python
      1. Introduction to  the HTTP Protocol
      2. Building an HTTP Client with httplib
    3. Building an HTTP Client with urllib2
      1. Introduction to urllib2
      2. Response objects
      3. Status codes
      4. Checking HTTP headers with urllib2
      5. Using the urllib2 Request class
      6. Customizing requests with urllib2
        1. Getting emails from a URL with urllib2
        2. Getting links from a URL with urllib2
    4. Building an HTTP Client with requests
      1. Introduction to requests
      2. Requests advantages
      3. Making GET Requests with the REST API
      4. Making POST Requests with the REST API
      5. Making Proxy Requests
      6. Managing exceptions with requests
    5. Authentication mechanisms with Python
      1. Authentication with the requests module
      2. HTTP Basic authentication
      3. HTTP Digest Authentication
    6. Summary
    7. Questions
    8. Further Reading
  10. Analyzing Network Traffic
    1. Technical requirements
    2. Capturing and injecting packets with pcapy
      1. Introduction to pcapy
      2. Capturing packets with pcapy
      3. Reading headers from packets
    3. Capturing and injecting packets with scapy
      1. What can we do with scapy?
      2. Scapy advantages and disadvantages
      3. Introduction to scapy
      4. Scapy commands
      5. Sending packets with scapy
      6. Packet-sniffing with scapy
      7. Using Lamda functions with scapy
        1. Filtering UDP packets
    4. Port-scanning and traceroute with scapy
      1. Port-scanning with scapy
      2. Traceroute command with scapy
    5. Reading pcap files with scapy
      1. Introduction to the PCAP format
      2. Reading pcap files with scapy
      3. Writing a pcap file
      4. Sniffing from a pcap file with scapy
        1. Network Forensic with scapy
    6. Summary
    7. Questions
    8. Further reading
  11. Gathering Information from Servers
    1. Technical requirements
    2. Introduction to gathering information
    3. Extracting information from servers with Shodan
      1. Introduction to Shodan
      2. Accessing Shodan services
      3. Shodan filters
      4. Shodan search with python
      5. Performing searches by a given host
      6. Searching for FTP servers
    4. Using python to obtain server information
      1. Extracting servers banners with python
      2. Finding whois information about a server
    5. Getting information on dns servers with DNSPython
      1. DNS protocol
      2. DNS servers
      3. The DNSPython module
    6. Getting vulnerable addresses in servers with Fuzzing
      1. The Fuzzing process
      2. The FuzzDB project
      3. Fuzzing with python with pywebfuzz
    7. Summary
    8. Questions
    9. Further reading
  12. Interacting with FTP, SSH, and SNMP Servers
    1. Technical requirements
    2. Connecting with FTP servers
      1. The File Transfer Protocol (FTP)
      2. The Python ftplib module
      3. Transferring files with FTP
      4. Using ftplib to brute force FTP user credentials
      5. Building an anonymous FTP scanner with Python
    3. Connecting with SSH servers
      1. The Secure Shell (SSH) protocol
      2. Introduction to Paramiko
        1. Installing Paramiko
      3. Establishing SSH connection with Paramiko
      4. Running commands with Paramiko
      5. SSH connection with brute-force processing
      6. SSH connection with pxssh
      7. Running a command on a remote SSH server
    4. Connecting with SNMP servers
      1. The Simple Network Management Protocol (SNMP)
      2. PySNMP
    5. Summary
    6. Questions
    7. Further reading
  13. Working with Nmap Scanners
    1. Technical requirements
    2. Introducing port scanning with Nmap
      1. Introducing to port scanning
      2. Scanning types with Nmap
    3. Port scanning with python-nmap
      1. Introduction to python-nmap
      2. Installing python-nmap
      3. Using python-nmap
    4. Scan modes with python-nmap
      1. Synchronous scanning
      2. Asynchronous scanning
    5. Vulnerabilities with Nmap scripts
      1. Executing Nmap scripts to detect vulnerabilities
      2. Detecting vulnerabilities in FTP service
    6. Summary
    7. Questions
    8. Further reading
  14. Connecting with the Metasploit Framework
    1. Technical requirements
    2. Introducing the Metasploit framework
      1. Introduction to exploiting
      2. Metasploit framework
      3. Metasploit architecture
    3. Interacting with the Metasploit framework
      1. Introduction to msfconsole
      2. Introduction to the Metasploit exploit module
      3. Introduction to the Metasploit payload module
      4. Introduction to msgrpc
    4. Connecting the Metasploit framework and Python
      1. Introduction to MessagePack
      2. Installing python-msfrpc
      3. Executing API calls
      4. Exploiting the Tomcat service with Metasploit
      5. Using the tomcat_mgr_deploy exploit
    5. Connecting Metasploit with pyMetasploit
      1. Introduction to PyMetasploit
      2. Interacting with the Metasploit framework from python
    6. Summary
    7. Questions
    8. Further reading
  15. Interacting with the Vulnerabilities Scanner
    1. Technical requirements
    2. Introducing vulnerabilities
      1. Vulnerabilities and exploits
        1. What is a vulnerability?
        2. What is an exploit?
      2. Vulnerabilities format
    3. Introducing the Nessus Vulnerabilities scanner
      1. Installing the Nessus Vulnerabilities scanner
      2. Executing the Nessus Vulnerabilities scanner
      3. Identifying vulnerabilities with Nessus
    4. Accessing the Nessus API with Python
      1. Installing the nessrest Python module
      2. Interacting with the nesssus server
    5. Introducing the Nexpose Vulnerabilities scanner
      1. Installing the Nexpose Vulnerabilities scanner
      2. Executing the Nexpose Vulnerabilities scanner
    6. Accessing the Nexpose API with Python
      1. Installing the pynexpose Python Module
    7. Summary
    8. Questions
    9. Further reading
  16. Identifying Server Vulnerabilities in Web Applications
    1. Technical requirements
    2. Introducing vulnerabilities in web applications with OWASP
      1. Introduction to OWASP
      2. OWASP common attacks
      3. Testing Cross-site scripting (XSS)
    3. W3af scanner vulnerabilities in web applications
      1. W3af overview
      2. W3AF profiles
      3. W3af install
      4. W3af in Python
    4. Discovering sql vulnerabilities with Python tools
      1. Introduction to SQL injection
      2. Identifying pages vulnerable to SQL Injection
      3. Introducing SQLmap
      4. Installing SQLmap
      5. Using SQLMAP to test a website for a SQL Injection vulnerability
        1. Other commands
      6. Other tools for detecting SQL Injection vulnerabilities
        1. DorkMe
        2. XSScrapy
    5. Testing heartbleed and SSL/TLS vulnerabilities
      1. Introducing OpenSSL
      2. Finding vulnerable servers in Shodan
      3. Heartbleed vulnerability (OpenSSL CVE-2014-0160)
      4. Other tools for testing openssl vulnerability
        1. Heartbleed-masstest
        2. Scanning for Heartbleed with the nmap port scanner
        3. Analyzing SSL/TLS configurations with SSLyze script 
        4. Other services
    6. Summary
    7. Questions
    8. Further reading
  17. Extracting Geolocation and Metadata from Documents, Images, and Browsers
    1. Technical Requirements
    2. Extracting geolocation information
      1. Introduction to geolocation
      2. Introduction to Pygeoip
      3. Introduction to pygeocoder
      4. The MaxMind database in Python
    3. Extracting metadata from images
      1. Introduction to Exif and the PIL module
      2. Getting the EXIF data from an image
      3. Understanding Exif Metadata
      4. Extracting metadata from web images
    4. Extracting metadata from pdf documents
      1. Introduction to PyPDF2
      2. Peepdf
    5. Identifying the technology used by a website
      1. Introduction to the builtwith module
      2. Wappalyzer
      3. wig – webapp information gatherer
    6. Extracting metadata from web browsers
      1. Firefox Forensics in Python with dumpzilla
        1. Dumpzilla command line
      2. Firefox forensics in Python with firefeed
      3. Chrome forensics with python
      4. Chrome forensics with Hindsight
    7. Summary
    8. Questions
    9. Further reading
  18. Cryptography and Steganography
    1. Technical requirements
    2. Encrypting and decrypting information with pycrypto
      1. Introduction to cryptography
      2. Introduction to pycrypto
      3. Encrypting and decrypting with the DES algorithm
      4. Encrypting and decrypting with the AES algorithm
      5. File encryption with AES
      6. File decryption with AES
    3.  Encrypting and decrypting information with cryptography
      1. Introduction to cryptography
      2. Symmetric encryption with the fernet package
      3. Using passwords with the fernet package
      4. Symmetric encryption with the ciphers package
    4. Steganography techniques for hiding information in images
      1. Introduction to Steganography
      2. Steganography with Stepic
      3. Hiding data inside images with stepic
    5. Summary
    6. Questions
    7. Further reading
  19. Assessments
    1. Chapter 1 : Working with Python Scripting
    2. Chapter 2: System Programming Packages
    3. Chapter 3: Socket Programming
    4. Chapter 4: HTTP Programming
    5. Chapter 5: Analyzing Network Traffic
    6. Chapter 6: Gathering Information from Servers
    7. Chapter 7: Interacting with FTP, SSH, and SNMP Servers
    8. Chapter 8: Working with Nmap Scanners
    9. Chapter 9: Connecting with the Metasploit Framework
    10. Chapter 10: Interacting with the Vulnerabilities Scanner
    11. Chapter 11: Identifying Server Vulnerabilities in Web Applications
    12. Chapter 12: Extracting Geolocation and Metadata from Documents, Images, and Browsers
    13. Chapter 13: Cryptography and Steganography
  20. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering Python for Networking and Security
  • Author(s): Jose Manuel Ortega
  • Release date: September 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788992510