package portfinger import ( "fmt" "regexp" "strconv" "strings" "github.com/shadow1ng/fscan/common" ) // ParseVersionInfo 解析版本信息并返回额外信息结构 func (m *Match) ParseVersionInfo(response []byte) Extras { common.LogDebug("开始解析版本信息") var extras = Extras{} // 确保有匹配项 if len(m.FoundItems) == 0 { common.LogDebug("没有匹配项,无法解析版本信息") return extras } // 替换版本信息中的占位符 foundItems := m.FoundItems versionInfo := m.VersionInfo for index, value := range foundItems { dollarName := "$" + strconv.Itoa(index+1) versionInfo = strings.Replace(versionInfo, dollarName, value, -1) } common.LogDebug("替换后的版本信息: " + versionInfo) // 定义解析函数 parseField := func(field, pattern string) string { patterns := []string{ pattern + `/([^/]*)/`, // 斜线分隔 pattern + `\|([^|]*)\|`, // 竖线分隔 } for _, p := range patterns { if strings.Contains(versionInfo, pattern) { regex := regexp.MustCompile(p) if matches := regex.FindStringSubmatch(versionInfo); len(matches) > 1 { common.LogDebug(fmt.Sprintf("解析到%s: %s", field, matches[1])) return matches[1] } } } return "" } // 解析各个字段 extras.VendorProduct = parseField("厂商产品", " p") extras.Version = parseField("版本", " v") extras.Info = parseField("信息", " i") extras.Hostname = parseField("主机名", " h") extras.OperatingSystem = parseField("操作系统", " o") extras.DeviceType = parseField("设备类型", " d") // 特殊处理CPE if strings.Contains(versionInfo, " cpe:/") || strings.Contains(versionInfo, " cpe:|") { cpePatterns := []string{`cpe:/([^/]*)`, `cpe:\|([^|]*)`} for _, pattern := range cpePatterns { regex := regexp.MustCompile(pattern) if cpeName := regex.FindStringSubmatch(versionInfo); len(cpeName) > 0 { if len(cpeName) > 1 { extras.CPE = cpeName[1] } else { extras.CPE = cpeName[0] } common.LogDebug("解析到CPE: " + extras.CPE) break } } } return extras } // ToMap 将 Extras 转换为 map[string]string func (e *Extras) ToMap() map[string]string { common.LogDebug("开始转换Extras为Map") result := make(map[string]string) // 定义字段映射 fields := map[string]string{ "vendor_product": e.VendorProduct, "version": e.Version, "info": e.Info, "hostname": e.Hostname, "os": e.OperatingSystem, "device_type": e.DeviceType, "cpe": e.CPE, } // 添加非空字段到结果map for key, value := range fields { if value != "" { result[key] = value common.LogDebug(fmt.Sprintf("添加字段 %s: %s", key, value)) } } common.LogDebug(fmt.Sprintf("转换完成,共有 %d 个字段", len(result))) return result } // TrimBanner 清理横幅数据,移除不可打印字符 func TrimBanner(banner string) string { // 移除开头和结尾的空白字符 banner = strings.TrimSpace(banner) // 移除控制字符,但保留换行符和制表符 var result strings.Builder for _, r := range banner { if r >= 32 && r <= 126 { // 可打印ASCII字符 result.WriteRune(r) } else if r == '\n' || r == '\t' { // 保留换行符和制表符 result.WriteRune(r) } else { result.WriteRune(' ') // 其他控制字符替换为空格 } } // 压缩多个连续空格为单个空格 resultStr := result.String() spaceRe := regexp.MustCompile(`\s+`) resultStr = spaceRe.ReplaceAllString(resultStr, " ") return strings.TrimSpace(resultStr) }