Mastering Python for Networking and Security - Second Edition

Book description

Tackle security and networking issues using Python libraries such as Nmap, requests, asyncio, and scapy

Key Features

  • Enhance your Python programming skills in securing systems and executing networking tasks
  • Explore Python scripts to debug and secure complex networks
  • Learn to avoid common cyber events with modern Python scripting

Book Description

It’s now more apparent than ever that security is a critical aspect of IT infrastructure, and that devastating data breaches can occur from simple network line hacks. As shown in this book, combining the latest version of Python with an increased focus on network security can help you to level up your defenses against cyber attacks and cyber threats.

Python is being used for increasingly advanced tasks, with the latest update introducing new libraries and packages featured in the Python 3.7.4 recommended version. Moreover, most scripts are compatible with the latest versions of Python and can also be executed in a virtual environment.

This book will guide you through using these updated packages to build a secure network with the help of Python scripting. You’ll cover a range of topics, from building a network to the procedures you need to follow to secure it. Starting by exploring different packages and libraries, you’ll learn about various ways to build a network and connect with the Tor network through Python scripting. You will also learn how to assess a network's vulnerabilities using Python security scripting. Later, you’ll learn how to achieve endpoint protection by leveraging Python packages, along with writing forensic scripts.

By the end of this Python book, you’ll be able to use Python to build secure apps using cryptography and steganography techniques.

What you will learn

  • Create scripts in Python to automate security and pentesting tasks
  • Explore Python programming tools that are used in network security processes
  • Automate tasks such as analyzing and extracting information from servers
  • Understand how to detect server vulnerabilities and analyze security modules
  • Discover ways to connect to and get information from the Tor network
  • Focus on how to extract information with Python forensics tools

Who this book is for

This Python network security book is for network engineers, system administrators, or any security professional looking to overcome networking and security challenges. You will also find this book useful if you’re a programmer with prior experience in Python. A basic understanding of general programming structures and the Python programming language is required before getting started.

