mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-22 19:21:52 +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)
212 lines
4.9 KiB
Go
212 lines
4.9 KiB
Go
//go:build plugin_mysql || !plugin_selective
|
||
|
||
package services
|
||
|
||
import (
|
||
"context"
|
||
"database/sql"
|
||
"fmt"
|
||
"log"
|
||
"net"
|
||
"time"
|
||
|
||
"github.com/go-sql-driver/mysql"
|
||
"github.com/shadow1ng/fscan/common"
|
||
"github.com/shadow1ng/fscan/common/i18n"
|
||
"github.com/shadow1ng/fscan/plugins"
|
||
)
|
||
|
||
type nullWriter struct{}
|
||
|
||
func (nullWriter) Write(p []byte) (int, error) { return len(p), nil }
|
||
|
||
func init() {
|
||
// 禁用mysql驱动的错误日志(如unexpected EOF)
|
||
_ = mysql.SetLogger(log.New(&nullWriter{}, "", 0))
|
||
}
|
||
|
||
// MySQLPlugin MySQL数据库扫描插件
|
||
type MySQLPlugin struct {
|
||
plugins.BasePlugin
|
||
}
|
||
|
||
func NewMySQLPlugin() *MySQLPlugin {
|
||
return &MySQLPlugin{
|
||
BasePlugin: plugins.NewBasePlugin("mysql"),
|
||
}
|
||
}
|
||
|
||
func (p *MySQLPlugin) Scan(ctx context.Context, info *common.HostInfo, config *common.Config, state *common.State) *ScanResult {
|
||
if config.DisableBrute {
|
||
return p.identifyService(info, config)
|
||
}
|
||
|
||
credentials := GenerateCredentials("mysql", config)
|
||
if len(credentials) == 0 {
|
||
return &ScanResult{
|
||
Success: false,
|
||
Service: "mysql",
|
||
Error: fmt.Errorf("没有可用的测试凭据"),
|
||
}
|
||
}
|
||
|
||
target := info.Target()
|
||
|
||
// 使用公共框架进行并发凭据测试
|
||
authFn := p.createAuthFunc(info, config, state)
|
||
testConfig := DefaultConcurrentTestConfig(config)
|
||
|
||
result := TestCredentialsConcurrently(ctx, credentials, authFn, "mysql", testConfig)
|
||
|
||
if result.Success {
|
||
common.LogSuccess(i18n.Tr("mysql_credential", target, result.Username, result.Password))
|
||
}
|
||
|
||
return result
|
||
}
|
||
|
||
// createAuthFunc 创建MySQL认证函数
|
||
func (p *MySQLPlugin) createAuthFunc(info *common.HostInfo, config *common.Config, state *common.State) AuthFunc {
|
||
return func(ctx context.Context, cred Credential) *AuthResult {
|
||
return p.doMySQLAuth(ctx, info, cred, config, state)
|
||
}
|
||
}
|
||
|
||
// doMySQLAuth 执行MySQL认证
|
||
func (p *MySQLPlugin) doMySQLAuth(ctx context.Context, info *common.HostInfo, cred Credential, config *common.Config, state *common.State) *AuthResult {
|
||
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/information_schema?charset=utf8&timeout=%ds",
|
||
cred.Username, cred.Password, info.Host, info.Port, int64(config.Timeout.Seconds()))
|
||
|
||
db, err := sql.Open("mysql", connStr)
|
||
if err != nil {
|
||
state.IncrementTCPFailedPacketCount()
|
||
return &AuthResult{
|
||
Success: false,
|
||
ErrorType: classifyMySQLErrorType(err),
|
||
Error: err,
|
||
}
|
||
}
|
||
|
||
db.SetConnMaxLifetime(config.Timeout)
|
||
db.SetMaxOpenConns(1)
|
||
db.SetMaxIdleConns(0)
|
||
|
||
err = db.PingContext(ctx)
|
||
if err != nil {
|
||
_ = db.Close()
|
||
state.IncrementTCPFailedPacketCount()
|
||
return &AuthResult{
|
||
Success: false,
|
||
ErrorType: classifyMySQLErrorType(err),
|
||
Error: err,
|
||
}
|
||
}
|
||
|
||
state.IncrementTCPSuccessPacketCount()
|
||
|
||
// MySQL 使用 sql.DB,包装为 io.Closer
|
||
return &AuthResult{
|
||
Success: true,
|
||
Conn: &sqlDBWrapper{db},
|
||
ErrorType: ErrorTypeUnknown,
|
||
Error: nil,
|
||
}
|
||
}
|
||
|
||
// sqlDBWrapper 包装 sql.DB 以实现 io.Closer
|
||
type sqlDBWrapper struct {
|
||
*sql.DB
|
||
}
|
||
|
||
func (w *sqlDBWrapper) Close() error {
|
||
return w.DB.Close()
|
||
}
|
||
|
||
// classifyMySQLErrorType MySQL错误分类
|
||
func classifyMySQLErrorType(err error) ErrorType {
|
||
if err == nil {
|
||
return ErrorTypeUnknown
|
||
}
|
||
|
||
mysqlAuthErrors := []string{
|
||
"access denied for user",
|
||
"unknown database",
|
||
"host is not allowed",
|
||
"authentication failed",
|
||
"permission denied",
|
||
"user does not exist",
|
||
}
|
||
|
||
mysqlNetworkErrors := append(CommonNetworkErrors,
|
||
"too many connections",
|
||
"can't connect to mysql server",
|
||
"lost connection to mysql server",
|
||
"mysql server has gone away",
|
||
)
|
||
|
||
return ClassifyError(err, mysqlAuthErrors, mysqlNetworkErrors)
|
||
}
|
||
|
||
func (p *MySQLPlugin) identifyService(info *common.HostInfo, config *common.Config) *ScanResult {
|
||
target := info.Target()
|
||
|
||
conn, err := common.SafeTCPDial(target, config.Timeout)
|
||
if err != nil {
|
||
return &ScanResult{
|
||
Success: false,
|
||
Service: "mysql",
|
||
Error: err,
|
||
}
|
||
}
|
||
defer func() { _ = conn.Close() }()
|
||
|
||
if banner := p.readMySQLBanner(conn, config); banner != "" {
|
||
common.LogSuccess(i18n.Tr("mysql_service", target, banner))
|
||
return &ScanResult{
|
||
Type: plugins.ResultTypeService,
|
||
Success: true,
|
||
Service: "mysql",
|
||
Banner: banner,
|
||
}
|
||
}
|
||
|
||
return &ScanResult{
|
||
Success: false,
|
||
Service: "mysql",
|
||
Error: fmt.Errorf("无法识别为MySQL服务"),
|
||
}
|
||
}
|
||
|
||
func (p *MySQLPlugin) readMySQLBanner(conn net.Conn, config *common.Config) string {
|
||
_ = conn.SetReadDeadline(time.Now().Add(config.Timeout))
|
||
|
||
handshake := make([]byte, 256)
|
||
n, err := conn.Read(handshake)
|
||
if err != nil || n < 10 {
|
||
return ""
|
||
}
|
||
|
||
if handshake[4] != 10 {
|
||
return ""
|
||
}
|
||
|
||
versionStart := 5
|
||
versionEnd := versionStart
|
||
for versionEnd < n && handshake[versionEnd] != 0 {
|
||
versionEnd++
|
||
}
|
||
|
||
if versionEnd <= versionStart {
|
||
return ""
|
||
}
|
||
|
||
versionStr := string(handshake[versionStart:versionEnd])
|
||
return fmt.Sprintf("MySQL %s", versionStr)
|
||
}
|
||
|
||
func init() {
|
||
RegisterPluginWithPorts("mysql", func() Plugin {
|
||
return NewMySQLPlugin()
|
||
}, []int{3306, 3307, 33060})
|
||
}
|