fix: preserve explicit target port in network templates (fixes #7323) - #7465
Conversation
…tdiscovery#7323) Problem ------- When a user specifies a port explicitly on the command line, e.g.: nuclei -target TARGET:80 -t network/cves/2001/CVE-2001-1473.yaml UseNetworkPort() was overriding port 80 with the template's port (22). This happened because reservedPorts (80, 443, 8080, …) were always replaced by the template port, regardless of whether the port was deliberately chosen by the operator or merely implied by the URL scheme. As a result, services running on non-standard ports (SSH on 80, FTP on 443, etc.) were silently scanned on the wrong port, or the connection was refused, and the target was effectively invisible to Nuclei. Root cause ---------- UseNetworkPort() treated a bare 'host:80' the same as 'http://host:80'. In the former the operator explicitly chose port 80; in the latter the port was implied by the http:// scheme. Fix --- Only replace a reserved port when the input contains a URL scheme ('://'), indicating the port was scheme-implied. A bare 'host:port' form means the operator deliberately chose that port and it is preserved unchanged. Regression tests ---------------- Seven table-driven cases added to contextargs_test.go covering: - No port in input → template port used (existing behaviour) - Explicit non-reserved port → preserved - Bare host:80 (key regression) → preserved - http://host:80 (scheme-implied) → replaced - http://host (no port) → template port used - Empty template port → no-op - Explicit port == template port → unchanged Fixes projectdiscovery#7323
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
Walkthrough
ChangesUseNetworkPort port-override fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem
When a user explicitly specifies a port on the CLI:
Nuclei ignores the user's port and connects to the template's port (e.g. 22) instead:
This makes it impossible to scan services running on non-standard ports — SSH on 80, FTP on 443, etc. The target is silently scanned on the wrong port.
Root Cause
UseNetworkPort()incontextargs.gounconditionally replaces any port inreservedPorts(80, 443, 8080, …) with the template port, even when the operator deliberately typedTARGET:80.The function cannot distinguish between:
http://TARGET:80— port 80 is scheme-implied, replacing it is correctTARGET:80— port 80 is explicitly chosen, replacing it is wrongFix
Only replace a reserved port when the input contains a URL scheme (
://), which means the port was implied by the scheme rather than typed by the operator.A bare
host:portform means the operator chose that port on purpose — it is preserved unchanged.Change in
contextargs.gois ~15 lines.Tests
Seven table-driven cases added to
contextargs_test.go(new file):example.comexample.com:22example.com:2222example.com:2222✓example.com:80example.com:80✓http://example.com:80example.com:22http://example.comexample.com:8888example.com:9999example.com:9999example.com:22example.com:22All pass.
Related
Fixes #7323
Would it be possible to add a
💎 Bountylabel to this issue/PR? The bug completely silences scanning of services on non-standard ports, which is a meaningful impact for anyone doing custom port scanning with nuclei.Summary by CodeRabbit
host:portinputs.http:///https://); scheme-lesshost:portvalues are preserved.