fscan/Plugins/services/mysql/plugin.go
ZacharyZcR de286026e8 refactor: 统一插件超时机制实现Context-First架构
- 重构SSH/MySQL/Redis插件超时控制,移除第三方库超时依赖
- 统一使用Go Context超时机制,提升超时控制可靠性和精确度
- 扩展MySQL/Redis/SSH插件默认端口支持,提升扫描覆盖率
- 修复插件系统中ConcurrentScanConfig超时配置缺失问题
- 优化插件检测逻辑,正确识别新架构插件并显示准确状态信息
- 解决插件在错误端口上长时间等待问题,显著提升扫描效率
2025-08-07 14:01:50 +08:00

158 lines
5.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mysql
import (
"context"
"fmt"
"github.com/shadow1ng/fscan/common"
"github.com/shadow1ng/fscan/common/i18n"
"github.com/shadow1ng/fscan/plugins/base"
)
// MySQL插件新一代插件架构的完整实现示例
// 展示了如何正确实现服务扫描、凭据爆破、自动利用等功能
// 本插件可作为其他数据库插件迁移的标准参考模板
// MySQLPlugin MySQL数据库扫描和利用插件
// 集成了弱密码检测、自动利用、信息收集等完整功能
type MySQLPlugin struct {
*base.ServicePlugin // 继承基础服务插件功能
exploiter *MySQLExploiter // MySQL专用利用模块
}
// NewMySQLPlugin 创建新的MySQL插件实例
// 这是标准的插件工厂函数,展示了新架构的完整初始化流程
func NewMySQLPlugin() *MySQLPlugin {
// 定义插件元数据 - 这些信息用于插件注册和管理
metadata := &base.PluginMetadata{
Name: "mysql", // 插件唯一标识符
Version: "2.0.0", // 插件版本(新架构版本)
Author: "fscan-team", // 开发团队
Description: "MySQL数据库扫描和利用插件", // 功能描述
Category: "service", // 插件类别
Ports: []int{3306, 3307, 33060, 33061, 33062}, // MySQL常用端口包括默认端口和备用端口
Protocols: []string{"tcp"}, // 支持的协议
Tags: []string{"database", "mysql", "bruteforce", "exploit"}, // 功能标签
}
// 创建MySQL专用连接器
connector := NewMySQLConnector()
// 基于连接器创建基础服务插件
servicePlugin := base.NewServicePlugin(metadata, connector)
// 组装完整的MySQL插件
plugin := &MySQLPlugin{
ServicePlugin: servicePlugin,
exploiter: NewMySQLExploiter(), // 集成利用模块
}
// 声明插件具备的安全测试能力
plugin.SetCapabilities([]base.Capability{
base.CapWeakPassword, // 弱密码检测
base.CapDataExtraction, // 数据提取
base.CapFileWrite, // 文件写入
base.CapSQLInjection, // SQL注入
base.CapInformationLeak, // 信息泄露
})
return plugin
}
// Scan 执行MySQL服务的完整安全扫描
// 重写基础扫描方法,集成弱密码检测和自动利用功能
func (p *MySQLPlugin) Scan(ctx context.Context, info *common.HostInfo) (*base.ScanResult, error) {
// 调用基础服务插件进行弱密码扫描
result, err := p.ServicePlugin.Scan(ctx, info)
if err != nil || !result.Success {
return result, err // 扫描失败,直接返回
}
// 记录成功的弱密码发现使用i18n
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
cred := result.Credentials[0]
common.LogSuccess(i18n.GetText("mysql_scan_success", target, cred.Username, cred.Password))
// 自动利用功能(可通过-nobr参数禁用
if result.Success && len(result.Credentials) > 0 && !common.DisableBrute {
// 异步执行利用攻击,避免阻塞扫描进程
go p.autoExploit(context.Background(), info, result.Credentials[0])
}
return result, nil
}
// autoExploit 自动利用
func (p *MySQLPlugin) autoExploit(ctx context.Context, info *common.HostInfo, creds *base.Credential) {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
common.LogDebug(i18n.GetText("plugin_exploit_start", "MySQL", target))
// 执行利用
result, err := p.exploiter.Exploit(ctx, info, creds)
if err != nil {
common.LogError(i18n.GetText("plugin_exploit_failed", "MySQL", err))
return
}
if result != nil && result.Success {
common.LogSuccess(i18n.GetText("plugin_exploit_success", "MySQL", i18n.GetExploitMethodName(result.Method)))
base.SaveExploitResult(info, result, "MySQL")
}
}
// Exploit 手动利用接口
func (p *MySQLPlugin) Exploit(ctx context.Context, info *common.HostInfo, creds *base.Credential) (*base.ExploitResult, error) {
return p.exploiter.Exploit(ctx, info, creds)
}
// GetExploitMethods 获取利用方法
func (p *MySQLPlugin) GetExploitMethods() []base.ExploitMethod {
return p.exploiter.GetExploitMethods()
}
// IsExploitSupported 检查利用支持
func (p *MySQLPlugin) IsExploitSupported(method base.ExploitType) bool {
return p.exploiter.IsExploitSupported(method)
}
// generateCredentials 重写凭据生成方法
func (p *MySQLPlugin) generateCredentials() []*base.Credential {
// 获取MySQL专用的用户名字典
usernames := common.Userdict["mysql"]
if len(usernames) == 0 {
// 默认MySQL用户名
usernames = []string{"root", "admin", "mysql"}
}
return base.GenerateCredentials(usernames, common.Passwords)
}
// =============================================================================
// 插件注册
// =============================================================================
// RegisterMySQLPlugin 注册MySQL插件
func RegisterMySQLPlugin() {
factory := base.NewSimplePluginFactory(
&base.PluginMetadata{
Name: "mysql",
Version: "2.0.0",
Author: "fscan-team",
Description: "MySQL数据库扫描和利用插件",
Category: "service",
Ports: []int{3306, 3307, 33060, 33061, 33062},
Protocols: []string{"tcp"},
Tags: []string{"database", "mysql", "bruteforce", "exploit"},
},
func() base.Plugin {
return NewMySQLPlugin()
},
)
base.GlobalPluginRegistry.Register("mysql", factory)
}
// 自动注册
func init() {
RegisterMySQLPlugin()
}