Port scanning is a fundamental network reconnaissance technique used to discover open ports and services on target systems. Understanding scan results is crucial for security hardening.
Port States:
- Open: Port is accepting connections. Service is listening and reachable.
- TCP: SYN/ACK response received
- UDP: Application response or no ICMP unreachable
- Security implication: Potential attack vector
- Closed: Port is reachable but no service is listening.
- TCP: RST response received
- UDP: ICMP port unreachable received
- Security implication: Low risk, but reveals host is up
- Filtered: Firewall or packet filter blocking access.
- No response or ICMP unreachable (type 3, code 1/2/3/9/10/13)
- Cannot determine if port is open or closed
- Security implication: Firewall present (good for security)
- Open|Filtered: nmap cannot determine if open or filtered (UDP scans)
- Closed|Filtered: nmap cannot determine if closed or filtered (IPID idle scan)
Common Ports and Security Implications:
- Port 22 (SSH):
- Service: Secure Shell for remote administration
- Risk: Low if properly configured
- Hardening: Use key-based auth, disable root login, fail2ban, non-standard port
- Attacks: Brute force, credential stuffing
- Port 23 (Telnet):
- Service: Unencrypted remote access
- Risk: Critical - transmits passwords in cleartext
- Recommendation: Disable immediately, replace with SSH
- Never use on untrusted networks
- Port 80 (HTTP):
- Service: Web server (unencrypted)
- Risk: Medium - traffic can be intercepted
- Hardening: Redirect to HTTPS (443), use HSTS headers
- Attacks: MITM, session hijacking, XSS, SQL injection
- Port 443 (HTTPS):
- Service: Encrypted web server
- Risk: Low if using modern TLS
- Hardening: TLS 1.2+, strong cipher suites, HSTS, certificate pinning
- Verify certificate validity and chain
- Port 3306 (MySQL):
- Service: MySQL database
- Risk: High - should NEVER be exposed to internet
- Hardening: Bind to localhost (127.0.0.1), use SSH tunnel or VPN
- Attacks: SQL injection, brute force, credential leaks
- Port 3389 (RDP):
- Service: Windows Remote Desktop
- Risk: High - frequent ransomware attack vector
- Hardening: NLA, strong passwords, MFA, VPN, IP whitelist
- Attacks: BlueKeep (CVE-2019-0708), brute force, ransomware
Port Scanning Techniques:
- TCP SYN Scan (nmap -sS): "Stealth" scan, doesn't complete handshake
- Sends SYN, waits for SYN/ACK (open) or RST (closed)
- Faster than full connect scan
- Requires root/administrator privileges
- TCP Connect Scan (nmap -sT): Completes full 3-way handshake
- Uses OS connect() system call
- Logged by target system (more detectable)
- Works without privileges
- UDP Scan (nmap -sU): Scans UDP ports (DNS, SNMP, DHCP)
- Slower than TCP (no handshake, relies on timeouts)
- ICMP unreachable = closed, no response = open|filtered
- Important for discovering DNS, SNMP vulnerabilities
- Service Version Detection (nmap -sV): Identifies service versions
- Sends probes to open ports
- Matches responses against signature database
- Critical for vulnerability assessment
Risk Levels Explained:
- Critical: Service is inherently insecure (Telnet, FTP, SNMP v1/v2)
- Transmits credentials in cleartext
- No encryption of data
- Action: Disable immediately or replace with secure alternative
- High: Service should not be exposed to internet (databases, RDP, SMB)
- Frequent target of automated attacks
- Known exploits and vulnerabilities
- Action: Restrict to localhost or VPN, firewall by IP
- Medium: Service requires additional hardening (HTTP, SMTP)
- Can be secure with proper configuration
- Consider encryption (TLS/SSL)
- Action: Enable auth, use encryption, monitor access
- Low: Service is reasonably secure (HTTPS, SSH with keys)
- Uses modern encryption
- Follow best practices for configuration
- Action: Keep updated, monitor logs, use strong auth
Security Hardening Best Practices:
- Principle of Least Privilege: Only open ports required for functionality
- Defense in Depth: Multiple layers (firewall, auth, encryption, monitoring)
- Disable Unused Services: Reduce attack surface by stopping unnecessary services
- Use Encryption: TLS/SSL for all internet-facing services
- Strong Authentication: Key-based auth, MFA, strong passwords
- Firewall Rules: Whitelist specific IPs, block by default
- Network Segmentation: Isolate critical services in VLANs or DMZ
- Regular Updates: Patch services for known vulnerabilities (CVEs)
- Monitoring and Logging: Detect intrusion attempts and anomalies
- Fail2ban/IP Blocking: Automatic blocking after failed auth attempts
Common Attack Vectors:
- Brute Force: Automated password guessing (SSH, RDP, FTP)
- Mitigation: Fail2ban, rate limiting, strong passwords, MFA
- Exploiting Known Vulnerabilities: CVEs in outdated software
- Mitigation: Regular patching, vulnerability scanning
- Default Credentials: Services with unchanged default passwords
- Mitigation: Change all defaults immediately after installation
- Service Exploitation: Buffer overflows, RCE vulnerabilities
- Mitigation: Web Application Firewall (WAF), IDS/IPS, sandboxing
Compliance Considerations:
- PCI-DSS: Quarterly vulnerability scans, firewall configuration, disable unnecessary services
- HIPAA: Encryption for ePHI transmission, access controls, audit logging
- SOC 2: Security monitoring, change management, incident response
- CIS Benchmarks: Industry-standard security configurations for services
Tools for Port Scanning:
- nmap: Industry-standard network mapper with extensive features
- masscan: High-speed scanner for large networks
- netcat (nc): Manual port checking and banner grabbing
- Shodan/Censys: Internet-wide port scan databases
- OpenVAS/Nessus: Vulnerability scanners with port scanning capabilities