Masscan

The Masscan Masterclass: Professional High-Speed Port Scanning

Masscan is the world’s fastest port scanner, capable of scanning entire internet ranges in minutes. It is essential for large-scale reconnaissance, asset discovery, and red team operations. This guide covers advanced usage, workflow integration, and best practices for professional penetration testers—no installation steps included.


I. Environment Setup: Dynamic Variables

Export variables for flexible, repeatable scanning workflows:

export IP_RANGE="10.0.0.0/8"
export PORTS="80,443,22,3389"
export RATE=10000            # Packets per second
export INTERFACE="eth0"     # Network interface (e.g., eth0, tun0)
export OUTPUT_DIR="masscan-results"
export OUTPUT_FILE="$OUTPUT_DIR/scan.txt"
export EXCLUDE_IPS="192.168.1.1,10.0.0.1"
export EXCLUDE_FILE="exclude.txt"
export BANNERS=true          # true/false for banner grabbing
export SRC_IP="10.0.0.100"  # Optional: custom source IP
export SRC_PORT=40000        # Optional: custom source port

II. Core Capabilities & Workflow

  • Lightning-fast SYN scanning: Scans millions of hosts/ports per minute using asynchronous SYN packets.

  • Flexible target specification: Supports single IPs, CIDR ranges, and input files.

  • Customizable speed and stealth: Fine-tune packet rate, retries, and interface for operational security.

  • Output versatility: Supports grepable, XML, JSON, binary, and list formats for easy integration.

  • Banner grabbing: Optionally grabs service banners for open ports.

  • Exclusion and filtering: Exclude IPs/ranges to avoid scanning sensitive or out-of-scope assets.

  • Integration with Nmap: Pipe discovered hosts/ports into Nmap for deep service enumeration.


III. Professional Usage Examples

1. Fast Scan of Common Ports Across a Large Range

masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" -e "$INTERFACE" -oL "$OUTPUT_FILE"

2. Full Port Scan (All 65535 Ports)

masscan -p1-65535 "$IP_RANGE" --rate "$RATE" -e "$INTERFACE" -oL "$OUTPUT_FILE"

3. Scan with Exclusions (IPs or Ranges)

masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" --exclude "$EXCLUDE_IPS" -e "$INTERFACE" -oL "$OUTPUT_FILE"

Exclude from file:

masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" --excludefile "$EXCLUDE_FILE" -e "$INTERFACE" -oL "$OUTPUT_FILE"

4. Banner Grabbing (Service Detection)

masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" --banners -e "$INTERFACE" -oJ "$OUTPUT_DIR/scan.json"

5. UDP Scanning (Selected Ports)

masscan -pU:53,161 "$IP_RANGE" --rate "$RATE" -e "$INTERFACE" -oL "$OUTPUT_DIR/udp.txt"

6. Custom Source IP/Port (OPSEC/Firewall Evasion)

masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" --src-ip "$SRC_IP" --src-port "$SRC_PORT" -e "$INTERFACE" -oL "$OUTPUT_FILE"

7. Output in Multiple Formats

masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" -e "$INTERFACE" -oX "$OUTPUT_DIR/scan.xml"
masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" -e "$INTERFACE" -oJ "$OUTPUT_DIR/scan.json"
masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" -e "$INTERFACE" -oG "$OUTPUT_DIR/scan.grep"

8. Scan from List of Targets

masscan -p "$PORTS" -iL targets.txt --rate "$RATE" -e "$INTERFACE" -oL "$OUTPUT_FILE"

9. Only Show Open Ports

masscan -p "$PORTS" "$IP_RANGE" --rate "$RATE" --open-only -e "$INTERFACE" -oL "$OUTPUT_FILE"

IV. Advanced Techniques & Integration

1. Pipelining to Nmap for Deep Enumeration

awk '/open/{print $6}' "$OUTPUT_FILE" | sort -u > live_hosts.txt
nmap -sV -p "$PORTS" -iL live_hosts.txt -oA nmap/$(echo "$IP_RANGE" | tr '/:' '_')-services

2. Rate Limiting & Stealth

  • Lower --rate for stealthier scans or to avoid network disruption.

  • Use --adapter-ip and --router-mac for advanced network setups.

  • Use --retries to control retransmissions for reliability.

3. Automation & Scripting

  • Integrate Masscan into CI/CD or monitoring pipelines for asset discovery.

  • Use JSON output for programmatic parsing and alerting.


V. Pro Tips & Best Practices

  • Start with small ranges and low rates to avoid accidental DoS or detection.

  • Always get authorization before scanning networks you do not own.

  • Document exclusions to avoid scanning sensitive or out-of-scope assets.

  • Combine with Nmap for full reconnaissance: Masscan for breadth, Nmap for depth.

  • Monitor network impact—high rates can overwhelm firewalls, IDS/IPS, or network links.

  • Use output files for repeatability, reporting, and integration with other tools.

  • Banner grabbing is best-effort and may not always return full service details.

  • Understand SYN scanning: Masscan does not complete TCP handshakes, so some hosts may not respond as expected.


VI. Troubleshooting & OPSEC

  • False positives/negatives: Tune --rate, --retries, and check for packet loss.

  • Firewall evasion: Use custom source IP/port, randomize scan order, or throttle rate.

  • Interface errors: Use --iflist to list available interfaces and select the correct one.

  • Legal compliance: Always scan within scope and with explicit permission.


This professional Masscan guide equips you for high-speed, large-scale port scanning, asset discovery, and integration with advanced security workflows.

Last updated

Was this helpful?