Files
fscan/plugins/services/types.go
T
ZacharyZcR 28686f845d fix: 彻底解决UDP插件阻塞导致扫描无法结束的问题
根因分析(通过 goroutine dump 定位):
1. UDP conn.Write 在 WSL2 上可能永久阻塞(SetDeadline 对 Write 不生效)
2. SNMP community 爆破混入通用密码字典(57个),串行 × 10s超时 = 10分钟

修复:
- 提取 udpProbe() 公共函数,用 context timeout + conn.Close 双保险
  超时后强制关闭连接,中断阻塞的 Write/Read
- BACnet/DNS/IPMI/TFTP 统一使用 udpProbe()
- SNMP probe 使用 goroutine + context select 保护
- SNMP community 列表不再混入通用密码字典(8个专用 community 足够)
- SNMP 后续 community 爆破用 3s 短超时 + 连续失败 3 次快速退出

效果: 同样的扫描从无限卡死 → 9秒完成
2026-06-13 22:07:03 +08:00

82 lines
1.9 KiB
Go

package services
import (
"context"
"time"
"github.com/shadow1ng/fscan/common"
"github.com/shadow1ng/fscan/plugins"
)
// 插件接口定义 - 统一命名风格
type Plugin interface {
Name() string
Scan(ctx context.Context, info *common.HostInfo, session *common.ScanSession) *ScanResult
}
type ScanResult = plugins.Result
type ExploitResult = plugins.ExploitResult
type Exploiter = plugins.Exploiter
type Credential = plugins.Credential
// RegisterPluginWithPorts 高效注册:直接传递端口信息,避免实例创建
func RegisterPluginWithPorts(name string, factory func() Plugin, ports []int) {
plugins.RegisterWithPorts(name, func() plugins.Plugin {
return factory()
}, ports)
}
// RegisterUDPPluginWithPorts 注册UDP协议插件
func RegisterUDPPluginWithPorts(name string, factory func() Plugin, ports []int) {
plugins.RegisterUDPWithPorts(name, func() plugins.Plugin {
return factory()
}, ports)
}
var GenerateCredentials = plugins.GenerateCredentials
// udpProbe 执行带超时保护的 UDP 探测(防止 Write/Read 在某些内核下永久阻塞)
// 返回读到的数据和长度,超时/错误返回 nil
func udpProbe(ctx context.Context, session *common.ScanSession, target string, timeout time.Duration, pkt []byte, bufSize int) ([]byte, int) {
probeCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
conn, err := session.DialUDP(probeCtx, target, timeout)
if err != nil {
return nil, 0
}
defer conn.Close()
_ = conn.SetDeadline(time.Now().Add(timeout))
type result struct {
data []byte
n int
}
ch := make(chan *result, 1)
go func() {
if _, err := conn.Write(pkt); err != nil {
ch <- nil
return
}
buf := make([]byte, bufSize)
n, err := conn.Read(buf)
if err != nil {
ch <- nil
return
}
ch <- &result{data: buf[:n], n: n}
}()
select {
case <-probeCtx.Done():
_ = conn.Close()
return nil, 0
case r := <-ch:
if r == nil {
return nil, 0
}
return r.data, r.n
}
}