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)
217 lines
5.0 KiB
Go
217 lines
5.0 KiB
Go
//go:build plugin_mssql || !plugin_selective
|
|
|
|
package services
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
_ "github.com/denisenkom/go-mssqldb" // MSSQL driver
|
|
"github.com/shadow1ng/fscan/common"
|
|
"github.com/shadow1ng/fscan/common/i18n"
|
|
"github.com/shadow1ng/fscan/plugins"
|
|
)
|
|
|
|
// MSSQLPlugin MSSQL扫描插件
|
|
type MSSQLPlugin struct {
|
|
plugins.BasePlugin
|
|
}
|
|
|
|
func NewMSSQLPlugin() *MSSQLPlugin {
|
|
return &MSSQLPlugin{
|
|
BasePlugin: plugins.NewBasePlugin("mssql"),
|
|
}
|
|
}
|
|
|
|
func (p *MSSQLPlugin) Scan(ctx context.Context, info *common.HostInfo, config *common.Config, state *common.State) *ScanResult {
|
|
if config.DisableBrute {
|
|
return p.identifyService(ctx, info, config, state)
|
|
}
|
|
|
|
target := info.Target()
|
|
|
|
credentials := GenerateCredentials("mssql", config)
|
|
if len(credentials) == 0 {
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "mssql",
|
|
Error: fmt.Errorf("没有可用的测试凭据"),
|
|
}
|
|
}
|
|
|
|
// 使用公共框架进行并发凭据测试
|
|
authFn := p.createAuthFunc(info, config, state)
|
|
testConfig := DefaultConcurrentTestConfig(config)
|
|
|
|
result := TestCredentialsConcurrently(ctx, credentials, authFn, "mssql", testConfig)
|
|
|
|
if result.Success {
|
|
common.LogSuccess(i18n.Tr("mssql_credential", target, result.Username, result.Password))
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// createAuthFunc 创建MSSQL认证函数
|
|
func (p *MSSQLPlugin) createAuthFunc(info *common.HostInfo, config *common.Config, state *common.State) AuthFunc {
|
|
return func(ctx context.Context, cred Credential) *AuthResult {
|
|
return p.doMSSQLAuth(ctx, info, cred, config, state)
|
|
}
|
|
}
|
|
|
|
// doMSSQLAuth 执行MSSQL认证
|
|
func (p *MSSQLPlugin) doMSSQLAuth(ctx context.Context, info *common.HostInfo, cred Credential, config *common.Config, state *common.State) *AuthResult {
|
|
connStr := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=master;connection timeout=%d",
|
|
info.Host, cred.Username, cred.Password, info.Port, int64(config.Timeout.Seconds()))
|
|
|
|
db, err := sql.Open("mssql", connStr)
|
|
if err != nil {
|
|
state.IncrementTCPFailedPacketCount()
|
|
return &AuthResult{
|
|
Success: false,
|
|
ErrorType: classifyMSSQLErrorType(err),
|
|
Error: err,
|
|
}
|
|
}
|
|
|
|
db.SetConnMaxLifetime(config.Timeout)
|
|
db.SetMaxOpenConns(1)
|
|
db.SetMaxIdleConns(0)
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, config.Timeout)
|
|
defer cancel()
|
|
|
|
err = db.PingContext(pingCtx)
|
|
if err != nil {
|
|
_ = db.Close()
|
|
state.IncrementTCPFailedPacketCount()
|
|
return &AuthResult{
|
|
Success: false,
|
|
ErrorType: classifyMSSQLErrorType(err),
|
|
Error: err,
|
|
}
|
|
}
|
|
|
|
state.IncrementTCPSuccessPacketCount()
|
|
|
|
return &AuthResult{
|
|
Success: true,
|
|
Conn: &mssqlDBWrapper{db},
|
|
ErrorType: ErrorTypeUnknown,
|
|
Error: nil,
|
|
}
|
|
}
|
|
|
|
// mssqlDBWrapper 包装 sql.DB 以实现 io.Closer
|
|
type mssqlDBWrapper struct {
|
|
*sql.DB
|
|
}
|
|
|
|
func (w *mssqlDBWrapper) Close() error {
|
|
return w.DB.Close()
|
|
}
|
|
|
|
// classifyMSSQLErrorType MSSQL错误分类
|
|
func classifyMSSQLErrorType(err error) ErrorType {
|
|
if err == nil {
|
|
return ErrorTypeUnknown
|
|
}
|
|
|
|
mssqlAuthErrors := []string{
|
|
"login failed",
|
|
"password incorrect",
|
|
"authentication failed",
|
|
"invalid credentials",
|
|
"access denied",
|
|
"invalid login",
|
|
"invalid user",
|
|
"invalid password",
|
|
"bad login",
|
|
"authentication failure",
|
|
"login error",
|
|
"credential",
|
|
"user login failed",
|
|
"logon failure",
|
|
"account locked",
|
|
"user not found",
|
|
"invalid account",
|
|
}
|
|
|
|
mssqlNetworkErrors := append(CommonNetworkErrors,
|
|
"dial tcp",
|
|
"connection closed",
|
|
"eof",
|
|
"network error",
|
|
"context deadline exceeded",
|
|
"server closed the connection",
|
|
"connection lost",
|
|
)
|
|
|
|
return ClassifyError(err, mssqlAuthErrors, mssqlNetworkErrors)
|
|
}
|
|
|
|
func (p *MSSQLPlugin) identifyService(ctx context.Context, info *common.HostInfo, config *common.Config, state *common.State) *ScanResult {
|
|
target := info.Target()
|
|
|
|
connStr := fmt.Sprintf("server=%s;user id=invalid;password=invalid;port=%d;database=master;connection timeout=%d",
|
|
info.Host, info.Port, int64(config.Timeout.Seconds()))
|
|
|
|
db, err := sql.Open("mssql", connStr)
|
|
if err != nil {
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "mssql",
|
|
Error: err,
|
|
}
|
|
}
|
|
defer func() { _ = db.Close() }()
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, config.Timeout)
|
|
defer cancel()
|
|
|
|
err = db.PingContext(pingCtx)
|
|
|
|
if err != nil {
|
|
state.IncrementTCPFailedPacketCount()
|
|
} else {
|
|
state.IncrementTCPSuccessPacketCount()
|
|
}
|
|
|
|
var banner string
|
|
errLower := ""
|
|
if err != nil {
|
|
errLower = strings.ToLower(err.Error())
|
|
}
|
|
|
|
if err != nil && (strings.Contains(errLower, "login failed") ||
|
|
strings.Contains(errLower, "mssql") ||
|
|
strings.Contains(errLower, "sql server")) {
|
|
banner = "MSSQL"
|
|
} else if err == nil {
|
|
banner = "MSSQL"
|
|
} else {
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "mssql",
|
|
Error: fmt.Errorf("无法识别为MSSQL服务"),
|
|
}
|
|
}
|
|
|
|
common.LogSuccess(i18n.Tr("mssql_service", target, banner))
|
|
|
|
return &ScanResult{
|
|
Type: plugins.ResultTypeService,
|
|
Success: true,
|
|
Service: "mssql",
|
|
Banner: banner,
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
RegisterPluginWithPorts("mssql", func() Plugin {
|
|
return NewMSSQLPlugin()
|
|
}, []int{1433, 1434})
|
|
}
|