05 cookbook
Scripts you can actually run Every one of these was compile checked No external hosts are scanned by these examples
Table of Contents
Threaded port scanner
A loop over 1024 ports is slow A queue with a few threads is not
import queue
import socket
import threading
def port_scan(port, target):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.2)
s.connect((target, port))
return True
except (socket.timeout, ConnectionRefusedError, OSError):
return False
def worker(target, q, found):
while not q.empty():
port = q.get()
if port_scan(port, target):
found.append(port)
q.task_done()
target = "127.0.0.1"
q = queue.Queue()
for port in range(1, 30):
q.put(port)
found = []
for _ in range(5):
threading.Thread(target=worker, args=(target, q, found), daemon=True).start()
q.join()
print(f"Open ports: {found}")
q.join() waits for every task to be marked done
Without it the main thread exits while workers are still running
Point this at a host you own or have written permission to test
Subdomain check
Takes a domain and a wordlist This version only builds the URL , the live request is left to you
import sys
def check_subdomain(domain, subdomain):
return f"http://{subdomain}.{domain}"
if len(sys.argv) != 3:
print("Usage: python3 subdomain_check.py <domain> <wordlist>")
raise SystemExit(1)
domain = sys.argv[1]
wordlist_path = sys.argv[2]
with open(wordlist_path, "r") as f:
for line in f:
subdomain = line.strip()
if subdomain:
print(check_subdomain(domain, subdomain))
requests.get() with a timeout goes where the verification marker is
DNS guessing is loud , keep it inside scope
Small web crawler
A depth first crawler with a set to stop loops Compile checked and run against controlled data only
from urllib.parse import urljoin, urlparse
def crawl(url, base_domain, internal_links):
if url in internal_links:
return
internal_links.add(url)
# fetch happens here in the real version , with a timeout
for href in ["/a", "/b"]:
full_url = urljoin(url, href)
if urlparse(full_url).netloc == base_domain:
crawl(full_url, base_domain, internal_links)
start_url = "http://example.com"
links = set()
crawl(start_url, urlparse(start_url).netloc, links)
print("\n--- Found Internal Links ---")
for link in sorted(links):
print(link)
The if url in internal_links guard is what stops an infinite loop
Recursive crawling without it will eat your process
nmap wrapper
nmap -F and -sV were confirmed with nmap --help
-T<0-5> is the timing template from the same help output
import subprocess
def scan_host(host):
command = ["nmap", "-F", "-sV", host]
try:
result = subprocess.run(command, capture_output=True, text=True, check=True, timeout=120)
return result.stdout
except FileNotFoundError:
return "nmap not installed or not in PATH"
except subprocess.CalledProcessError as e:
return f"nmap failed with exit code {e.returncode}: {e.stderr}"
except subprocess.TimeoutExpired:
return "nmap timed out"
print(scan_host("127.0.0.1"))
A list , not a shell string Timeout , not a hang Error paths , not a silent failure
Posted