From 68f990d20b8a8d0668f8f166ec016d93efda57db Mon Sep 17 00:00:00 2001 From: ZacharyZcR <2903735704@qq.com> Date: Thu, 4 Jun 2026 14:26:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BA=20Web=20=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E8=AF=86=E5=88=AB=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/port_scan.go | 32 ++++++++++++++++++++++++++++++-- core/port_scan_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/core/port_scan.go b/core/port_scan.go index 1e81722..d015b55 100644 --- a/core/port_scan.go +++ b/core/port_scan.go @@ -420,10 +420,14 @@ func matchFold(a, b string) bool { } // buildServiceLogMessage 构建服务识别的日志信息 -// 格式: addr service [Product:xxx ||Version:xxx] Banner:(xxx) +// 格式: addr-or-url service [Product:xxx ||Version:xxx] Banner:(xxx) func buildServiceLogMessage(addr string, serviceInfo *ServiceInfo, isWeb bool) string { var msg strings.Builder - fmt.Fprintf(&msg, "%-21s", addr) + displayTarget := addr + if isWeb { + displayTarget = buildWebServiceURL(addr, serviceInfo) + } + fmt.Fprintf(&msg, "%-30s", displayTarget) if serviceInfo.Name != "unknown" { fmt.Fprintf(&msg, " %-8s", serviceInfo.Name) @@ -453,6 +457,30 @@ func buildServiceLogMessage(addr string, serviceInfo *ServiceInfo, isWeb bool) s return msg.String() } +func buildWebServiceURL(addr string, serviceInfo *ServiceInfo) string { + protocol := "http" + serviceName := "" + if serviceInfo != nil { + serviceName = strings.ToLower(serviceInfo.Name) + } + + if strings.Contains(serviceName, "https") || strings.Contains(serviceName, "ssl") || strings.Contains(serviceName, "tls") { + protocol = "https" + } + + host, port, err := net.SplitHostPort(addr) + if err != nil { + return fmt.Sprintf("%s://%s", protocol, addr) + } + if protocol == "http" && port == "80" { + return fmt.Sprintf("http://%s", host) + } + if protocol == "https" && port == "443" { + return fmt.Sprintf("https://%s", host) + } + return fmt.Sprintf("%s://%s", protocol, net.JoinHostPort(host, port)) +} + // scanSinglePort 扫描单个端口并进行服务识别(重构后的简洁版本) func scanSinglePort(ctx context.Context, host string, port int, addr string, adaptiveTO *AdaptiveTimeout, count *atomic.Int64, collector *resultCollector, failedCollector *failedPortCollector, session *common.ScanSession) { config := session.Config diff --git a/core/port_scan_test.go b/core/port_scan_test.go index e527964..0760338 100644 --- a/core/port_scan_test.go +++ b/core/port_scan_test.go @@ -478,7 +478,31 @@ func TestBuildServiceLogMessage(t *testing.T) { Extras: map[string]string{}, }, isWeb: true, - wantContain: []string{"192.168.1.1:80", "http", "1.1"}, + wantContain: []string{"http://192.168.1.1", "http", "1.1"}, + }, + { + name: "非标准端口HTTP服务显示URL", + addr: "192.168.1.1:8080", + serviceInfo: &ServiceInfo{ + Name: "http", + Version: "1.1", + Banner: "", + Extras: map[string]string{}, + }, + isWeb: true, + wantContain: []string{"http://192.168.1.1:8080", "http", "1.1"}, + }, + { + name: "HTTPS服务显示HTTPS URL", + addr: "192.168.1.1:443", + serviceInfo: &ServiceInfo{ + Name: "https", + Version: "1.1", + Banner: "", + Extras: map[string]string{}, + }, + isWeb: true, + wantContain: []string{"https://192.168.1.1", "https", "1.1"}, }, { name: "带Banner的SSH服务",