Python for Security and Networking - Third Edition

Book description

Gain a firm, practical understanding of securing your network and utilize Python’s packages to detect vulnerabilities in your application

Key Features

  • Discover security techniques to protect your network and systems using Python
  • Create scripts in Python to automate security and pentesting tasks
  • Analyze traffic in a network and extract information using Python

Book Description

Python’s latest updates add numerous libraries that can be used to perform critical security-related missions, including detecting vulnerabilities in web applications, taking care of attacks, and helping to build secure and robust networks that are resilient to them. This fully updated third edition will show you how to make the most of them and improve your security posture.

The first part of this book will walk you through Python scripts and libraries that you’ll use throughout the book. Next, you’ll dive deep into the core networking tasks where you will learn how to check a network’s vulnerability using Python security scripting and understand how to check for vulnerabilities in your network – including tasks related to packet sniffing. You’ll also learn how to achieve endpoint protection by leveraging Python packages along with writing forensics scripts.

The next part of the book will show you a variety of modern techniques, libraries, and frameworks from the Python ecosystem that will help you extract data from servers and analyze the security in web applications. You’ll take your first steps in extracting data from a domain using OSINT tools and using Python tools to perform forensics tasks.

By the end of this book, you will be able to make the most of Python to test the security of your network and applications.

What you will learn

  • Program your own tools in Python that can be used in a Network Security process
  • Automate tasks of analysis and extraction of information from servers
  • Detect server vulnerabilities and analyze security in web applications
  • Automate security and pentesting tasks by creating scripts with Python
  • Utilize the ssh-audit tool to check the security in SSH servers
  • Explore WriteHat as a pentesting reports tool written in Python
  • Automate the process of detecting vulnerabilities in applications with tools like Fuxploider

Who this book is for

