Python Cheat Sheet for Ethical Hackers

Python is a versatile programming language that can be used for various purposes, including ethical hacking and penetration testing

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 = ""

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))


    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 = ""

payload = "; ls -la"

response = requests.get(url + payload)


3. Web Application Testing

Commands for sending a POST request with parameters

import requests

url = ""

data = {

    "username": "admin",

    "password": "password123"


response =, data=data)


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()


5. Wireless Network Auditing

Commands for sniffing packets on a wireless network interface.

import pyshark

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


for packet in capture:


6. Social Engineering

Commands for sending a phishing email using smtplib:

import smtplib

from_email = ""

to_email = ""

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("", 587)


server.login("", "password")

server.sendmail(from_email, to_email, msg)


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.