Table of contents

  1. Mastering Python for Networking and Security Second Edition
  2. Why subscribe?
  3. Contributors
  4. About the author
  5. About the reviewers
  6. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Code in Action
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Reviews
  8. Section 1: The Python Environment and System Programming Tools
  9. Chapter 1: Working with Python Scripting
    1. Technical requirements
    2. Introduction to Python scripting
      1. Why choose Python?
      2. Multi-platform capabilities and versions
      3. Python 3 features
    3. Exploring Python data structures
      1. Lists
      2. Tuples
      3. Python dictionaries
    4. Python functions, classes, and managing exceptions
      1. Python functions
      2. Python classes
      3. Python inheritance
      4. Managing exceptions
    5. Python modules and packages
      1. What is a module in Python?
      2. Getting information from standard modules
      3. Difference between a Python module and a Python package
      4. Python Module Index
      5. Managing parameters in Python
    6. Managing dependencies and virtual environments
      1. Managing dependencies in a Python project
      2. Generating the requirements.txt file
      3. Working with virtual environments
      4. Configuring virtualenv
    7. Development environments for Python scripting
      1. Setting up a development environment
      2. PyCharm
      3. Debugging with PyCharm
      4. Debugging with Python IDLE
    8. Summary
    9. Questions
    10. Further reading
  10. Chapter 2: System Programming Packages
    1. Technical requirements
    2. Introducing system modules in Python
      1. The system (sys) module
      2. The operating system (os) module
      3. The platform module
      4. The subprocess module
    3. Working with the filesystem in Python
      1. Working with files and directories
      2. Reading and writing files in Python
      3. Opening a file with a context manager
      4. Reading a ZIP file using Python
    4. Managing threads in Python
      1. Creating a simple thread
      2. Working with the threading module
      3. Multithreading in Python
      4. Limitations of classic Python threads
      5. Concurrency in Python with ThreadPoolExecutor
      6. Executing ThreadPoolExecutor with a context manager
    5. Working with socket.io
      1. Implementing a server with socket.io
      2. Implementing a client that connects to the server
    6. Summary
    7. Questions
    8. Further reading
  11. Section 2: Network Scripting and Extracting Information from the Tor Network with Python
  12. Chapter 3: Socket Programming
    1. Technical requirements
    2. Introducing sockets in Python
      1. Network sockets in Python
      2. The socket module
      3. Basic client with the socket module
    3. Implementing an HTTP server in Python
      1. Testing the HTTP server
    4. Implementing a reverse shell with sockets
    5. Resolving IPS domains, addresses, and managing exceptions
      1. Gathering information with sockets
      2. Using the reverse lookup command
      3. Managing socket exceptions
    6. Port scanning with sockets
      1. Implementing a basic port scanner
      2. Advanced port scanner
    7. Implementing a simple TCP client and TCP server
      1. Implementing a server and client with sockets
      2. Implementing the TCP server
      3. Implementing the TCP client
    8. Implementing a simple UDP client and UDP server
      1. Implementing the UDP server
      2. Implementing the UDP client
    9. Summary
    10. Questions
    11. Further reading
  13. Chapter 4: HTTP Programming
    1. Technical requirements
    2. Introducing the HTTP protocol
      1. Reviewing the status codes
    3. Building an HTTP client with http.client
    4. Building an HTTP client with urllib.request
    5. Get response and request headers
      1. Extracting emails from a URL with urllib.request
      2. Downloading files with urllib.request
      3. Handling exceptions with urllib.request
    6. Building an HTTP client with requests
      1. Getting images and links from a URL with requests
      2. Making GET requests with the REST API
      3. Making POST requests with the REST API
      4. Managing a proxy with requests
      5. Managing exceptions with requests
    7. Building an HTTP client with httpx
    8. Authentication mechanisms with Python
      1. HTTP basic authentication with a requests module
      2. HTTP digest authentication with the requests module
    9. Summary
    10. Questions
    11. Further reading
  14. Chapter 5: Connecting to the Tor Network and Discovering Hidden Services
    1. Technical requirements
    2. Understanding the Tor Project and hidden services
      1. Exploring the Tor network
      2. What are hidden services?
    3. Tools for anonymity in the Tor network
      1. Connecting to the Tor network
      2. Node types in the Tor network
      3. Installing the Tor service
      4. ExoneraTor and Nyx
    4. Discovering hidden services with OSINT tools
      1. Search engines
      2. Inspecting onion address with onioff
      3. OnionScan as a research tool for the deep web
      4. Docker onion-nmap
    5. Modules and packages in Python for connecting to the Tor network
      1. Connecting to the Tor network from Python
      2. Extracting information from the Tor network with the stem module
    6. Tools that allow us to search hidden services and automate the crawling process in the Tor network
      1. Scraping information from the Tor network with Python tools
    7. Summary
    8. Questions
  15. Section 3: Server Scripting and Port Scanning with Python
  16. Chapter 6: Gathering Information from Servers
    1. Technical requirements
    2. Extracting information from servers with Shodan
      1. Accessing Shodan services
      2. The Shodan RESTful API
      3. Shodan search with Python
    3. Using Shodan filters and the BinaryEdge search engine
      1. Shodan filters
      2. BinaryEdge search engine
    4. Using the socket module to obtain server information
      1. Extracting server banners with Python
    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. Understanding and using the FuzzDB project
    7. Summary
    8. Questions
    9. Further reading
  17. Chapter 7: Interacting with FTP, SFTP, and SSH Servers
    1. Technical requirements
    2. Connecting with FTP servers
      1. Using the Python ftplib module
      2. Using ftplib to brute-force FTP user credentials
    3. Building an anonymous FTP scanner with Python
    4. Connecting with SSH servers with paramiko and pysftp
      1. Executing an SSH server on Debian Linux
      2. Introducing the paramiko module
      3. Establishing an SSH connection with paramiko
      4. Running commands with paramiko
      5. Using paramiko to brute-force SSH user credentials
      6. Establishing an SSH connection with pysftp
    5. Implementing SSH clients and servers with the asyncSSH and asyncio modules
    6. Checking the security in SSH servers with the ssh-audit tool
      1. Installing and executing ssh-audit
      2. Rebex SSH Check
    7. Summary
    8. Questions
    9. Further reading
  18. Chapter 8: Working with Nmap Scanner
    1. Technical requirements
    2. Introducing port scanning with Nmap
    3. Scan modes with python-nmap
      1. Implementing synchronous scanning
      2. Implementing asynchronous scanning
    4. Working with Nmap through the os and subprocess modules
    5. Discovering services and vulnerabilities with Nmap scripts
      1. Executing Nmap scripts to discover services
      2. Executing Nmap scripts to discover vulnerabilities
    6. Summary
    7. Questions
    8. Further reading
  19. Section 4: Server Vulnerabilities and Security in Python Modules
  20. Chapter 9: Interacting with Vulnerability Scanners
    1. Technical requirements
    2. Understanding vulnerabilities and exploits
      1. What is an exploit?
      2. Vulnerability formats
    3. Introducing the Nessus vulnerability scanner
      1. Installing and executing the Nessus vulnerability scanner
      2. Nessus vulnerabilities reports
      3. Accessing the Nessus API with Python
      4. Interacting with the Nessus server
    4. Introducing the OpenVAS vulnerability scanner
      1. Installing the OpenVAS vulnerability scanner
      2. Understanding the web interface
      3. Scanning a machine using OpenVAS
    5. Accessing OpenVAS with Python
    6. Summary
    7. Questions
    8. Further reading
  21. Chapter 10: Identifying Server Vulnerabilities in Web Applications
    1. Technical requirements
    2. Understanding vulnerabilities in web applications with OWASP
      1. Testing XSS
    3. Analyzing and discovering vulnerabilities in CMS web applications
      1. Using CMSMap
      2. Other CMS scanners
    4. Discovering SQL vulnerabilities with Python tools
      1. Introduction to SQL injection
      2. Identifying pages vulnerable to SQL injection
      3. Introducing SQLmap
      4. Using SQLmap to test a website for a SQL injection vulnerability
      5. Scanning for SQL injection vulnerabilities with the Nmap port scanner
    5. Testing Heartbleed and SSL/TLS vulnerabilities
      1. Vulnerabilities in the Secure Sockets Layer (SSL) protocol
      2. Finding vulnerable servers in the Censys search engine
      3. Analyzing and exploiting the Heartbleed vulnerability (OpenSSL CVE-2014-0160)
      4. Scanning for the Heartbleed vulnerability with the Nmap port scanner
    6. Scanning TLS/SSL configurations with SSLyze
    7. Summary
    8. Questions
    9. Further reading
  22. Chapter 11: Security and Vulnerabilities in Python Modules
    1. Technical requirements
    2. Exploring security in Python modules
      1. Python functions with security issues
      2. Input/output validation
      3. Eval function security
      4. Controlling user input in dynamic code evaluation
      5. Pickle module security
      6. Security in a subprocess module
      7. Using the shlex module
      8. Insecure temporary files
    3. Static code analysis for detecting vulnerabilities
      1. Introducing static code analysis
      2. Introducing Pylint and Dlint
      3. The Bandit static code analyzer
      4. Bandit test plugins
    4. Detecting Python modules with backdoors and malicious code
      1. Insecure packages in PyPi
      2. Backdoor detection in Python modules
      3. Denial-of-service vulnerability in urllib3
    5. Security in Python web applications with the Flask framework
      1. Rendering an HTML page with Flask
      2. Cross-site scripting (XSS) in Flask
      3. Disabling debug mode in the Flask app
      4. Security redirections with Flask
    6. Python security best practices
      1. Using packages with the __init__.py interface
      2. Updating your Python version
      3. Installing virtualenv
      4. Installing dependencies
      5. Using services to check security in Python projects
    7. Summary
    8. Questions
    9. Further reading
  23. Section 5: Python Forensics
  24. Chapter 12: Python Tools for Forensics Analysis
    1. Technical requirements
    2. Volatility framework for extracting data from memory and disk images
      1. Installing Volatility
      2. Identifying the image profile
      3. Volatility plugins
    3. Connecting and analyzing SQLite databases
      1. SQLite databases
      2. The sqlite3 module
    4. Network forensics with PcapXray
    5. Getting information from the Windows registry
      1. Introducing python-registry
    6. Logging in Python
      1. Logging levels
      2. Logging module components
    7. Summary
    8. Questions
    9. Further reading
  25. Chapter 13: Extracting Geolocation and Metadata from Documents, Images, and Browsers
    1. Technical requirements
    2. Extracting geolocation information
    3. Extracting metadata from images
      1. Introduction to EXIF and the PIL module
      2. Getting the EXIF data from an image
    4. Extracting metadata from PDF documents
    5. Identifying the technology used by a website
    6. Extracting metadata from web browsers
      1. Firefox forensics with Python
      2. Chrome forensics with Python
    7. Summary
    8. Questions
    9. Further reading
  26. Chapter 14: Cryptography and Steganography
    1. Technical requirements
    2. Encrypting and decrypting information with pycryptodome
      1. Introduction to cryptography
      2. Introduction to pycryptodome
    3. Encrypting and decrypting information with cryptography
      1. Introduction to the cryptography module
    4. Steganography techniques for hiding information in images
      1. Introduction to steganography
    5. Steganography with Stepic
    6. Generating keys securely with the secrets and hashlib modules
      1. Generating keys securely with the secrets module
      2. Generating keys securely with the hashlib module
    7. Summary
    8. Questions
    9. Further reading
  27. 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 – Connecting to the Tor Network and Discovering Hidden Services
    6. Chapter 6 – Gathering Information from Servers
    7. Chapter 7 – Interacting with FTP, SFTP, and SSH Servers
    8. Chapter 8 – Working with Nmap Scanner
    9. Chapter 9 – Interacting with Vulnerability Scanners
    10. Chapter 10 – Identifying Server Vulnerabilities in Web Applications
    11. Chapter 11 – Security and Vulnerabilities in Python Modules
    12. Chapter 12 – Python Tools for Forensics Analysis
    13. Chapter 13 – Extracting Geolocation and Metadata from Documents, Images, and Browsers
    14. Chapter 14 – Cryptography and Steganography
  28. 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 - Second Edition
  • Author(s): José Ortega
  • Release date: January 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781839217166