Files
fscan/core/portfinger/encoding_utils.go
T
ZacharyZcR 71b92d4408 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)
2026-01-11 20:16:23 +08:00

105 lines
2.1 KiB
Go

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
}