LESSON 92: Building a Simple Port Scanner Using Python (Python for Cybersecurity)

Опубликовано: 17 June 2026
на канале: Victor Akinode
139
14

Build a complete network port scanner from scratch using Python socket programming! Learn TCP connection mechanics, port scanning techniques, multi-port scanning logic, and create a professional security tool with a menu-driven interface. This is real penetration testing in action.

Port scanning is fundamental to penetration testing and network security assessment. This lesson builds a complete scanner that checks if ports are open, closed, or filtered on target systems. We walk through the code section by section, explaining socket programming concepts, TCP connect scans, error handling, and user interface design.

TOPICS COVERED:

Socket programming fundamentals with socket module, creating socket objects with socket.socket(), AF_INET for IPv4 addressing, SOCK_STREAM for TCP protocol, TCP three-way handshake explained, connect_ex() method for non-exception connections, return value 0 indicates open port, timeout configuration with settimeout() to prevent hanging, closing sockets with close() to free resources.

Building scan_port() core function returning port status, implementing scan_single_port() with timestamps and formatted output, creating scan_multiple_ports() with result tracking, developing scan_common_ports() using dictionary keys, IP address validation with socket.inet_aton(), port range validation (1-65535), handling invalid input gracefully.

COMMON_PORTS dictionary mapping port numbers to services, real-time scan result display with status symbols, [+] for open ports, [-] for closed ports, [!] for errors, scan summary statistics showing total/open/closed counts, listing discovered services by name, menu-driven interface with while True loop, input validation and error handling, KeyboardInterrupt handling for Ctrl+C graceful exit.

TCP connect scan mechanics: SYN packet sent to target, SYN-ACK received if port open, RST received if port closed, timeout if port filtered, full three-way handshake completion, why TCP connect scans get logged, comparison to SYN scans and stealth techniques.

Best practices demonstrated: modular function design for reusability, comprehensive error handling with try/except, user-friendly output with symbols and formatting, input validation preventing crashes, defensive programming for edge cases, clean exit handling, professional tool structure.

Real-world applications: network reconnaissance during penetration tests, identifying attack surface on target systems, discovering running services and versions, validating firewall rules, checking for unauthorized services, security auditing and compliance.

COMPLETE TOOL FEATURES:
✓ Single port scanning with service identification
✓ Multiple port scanning with comma-separated input
✓ Common ports scanning (13 well-known services)
✓ IP address format validation
✓ Port range validation (1-65535)
✓ Real-time result display with status symbols
✓ Scan summary statistics
✓ Timestamp tracking for documentation
✓ Error handling for network issues
✓ Graceful Ctrl+C interruption
✓ Menu-driven interactive interface

Socket programming concepts explained: socket creation and configuration, TCP vs UDP protocols, connect() vs connect_ex() methods, timeout importance in network operations, proper socket cleanup, handling connection errors.

Extensibility opportunities: multi-threading for concurrent scanning, banner grabbing for service version detection, UDP scanning support, output to file for reporting, command-line argument parsing, integration with vulnerability databases, colorized output with colorama, progress bars for long scans.

Legal and ethical usage: only scan systems you own or have explicit written permission to test, unauthorized port scanning may be illegal in your jurisdiction, use responsibly for legitimate security testing, document all scanning activities.

SETUP: Kali Linux, VS Code, Python 3

SOURCE CODE: https://docs.google.com/document/d/1W...

NEXT LESSON: Day 93 - Enhancing Port Scanner with Multi-Threading

🌐 JOIN: https://t.me/+dvi_feSCRCJmNjJk

#cybersecurity #python #portscanner #socketprogramming #networking #pentesting #ethicalhacking #securitytools #pythonprogramming #tcp #networksecurity #reconnaissance #infosec #coding #scanner