Programming & Scripting for Pentesters
Python for Pentesters (+ Build a Port Scanner)
The offensive Python toolkit — sockets, requests, and your first tool.
Python is the lingua franca of offensive security. Its sockets, requests library, and huge ecosystem let you build almost any tool quickly. Let's build a real (if simple) TCP port scanner.
import socket
from concurrent.futures import ThreadPoolExecutor
def scan(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.5)
if s.connect_ex((host, port)) == 0:
print(f'[+] {port} open')
s.close()
host = '127.0.0.1'
with ThreadPoolExecutor(max_workers=200) as pool:
for port in range(1, 1025):
pool.submit(scan, host, port)
Libraries you'll reach for constantly
- requests / httpx — HTTP clients for web testing and API abuse.
- socket — raw TCP/UDP for scanners and custom protocols.
- scapy — craft and sniff packets.
- impacket — Windows/AD protocols (SMB, Kerberos) — powers many AD tools.
- pwntools — binary exploitation and CTF automation.