Skip to main content

Educational Python labs and utilities for defensive cybersecurity

Project description

defensive-python-labs ๐Ÿ›ก๏ธ

Python License: MIT PRs Welcome Educational

Educational Python labs and utilities for defensive cybersecurity. Learn real-world security concepts through hands-on Python code โ€” no prior security experience needed.


What is this?

defensive-python-labs is a growing toolkit of pure-Python security utilities that you can use to:

  • Learn how common attacks work (so you can defend against them)
  • Audit your own systems, websites, and files
  • Practice cybersecurity concepts in a safe, legal environment
  • Teach security fundamentals with working, readable code

Every module is self-contained, well-documented, and designed to run with minimal dependencies.


๐Ÿš€ Quick Start

git clone https://github.com/cutewizzy11/defensive-python-labs.git
cd defensive-python-labs
python -m venv .venv && source .venv/bin/activate
pip install -e .

Now you have the dplabs command:

dplabs --help

๐Ÿงฐ Modules

๐Ÿ” Recon

Port Scanner โ€” Threaded TCP scanner with banner grabbing and service detection.

# Scan common ports (fast)
dplabs portscan example.com --common --banner

# Scan a custom range
dplabs portscan 192.168.1.1 --start 1 --end 65535
PORT     SERVICE         BANNER
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
22       SSH             SSH-2.0-OpenSSH_8.9p1
80       HTTP            HTTP/1.1 301 Moved Permanently
443      HTTPS
3306     MySQL

[+] 4 open port(s) found on example.com

DNS Recon โ€” Subdomain enumeration and reverse DNS lookups.

dplabs dns example.com --subdomains
dplabs dns 8.8.8.8 --reverse

๐Ÿ” Password Security

Password Strength Analyzer โ€” Entropy-based analysis with actionable feedback.

