feat: v2.1.0 核心重构与功能增强

## 架构重构
- 全局变量消除,迁移至 Config/State 对象
- SMB 插件融合(smb/smb2/smbghost/smbinfo)
- 服务探测重构,实现 Nmap 风格 fallback 机制
- 输出系统重构,TXT 实时刷盘 + 双写机制
- i18n 框架升级至 go-i18n

## 性能优化
- 正则表达式预编译
- 内存优化 map[string]struct{}
- 并发指纹匹配
- SOCKS5 连接复用
- 滑动窗口调度 + 自适应线程池

## 新功能
- Web 管理界面
- 多格式 POC 适配(xray/afrog)
- 增强指纹库(3139条)
- Favicon hash 指纹识别
- 插件选择性编译(Build Tags)
- fscan-lab 靶场环境
- 默认端口扩展(62→133)

## 构建系统
- 添加 no_local tag 支持排除本地插件
- 多版本构建:fscan/fscan-nolocal/fscan-web
- CI 添加 snapshot 模式支持仅测试构建

## Bug 修复
- 修复 120+ 个问题,包括 RDP panic、批量扫描漏报、
  JSON 输出格式、Redis 检测、Context 超时等

## 测试增强
- 单元测试覆盖率 74-100%
- 并发安全测试
- 集成测试(Web/端口/服务/SSH/ICMP)
This commit is contained in:
ZacharyZcR
2026-01-11 20:16:23 +08:00
parent 6b13b2e84f
commit 71b92d4408
948 changed files with 92335 additions and 24630 deletions
+104
View File
@@ -0,0 +1,104 @@
package portfinger
import (
"encoding/hex"
"strconv"
)
// DecodePattern 解码匹配模式
func DecodePattern(s string) ([]byte, error) {
b := []byte(s)
var result []byte
for i := 0; i < len(b); {
if b[i] == '\\' && i+1 < len(b) {
// 处理转义序列
switch b[i+1] {
case 'x':
// 十六进制编码 \xNN
if i+3 < len(b) {
if hexStr := string(b[i+2 : i+4]); isValidHex(hexStr) {
if decoded, err := hex.DecodeString(hexStr); err == nil {
result = append(result, decoded...)
i += 4
continue
}
}
}
case 'a':
result = append(result, '\a')
i += 2
continue
case 'f':
result = append(result, '\f')
i += 2
continue
case 't':
result = append(result, '\t')
i += 2
continue
case 'n':
result = append(result, '\n')
i += 2
continue
case 'r':
result = append(result, '\r')
i += 2
continue
case 'v':
result = append(result, '\v')
i += 2
continue
case '\\':
result = append(result, '\\')
i += 2
continue
default:
// 八进制编码 \NNN
if i+1 < len(b) && b[i+1] >= '0' && b[i+1] <= '7' {
octalStr := ""
j := i + 1
for j < len(b) && j < i+4 && b[j] >= '0' && b[j] <= '7' {
octalStr += string(b[j])
j++
}
// 使用16位解析避免int8溢出(\377=255超出int8范围)
if octal, err := strconv.ParseInt(octalStr, 8, 16); err == nil && octal <= 255 {
result = append(result, byte(octal))
i = j
continue
}
}
}
}
// 普通字符
result = append(result, b[i])
i++
}
return result, nil
}
// DecodeData 解码探测数据
func DecodeData(s string) ([]byte, error) {
// 移除首尾的分隔符
if len(s) > 0 && (s[0] == '"' || s[0] == '\'') {
s = s[1:]
}
if len(s) > 0 && (s[len(s)-1] == '"' || s[len(s)-1] == '\'') {
s = s[:len(s)-1]
}
return DecodePattern(s)
}
// isValidHex 检查字符串是否为有效的十六进制
func isValidHex(s string) bool {
for _, c := range s {
if (c < '0' || c > '9') && (c < 'A' || c > 'F') && (c < 'a' || c > 'f') {
return false
}
}
return len(s) == 2
}
+361
View File
@@ -0,0 +1,361 @@
package portfinger
import (
"bytes"
"testing"
)
// TestDecodePattern 测试nmap探测数据解码
func TestDecodePattern(t *testing.T) {
tests := []struct {
name string
input string
expected []byte
}{
{
name: "十六进制编码-单字节",
input: `\x48`,
expected: []byte{0x48}, // 'H'
},
{
name: "十六进制编码-多字节",
input: `\x48\x65\x6c\x6c\x6f`,
expected: []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f}, // "Hello"
},
{
name: "转义字符-换行",
input: `\n`,
expected: []byte{'\n'},
},
{
name: "转义字符-回车",
input: `\r`,
expected: []byte{'\r'},
},
{
name: "转义字符-制表符",
input: `\t`,
expected: []byte{'\t'},
},
{
name: "转义字符-响铃",
input: `\a`,
expected: []byte{'\a'},
},
{
name: "转义字符-换页",
input: `\f`,
expected: []byte{'\f'},
},
{
name: "转义字符-垂直制表符",
input: `\v`,
expected: []byte{'\v'},
},
{
name: "转义字符-反斜杠",
input: `\\`,
expected: []byte{'\\'},
},
{
name: "八进制编码-单字节",
input: `\101`,
expected: []byte{0101}, // 'A' (65)
},
{
name: "八进制编码-两位",
input: `\72`,
expected: []byte{072}, // ':' (58)
},
{
name: "八进制编码-一位",
input: `\7`,
expected: []byte{7},
},
{
name: "混合编码-nmap GET请求",
input: `GET / HTTP/1.0\r\n\r\n`,
expected: []byte("GET / HTTP/1.0\r\n\r\n"),
},
{
name: "混合编码-十六进制+文本",
input: `\x48ello`,
expected: []byte("Hello"),
},
{
name: "普通文本",
input: `Hello World`,
expected: []byte("Hello World"),
},
{
name: "空字符串",
input: ``,
expected: []byte{},
},
{
name: "复杂nmap探测数据",
input: `\x00\x00\x00\x01\x02\x03`,
expected: []byte{0x00, 0x00, 0x00, 0x01, 0x02, 0x03},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := DecodePattern(tt.input)
if err != nil {
t.Fatalf("DecodePattern() 错误 = %v", err)
}
if !bytes.Equal(result, tt.expected) {
t.Errorf("DecodePattern() = %v (%q), 期望 %v (%q)",
result, string(result), tt.expected, string(tt.expected))
}
})
}
}
// TestDecodePattern_InvalidHex 测试非法十六进制编码
func TestDecodePattern_InvalidHex(t *testing.T) {
tests := []struct {
name string
input string
}{
{
name: "不完整的十六进制-只有\\x",
input: `\x`,
},
{
name: "不完整的十六进制-只有一位",
input: `\xA`,
},
{
name: "非法十六进制字符",
input: `\xGH`,
},
{
name: "十六进制后截断",
input: `Hello\x`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := DecodePattern(tt.input)
// 非法的十六进制应该被忽略,返回原字符
if err != nil {
t.Errorf("DecodePattern() 不应返回错误: %v", err)
}
// 验证至少有输出(即使不正确也不应panic)
if result == nil {
t.Error("DecodePattern() 不应返回 nil")
}
})
}
}
// TestDecodePattern_OctalEdgeCases 测试八进制边界情况
func TestDecodePattern_OctalEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
expected []byte
}{
{
name: "八进制最大值int8-127",
input: `\177`,
expected: []byte{0177}, // 127, int8最大值
},
{
name: "八进制零",
input: `\0`,
expected: []byte{0},
},
{
name: "八进制混合",
input: `\101\102\103`,
expected: []byte{'A', 'B', 'C'},
},
{
name: "八进制后跟普通数字",
input: `\1018`,
expected: []byte{0101, '8'}, // 'A' + '8'
},
{
name: "八进制最大值-255",
input: `\377`,
expected: []byte{0xFF}, // 255, 八进制最大值
},
{
name: "八进制超出255-按原字符",
input: `\777`,
expected: []byte{'\\', '7', '7', '7'}, // 超出范围,按原字符处理
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := DecodePattern(tt.input)
if err != nil {
t.Fatalf("DecodePattern() 错误 = %v", err)
}
if !bytes.Equal(result, tt.expected) {
t.Errorf("DecodePattern() = %v, 期望 %v", result, tt.expected)
}
})
}
}
// TestDecodeData 测试DecodeData包装器
func TestDecodeData(t *testing.T) {
tests := []struct {
name string
input string
expected []byte
}{
{
name: "双引号包裹",
input: `"Hello"`,
expected: []byte("Hello"),
},
{
name: "单引号包裹",
input: `'World'`,
expected: []byte("World"),
},
{
name: "双引号包裹+转义",
input: `"\x48\x65\x6c\x6c\x6f"`,
expected: []byte("Hello"),
},
{
name: "无引号",
input: `Hello`,
expected: []byte("Hello"),
},
{
name: "只有开头引号",
input: `"Hello`,
expected: []byte("Hello"),
},
{
name: "只有结尾引号",
input: `Hello"`,
expected: []byte("Hello"),
},
{
name: "空字符串-双引号",
input: `""`,
expected: []byte{},
},
{
name: "nmap探测数据格式",
input: `"GET / HTTP/1.0\r\n\r\n"`,
expected: []byte("GET / HTTP/1.0\r\n\r\n"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := DecodeData(tt.input)
if err != nil {
t.Fatalf("DecodeData() 错误 = %v", err)
}
if !bytes.Equal(result, tt.expected) {
t.Errorf("DecodeData() = %v (%q), 期望 %v (%q)",
result, string(result), tt.expected, string(tt.expected))
}
})
}
}
// TestIsValidHex 测试十六进制验证
func TestIsValidHex(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{"合法-数字", "12", true},
{"合法-小写字母", "ab", true},
{"合法-大写字母", "AB", true},
{"合法-混合", "3F", true},
{"合法-全0", "00", true},
{"合法-全F", "FF", true},
{"非法-单字符", "A", false},
{"非法-三字符", "ABC", false},
{"非法-空字符串", "", false},
{"非法-包含G", "AG", false},
{"非法-包含特殊字符", "A@", false},
{"非法-包含空格", "A ", false},
{"非法-汉字", "中文", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isValidHex(tt.input)
if result != tt.expected {
t.Errorf("isValidHex(%q) = %v, 期望 %v", tt.input, result, tt.expected)
}
})
}
}
// TestDecodePattern_RealWorldNmapData 测试真实nmap探测数据
func TestDecodePattern_RealWorldNmapData(t *testing.T) {
tests := []struct {
name string
input string
desc string
}{
{
name: "HTTP GET请求",
input: `GET / HTTP/1.0\r\n\r\n`,
desc: "nmap HTTP探测",
},
{
name: "SSH握手",
input: `SSH-2.0-OpenSSH_8.0\r\n`,
desc: "SSH版本探测",
},
{
name: "MySQL握手",
input: `\x00\x00\x00\x0a5.7.0`,
desc: "MySQL协议",
},
{
name: "二进制协议",
input: `\x00\x01\x02\x03\x04\x05`,
desc: "纯二进制数据",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := DecodePattern(tt.input)
if err != nil {
t.Errorf("%s 解码失败: %v", tt.desc, err)
}
if len(result) == 0 {
t.Errorf("%s 解码结果为空", tt.desc)
}
t.Logf("%s 解码成功: %d 字节", tt.desc, len(result))
})
}
}
// BenchmarkDecodePattern 基准测试DecodePattern
func BenchmarkDecodePattern(b *testing.B) {
input := `GET / HTTP/1.0\r\n\r\n`
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = DecodePattern(input)
}
}
// BenchmarkDecodePattern_Complex 基准测试复杂编码
func BenchmarkDecodePattern_Complex(b *testing.B) {
input := `\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\r\n`
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = DecodePattern(input)
}
}
+71
View File
@@ -0,0 +1,71 @@
package portfinger
import (
"fmt"
"regexp"
"strings"
)
// parseMatchDirective 解析match/softmatch指令的通用实现
func (p *Probe) parseMatchDirective(data, prefix string, isSoft bool) (Match, error) {
match := Match{IsSoft: isSoft}
// 提取指令文本并解析语法
matchText := data[len(prefix)+1:]
directive := p.getDirectiveSyntax(matchText)
// 分割文本获取pattern和版本信息
textSplited := strings.Split(directive.DirectiveStr, directive.Delimiter)
if len(textSplited) == 0 {
return match, fmt.Errorf("无效的%s指令格式", prefix)
}
pattern := textSplited[0]
versionInfo := strings.Join(textSplited[1:], "")
// 解码并编译正则表达式
patternUnescaped, decodeErr := DecodePattern(pattern)
if decodeErr != nil {
return match, decodeErr
}
patternCompiled, compileErr := regexp.Compile(string(patternUnescaped))
if compileErr != nil {
return match, compileErr
}
match.Service = directive.DirectiveName
match.Pattern = pattern
match.PatternCompiled = patternCompiled
match.VersionInfo = versionInfo
return match, nil
}
// getMatch 解析match指令获取匹配规则
func (p *Probe) getMatch(data string) (Match, error) {
return p.parseMatchDirective(data, "match", false)
}
// getSoftMatch 解析softmatch指令获取软匹配规则
func (p *Probe) getSoftMatch(data string) (Match, error) {
return p.parseMatchDirective(data, "softmatch", true)
}
// MatchPattern 检查响应是否与匹配规则匹配
func (m *Match) MatchPattern(response []byte) bool {
if m.PatternCompiled == nil {
return false
}
matched := m.PatternCompiled.Match(response)
if matched {
// 提取匹配到的子组
submatches := m.PatternCompiled.FindStringSubmatch(string(response))
if len(submatches) > 1 {
m.FoundItems = submatches[1:] // 排除完整匹配,只保留分组
}
}
return matched
}
+372
View File
@@ -0,0 +1,372 @@
package portfinger
import (
"regexp"
"testing"
)
/*
match_engine_test.go - 服务指纹匹配引擎测试
测试重点:
1. MatchPattern - 核心匹配逻辑,错误会导致服务识别失败
2. 正则表达式子组提取 - 版本信息依赖此功能
3. 边界情况 - nil编译器、空响应
不测试:
- getMatch/getSoftMatch - 依赖复杂的probe解析上下文
*/
// =============================================================================
// MatchPattern 核心测试
// =============================================================================
// TestMatchPattern_BasicMatching 测试基本匹配功能
func TestMatchPattern_BasicMatching(t *testing.T) {
tests := []struct {
name string
pattern string
response []byte
expected bool
}{
{
name: "SSH版本匹配",
pattern: `SSH-[\d.]+-(.*)`,
response: []byte("SSH-2.0-OpenSSH_8.0"),
expected: true,
},
{
name: "HTTP协议匹配",
pattern: `HTTP/1\.[01] (\d{3})`,
response: []byte("HTTP/1.1 200 OK"),
expected: true,
},
{
name: "不匹配",
pattern: `SSH-`,
response: []byte("HTTP/1.1 200 OK"),
expected: false,
},
{
name: "空响应",
pattern: `.*`,
response: []byte{},
expected: true, // .* 匹配空字符串
},
{
name: "二进制数据匹配",
pattern: `^\x00\x01`,
response: []byte{0x00, 0x01, 0x02, 0x03},
expected: true,
},
{
name: "MySQL握手匹配",
pattern: `^\x00\x00\x00\x0a([\d.]+)`,
response: []byte("\x00\x00\x00\x0a5.7.33\x00"),
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compiled, err := regexp.Compile(tt.pattern)
if err != nil {
t.Fatalf("正则编译失败: %v", err)
}
m := &Match{
PatternCompiled: compiled,
}
result := m.MatchPattern(tt.response)
if result != tt.expected {
t.Errorf("MatchPattern() = %v, 期望 %v", result, tt.expected)
}
})
}
}
// TestMatchPattern_SubgroupExtraction 测试子组提取
//
// 这是关键功能:版本信息从正则表达式的分组中提取
func TestMatchPattern_SubgroupExtraction(t *testing.T) {
tests := []struct {
name string
pattern string
response []byte
expectedItems []string
}{
{
name: "提取SSH版本",
pattern: `SSH-[\d.]+-(.*)`,
response: []byte("SSH-2.0-OpenSSH_8.0"),
expectedItems: []string{"OpenSSH_8.0"},
},
{
name: "提取HTTP状态码",
pattern: `HTTP/1\.[01] (\d{3}) (.*)`,
response: []byte("HTTP/1.1 200 OK"),
expectedItems: []string{"200", "OK"},
},
{
name: "提取多个分组",
pattern: `(\w+)://([^:/]+):?(\d*)`,
response: []byte("https://example.com:443"),
expectedItems: []string{"https", "example.com", "443"},
},
{
name: "无分组",
pattern: `SSH-2\.0`,
response: []byte("SSH-2.0-OpenSSH"),
expectedItems: nil, // 无分组时为nil
},
{
name: "可选分组为空",
pattern: `HTTP/(\d+)\.(\d+)`,
response: []byte("HTTP/1.1 200 OK"),
expectedItems: []string{"1", "1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compiled, err := regexp.Compile(tt.pattern)
if err != nil {
t.Fatalf("正则编译失败: %v", err)
}
m := &Match{
PatternCompiled: compiled,
}
matched := m.MatchPattern(tt.response)
if !matched {
t.Fatal("应该匹配成功")
}
// 验证提取的子组
if tt.expectedItems == nil {
if len(m.FoundItems) != 0 {
t.Errorf("FoundItems 应为空,实际 %v", m.FoundItems)
}
return
}
if len(m.FoundItems) != len(tt.expectedItems) {
t.Fatalf("FoundItems 长度 = %d, 期望 %d",
len(m.FoundItems), len(tt.expectedItems))
}
for i, expected := range tt.expectedItems {
if m.FoundItems[i] != expected {
t.Errorf("FoundItems[%d] = %q, 期望 %q",
i, m.FoundItems[i], expected)
}
}
})
}
}
// TestMatchPattern_NilCompiler 测试nil编译器
//
// 边界情况:如果正则编译失败,PatternCompiled为nil
func TestMatchPattern_NilCompiler(t *testing.T) {
m := &Match{
PatternCompiled: nil,
}
result := m.MatchPattern([]byte("any data"))
if result {
t.Error("nil编译器应返回false")
}
}
// TestMatchPattern_RealWorldServices 测试真实服务指纹
func TestMatchPattern_RealWorldServices(t *testing.T) {
tests := []struct {
name string
pattern string
response []byte
expectedService string
expectMatch bool
}{
{
name: "OpenSSH",
pattern: `SSH-2\.0-OpenSSH[_\d\.p]+`,
response: []byte("SSH-2.0-OpenSSH_8.0p1 Ubuntu-6ubuntu0.1"),
expectedService: "ssh",
expectMatch: true,
},
{
name: "nginx",
pattern: `Server: nginx/?([\d.]+)?`,
response: []byte("HTTP/1.1 200 OK\r\nServer: nginx/1.18.0\r\n"),
expectedService: "http",
expectMatch: true,
},
{
name: "Redis",
pattern: `-ERR wrong number of arguments`,
response: []byte("-ERR wrong number of arguments for 'get' command\r\n"),
expectedService: "redis",
expectMatch: true,
},
{
name: "MySQL",
pattern: `mysql_native_password`,
response: []byte("\x00\x00\x00\x0a5.7.33\x00...mysql_native_password\x00"),
expectedService: "mysql",
expectMatch: true,
},
{
name: "FTP-220",
pattern: `^220[\s-]`,
response: []byte("220 (vsFTPd 3.0.3)\r\n"),
expectedService: "ftp",
expectMatch: true,
},
{
name: "SMTP-220",
pattern: `^220.*SMTP`,
response: []byte("220 mail.example.com ESMTP Postfix\r\n"),
expectedService: "smtp",
expectMatch: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compiled, err := regexp.Compile(tt.pattern)
if err != nil {
t.Fatalf("正则编译失败: %v", err)
}
m := &Match{
Service: tt.expectedService,
PatternCompiled: compiled,
}
result := m.MatchPattern(tt.response)
if result != tt.expectMatch {
t.Errorf("服务 %s 匹配失败: 期望 %v, 实际 %v",
tt.expectedService, tt.expectMatch, result)
}
})
}
}
// TestMatchPattern_FoundItemsReset 测试FoundItems在多次匹配时的重置
func TestMatchPattern_FoundItemsReset(t *testing.T) {
compiled, _ := regexp.Compile(`SSH-(\d+)\.(\d+)-(.*)`)
m := &Match{
PatternCompiled: compiled,
}
// 第一次匹配
m.MatchPattern([]byte("SSH-2.0-OpenSSH_8.0"))
firstItems := make([]string, len(m.FoundItems))
copy(firstItems, m.FoundItems)
// 第二次匹配不同内容
m.MatchPattern([]byte("SSH-1.99-Dropbear"))
// 验证FoundItems被更新
if len(m.FoundItems) < 1 {
t.Fatal("第二次匹配后FoundItems应有内容")
}
if m.FoundItems[2] == "OpenSSH_8.0" {
t.Error("FoundItems 未被更新为新的匹配结果")
}
if m.FoundItems[2] != "Dropbear" {
t.Errorf("FoundItems[2] = %q, 期望 Dropbear", m.FoundItems[2])
}
}
// =============================================================================
// Match 结构体属性测试
// =============================================================================
// TestMatch_IsSoftFlag 测试软匹配标志
func TestMatch_IsSoftFlag(t *testing.T) {
hardMatch := Match{IsSoft: false}
softMatch := Match{IsSoft: true}
if hardMatch.IsSoft {
t.Error("硬匹配的IsSoft应为false")
}
if !softMatch.IsSoft {
t.Error("软匹配的IsSoft应为true")
}
}
// =============================================================================
// 边界情况测试
// =============================================================================
// TestMatchPattern_LargeResponse 测试大响应数据
func TestMatchPattern_LargeResponse(t *testing.T) {
compiled, _ := regexp.Compile(`needle`)
m := &Match{
PatternCompiled: compiled,
}
// 构造包含关键字的大响应(100KB)
largeData := make([]byte, 100*1024)
for i := range largeData {
largeData[i] = 'x'
}
copy(largeData[50*1024:], []byte("needle"))
result := m.MatchPattern(largeData)
if !result {
t.Error("大响应中的关键字应被匹配")
}
}
// TestMatchPattern_BinaryData 测试二进制数据匹配
func TestMatchPattern_BinaryData(t *testing.T) {
// 测试二进制数据中的固定字符串匹配
compiled, _ := regexp.Compile(`SMB`)
m := &Match{
PatternCompiled: compiled,
}
// SMB协议头包含固定字符串 "SMB"
smbResponse := []byte{0x00, 0x00, 0x00, 0x45, 0xff, 'S', 'M', 'B', 0x00}
result := m.MatchPattern(smbResponse)
if !result {
t.Error("二进制数据中的SMB字符串应被匹配")
}
// 验证能提取SMB协议版本
compiled2, _ := regexp.Compile(`SMBr`)
m2 := &Match{PatternCompiled: compiled2}
smb2Response := []byte("SMBr\x00\x00\x00\x00")
result2 := m2.MatchPattern(smb2Response)
if !result2 {
t.Error("SMBr应被匹配")
}
}
// TestMatchPattern_UnicodeResponse 测试Unicode响应
func TestMatchPattern_UnicodeResponse(t *testing.T) {
compiled, _ := regexp.Compile(`服务器`)
m := &Match{
PatternCompiled: compiled,
}
response := []byte("HTTP/1.1 200 OK\r\nServer: 服务器\r\n")
result := m.MatchPattern(response)
if !result {
t.Error("Unicode内容应被匹配")
}
}
File diff suppressed because it is too large Load Diff
+246
View File
@@ -0,0 +1,246 @@
package portfinger
import (
"fmt"
"strconv"
"strings"
)
// 解析指令语法,返回指令结构
func (p *Probe) getDirectiveSyntax(data string) (directive Directive) {
directive = Directive{}
// 查找第一个空格的位置
blankIndex := strings.Index(data, " ")
if blankIndex == -1 {
return directive
}
// 解析各个字段
directiveName := data[:blankIndex]
Flag := data[blankIndex+1 : blankIndex+2]
delimiter := data[blankIndex+2 : blankIndex+3]
directiveStr := data[blankIndex+3:]
directive.DirectiveName = directiveName
directive.Flag = Flag
directive.Delimiter = delimiter
directive.DirectiveStr = directiveStr
return directive
}
// 解析探测器信息
func (p *Probe) parseProbeInfo(probeStr string) {
// 提取协议和其他信息
proto := probeStr[:4]
other := probeStr[4:]
// 验证协议类型
if proto != "TCP " && proto != "UDP " {
errMsg := "探测器协议必须是 TCP 或 UDP"
panic(errMsg)
}
// 验证其他信息不为空
if len(other) == 0 {
errMsg := "nmap-service-probes - 探测器名称无效"
panic(errMsg)
}
// 解析指令
directive := p.getDirectiveSyntax(other)
// 设置探测器属性
p.Name = directive.DirectiveName
p.Data = strings.Split(directive.DirectiveStr, directive.Delimiter)[0]
p.Protocol = strings.ToLower(strings.TrimSpace(proto))
}
// 从字符串解析探测器信息
func (p *Probe) fromString(data string) error {
var err error
// 预处理数据
data = strings.TrimSpace(data)
lines := strings.Split(data, "\n")
if len(lines) == 0 {
return fmt.Errorf("输入数据为空")
}
probeStr := lines[0]
p.parseProbeInfo(probeStr)
// 解析匹配规则和其他配置
var matchs []Match
for _, line := range lines {
switch {
case strings.HasPrefix(line, "match "):
match, matchErr := p.getMatch(line)
if matchErr != nil {
continue
}
matchs = append(matchs, match)
case strings.HasPrefix(line, "softmatch "):
softMatch, matchErr := p.getSoftMatch(line)
if matchErr != nil {
continue
}
matchs = append(matchs, softMatch)
case strings.HasPrefix(line, "ports "):
p.parsePorts(line)
case strings.HasPrefix(line, "sslports "):
p.parseSSLPorts(line)
case strings.HasPrefix(line, "totalwaitms "):
p.parseTotalWaitMS(line)
case strings.HasPrefix(line, "tcpwrappedms "):
p.parseTCPWrappedMS(line)
case strings.HasPrefix(line, "rarity "):
p.parseRarity(line)
case strings.HasPrefix(line, "fallback "):
p.parseFallback(line)
}
}
p.Matchs = &matchs
return err
}
// 解析端口配置
func (p *Probe) parsePorts(data string) {
p.Ports = data[len("ports")+1:]
}
// 解析SSL端口配置
func (p *Probe) parseSSLPorts(data string) {
p.SSLPorts = data[len("sslports")+1:]
}
// 解析总等待时间
func (p *Probe) parseTotalWaitMS(data string) {
waitMS, err := strconv.Atoi(strings.TrimSpace(data[len("totalwaitms")+1:]))
if err != nil {
return
}
p.TotalWaitMS = waitMS
}
// 解析TCP包装等待时间
func (p *Probe) parseTCPWrappedMS(data string) {
wrappedMS, err := strconv.Atoi(strings.TrimSpace(data[len("tcpwrappedms")+1:]))
if err != nil {
return
}
p.TCPWrappedMS = wrappedMS
}
// 解析稀有度
func (p *Probe) parseRarity(data string) {
rarity, err := strconv.Atoi(strings.TrimSpace(data[len("rarity")+1:]))
if err != nil {
return
}
p.Rarity = rarity
}
// 解析回退配置
func (p *Probe) parseFallback(data string) {
p.Fallback = data[len("fallback")+1:]
}
// 从内容解析探测器规则
func (v *VScan) parseProbesFromContent(content string) {
var probes []Probe
var lines []string
// 过滤注释和空行
linesTemp := strings.Split(content, "\n")
for _, lineTemp := range linesTemp {
lineTemp = strings.TrimSpace(lineTemp)
if lineTemp == "" || strings.HasPrefix(lineTemp, "#") {
continue
}
lines = append(lines, lineTemp)
}
// 验证文件内容
if len(lines) == 0 {
errMsg := "读取nmap-service-probes文件失败: 内容为空"
panic(errMsg)
}
// 检查Exclude指令
excludeCount := 0
for _, line := range lines {
if strings.HasPrefix(line, "Exclude ") {
excludeCount++
}
if excludeCount > 1 {
errMsg := "nmap-service-probes文件中只允许有一个Exclude指令"
panic(errMsg)
}
}
// 验证第一行格式
firstLine := lines[0]
if !strings.HasPrefix(firstLine, "Exclude ") && !strings.HasPrefix(firstLine, "Probe ") {
errMsg := "解析错误: 首行必须以\"Probe \"或\"Exclude \"开头"
panic(errMsg)
}
// 处理Exclude指令
if excludeCount == 1 {
v.Exclude = firstLine[len("Exclude")+1:]
lines = lines[1:]
}
// 合并内容并分割探测器
content = "\n" + strings.Join(lines, "\n")
probeParts := strings.Split(content, "\nProbe")[1:]
// 解析每个探测器
for _, probePart := range probeParts {
probe := Probe{}
if err := probe.fromString(probePart); err != nil {
continue
}
probes = append(probes, probe)
}
v.AllProbes = probes
}
// 将探测器转换为名称映射
func (v *VScan) parseProbesToMapKName() {
v.ProbesMapKName = map[string]Probe{}
for _, probe := range v.AllProbes {
v.ProbesMapKName[probe.Name] = probe
}
}
// SetusedProbes 设置使用的探测器
func (v *VScan) SetusedProbes() {
for _, probe := range v.AllProbes {
if strings.ToLower(probe.Protocol) == "tcp" {
if probe.Name == "SSLSessionReq" {
continue
}
v.Probes = append(v.Probes, probe)
// 特殊处理TLS会话请求
if probe.Name == "TLSSessionReq" {
sslProbe := v.ProbesMapKName["SSLSessionReq"]
v.Probes = append(v.Probes, sslProbe)
}
} else {
v.UDPProbes = append(v.UDPProbes, probe)
}
}
}
+151
View File
@@ -0,0 +1,151 @@
package portfinger
import (
"sort"
"strconv"
"strings"
)
// PortInRange 检查端口是否在指定的端口范围字符串内
// 端口范围格式: "21,22,80,1000-2000,8080"
func PortInRange(port int, portsStr string) bool {
if portsStr == "" {
return false
}
parts := strings.Split(portsStr, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
// 检查是否是范围 (如 "1000-2000")
if strings.Contains(part, "-") {
rangeParts := strings.Split(part, "-")
if len(rangeParts) == 2 {
start, err1 := strconv.Atoi(strings.TrimSpace(rangeParts[0]))
end, err2 := strconv.Atoi(strings.TrimSpace(rangeParts[1]))
if err1 == nil && err2 == nil && port >= start && port <= end {
return true
}
}
} else {
// 单个端口
p, err := strconv.Atoi(part)
if err == nil && p == port {
return true
}
}
}
return false
}
// GetProbesForPort 获取适用于指定端口的所有探测器
// 根据 Probe.Ports 字段筛选,并按 Rarity 从低到高排序
func (v *VScan) GetProbesForPort(port int) []*Probe {
var result []*Probe
for i := range v.Probes {
probe := &v.Probes[i]
// 跳过 UDP 探测器
if probe.Protocol == "udp" {
continue
}
// 检查端口是否在探测器的 ports 范围内
if PortInRange(port, probe.Ports) {
result = append(result, probe)
}
}
// 按 Rarity 从低到高排序 (rarity 越低越优先)
sort.Slice(result, func(i, j int) bool {
// rarity 为 0 表示未设置,视为最低优先级 (放最后)
ri, rj := result[i].Rarity, result[j].Rarity
if ri == 0 {
ri = 10
}
if rj == 0 {
rj = 10
}
return ri < rj
})
return result
}
// GetSSLProbesForPort 获取适用于指定端口的 SSL 探测器
func (v *VScan) GetSSLProbesForPort(port int) []*Probe {
var result []*Probe
for i := range v.Probes {
probe := &v.Probes[i]
// 检查端口是否在探测器的 sslports 范围内
if PortInRange(port, probe.SSLPorts) {
result = append(result, probe)
}
}
// 按 Rarity 排序
sort.Slice(result, func(i, j int) bool {
ri, rj := result[i].Rarity, result[j].Rarity
if ri == 0 {
ri = 10
}
if rj == 0 {
rj = 10
}
return ri < rj
})
return result
}
// GetAllProbesSortedByRarity 获取所有 TCP 探测器,按 Rarity 排序
func (v *VScan) GetAllProbesSortedByRarity() []*Probe {
result := make([]*Probe, 0, len(v.Probes))
for i := range v.Probes {
probe := &v.Probes[i]
if probe.Protocol != "udp" {
result = append(result, probe)
}
}
sort.Slice(result, func(i, j int) bool {
ri, rj := result[i].Rarity, result[j].Rarity
if ri == 0 {
ri = 10
}
if rj == 0 {
rj = 10
}
return ri < rj
})
return result
}
// FilterProbesByIntensity 根据 intensity 过滤探测器
// intensity 范围 1-9,默认 7
func FilterProbesByIntensity(probes []*Probe, intensity int) []*Probe {
if intensity <= 0 {
intensity = 7
}
if intensity > 9 {
intensity = 9
}
var result []*Probe
for _, probe := range probes {
// rarity 为 0 表示未设置,视为 1 (最常用)
rarity := probe.Rarity
if rarity == 0 {
rarity = 1
}
if rarity <= intensity {
result = append(result, probe)
}
}
return result
}
+341
View File
@@ -0,0 +1,341 @@
package portfinger
import (
"testing"
)
func TestPortInRange(t *testing.T) {
tests := []struct {
name string
port int
portsStr string
expected bool
}{
{
name: "单个端口匹配",
port: 80,
portsStr: "80",
expected: true,
},
{
name: "单个端口不匹配",
port: 81,
portsStr: "80",
expected: false,
},
{
name: "端口列表匹配",
port: 443,
portsStr: "80,443,8080",
expected: true,
},
{
name: "端口列表不匹配",
port: 8443,
portsStr: "80,443,8080",
expected: false,
},
{
name: "端口范围匹配-起点",
port: 1000,
portsStr: "1000-2000",
expected: true,
},
{
name: "端口范围匹配-终点",
port: 2000,
portsStr: "1000-2000",
expected: true,
},
{
name: "端口范围匹配-中间",
port: 1500,
portsStr: "1000-2000",
expected: true,
},
{
name: "端口范围不匹配-小于起点",
port: 999,
portsStr: "1000-2000",
expected: false,
},
{
name: "端口范围不匹配-大于终点",
port: 2001,
portsStr: "1000-2000",
expected: false,
},
{
name: "混合格式匹配-单端口",
port: 22,
portsStr: "22,80,443,1000-2000,8080",
expected: true,
},
{
name: "混合格式匹配-范围内",
port: 1234,
portsStr: "22,80,443,1000-2000,8080",
expected: true,
},
{
name: "混合格式不匹配",
port: 3000,
portsStr: "22,80,443,1000-2000,8080",
expected: false,
},
{
name: "空字符串",
port: 80,
portsStr: "",
expected: false,
},
{
name: "带空格的端口列表",
port: 443,
portsStr: "80, 443, 8080",
expected: true,
},
{
name: "Nmap格式-GetRequest探测器端口",
port: 8080,
portsStr: "80,81,82,83,84,85,86,87,88,89,90,280,443,591,593,623,664,777,808,832,888,901,981,1010,1080,1100,1241,1311,1352,1434,1944,2301,2381,2574,3000,3128,3268,4000,4001,4002,4100,4444,5000,5050,5432,5555,5800,5801,5802,5803,6080,7000,7001,7002,7103,7201,7777,7778,8000,8001,8002,8003,8006,8008,8009,8014,8042,8080,8081,8082,8083,8084,8085,8087,8088,8089,8090,8091,8100,8118,8123,8172,8180,8181,8200,8222,8243,8280,8281,8333,8383,8400,8443,8500,8509,8787,8800,8888,8899,8983,9000,9001,9002,9080,9090,9091,9100,9200,9443,9990,9999,10000,10443,12443,16080,18091,18092,20720,28017",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := PortInRange(tt.port, tt.portsStr)
if result != tt.expected {
t.Errorf("PortInRange(%d, %q) = %v, want %v", tt.port, tt.portsStr, result, tt.expected)
}
})
}
}
func TestGetProbesForPort(t *testing.T) {
// 确保全局 VScan 已初始化
InitializeGlobalVScan()
v := GetGlobalVScan()
// 测试常见端口
// 注意:SSH(22) 和 MySQL(3306) 等服务在 nmap 规则中不使用 ports 字段
// 它们依赖 NULL 探测器(等待服务主动发送 banner)
tests := []struct {
port int
expectFound bool
description string
}{
{port: 80, expectFound: true, description: "HTTP端口应该有探测器"},
{port: 22, expectFound: false, description: "SSH端口使用NULL探测(无ports字段)"},
{port: 443, expectFound: true, description: "HTTPS端口应该有探测器"},
{port: 3306, expectFound: false, description: "MySQL端口使用NULL探测(无ports字段)"},
{port: 1, expectFound: true, description: "端口1有GetRequest和Help探测器"},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
probes := v.GetProbesForPort(tt.port)
if tt.expectFound && len(probes) == 0 {
t.Errorf("端口 %d: 期望找到探测器,但找到 %d 个", tt.port, len(probes))
}
if len(probes) > 0 {
t.Logf("端口 %d: 找到 %d 个探测器", tt.port, len(probes))
for i, p := range probes {
t.Logf(" [%d] %s (rarity=%d)", i+1, p.Name, p.Rarity)
}
} else {
t.Logf("端口 %d: 无特定探测器(使用NULL探测)", tt.port)
}
})
}
}
func TestGetProbesForPort_RaritySorting(t *testing.T) {
InitializeGlobalVScan()
v := GetGlobalVScan()
// 获取端口80的探测器(应该有多个)
probes := v.GetProbesForPort(80)
if len(probes) < 2 {
t.Skip("端口80的探测器数量不足,跳过排序测试")
}
// 验证按 rarity 排序(从低到高)
for i := 1; i < len(probes); i++ {
prev := probes[i-1].Rarity
curr := probes[i].Rarity
// 将0视为10(最低优先级)
if prev == 0 {
prev = 10
}
if curr == 0 {
curr = 10
}
if prev > curr {
t.Errorf("探测器未按rarity排序: probes[%d].Rarity=%d > probes[%d].Rarity=%d",
i-1, probes[i-1].Rarity, i, probes[i].Rarity)
}
}
}
func TestFilterProbesByIntensity(t *testing.T) {
// 创建模拟探测器
probes := []*Probe{
{Name: "p1", Rarity: 1},
{Name: "p2", Rarity: 3},
{Name: "p3", Rarity: 5},
{Name: "p4", Rarity: 7},
{Name: "p5", Rarity: 9},
{Name: "p6", Rarity: 0}, // 0 视为 1
}
tests := []struct {
intensity int
expectedCount int
}{
{intensity: 1, expectedCount: 2}, // p1, p6
{intensity: 3, expectedCount: 3}, // p1, p2, p6
{intensity: 5, expectedCount: 4}, // p1, p2, p3, p6
{intensity: 7, expectedCount: 5}, // p1, p2, p3, p4, p6
{intensity: 9, expectedCount: 6}, // all
{intensity: 0, expectedCount: 5}, // 默认7,所以 p1, p2, p3, p4, p6
{intensity: -1, expectedCount: 5}, // 默认7
{intensity: 10, expectedCount: 6}, // 截断到9
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
result := FilterProbesByIntensity(probes, tt.intensity)
if len(result) != tt.expectedCount {
t.Errorf("FilterProbesByIntensity(intensity=%d): got %d probes, want %d",
tt.intensity, len(result), tt.expectedCount)
}
})
}
}
func TestGetSSLProbesForPort(t *testing.T) {
InitializeGlobalVScan()
v := GetGlobalVScan()
// 测试SSL端口
sslPorts := []int{443, 465, 636, 993, 995}
for _, port := range sslPorts {
probes := v.GetSSLProbesForPort(port)
t.Logf("SSL端口 %d: 找到 %d 个SSL探测器", port, len(probes))
}
}
func TestGetAllProbesSortedByRarity(t *testing.T) {
InitializeGlobalVScan()
v := GetGlobalVScan()
probes := v.GetAllProbesSortedByRarity()
if len(probes) == 0 {
t.Fatal("GetAllProbesSortedByRarity 返回空列表")
}
t.Logf("总共 %d 个TCP探测器", len(probes))
// 验证排序
for i := 1; i < len(probes); i++ {
prev := probes[i-1].Rarity
curr := probes[i].Rarity
if prev == 0 {
prev = 10
}
if curr == 0 {
curr = 10
}
if prev > curr {
t.Errorf("探测器未按rarity排序: probes[%d].Rarity=%d > probes[%d].Rarity=%d",
i-1, probes[i-1].Rarity, i, probes[i].Rarity)
}
}
// 打印前10个探测器
t.Log("前10个探测器(按rarity排序):")
for i := 0; i < 10 && i < len(probes); i++ {
t.Logf(" [%d] %s (rarity=%d)", i+1, probes[i].Name, probes[i].Rarity)
}
}
// TestFallbacksCompilation 验证 fallback 数组编译
func TestFallbacksCompilation(t *testing.T) {
InitializeGlobalVScan()
v := GetGlobalVScan()
// 获取 NULL 探测器
nullProbe, hasNull := v.ProbesMapKName["NULL"]
if !hasNull {
t.Fatal("NULL 探测器不存在")
}
// 验证 NULL 探测器的 fallback 只包含自身
if nullProbe.Fallbacks[0] == nil {
t.Error("NULL 探测器的 Fallbacks[0] 为 nil")
} else if nullProbe.Fallbacks[0].Name != "NULL" {
t.Errorf("NULL 探测器的 Fallbacks[0] 应该是自身,实际是 %s", nullProbe.Fallbacks[0].Name)
}
t.Log("✓ NULL 探测器的 fallback 只包含自身")
// 验证 GetRequest 探测器(TCP,无 fallback 指令)
getReq, hasGetReq := v.ProbesMapKName["GetRequest"]
if hasGetReq {
// fallbacks[0] 应该是自身
if getReq.Fallbacks[0] == nil || getReq.Fallbacks[0].Name != "GetRequest" {
t.Error("GetRequest 的 Fallbacks[0] 应该是自身")
}
// fallbacks[1] 应该是 NULL(TCP 探测器)
if getReq.Protocol == "tcp" && getReq.Fallbacks[1] != nil {
t.Logf("✓ GetRequest (TCP) 的 Fallbacks[1] = %s", getReq.Fallbacks[1].Name)
}
}
// 统计有 fallback 数组的探测器数量
countWithFallbacks := 0
countWithNullFallback := 0
for _, probe := range v.Probes {
if probe.Fallbacks[0] != nil {
countWithFallbacks++
}
// 检查 TCP 探测器是否有 NULL fallback
if probe.Protocol == "tcp" {
for i := 0; i < MaxFallbacks+1; i++ {
if probe.Fallbacks[i] == nil {
break
}
if probe.Fallbacks[i].Name == "NULL" {
countWithNullFallback++
break
}
}
}
}
t.Logf("✓ %d 个探测器有 fallback 数组", countWithFallbacks)
t.Logf("✓ %d 个 TCP 探测器有 NULL fallback", countWithNullFallback)
}
// TestFallbacksWithDirective 验证有 fallback 指令的探测器
func TestFallbacksWithDirective(t *testing.T) {
InitializeGlobalVScan()
v := GetGlobalVScan()
// 查找有 fallback 指令的探测器
for _, probe := range v.Probes {
if probe.Fallback != "" {
t.Logf("探测器 %s 有 fallback 指令: %s", probe.Name, probe.Fallback)
// 验证 fallback 数组
t.Logf(" Fallbacks 数组:")
for i := 0; i < MaxFallbacks+1; i++ {
if probe.Fallbacks[i] == nil {
break
}
t.Logf(" [%d] %s", i, probe.Fallbacks[i].Name)
}
}
}
}
+123
View File
@@ -0,0 +1,123 @@
package portfinger
import (
_ "embed"
"strings"
"sync"
)
// ProbeString 嵌入的nmap服务探测数据
//
//go:embed nmap-service-probes.txt
var ProbeString string
// 全局VScan实例(使用sync.Once确保只初始化一次)
var (
globalVScan VScan
globalNull *Probe
globalCommon *Probe
vscanOnce sync.Once
)
// Init 初始化VScan对象
func (vs *VScan) Init() {
vs.parseProbesFromContent(ProbeString)
vs.parseProbesToMapKName()
vs.SetusedProbes()
vs.compileFallbacks() // 编译 fallback 数组
}
// compileFallbacks 编译所有探测器的 fallback 数组
// 参考 Nmap 的 AllProbes::compileFallbacks() 实现
func (vs *VScan) compileFallbacks() {
// 获取 NULL 探测器指针
var nullProbe *Probe
if np, ok := vs.ProbesMapKName["NULL"]; ok {
nullProbe = &np
// NULL 探测器的 fallback 只包含自身
nullProbe.Fallbacks[0] = nullProbe
vs.ProbesMapKName["NULL"] = *nullProbe
}
// 遍历所有探测器,编译 fallback 数组
for i := range vs.Probes {
probe := &vs.Probes[i]
idx := 0
// fallbacks[0] = 自身
probe.Fallbacks[idx] = probe
idx++
if probe.Fallback == "" {
// 无 fallback 指令:TCP 使用 [自身, NULL],UDP 使用 [自身]
if probe.Protocol == "tcp" && nullProbe != nil {
probe.Fallbacks[idx] = nullProbe
}
} else {
// 有 fallback 指令:解析逗号分隔的探测器名称
fallbackNames := strings.Split(probe.Fallback, ",")
for _, name := range fallbackNames {
name = strings.TrimSpace(name)
if name == "" {
continue
}
if idx >= MaxFallbacks {
break
}
if fbProbe, ok := vs.ProbesMapKName[name]; ok {
probe.Fallbacks[idx] = &fbProbe
idx++
}
}
// TCP 探测器在末尾添加 NULL 探测器
if probe.Protocol == "tcp" && nullProbe != nil && idx < MaxFallbacks {
probe.Fallbacks[idx] = nullProbe
}
}
}
// 更新 ProbesMapKName 中的探测器(因为我们修改了 Fallbacks)
for i := range vs.Probes {
vs.ProbesMapKName[vs.Probes[i].Name] = vs.Probes[i]
}
}
// InitializeGlobalVScan 初始化全局VScan实例(线程安全,只执行一次)
func InitializeGlobalVScan() {
vscanOnce.Do(func() {
globalVScan = VScan{}
globalVScan.Init()
// 获取并检查 NULL 探测器
if nullProbe, ok := globalVScan.ProbesMapKName["NULL"]; ok {
globalNull = &nullProbe
}
// 获取并检查 GenericLines 探测器
if genericProbe, ok := globalVScan.ProbesMapKName["GenericLines"]; ok {
globalCommon = &genericProbe
}
})
}
// GetGlobalVScan 获取全局VScan实例
func GetGlobalVScan() *VScan {
InitializeGlobalVScan() // 确保已初始化
return &globalVScan
}
// GetNullProbe 获取NULL探测器
func GetNullProbe() *Probe {
InitializeGlobalVScan() // 确保已初始化
return globalNull
}
// GetCommonProbe 获取通用探测器
func GetCommonProbe() *Probe {
InitializeGlobalVScan() // 确保已初始化
return globalCommon
}
func init() {
InitializeGlobalVScan()
}
+73
View File
@@ -0,0 +1,73 @@
package portfinger
import (
"regexp"
)
// VScan 主扫描器结构体
type VScan struct {
Exclude string
AllProbes []Probe
UDPProbes []Probe
Probes []Probe
ProbesMapKName map[string]Probe
}
// MaxFallbacks 最大 fallback 数量(与 Nmap 一致)
const MaxFallbacks = 20
// Probe 探测器结构体
type Probe struct {
Name string // 探测器名称
Data string // 探测数据
Protocol string // 协议
Ports string // 端口范围
SSLPorts string // SSL端口范围
TotalWaitMS int // 总等待时间
TCPWrappedMS int // TCP包装等待时间
Rarity int // 稀有度
Fallback string // 回退探测器名称(原始字符串)
// Fallbacks 编译后的 fallback 探测器数组
// 顺序: [自身, fallback指令中的探测器..., NULL探测器(TCP)]
Fallbacks [MaxFallbacks + 1]*Probe
Matchs *[]Match // 匹配规则列表
}
// Match 匹配规则结构体
type Match struct {
IsSoft bool // 是否为软匹配
Service string // 服务名称
Pattern string // 匹配模式
VersionInfo string // 版本信息格式
FoundItems []string // 找到的项目
PatternCompiled *regexp.Regexp // 编译后的正则表达式
}
// Directive 指令结构体
type Directive struct {
DirectiveName string
Flag string
Delimiter string
DirectiveStr string
}
// Extras 额外信息结构体
type Extras struct {
VendorProduct string
Version string
Info string
Hostname string
OperatingSystem string
DeviceType string
CPE string
}
// Target 目标结构体
type Target struct {
Host string
Port int
Timeout int
}
+129
View File
@@ -0,0 +1,129 @@
package portfinger
import (
"regexp"
"strconv"
"strings"
)
// 预编译正则表达式
var (
whitespaceRegex = regexp.MustCompile(`\s+`)
// 版本信息字段解析正则 - 支持斜线和竖线两种分隔符
fieldRegexes = map[string][]*regexp.Regexp{
" p": {regexp.MustCompile(` p/([^/]*)/`), regexp.MustCompile(` p\|([^|]*)\|`)},
" v": {regexp.MustCompile(` v/([^/]*)/`), regexp.MustCompile(` v\|([^|]*)\|`)},
" i": {regexp.MustCompile(` i/([^/]*)/`), regexp.MustCompile(` i\|([^|]*)\|`)},
" h": {regexp.MustCompile(` h/([^/]*)/`), regexp.MustCompile(` h\|([^|]*)\|`)},
" o": {regexp.MustCompile(` o/([^/]*)/`), regexp.MustCompile(` o\|([^|]*)\|`)},
" d": {regexp.MustCompile(` d/([^/]*)/`), regexp.MustCompile(` d\|([^|]*)\|`)},
}
// CPE解析正则
cpeRegexSlash = regexp.MustCompile(`cpe:/([^/]*)`)
cpeRegexPipe = regexp.MustCompile(`cpe:\|([^|]*)`)
)
// ParseVersionInfo 解析版本信息并返回额外信息结构
func (m *Match) ParseVersionInfo(response []byte) Extras {
var extras = Extras{}
// 确保有匹配项
if len(m.FoundItems) == 0 {
return extras
}
// 替换版本信息中的占位符(单次扫描)
versionInfo := m.VersionInfo
if len(m.FoundItems) > 0 {
replacements := make([]string, 0, len(m.FoundItems)*2)
for i, value := range m.FoundItems {
replacements = append(replacements, "$"+strconv.Itoa(i+1), value)
}
versionInfo = strings.NewReplacer(replacements...).Replace(versionInfo)
}
// 定义解析函数 - 使用预编译正则
parseField := func(field string) string {
regexes, ok := fieldRegexes[field]
if !ok || !strings.Contains(versionInfo, field) {
return ""
}
for _, regex := range regexes {
if matches := regex.FindStringSubmatch(versionInfo); len(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:|") {
for _, regex := range []*regexp.Regexp{cpeRegexSlash, cpeRegexPipe} {
if matches := regex.FindStringSubmatch(versionInfo); len(matches) > 1 {
extras.CPE = matches[1]
break
}
}
}
return extras
}
// ToMap 将 Extras 转换为 map[string]string
func (e *Extras) ToMap() map[string]string {
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
}
}
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()
resultStr = whitespaceRegex.ReplaceAllString(resultStr, " ")
return strings.TrimSpace(resultStr)
}
+531
View File
@@ -0,0 +1,531 @@
package portfinger
import (
"strings"
"testing"
)
/*
version_parser_test.go - Banner清理与版本解析测试
测试目标:TrimBanner 函数
价值:Banner清理是服务识别的预处理步骤,错误会导致:
- 误识别服务类型
- 正则匹配失败
- 日志输出混乱(控制字符污染)
"Banner清理看起来简单,但涉及ASCII控制字符、Unicode、空格压缩。
这是真实的网络数据处理,必须测试边界情况。"
*/
// =============================================================================
// TrimBanner - Banner清理测试
// =============================================================================
// TestTrimBanner_BasicCases 测试基本的清理功能
func TestTrimBanner_BasicCases(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "普通字符串-无需清理",
input: "SSH-2.0-OpenSSH_8.0",
expected: "SSH-2.0-OpenSSH_8.0",
},
{
name: "前后有空格",
input: " SSH-2.0-OpenSSH_8.0 ",
expected: "SSH-2.0-OpenSSH_8.0",
},
{
name: "多个连续空格",
input: "SSH 2.0 OpenSSH",
expected: "SSH 2.0 OpenSSH",
},
{
name: "空字符串",
input: "",
expected: "",
},
{
name: "只有空格",
input: " ",
expected: "",
},
{
name: "只有制表符",
input: "\t\t\t",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TrimBanner(tt.input)
if result != tt.expected {
t.Errorf("TrimBanner(%q) = %q, want %q",
tt.input, result, tt.expected)
}
})
}
}
// TestTrimBanner_ControlCharacters 测试控制字符处理
func TestTrimBanner_ControlCharacters(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "NULL字符-移除",
input: "SSH\x00-2.0",
expected: "SSH -2.0",
},
{
name: "BEL响铃-移除",
input: "SSH\x07-2.0",
expected: "SSH -2.0",
},
{
name: "退格符-移除",
input: "SSH\x08-2.0",
expected: "SSH -2.0",
},
{
name: "ESC转义符-移除控制字符部分",
input: "SSH\x1b[31m-2.0",
expected: "SSH [31m-2.0", // ESC被移除,但[31m是可打印字符
},
{
name: "DEL删除符-移除",
input: "SSH\x7f-2.0",
expected: "SSH -2.0",
},
{
name: "多个控制字符",
input: "\x01\x02SSH\x03\x04-2.0\x05\x06",
expected: "SSH -2.0",
},
{
name: "只有控制字符",
input: "\x00\x01\x02\x03\x04\x05",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TrimBanner(tt.input)
if result != tt.expected {
t.Errorf("TrimBanner(%q) = %q, want %q",
tt.input, result, tt.expected)
}
})
}
}
// TestTrimBanner_PreservedCharacters 测试保留的特殊字符
func TestTrimBanner_PreservedCharacters(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "保留换行符",
input: "SSH-2.0\nOpenSSH_8.0",
expected: "SSH-2.0 OpenSSH_8.0", // 连续空白被压缩
},
{
name: "保留制表符",
input: "SSH-2.0\tOpenSSH_8.0",
expected: "SSH-2.0 OpenSSH_8.0", // 制表符被压缩为空格
},
{
name: "混合换行符和制表符",
input: "SSH\n\t2.0\n\tOpenSSH",
expected: "SSH 2.0 OpenSSH",
},
{
name: "多个连续换行符",
input: "SSH\n\n\n2.0",
expected: "SSH 2.0",
},
{
name: "Windows换行符CRLF",
input: "SSH-2.0\r\nOpenSSH_8.0",
expected: "SSH-2.0 OpenSSH_8.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TrimBanner(tt.input)
if result != tt.expected {
t.Errorf("TrimBanner(%q) = %q, want %q",
tt.input, result, tt.expected)
}
})
}
}
// TestTrimBanner_SpaceCompression 测试空格压缩
func TestTrimBanner_SpaceCompression(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "两个空格",
input: "SSH 2.0",
expected: "SSH 2.0",
},
{
name: "多个空格",
input: "SSH 2.0 OpenSSH",
expected: "SSH 2.0 OpenSSH",
},
{
name: "混合空白字符",
input: "SSH \t \n 2.0",
expected: "SSH 2.0",
},
{
name: "开头多个空格",
input: " SSH-2.0",
expected: "SSH-2.0",
},
{
name: "结尾多个空格",
input: "SSH-2.0 ",
expected: "SSH-2.0",
},
{
name: "前后和中间都有多余空格",
input: " SSH 2.0 OpenSSH ",
expected: "SSH 2.0 OpenSSH",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TrimBanner(tt.input)
if result != tt.expected {
t.Errorf("TrimBanner(%q) = %q, want %q",
tt.input, result, tt.expected)
}
})
}
}
// TestTrimBanner_ProductionScenarios 测试生产环境真实场景
func TestTrimBanner_ProductionScenarios(t *testing.T) {
t.Run("SSH服务Banner", func(t *testing.T) {
// 真实的SSH banner,可能包含控制字符
input := "\x00\x00SSH-2.0-OpenSSH_8.0 Ubuntu\x00\x00"
expected := "SSH-2.0-OpenSSH_8.0 Ubuntu"
result := TrimBanner(input)
if result != expected {
t.Errorf("SSH banner清理失败: got %q, want %q", result, expected)
}
})
t.Run("HTTP服务Banner", func(t *testing.T) {
// HTTP响应可能包含多余空白
input := " HTTP/1.1 200 OK\r\nServer: nginx/1.18.0 "
expected := "HTTP/1.1 200 OK Server: nginx/1.18.0"
result := TrimBanner(input)
if result != expected {
t.Errorf("HTTP banner清理失败: got %q, want %q", result, expected)
}
})
t.Run("FTP服务Banner", func(t *testing.T) {
// FTP欢迎消息,可能包含换行符
input := "220\tProFTPD Server\n(Welcome)\n"
expected := "220 ProFTPD Server (Welcome)"
result := TrimBanner(input)
if result != expected {
t.Errorf("FTP banner清理失败: got %q, want %q", result, expected)
}
})
t.Run("MySQL服务Banner", func(t *testing.T) {
// MySQL握手包可能包含二进制数据
input := "\x00\x00\x005.7.30-log\x00"
expected := "5.7.30-log"
result := TrimBanner(input)
if result != expected {
t.Errorf("MySQL banner清理失败: got %q, want %q", result, expected)
}
})
t.Run("Telnet服务Banner", func(t *testing.T) {
// Telnet可能包含ANSI转义序列
// 注意:当前实现只移除控制字符,ANSI序列的参数部分(可打印字符)会保留
input := "\x1b[2J\x1b[HWelcome to Linux\x1b[0m"
expected := "[2J [HWelcome to Linux [0m" // ESC被移除,参数保留
result := TrimBanner(input)
if result != expected {
t.Errorf("Telnet banner清理失败: got %q, want %q", result, expected)
}
})
}
// TestTrimBanner_EdgeCases 测试边界情况
func TestTrimBanner_EdgeCases(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "单个字符",
input: "S",
expected: "S",
},
{
name: "单个空格",
input: " ",
expected: "",
},
{
name: "单个控制字符",
input: "\x00",
expected: "",
},
{
name: "所有可打印ASCII字符",
input: " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
expected: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
},
{
name: "混合可打印和不可打印字符",
input: "A\x00B\x01C\x1fD E",
expected: "A B C D E",
},
{
name: "长Banner-1000字符",
input: strings.Repeat("SSH-2.0 ", 125), // 1000字符
expected: strings.TrimSpace(strings.Repeat("SSH-2.0 ", 125)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TrimBanner(tt.input)
if result != tt.expected {
t.Errorf("TrimBanner(%q) = %q, want %q",
tt.input, result, tt.expected)
}
})
}
}
// TestTrimBanner_ASCIIRanges 测试ASCII范围边界
func TestTrimBanner_ASCIIRanges(t *testing.T) {
t.Run("ASCII-31-控制字符边界", func(t *testing.T) {
// ASCII 0-31 是控制字符(除了\n和\t)
input := string([]byte{31, 32, 33}) // US控制符, 空格, !
expected := "!" // 31被移除,32变空格被trim,33保留
result := TrimBanner(input)
if result != expected {
t.Errorf("ASCII 31边界测试失败: got %q, want %q", result, expected)
}
})
t.Run("ASCII-32-空格-最小可打印字符", func(t *testing.T) {
input := string([]byte{32}) // 空格
expected := "" // trim掉
result := TrimBanner(input)
if result != expected {
t.Errorf("ASCII 32测试失败: got %q, want %q", result, expected)
}
})
t.Run("ASCII-126-波浪号-最大可打印字符", func(t *testing.T) {
input := string([]byte{126}) // ~
expected := "~"
result := TrimBanner(input)
if result != expected {
t.Errorf("ASCII 126测试失败: got %q, want %q", result, expected)
}
})
t.Run("ASCII-127-DEL-控制字符", func(t *testing.T) {
input := string([]byte{127}) // DEL
expected := "" // 被移除
result := TrimBanner(input)
if result != expected {
t.Errorf("ASCII 127测试失败: got %q, want %q", result, expected)
}
})
}
// TestTrimBanner_SpecialCases 测试特殊场景
func TestTrimBanner_SpecialCases(t *testing.T) {
t.Run("换行符保留-但被压缩", func(t *testing.T) {
input := "Line1\nLine2"
result := TrimBanner(input)
// 换行符应该被保留,但被压缩为空格
if !strings.Contains(result, "Line1") || !strings.Contains(result, "Line2") {
t.Errorf("换行符处理错误: got %q", result)
}
})
t.Run("制表符保留-但被压缩", func(t *testing.T) {
input := "Col1\tCol2"
result := TrimBanner(input)
// 制表符应该被保留,但被压缩为空格
if !strings.Contains(result, "Col1") || !strings.Contains(result, "Col2") {
t.Errorf("制表符处理错误: got %q", result)
}
})
t.Run("连续控制字符-被替换为单个空格", func(t *testing.T) {
input := "SSH\x00\x01\x02-2.0"
result := TrimBanner(input)
// 多个控制字符应该被压缩
expected := "SSH -2.0"
if result != expected {
t.Errorf("控制字符压缩错误: got %q, want %q", result, expected)
}
})
t.Run("空字符串不panic", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("空字符串导致panic: %v", r)
}
}()
result := TrimBanner("")
if result != "" {
t.Errorf("空字符串处理错误: got %q", result)
}
})
}
// TestTrimBanner_PerformanceBaseline 性能基准测试
func TestTrimBanner_PerformanceBaseline(t *testing.T) {
// 测试大字符串不会超时
largeInput := strings.Repeat("SSH-2.0-OpenSSH_8.0 ", 10000) // ~200KB
result := TrimBanner(largeInput)
if len(result) == 0 {
t.Error("大字符串处理失败")
}
}
// =============================================================================
// ToMap - 结构体转Map测试
// =============================================================================
// TestExtras_ToMap_BasicCases 测试基本的ToMap功能
func TestExtras_ToMap_BasicCases(t *testing.T) {
tests := []struct {
name string
extras Extras
expected map[string]string
}{
{
name: "所有字段都有值",
extras: Extras{
VendorProduct: "Apache httpd",
Version: "2.4.41",
Info: "Ubuntu",
Hostname: "web-server",
OperatingSystem: "Linux",
DeviceType: "general purpose",
CPE: "cpe:/a:apache:http_server:2.4.41",
},
expected: map[string]string{
"vendor_product": "Apache httpd",
"version": "2.4.41",
"info": "Ubuntu",
"hostname": "web-server",
"os": "Linux",
"device_type": "general purpose",
"cpe": "cpe:/a:apache:http_server:2.4.41",
},
},
{
name: "所有字段都为空",
extras: Extras{},
expected: map[string]string{},
},
{
name: "只有部分字段有值",
extras: Extras{
VendorProduct: "OpenSSH",
Version: "8.0",
},
expected: map[string]string{
"vendor_product": "OpenSSH",
"version": "8.0",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.extras.ToMap()
// 验证长度
if len(result) != len(tt.expected) {
t.Errorf("ToMap() 返回map长度 = %d, want %d",
len(result), len(tt.expected))
}
// 验证每个字段
for key, expectedValue := range tt.expected {
if actualValue, ok := result[key]; !ok {
t.Errorf("ToMap() 缺少字段 %q", key)
} else if actualValue != expectedValue {
t.Errorf("ToMap()[%q] = %q, want %q",
key, actualValue, expectedValue)
}
}
// 验证没有多余字段
for key := range result {
if _, ok := tt.expected[key]; !ok {
t.Errorf("ToMap() 包含意外字段 %q = %q",
key, result[key])
}
}
})
}
}
// TestExtras_ToMap_EmptyStringFiltering 测试空字符串过滤
func TestExtras_ToMap_EmptyStringFiltering(t *testing.T) {
t.Run("空字符串不应出现在map中", func(t *testing.T) {
extras := Extras{
VendorProduct: "Apache",
Version: "", // 空
Info: "Ubuntu",
Hostname: "", // 空
OperatingSystem: "",
DeviceType: "",
CPE: "",
}
result := extras.ToMap()
// 应该只有两个非空字段
if len(result) != 2 {
t.Errorf("ToMap() 应该过滤空字符串, got length %d, want 2", len(result))
}
// 验证空字段不存在
emptyFields := []string{"version", "hostname", "os", "device_type", "cpe"}
for _, field := range emptyFields {
if _, exists := result[field]; exists {
t.Errorf("ToMap() 不应包含空字段 %q", field)
}
}
})
}