Python for Security and Networking - José Manuel Ortega - E-Book

Python for Security and Networking E-Book

José Manuel Ortega

0,0
28,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.


 

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 624

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Python for Security and Networking

Third Edition

Leverage Python modules and tools in securing your network and applications

José Manuel Ortega

BIRMINGHAM—MUMBAI

“Python” and the Python Logo are trademarks of the Python Software Foundation.

Python for Security and Networking

Third Edition

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Senior Publishing Product Manager: Aaron Tanna

Acquisition Editor – Peer Reviews: Gaurav Gavas

Project Editor: Namrata Katare

Content Development Editor: Liam Thomas Draper

Copy Editor: Safis Editing

Technical Editor: Aniket Shetty

Proofreader: Safis Editing

Indexer: Rekha Nair

Presentation Designer: Rajesh Shirsath

Developer Relations Marketing Executive: Meghal Patel

First published: September 2018

Second edition: December 2020

Third edition: June 2023

Production reference: 1310523

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-83763-755-3

www.packt.com

Contributors

About the author

José Manuel Ortega is a software engineer with focus on new technologies, open source, security, and testing. His career target from the beginning has been to specialize in Python and security testing projects.

He has worked as a security tester engineer and his functions have been analyzing and testing the security of applications, both in web and mobile environments. In recent years, he has developed an interest in security development, especially in pentesting with Python.

He has collaborated with universities and other institutions, presenting articles and holding conferences. He has also been a speaker at various conferences, both nationally and internationally, and is very enthusiastic to learn about new technologies and loves to share his knowledge with the developer community.

I would like to thank my family and friends for their support in writing this book, the publisher for giving me the opportunity to write a new edition of this book, and the people involved in its revision.

About the reviewer

Christian Ghigliotty is an experienced technologist with over eight and half years of experience across multiple disciplines within information security, serving as both a practitioner and a leader. He was part of the influential security program at Etsy, and helped build the security organization at Compass, a tech-enabled real estate brokerage. He is currently building the security architecture and engineering functions at the New York-based tech company Justworks.

Join our community on Discord

Join our community’s Discord space for discussions with the author and other readers:

https://packt.link/SecNet

Contents

Preface

Who this book is for

What this book covers

To get the most out of this book

Get in touch

Section 1: Python Environment and System Programming Tools

Working with Python Scripting

Technical requirements

Learn about data structures and collections in Python

Python lists

Adding elements to a list

Reversing a list

Searching elements in a list

Python tuples

Python dictionaries

Remove an item from a dictionary in Python

Working with functions, classes, and objects in Python

Python functions

Python classes

Python inheritance

Advantages of Python inheritance

Working with files in Python

Reading and writing files in Python

Learn and understand exceptions management in Python

Python modules and packages

What is a module in Python?

How to import modules in Python

Getting information from modules

Difference between a Python module and a Python package

Managing parameters in Python

Managing parameters with OptionParser

Managing dependencies and virtual environments

Managing dependencies in a Python project

Install Python modules

Generating the requirements.txt file

Working with virtual environments

Configuring virtualenv

Development environments for Python scripting

Setting up a development environment

Debugging with Python IDLE

PyCharm

Debugging with PyCharm

Summary

Questions

Further reading

System Programming Packages

Technical requirements

Interact with the operating system in Python

Working with the filesystem in Python

Working with files and directories

Reading a ZIP file using Python

Executing commands with the subprocess module

Setting up a virtualenv with subprocess

Managing threads in Python

Creating a simple thread

Working with the threading module

Multiprocessing in Python

Multithreading and concurrency in Python

Multithreading in Python

Concurrency in Python with ThreadPoolExecutor

Executing ThreadPoolExecutor with a context manager

Summary

Questions

Further reading

Section 2: Network Scripting and Packet Sniffing with Python

Socket Programming

Technical requirements

Understanding the socket package for network requests

Network sockets in Python

The socket module

Server and client socket methods

Gathering information with sockets

Managing socket exceptions

Basic client with the socket module

Port scanning with sockets

Implementing a port scanner

Advanced port scanner

Implementing a reverse shell with sockets

Implementing a simple TCP client and TCP server

Implementing a server and client with sockets

Implementing the TCP server

Implementing the TCP client

Implementing a simple UDP client and UDP server

Implementing the UDP server

Implementing the UDP client

Implementing an HTTP server in Python

Testing the HTTP server