This Python book is for network engineers, system administrators, and other security professionals looking to overcome common networking and security issues using Python. You will also find this book useful if you're an experienced programmer looking to explore Python’s full range of capabilities. A basic understanding of general programming structures as well as familiarity with the Python programming language is a prerequisite.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
  2. Section 1: Python Environment and System Programming Tools
  3. Working with Python Scripting
    1. Technical requirements
    2. Learn about data structures and collections in Python
      1. Python lists
        1. Adding elements to a list
        2. Reversing a list
        3. Searching elements in a list
      2. Python tuples
      3. Python dictionaries
      4. Remove an item from a dictionary in Python
    3. Working with functions, classes, and objects in Python
      1. Python functions
      2. Python classes
      3. Python inheritance
      4. Advantages of Python inheritance
    4. Working with files in Python
      1. Reading and writing files in Python
      2. Learn and understand exceptions management in Python
    5. Python modules and packages
      1. What is a module in Python?
      2. How to import modules in Python
      3. Getting information from modules
      4. Difference between a Python module and a Python package
      5. Managing parameters in Python
      6. Managing parameters with OptionParser
    6. Managing dependencies and virtual environments
      1. Managing dependencies in a Python project
      2. Install Python modules
      3. Generating the requirements.txt file
      4. Working with virtual environments
      5. Configuring virtualenv
    7. Development environments for Python scripting
      1. Setting up a development environment
      2. Debugging with Python IDLE
      3. PyCharm
      4. Debugging with PyCharm
    8. Summary
    9. Questions
    10. Further reading
  4. System Programming Packages
    1. Technical requirements
      1. Interact with the operating system in Python
    2. Working with the filesystem in Python
      1. Working with files and directories
      2. Reading a ZIP file using Python
    3. Executing commands with the subprocess module
      1. Setting up a virtualenv with subprocess
    4. Managing threads in Python
      1. Creating a simple thread
      2. Working with the threading module
      3. Multiprocessing in Python
    5. Multithreading and concurrency in Python
      1. Multithreading in Python
      2. Concurrency in Python with ThreadPoolExecutor
      3. Executing ThreadPoolExecutor with a context manager
    6. Summary
    7. Questions
    8. Further reading
  5. Section 2: Network Scripting and Packet Sniffing with Python
  6. Socket Programming
    1. Technical requirements
    2. Understanding the socket package for network requests
      1. Network sockets in Python
      2. The socket module
        1. Server and client socket methods
      3. Gathering information with sockets
      4. Managing socket exceptions
      5. Basic client with the socket module
    3. Port scanning with sockets
      1. Implementing a port scanner
      2. Advanced port scanner
    4. Implementing a reverse shell with sockets
    5. 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
    6. Implementing a simple UDP client and UDP server
      1. Implementing the UDP server
      2. Implementing the UDP client
    7. Implementing an HTTP server in Python
      1. Testing the HTTP server
      2. Sending files via sockets
    8. Implementing secure sockets with the TLS and SSL modules
    9. Summary
    10. Questions
    11. Further reading
  7. HTTP Programming and Web Authentication
    1. Technical requirements
    2. Building an HTTP client with urllib.request
      1. Introducing the HTTP protocol
      2. Introducing the urllib module
    3. Get request and response headers
      1. Extracting emails from a URL with urllib.request
      2. Downloading files with urllib.request
      3. Handling exceptions with urllib.request
    4. Building an HTTP client with requests
      1. Getting images and links from a URL with requests
      2. Making requests with the REST API
      3. Managing a proxy with requests
      4. Managing exceptions with requests
    5. Authentication mechanisms with Python
      1. HTTP basic authentication with the requests module
      2. HTTP digest authentication with the requests module
    6. Implementing OAuth clients in Python with the requests-oauthlib module
      1. OAuth roles
      2. OAuth workflow
    7. Implementing a client with requests_oauthlib
    8. Implementing JSON Web Tokens (JWTs) in Python
      1. How does a JSON Web Token work?
      2. Working with PyJWT
    9. Summary
    10. Questions
    11. Further reading
  8. Analyzing Network Traffic and Packet Sniffing
    1. Technical requirements
    2. Capturing and injecting packets with pcapy-ng
      1. Capturing packets with pcapy-ng
      2. Reading headers from packets
      3. Reading pcap files with pcapy-ng
    3. Capturing and injecting packets with scapy
      1. Introduction to scapy
      2. Scapy commands
      3. Sending packets with scapy
      4. Network discovery with scapy
    4. Port scanning and traceroute with scapy
      1. Port scanning with scapy
      2. Traceroute with scapy
    5. Reading pcap files with scapy
      1. Read DHCP requests
      2. Writing a pcap file
    6. Packet-sniffing with scapy
      1. Network forensics with scapy
    7. Working with scapy to detect ARP spoofing attacks
      1. Detection of false ARP attacks using Scapy
    8. Summary
    9. Questions
    10. Further reading
  9. Section 3: Server Scripting and Port Scanning with Python
  10. Gathering Information from Servers with OSINT Tools
    1. Technical requirements
    2. Introducing Open Source Intelligence (OSINT)
      1. Google Dorks and the Google Hacking Database
      2. Maltego
      3. Photon
      4. The Harvester
      5. Censys
      6. crt.sh
      7. DnsDumpster
      8. WaybackMachine
      9. OSINT framework
      10. Blackbird
      11. The Shodan search engine
      12. The BinaryEdge search engine
    3. Getting information using Google Dorks
      1. Google Dorks
      2. Katana: a Python Tool for Google Hacking
      3. Dorks hunter
    4. Getting information using SpiderFoot
      1. SpiderFoot modules
    5. Getting information on DNS servers with DNSPython and DNSRecon
      1. The DNS protocol
      2. The DNSPython module
      3. DNSRecon
    6. Getting vulnerable addresses in servers with fuzzing
      1. The fuzzing process
      2. Web fuzzing
      3. Understanding and using the FuzzDB project
        1. Identifying predictable login pages with the FuzzDB project
        2. Discovering SQL injection with the FuzzDB project
      4. Wfuzz
    7. Summary
    8. Questions
    9. Further reading
  11. Interacting with FTP, SFTP, and SSH Servers
    1. Technical requirements
    2. Connecting to FTP servers
      1. FTP protocol
      2. Using the Python ftplib module
        1. Transferring files with FTP
        2. Other ftplib functions
      3. 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. Using AutoAddPolicy
      5. Running commands with paramiko
      6. Using paramiko to brute-force SSH user credentials
      7. Establishing an SSH connection with pysftp
    5. Implementing an SSH server with paramiko
    6. Checking the security of SSH servers
      1. Installing and executing ssh-audit
      2. Rebex SSH Check
    7. Summary
    8. Questions
    9. Further reading
  12. Working with Nmap Scanner
    1. Technical requirements
    2. Introducing port scanning with Nmap
      1. Scanning types with nmap
      2. Port scanning with python-nmap
      3. Extracting information with nmap
    3. Synchronous and asynchronous scanning with python-nmap
      1. Implementing synchronous scanning
      2. Implementing asynchronous scanning
    4. Discovering services and vulnerabilities with Nmap scripts
      1. Executing Nmap scripts to discover services
      2. Executing Nmap scripts to discover vulnerabilities
      3. Detecting vulnerabilities with Nmap-vulners script
      4. Detecting vulnerabilities with the Nmap-vulscan script
    5. Port scanning via online services
      1. Scanless port scanner
    6. Summary
    7. Questions
    8. Further reading
  13. Section 4: Server Vulnerabilities and Security in Web Applications
  14. Interacting with Vulnerability Scanners
    1. Technical requirements
    2. Introducing the OpenVAS vulnerability scanner
      1. Installing the OpenVAS vulnerability scanner
      2. Understanding the web interface
      3. Scanning a target using OpenVAS
        1. Creating the target
        2. Creating the task
        3. Analyzing reports
        4. Vulnerabilities databases
    3. Accessing OpenVAS with Python
    4. Introducing OWASP ZAP as an automated security testing tool
      1. Using OWASP ZAP
    5. Interacting with OWASP ZAP using Python
    6. WriteHat as a pentesting reports tool
    7. Summary
    8. Questions
    9. Further reading
  15. Interacting with Server Vulnerabilities in Web Applications
    1. Technical requirements
    2. Understanding vulnerabilities in web applications with OWASP
      1. Testing Cross-Site Scripting (XSS) vulnerabilities
    3. Analyzing and discovering vulnerabilities in CMS web applications
      1. Using CMSmap
      2. Vulnx as a CMS scanner
    4. Discovering vulnerabilities in Tomcat server applications
      1. Installing the Tomcat server
      2. Testing the Tomcat server with ApacheTomcatScanner
      3. Finding vulnerable Tomcat servers in the Censys search engine
      4. Scanning vulnerabilities with the Nmap port scanner
    5. Discovering SQL vulnerabilities with Python tools
      1. Introduction to SQL injection
      2. Identifying websites 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 sqlifinder
      6. Scanning for SQL injection vulnerabilities with the Nmap port scanner
    6. Automating the process of detecting vulnerabilities in web applications
      1. Detecting an open redirect vulnerability
      2. Detecting vulnerabilities with Fuxploider
    7. Summary
    8. Questions
    9. Further reading
  16. Obtain Information from Vulnerabilities Databases
    1. Technical requirements
    2. Identify and understand vulnerabilities and exploits
      1. What is an exploit?
      2. Vulnerability formats
    3. Searching for vulnerabilities in the NVD
      1. Introducing NIST’s NVD
      2. Searching for vulnerabilities
    4. Searching for vulnerabilities in the Vulners database
    5. Searching for vulnerabilities with Pompem
    6. Summary
    7. Questions
    8. Further reading
  17. Section 5: Python Forensics
  18. Extracting Geolocation and Metadata from Documents, Images, and Browsers
    1. Technical requirements
    2. Extracting geolocation information
    3. Python modules for extracting geolocation information
    4. Extracting metadata from images
      1. Introduction to EXIF and the PIL module
      2. Getting the EXIF data from an image
    5. Extracting metadata from PDF documents
    6. Extracting metadata with PyPDF2
    7. Extracting metadata with PyMuPDF
    8. Identifying the technology used by a website
      1. Wappalyzer
      2. WebApp Information Gatherer (WIG)
    9. Extracting metadata from web browsers
      1. Firefox forensics with Python
      2. Chrome forensics with Python
        1. Chrome forensics with Hindsight
    10. Summary
    11. Questions
    12. Further reading
  19. Python Tools for Brute-Force Attacks
    1. Technical requirements
    2. Dictionary builders for brute-force attacks
      1. Brute-force dictionary generation with pydictor
    3. Password list generator
    4. Tools for brute-force attacks in Python
      1. Obtaining subdomains by brute force
      2. Brute-force attacks with BruteSpray
      3. Brute-force attacks with Cerbrutus
    5. Executing brute-force attacks for web applications
      1. Executing a WordPress site
    6. Executing brute-force attacks for ZIP files
      1. Handling ZIP files in Python
      2. Executing brute-force attacks for password-protected ZIP files
    7. Summary
    8. Questions
    9. Further reading
  20. Cryptography and Code Obfuscation
    1. Technical requirements
      1. Introduction to cryptography
    2. Encrypting and decrypting information with pycryptodome
      1. Introduction to pycryptodome
        1. Encrypting and decrypting with the DES algorithm
        2. Encrypting and decrypting with the AES algorithm
        3. Generating RSA signatures using pycryptodome
    3. Encrypting and decrypting information with cryptography
      1. Introduction to the cryptography module
        1. Symmetric encryption with the fernet package
        2. Symmetric encryption with the ciphers package
    4. 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
        1. Checking the integrity of a file
    5. Python tools for code obfuscation
      1. Code obfuscation with pyarmor
    6. Summary
    7. Questions
    8. Further reading
  21. Assessments – Answers to the End-of-Chapter Questions
  22. Other Books You May Enjoy
  23. Index

Product information

  • Title: Python for Security and Networking - Third Edition
  • Author(s): José Manuel Ortega
  • Release date: June 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837637553