Author: rishi@globaldigitalsecurity.ca
- Scapy: Scapy can be used to capture and manipulate network packets. Here’s an example of how to use Scapy to send an ICMP ping request to a remote host:
Example:
python
from scapy.all import IP, ICMP, sr1
ip = IP(dst=”google.com”)
icmp = ICMP()
ping = ip/icmp
response = sr1(ping, timeout=2)
if response:
print(f”{response.src} is up!”)
else:
print(“No response”)
- PyCrypto: PyCrypto provides a collection of cryptographic algorithms for Python. Here’s an example of how to use PyCrypto to encrypt and decrypt a message using the AES algorithm:
Example:
python
from Crypto.Cipher import AES
import base64
key = b’0123456789abcdef’
message = “Hello, world!”
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(message.encode())
decoded_ciphertext = base64.b64encode(ciphertext).decode()
print(f”Encoded ciphertext: {decoded_ciphertext}”)
decrypted_ciphertext = cipher.decrypt(ciphertext).decode()
print(f”Decrypted ciphertext: {decrypted_ciphertext}”)
- Requests: Requests simplify sending HTTP requests and handling responses. Here’s an example of how to use Requests to download a file from a URL:
Example:
python
import requests
url = “https://example.com/file.txt”
response = requests.get(url)
if response.status_code == 200:
with open(“file.txt”, “wb”) as f:
f.write(response.content)
print(“File downloaded successfully”)
else:
print(“Error downloading file”)
- Nmap: Nmap can be used to discover hosts and services on a network. Here’s an example of how to use Nmap to scan a network for open ports:
Example:
python
import nmap
nm = nmap.PortScanner()
nm.scan(hosts=”192.168.1.0/24″, arguments=”-p 80″)
for host in nm.all_hosts():
if nm[host][“tcp”][80][“state”] == “open”:
print(f”Port 80 is open on {host}”)
These are just a few examples of how these libraries can be used for cybersecurity. There are many more use cases for each library, depending on the specific needs of the project.
- Paramiko: Here’s an example of how to use Paramiko to remotely execute a command on a network device:
Example:
python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=”example.com”, username=”user”, password=”password”)
stdin, stdout, stderr = ssh.exec_command(“show run”)
output = stdout.read().decode()
print(output)
ssh.close()
This example logs into a network device using SSH, executes the “show run” command to retrieve the device’s configuration, and prints the output to the console.
- Netifaces: Here’s an example of how to use Netifaces to retrieve the IP address of the default network interface:
Example:
python
import netifaces
iface = netifaces.gateways()[“default”][netifaces.AF_INET][1]
ip_address = netifaces.ifaddresses(iface)[netifaces.AF_INET][0][“addr”]
print(f”IP address: {ip_address}”)
This example retrieves the default gateway for the system and uses the interface associated with that gateway to retrieve the IP address. This information can be useful for identifying the network to which the system is connected.
- Python Whois: Here’s an example of how to use Python Whois to retrieve WHOIS information for a domain:
python
from whois import whois
domain = “example.com”
result = whois(domain)
print(result)
This example retrieves WHOIS information for the “example.com” domain and prints the results to the console. This information can be useful for identifying the owner of a domain, as well as any associated contact information or registration details.
- OpenSSL: Here’s an example of how to use OpenSSL to encrypt and decrypt data using the AES algorithm:
Example:
python
from OpenSSL.crypto import Cipher, rand_bytes
key = rand_bytes(16)
iv = rand_bytes(16)
plaintext = b”Hello, world!”
encryptor = Cipher(alg=”aes_128_cbc”, key=key, iv=iv, op=1)
ciphertext = encryptor.update(plaintext) + encryptor.final()
decryptor = Cipher(alg=”aes_128_cbc”, key=key, iv=iv, op=0)
decrypted_plaintext = decryptor.update(ciphertext) + decryptor.final()
print(f”Original message: {plaintext}”)
print(f”Encrypted message: {ciphertext}”)
print(f”Decrypted message: {decrypted_plaintext}”)
This example generates a random encryption key and initialization vector (IV), encrypts the plaintext message using the AES algorithm, and then decrypts the ciphertext back to the original plaintext. This can be useful for securely transmitting sensitive data over a network.
- Pytesseract: Here’s an example of how to use Pytesseract to extract text from an image:
Example:
python
import pytesseract
from PIL import Image
image = Image.open(“image.png”)
text = pytesseract.image_to_string(image)
print(text)
This example uses Pytesseract to read the text in an image file and print the results to the console. This can be useful for extracting text from images containing important information, such as a screenshot of a configuration page for a network device.
- Beautiful Soup: Here’s an example of how to use Beautiful Soup to extract information from an HTML page:
Example:
import requests
from bs4 import BeautifulSoup
URL = “https://realpython.github.io/fake-jobs/”
page = requests.get(URL)
soup = BeautifulSoup(page.content, “html.parser”)