mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-24 20:21:52 +08:00
Harden scan robustness and tests
This commit is contained in:
+7
-3
@@ -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") }
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user