mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-23 11:41:53 +08:00
## 架构重构
- 全局变量消除,迁移至 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)
130 lines
3.6 KiB
Go
130 lines
3.6 KiB
Go
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)
|
|
}
|