Masscan High-Speed Port Scanning in Practice: Asset Exposure Detection Guide
Masscan can finish a full subnet port scan in minutes and is a sharp tool for exposure inventory. This article covers install, rate-limited scans, result export, and Nmap follow-up verification, plus lawful-use bounds. Configs are copy-paste ready.
Masscan is currently the fastest port scanner. A single host can theoretically send tens of millions of packets per second. Typical use: inventory exposure on owned assets and shrink attack surface. This article uses Debian/Ubuntu to cover install, rate limits, result export, and an end-to-end workflow with Nmap.
Scope
- Periodic inventory of open ports on public IP ranges; find unregistered shadow assets
- Confirm a new server exposes only required ports before go-live
- Feed asset mapping and exposure reduction into a situational-awareness platform
- Fast recon in an authorized red/blue exercise
Prerequisites
- Debian/Ubuntu or CentOS host, root or sudo
- Explicit scan authorization: only owned ranges or ranges with written permission
- Prefer a dedicated egress so high-rate sends do not hit production traffic
How Masscan works
Masscan is asynchronous and stateless: it crafts SYNs and parses replies itself, bypassing the OS stack, which is why it outruns classic scanners. Two costs: packet loss at high rate causes false negatives/positives; ultra-high rate easily trips ISP blocks or target defenses. Always cap public scans with `--rate`.
Procedure
Step 1: Install Masscan
# Debian/Ubuntu package
apt update && apt install -y masscan
masscan --version
# Or build latest from source
apt install -y git gcc make libpcap-dev
git clone https://github.com/robertdavidgraham/masscan.git
cd masscan && make && make install
Step 2: Basic port scan
# Scan common Web ports on one /24, 1000 pps
masscan 203.0.113.0/24 -p80,443,8080 --rate 1000 -oG scan_result.gnmap
# Key flags:
# -p ports, comma-separated; ranges work, e.g. -p1-65535
# --rate packets per second; public scans: 500~2000
# -oG grepable output for downstream tools
# --exclude skip sensitive IPs, e.g. --exclude 203.0.113.1
On a large internal range you can raise rate, but pin egress NIC and source IP:
masscan 10.0.0.0/8 -p22,3389,6379 --rate 10000 \
--adapter-ip 10.0.1.50 --adapter-mac 00:0c:29:aa:bb:cc \
--router-mac 00:0c:29:dd:ee:ff -oG intranet.gnmap
Step 3: Export results and Nmap follow-up
# JSON export for programmatic use
masscan 203.0.113.0/24 -p80,443 --rate 1000 -oJ result.json --append
# Extract hosts with 6379 (Redis) open; hand to Nmap for service fingerprinting
nmap -sV -p6379 -iL redis_hosts.txt -oN redis_detail.txt
Masscan finds open ports fast. Nmap identifies service versions accurately. That pairing is the standard exposure-inventory combo.
Step 4: Close the exposure loop
Scanning is a means. Disposition is the point. Diff results against the asset register, then tighten the firewall:
# Host firewall: allow only business ports (iptables example)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 10.0.1.0/24 -j ACCEPT # SSH from internal range only
iptables -A INPUT -p tcp -j DROP
iptables-save > /etc/iptables/rules.v4
- Unregistered open ports: confirm why they are open, then close or register
- Database, cache, and middleware ports never face the public Internet
- Rescan one week after remediation to confirm the reduction
Verification
# Loopback self-test: start a test listener, then scan it
python3 -m http.server 8080 &
masscan 127.0.0.1 -p8080 --rate 100
# Expected: Discovered open port 8080/tcp on 127.0.0.1
# Count scan hits
grep open scan_result.gnmap | wc -l
FAQ
FAQ1: Why false negatives or false positives?
Masscan is stateless. Too-high rate plus loss misses ports; middleboxes that answer SYNs create false opens. Fix: confirm critical assets with `nmap -sV`; rescan the same range twice at a lower rate and take the intersection; verify adapter-ip and return path.
FAQ2: Scan mid-run, then the cloud vendor rate-limits or bans the host. What now?
Public high-rate scans look like attacks to cloud defenses. Keep rate ≤ 1000; `--exclude` vendor infrastructure ranges; scan owned VPC over the internal network when possible. If temporarily banned, open a ticket stating this is authorized security testing and request unban.
Summary
Masscan solves large-scale exposure inventory with raw scan speed. Pair `--rate` caps with Nmap verification and you get a closed loop: discover, firewall-reduce, rescan. Use only inside authorized scope. Unauthorized scanning is itself a legal risk.