fscan/plugins/services/mssql/plugin.go
ZacharyZcR 4a3f281b6b refactor: 统一Plugins目录大小写为小写
- 将所有Plugins路径重命名为plugins
- 修复Git索引与实际文件系统大小写不一致问题
- 确保跨平台兼容性和路径一致性
2025-08-12 13:08:06 +08:00

199 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 mssql
import (
"context"
"fmt"
"strings"
"github.com/shadow1ng/fscan/common"
"github.com/shadow1ng/fscan/common/i18n"
"github.com/shadow1ng/fscan/plugins/base"
)
// MSSQLPlugin MSSQL插件实现
type MSSQLPlugin struct {
*base.ServicePlugin
exploiter *MSSQLExploiter
}
// NewMSSQLPlugin 创建MSSQL插件
func NewMSSQLPlugin() *MSSQLPlugin {
// 插件元数据
metadata := &base.PluginMetadata{
Name: "mssql",
Version: "2.0.0",
Author: "fscan-team",
Description: "Microsoft SQL Server扫描和利用插件",
Category: "service",
Ports: []int{1433, 1434}, // 默认MSSQL端口
Protocols: []string{"tcp"},
Tags: []string{"mssql", "sqlserver", "database", "weak-password"},
}
// 创建连接器和服务插件
connector := NewMSSQLConnector()
servicePlugin := base.NewServicePlugin(metadata, connector)
// 创建MSSQL插件
plugin := &MSSQLPlugin{
ServicePlugin: servicePlugin,
exploiter: NewMSSQLExploiter(),
}
// 设置能力
plugin.SetCapabilities([]base.Capability{
base.CapWeakPassword,
base.CapDataExtraction,
})
return plugin
}
// Scan 重写扫描方法进行MSSQL服务扫描
func (p *MSSQLPlugin) Scan(ctx context.Context, info *common.HostInfo) (*base.ScanResult, error) {
// 如果禁用了暴力破解,只进行服务识别
if common.DisableBrute {
return p.performServiceIdentification(ctx, info)
}
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
// 生成凭据进行暴力破解
credentials := p.generateCredentials()
// 遍历凭据进行测试
for _, cred := range credentials {
result, err := p.ScanCredential(ctx, info, cred)
if err == nil && result.Success {
// 认证成功
common.LogSuccess(i18n.GetText("mssql_auth_success", target, cred.Username, cred.Password))
return &base.ScanResult{
Success: true,
Service: "Microsoft SQL Server",
Credentials: []*base.Credential{cred},
Banner: result.Banner,
Extra: map[string]interface{}{
"service": "Microsoft SQL Server",
"port": info.Ports,
"username": cred.Username,
"password": cred.Password,
},
}, nil
}
}
// 所有凭据都失败但可能识别到了MSSQL服务
return p.performServiceIdentification(ctx, info)
}
// generateCredentials 生成MSSQL凭据
func (p *MSSQLPlugin) generateCredentials() []*base.Credential {
var credentials []*base.Credential
// 获取MSSQL用户名字典
usernames := common.Userdict["mssql"]
if len(usernames) == 0 {
usernames = []string{"sa", "admin", "administrator", "root", "mssql"}
}
// 获取密码字典
passwords := common.Passwords
if len(passwords) == 0 {
passwords = []string{"", "sa", "admin", "password", "123456", "root"}
}
// 生成用户名密码组合
for _, username := range usernames {
for _, password := range passwords {
// 替换密码中的用户名占位符
actualPassword := strings.Replace(password, "{user}", username, -1)
credentials = append(credentials, &base.Credential{
Username: username,
Password: actualPassword,
})
}
}
return credentials
}
// Exploit 使用exploiter执行利用
func (p *MSSQLPlugin) Exploit(ctx context.Context, info *common.HostInfo, creds *base.Credential) (*base.ExploitResult, error) {
return p.exploiter.Exploit(ctx, info, creds)
}
// GetExploitMethods 获取利用方法
func (p *MSSQLPlugin) GetExploitMethods() []base.ExploitMethod {
return p.exploiter.GetExploitMethods()
}
// IsExploitSupported 检查利用支持
func (p *MSSQLPlugin) IsExploitSupported(method base.ExploitType) bool {
return p.exploiter.IsExploitSupported(method)
}
// performServiceIdentification 执行MSSQL服务识别-nobr模式
func (p *MSSQLPlugin) performServiceIdentification(ctx context.Context, info *common.HostInfo) (*base.ScanResult, error) {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
// 尝试识别MSSQL服务
connector := NewMSSQLConnector()
conn, err := connector.Connect(ctx, info)
if err == nil && conn != nil {
if mssqlConn, ok := conn.(*MSSQLConnection); ok {
// 记录服务识别成功
common.LogSuccess(i18n.GetText("mssql_service_identified", target, mssqlConn.info))
connector.Close(conn)
return &base.ScanResult{
Success: true,
Service: "Microsoft SQL Server",
Banner: mssqlConn.info,
Extra: map[string]interface{}{
"service": "Microsoft SQL Server",
"port": info.Ports,
"info": mssqlConn.info,
},
}, nil
}
}
// 如果无法识别为MSSQL返回失败
return &base.ScanResult{
Success: false,
Error: fmt.Errorf("无法识别为MSSQL服务"),
}, nil
}
// =============================================================================
// 插件注册
// =============================================================================
// RegisterMSSQLPlugin 注册MSSQL插件
func RegisterMSSQLPlugin() {
factory := base.NewSimplePluginFactory(
&base.PluginMetadata{
Name: "mssql",
Version: "2.0.0",
Author: "fscan-team",
Description: "Microsoft SQL Server扫描和利用插件",
Category: "service",
Ports: []int{1433, 1434}, // 默认MSSQL端口
Protocols: []string{"tcp"},
Tags: []string{"mssql", "sqlserver", "database", "weak-password"},
},
func() base.Plugin {
return NewMSSQLPlugin()
},
)
base.GlobalPluginRegistry.Register("mssql", factory)
}
// 自动注册
func init() {
RegisterMSSQLPlugin()
}