Sending files via sockets

Implementing secure sockets with the TLS and SSL modules

Summary

Questions

Further reading

HTTP Programming and Web Authentication

Technical requirements

Building an HTTP client with urllib.request

Introducing the HTTP protocol

Introducing the urllib module

Get request and response headers

Extracting emails from a URL with urllib.request

Downloading files with urllib.request

Handling exceptions with urllib.request

Building an HTTP client with requests

Getting images and links from a URL with requests

Making requests with the REST API

Managing a proxy with requests

Managing exceptions with requests

Authentication mechanisms with Python

HTTP basic authentication with the requests module

HTTP digest authentication with the requests module

Implementing OAuth clients in Python with the requests-oauthlib module

OAuth roles

OAuth workflow

Implementing a client with requests_oauthlib

Implementing JSON Web Tokens (JWTs) in Python

How does a JSON Web Token work?

Working with PyJWT

Summary

Questions

Further reading

Analyzing Network Traffic and Packet Sniffing

Technical requirements

Capturing and injecting packets with pcapy-ng

Capturing packets with pcapy-ng

Reading headers from packets

Reading pcap files with pcapy-ng

Capturing and injecting packets with scapy

Introduction to scapy

Scapy commands

Sending packets with scapy

Network discovery with scapy

Port scanning and traceroute with scapy

Port scanning with scapy

Traceroute with scapy

Reading pcap files with scapy

Read DHCP requests

Writing a pcap file

Packet-sniffing with scapy

Network forensics with scapy

Working with scapy to detect ARP spoofing attacks

Detection of false ARP attacks using Scapy

Summary

Questions

Further reading

Section 3: Server Scripting and Port Scanning with Python

Gathering Information from Servers with OSINT Tools

Technical requirements

Introducing Open Source Intelligence (OSINT)

Google Dorks and the Google Hacking Database

Maltego

Photon

The Harvester

Censys

crt.sh

DnsDumpster

WaybackMachine

OSINT framework

Blackbird

The Shodan search engine

The BinaryEdge search engine

Getting information using Google Dorks

Google Dorks

Katana: a Python Tool for Google Hacking

Dorks hunter

Getting information using SpiderFoot

SpiderFoot modules

Getting information on DNS servers with DNSPython and DNSRecon

The DNS protocol

The DNSPython module

DNSRecon

Getting vulnerable addresses in servers with fuzzing

The fuzzing process

Web fuzzing

Understanding and using the FuzzDB project

Identifying predictable login pages with the FuzzDB project

Discovering SQL injection with the FuzzDB project

Wfuzz

Summary

Questions

Further reading

Interacting with FTP, SFTP, and SSH Servers

Technical requirements

Connecting to FTP servers

FTP protocol

Using the Python ftplib module

Transferring files with FTP

Other ftplib functions

Using ftplib to brute-force FTP user credentials

Building an anonymous FTP scanner with Python

Connecting with SSH servers with paramiko and pysftp

Executing an SSH server on Debian Linux

Introducing the paramiko module

Establishing an SSH connection with paramiko

Using AutoAddPolicy

Running commands with paramiko

Using paramiko to brute-force SSH user credentials

Establishing an SSH connection with pysftp

Implementing an SSH server with paramiko

Checking the security of SSH servers

Installing and executing ssh-audit

Rebex SSH Check

Summary

Questions

Further reading

Working with Nmap Scanner

Technical requirements

Introducing port scanning with Nmap

Scanning types with nmap

Port scanning with python-nmap

Extracting information with nmap

Synchronous and asynchronous scanning with python-nmap

Implementing synchronous scanning

Implementing asynchronous scanning

Discovering services and vulnerabilities with Nmap scripts

Executing Nmap scripts to discover services

Executing Nmap scripts to discover vulnerabilities

Detecting vulnerabilities with Nmap-vulners script

Detecting vulnerabilities with the Nmap-vulscan script

Port scanning via online services

Scanless port scanner

Summary

Questions

Further reading

Section 4: Server Vulnerabilities and Security in Web Applications

Interacting with Vulnerability Scanners

Technical requirements

Introducing the OpenVAS vulnerability scanner

Installing the OpenVAS vulnerability scanner

Understanding the web interface

Scanning a target using OpenVAS

Creating the target

Creating the task

Analyzing reports

Vulnerabilities databases

Accessing OpenVAS with Python

Introducing OWASP ZAP as an automated security testing tool

