Python is a versatile programming language that is widely used in cybersecurity for automation, penetration testing, and creating custom tools. This chapter introduces Python basics and demonstrates how to use it for hacking-related tasks.
- Easy to learn and use.
- Extensive libraries for networking, cryptography, and data manipulation.
- Cross-platform compatibility.
- Most Linux distributions come with Python pre-installed. To check:
python3 --version
- Install Python if not present:
sudo apt install python3 sudo apt install python3-pip
- Create a file with the
.py
extension (e.g.,script.py
). - Write your Python code.
python3 script.py
#!/usr/bin/env python3
print("Hello, World!")
Make the script executable:
chmod +x script.py
./script.py
name = "Hacker"
age = 25
print(f"Name: {name}, Age: {age}")
if age > 18:
print("Adult")
else:
print("Minor")
for i in range(5):
print(i)
count = 0
while count < 5:
print(count)
count += 1
def greet(name):
return f"Hello, {name}!"
print(greet("Hacker"))
- Create a simple TCP client:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 8080))
client.send(b"Hello, Server!")
data = client.recv(1024)
print(f"Received: {data.decode()}")
client.close()
- Create a simple TCP server:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 8080))
server.listen(5)
print("Server listening...")
while True:
client, addr = server.accept()
print(f"Connection from {addr}")
client.send(b"Hello, Client!")
client.close()
# Write to a file
with open("output.txt", "w") as file:
file.write("Hello, File!")
# Read from a file
with open("output.txt", "r") as file:
print(file.read())
-
Install the libraries:
pip3 install requests beautifulsoup4
-
Example:
import requests
from bs4 import BeautifulSoup
response = requests.get("http://example.com")
soup = BeautifulSoup(response.text, "html.parser")
print(soup.title.string)
import requests
url = "https://api.github.com"
response = requests.get(url)
print(response.json())
-
Install the library:
pip3 install cryptography
-
Example:
from cryptography.fernet import Fernet
# Generate a key
key = Fernet.generate_key()
cipher = Fernet(key)
# Encrypt data
data = b"Secret Message"
encrypted = cipher.encrypt(data)
print(f"Encrypted: {encrypted}")
# Decrypt data
decrypted = cipher.decrypt(encrypted)
print(f"Decrypted: {decrypted.decode()}")
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
By the end of this chapter, you should be able to:
- Write and execute basic Python scripts.
- Use Python for networking, file handling, and automation.
- Perform web scraping and interact with APIs.
- Implement cryptography for secure data handling.
- Explore advanced scripting concepts and integrate Python with tools like Metasploit.
- Move to Excercises to test your Python and cybersecurity skills.
- Write a Python script that scans a range of IP addresses for open ports.
- Automate downloading a file from a URL and save it locally.
- Create a script that encrypts and decrypts text using
cryptography
. - Use
requests
to fetch and display the latest news headlines from an API. - Build a simple chat application using Python sockets.