Harden scan robustness and tests

This commit is contained in:
ZacharyZcR
2026-06-14 22:23:48 +08:00
parent 5ad914a1bb
commit c49c23c7f0
100 changed files with 4483 additions and 412 deletions
+7 -3
View File
@@ -135,21 +135,25 @@ func (r Result) DetailBool(key string) (bool, bool) {
// Port returns the result port from details, or from a target in host:port form.
func (r Result) Port() (int, bool) {
if port, ok := r.DetailInt("port"); ok {
return port, true
return port, validPort(port)
}
if _, portText, err := net.SplitHostPort(r.Target); err == nil {
port, err := strconv.Atoi(portText)
return port, err == nil
return port, err == nil && validPort(port)
}
if strings.Count(r.Target, ":") == 1 {
if idx := strings.LastIndex(r.Target, ":"); idx >= 0 && idx+1 < len(r.Target) {
port, err := strconv.Atoi(r.Target[idx+1:])
return port, err == nil
return port, err == nil && validPort(port)
}
}
return 0, false
}
func validPort(port int) bool {
return port >= 1 && port <= 65535
}
// Service returns the detected service name when present.
func (r Result) Service() (string, bool) { return r.DetailString("service") }
+15 -1
View File
@@ -76,6 +76,21 @@ func TestResultPortNoPort(t *testing.T) {
}
}
func TestResultPortRejectsOutOfRangePorts(t *testing.T) {
tests := []Result{
{Target: "10.0.0.1:70000"},
{Target: "[::1]:0"},
{Details: map[string]interface{}{"port": 70000}},
{Details: map[string]interface{}{"port": 0}},
}
for _, result := range tests {
if port, ok := result.Port(); ok {
t.Fatalf("Port(%#v) = %d/true, want false", result, port)
}
}
}
func TestResultCredentialHelpers(t *testing.T) {
result := Result{
Type: ResultTypeVuln,
@@ -715,4 +730,3 @@ func TestResultSummaryJSON(t *testing.T) {
t.Fatalf("round-trip failed: %#v", decoded)
}
}