Using OWASP ZAP

Interacting with OWASP ZAP using Python

WriteHat as a pentesting reports tool

Summary

Questions

Further reading

Interacting with Server Vulnerabilities in Web Applications

Technical requirements

Understanding vulnerabilities in web applications with OWASP

Testing Cross-Site Scripting (XSS) vulnerabilities

Analyzing and discovering vulnerabilities in CMS web applications

Using CMSmap

Vulnx as a CMS scanner

Discovering vulnerabilities in Tomcat server applications

Installing the Tomcat server

Testing the Tomcat server with ApacheTomcatScanner

Finding vulnerable Tomcat servers in the Censys search engine

Scanning vulnerabilities with the Nmap port scanner

Discovering SQL vulnerabilities with Python tools

Introduction to SQL injection

Identifying websites vulnerable to SQL injection

Introducing sqlmap

Using sqlmap to test a website for a SQL injection vulnerability

Scanning for SQL injection vulnerabilities with sqlifinder

Scanning for SQL injection vulnerabilities with the Nmap port scanner

Automating the process of detecting vulnerabilities in web applications

Detecting an open redirect vulnerability

Detecting vulnerabilities with Fuxploider

Summary

Questions

Further reading

Obtain Information from Vulnerabilities Databases

Technical requirements

Identify and understand vulnerabilities and exploits

What is an exploit?

Vulnerability formats

Searching for vulnerabilities in the NVD

Introducing NIST’s NVD

Searching for vulnerabilities

Searching for vulnerabilities in the Vulners database

Searching for vulnerabilities with Pompem

Summary

Questions

Further reading

Section 5: Python Forensics

Extracting Geolocation and Metadata from Documents, Images, and Browsers

Technical requirements

Extracting geolocation information

Python modules for extracting geolocation information

Extracting metadata from images

Introduction to EXIF and the PIL module

Getting the EXIF data from an image

Extracting metadata from PDF documents

Extracting metadata with PyPDF2

Extracting metadata with PyMuPDF

Identifying the technology used by a website

Wappalyzer

WebApp Information Gatherer (WIG)

Extracting metadata from web browsers

Firefox forensics with Python

Chrome forensics with Python

Chrome forensics with Hindsight

Summary

Questions

Further reading

Python Tools for Brute-Force Attacks

Technical requirements

Dictionary builders for brute-force attacks

Brute-force dictionary generation with pydictor

Password list generator

Tools for brute-force attacks in Python

Obtaining subdomains by brute force

Brute-force attacks with BruteSpray

Brute-force attacks with Cerbrutus

Executing brute-force attacks for web applications

Executing a WordPress site

Executing brute-force attacks for ZIP files

Handling ZIP files in Python

Executing brute-force attacks for password-protected ZIP files

Summary

Questions

Further reading

Cryptography and Code Obfuscation

Technical requirements

Introduction to cryptography

Encrypting and decrypting information with pycryptodome

Introduction to pycryptodome

Encrypting and decrypting with the DES algorithm

Encrypting and decrypting with the AES algorithm

Generating RSA signatures using pycryptodome

Encrypting and decrypting information with cryptography

Introduction to the cryptography module

Symmetric encryption with the fernet package

Symmetric encryption with the ciphers package

Generating keys securely with the secrets and hashlib modules

Generating keys securely with the secrets module

Generating keys securely with the hashlib module

Checking the integrity of a file

Python tools for code obfuscation

Code obfuscation with pyarmor

Summary

Questions

Further reading

Assessments – Answers to the End-of-Chapter Questions

Other Books You May Enjoy

Index

Landmarks

Cover

Index

Share your thoughts

Once you’ve read Python for Security and Networking, Third Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application. 

The perks don’t stop there. You can get exclusive access to discounts, newsletters, and great free content in your inbox daily.

Follow these simple steps to get the benefits:

Scan the QR code or visit the link below:

https://packt.link/free-ebook/9781837637553

Submit your proof of purchase.That’s it! We’ll send your free PDF and other benefits to your email directly.

Section 1

Python Environment and System Programming Tools

In this section, you will learn the basics of Python programming, including the development environment and the methodology to follow to write our scripts. Also, it is important to know the main modules and packages for security and system programming tasks such as reading and writing files, and using threads, sockets, multithreading, and concurrency.

This part of the book comprises the following chapters:

Chapter 1, Working with Python ScriptingChapter 2, System Programming Packages