dplabs password --password "MyS3cur3P@ss!"
dplabs password --file passwords.txt        # audit a list
Password : *************
Strength : Strong  (score 78/100)
Entropy  : 72.4 bits
Tips     :
  โ€ข Aim for 16+ characters for better security.
  โ€ข Add at least one special character (!@#$%^&*).

Hash Cracker (Educational Demo) โ€” See WHY unsalted MD5 is broken in seconds.

# Dictionary attack
dplabs crack 5f4dcc3b5aa765d61d8327deb882cf99 --wordlist rockyou.txt

# Brute force (short passwords only โ€” proves the point)
dplabs crack 900150983cd24fb0d6963f7d28e17f72 --algorithm md5
Algorithm : MD5
Result    : โœ“ CRACKED: 'abc'
Attempts  : 3
Time      : 0.001s

This demonstrates in real time why md5(password) without a salt is dangerously weak.


๐ŸŒ Web Security

HTTP Security Headers Analyzer โ€” Grade any website's security posture in seconds.

dplabs headers https://yourwebsite.com
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  Security Headers Report
  URL   : https://yourwebsite.com
  Score : 42/100   Grade: D
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

โœ— Missing headers:
  [C] Content-Security-Policy
     โ†’ Prevents XSS and data injection attacks. Set to: default-src 'self'
  [C] Strict-Transport-Security
     โ†’ Forces browsers to use HTTPS. Set to: max-age=31536000; includeSubDomains
  [H] X-Frame-Options
     โ†’ Prevents clickjacking. Set to: DENY or SAMEORIGIN

โš   Information-leaking headers found:
  โ€ข X-Powered-By: PHP/8.1.2 โ€” remove or obscure this header

Use this against your own site before an attacker does.


๐Ÿ”ฌ Forensics

File Metadata Extractor โ€” Hashes, timestamps, EXIF data, type detection, anomaly flags.

dplabs metadata suspicious_image.jpg
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  File Metadata Report
  File    : suspicious_image.jpg
  Size    : 2,048,312 bytes (2000.3 KB)
  Type    : JPEG Image (image/jpeg)
  MD5     : d41d8cd98f00b204e9800998ecf8427e
  SHA256  : e3b0c44298fc1c149afbf4c8996fb92427ae41...
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  GPS: {'GPSLatitude': (40, 42, 46.0), 'GPSLongitude': (74, 0, 21.0)}
  Make: Apple
  Model: iPhone 14 Pro

โš  Anomalies detected:
  โ€ข File was modified less than 24 hours ago

Log Analyzer โ€” Detect brute-force attacks, port scans, and web exploits in your logs.

dplabs logs /var/log/apache2/access.log --type apache
dplabs logs /var/log/auth.log --type ssh
  โš  Possible brute-force sources:
    185.220.101.42       4,821 requests
    45.33.32.156           312 requests

  โš  Suspicious requests detected: 47
    [185.220.101.42] /admin.php?cmd=whoami โ€” Suspicious path pattern
    [185.220.101.42] /../../../etc/passwd โ€” path traversal

๐Ÿฆ  Malware Analysis (Static)

Analyze suspicious files without running them.

dplabs malware suspicious.exe
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  Static Malware Analysis Report
  File      : suspicious.exe
  Size      : 245,760 bytes
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  MD5    : 1f3870be274f6c49b3e31a0c6728957f
  SHA256 : 2c624232cdd221771294dfbb310acbc...

  Entropy   : 7.82 / 8.0  [VERY HIGH โ€” likely packed/encrypted]
  Risk      : HIGH RISK โ€” manual review strongly recommended  (85/100)

  โš  Suspicious strings (12):
    โ€ข CreateRemoteThread
    โ€ข VirtualAllocEx
    โ€ข cmd.exe /c whoami

  Indicators of Compromise (IOCs):
    URL:
      - http://evil-c2.example.com/shell
    IPv4:
      - 185.220.101.42

๐Ÿ“ Project Structure

src/defensive_python_labs/
โ”œโ”€โ”€ recon/
โ”‚   โ”œโ”€โ”€ port_scanner.py       # Threaded TCP scanner + banner grabbing
โ”‚   โ””โ”€โ”€ dns_recon.py          # Subdomain enum, reverse DNS
โ”œโ”€โ”€ crypto/
โ”‚   โ””โ”€โ”€ classic_ciphers.py    # Caesar, Vigenรจre (educational)
โ”œโ”€โ”€ forensics/
โ”‚   โ”œโ”€โ”€ metadata_extractor.py # File hashes, EXIF, anomaly detection
โ”‚   โ””โ”€โ”€ log_analyzer.py       # Apache/SSH log analysis
โ”œโ”€โ”€ password_security/
โ”‚   โ”œโ”€โ”€ password_strength.py  # Entropy-based strength analyzer
โ”‚   โ””โ”€โ”€ hash_cracker.py       # Dictionary + brute force demo
โ”œโ”€โ”€ malware_analysis/
โ”‚   โ””โ”€โ”€ static_analyzer.py    # Entropy, strings, IOC extraction
โ””โ”€โ”€ cli.py                    # Unified CLI entry point

๐ŸŽ“ Who is this for?

You are... How to use this
Beginners learning cybersecurity Read each module top-to-bottom โ€” they're heavily commented
Developers building secure apps Use headers_analyzer against your app; use password_strength in your backend
Students in security courses Run the labs, modify the code, write your own modules
Sysadmins Drop log_analyzer on your access logs right now
CTF players The crypto and forensics modules have obvious applications

๐Ÿค Contributing

Contributions are very welcome โ€” this project grows by community additions.

Ideas for new labs:

  • Network: ARP spoofing detector, PCAP analyzer
  • Web: SQL injection scanner (on your own sites), CORS misconfiguration checker
  • Crypto: RSA key size checker, TLS cipher suite auditor
  • Forensics: Memory dump string extractor, registry hive reader
  • Threat Intel: IOC checker against AbuseIPDB, VirusTotal API wrapper

See CONTRIBUTING.md โ€” we label good starter issues with good first issue.


โš ๏ธ Disclaimer

This project is for educational and defensive purposes only.

Do not use any tool in this repository against systems, networks, or accounts you do not own or have explicit written permission to test. Unauthorized scanning and testing is illegal in most jurisdictions.


๐Ÿ“„ License

MIT ยฉ Paul Anyebe

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

defensive_python_labs-0.2.0.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

defensive_python_labs-0.2.0-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file defensive_python_labs-0.2.0.tar.gz.

File metadata

  • Download URL: defensive_python_labs-0.2.0.tar.gz
  • Upload date:
  • Size: 30.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for defensive_python_labs-0.2.0.tar.gz
Algorithm Hash digest
SHA256 844d7fdbc89aa7dede9cff6edaa6f48ce648ac732c5c133b37eb1bab9dab019d
MD5 a51f5da9bb3c3971ce5cd76201ba6291
BLAKE2b-256 7f6c9378bac694f6436b6d466369fb67476de8e09335fed3e8d36576ab7d8b58

See more details on using hashes here.

File details

Details for the file defensive_python_labs-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for defensive_python_labs-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 768c3d8d17ff27af8966a6b04de7713445556130e7d5599f75366d2cf51f9ed2
MD5 cf4ba47ed40a0da5df73f2ce9f5bf8cf
BLAKE2b-256 5385ad4e1a61a84c2cb4144e2129a17b41e4ec72a2062f2824f2c199d942c252

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page