mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00

经Linus式架构审计,发现并修复插件系统中的具体问题: ## 核心修复 ### 1. 消除local插件GetPorts()方法冗余 - 删除21个local插件中无意义的GetPorts()方法 - 简化local.Plugin接口:移除端口概念 - 理由:本地插件不涉及网络,端口概念完全多余 ### 2. 消除web插件GetPorts()方法冗余 - 删除2个web插件中无用的GetPorts()方法 - 简化web.WebPlugin接口:专注智能HTTP检测 - 理由:Web插件使用动态HTTP检测,预定义端口无价值 ### 3. 统一插件命名规范 - 统一所有插件接口使用Name()方法(符合Go惯例) - 消除GetName()与Name()不一致问题 - 简化适配器:不再需要方法名转换 ## 技术改进 接口精简: - local插件:GetName() + GetPorts() → Name() - web插件:GetName() + GetPorts() → Name() - services插件:GetName() → Name()(保留GetPorts(),业务必需) 代码减少: - 删除23个无用GetPorts()方法 - 重命名52个Name()方法 - 简化3个插件接口定义 ## 影响范围 修改文件:55个插件文件 代码变更:-155行 +61行(净减少94行) 功能影响:零破坏性,保持所有业务逻辑不变 这是基于业务需求分析的精准重构,消除真正多余的部分, 保持系统架构合理性和向后兼容性。
235 lines
5.3 KiB
Go
235 lines
5.3 KiB
Go
package services
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"time"
|
||
|
||
"github.com/shadow1ng/fscan/common"
|
||
"github.com/stacktitan/smb/smb"
|
||
)
|
||
|
||
// SmbPlugin SMB弱密码检测插件
|
||
type SmbPlugin struct {
|
||
name string
|
||
ports []int
|
||
}
|
||
|
||
// NewSmbPlugin 创建SMB插件
|
||
func NewSmbPlugin() *SmbPlugin {
|
||
return &SmbPlugin{
|
||
name: "smb",
|
||
ports: []int{445},
|
||
}
|
||
}
|
||
|
||
// GetName 实现Plugin接口
|
||
func (p *SmbPlugin) Name() string {
|
||
return p.name
|
||
}
|
||
|
||
// GetPorts 实现Plugin接口
|
||
func (p *SmbPlugin) GetPorts() []int {
|
||
return p.ports
|
||
}
|
||
|
||
// Scan 执行SMB扫描
|
||
func (p *SmbPlugin) Scan(ctx context.Context, info *common.HostInfo) *ScanResult {
|
||
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
|
||
|
||
// 检查端口
|
||
if info.Ports != "445" {
|
||
return &ScanResult{
|
||
Success: false,
|
||
Service: "smb",
|
||
Error: fmt.Errorf("SMB插件仅支持445端口"),
|
||
}
|
||
}
|
||
|
||
// 如果禁用暴力破解,只做服务识别
|
||
if common.DisableBrute {
|
||
return p.identifyService(ctx, info)
|
||
}
|
||
|
||
// 生成测试凭据
|
||
credentials := GenerateCredentials("smb")
|
||
if len(credentials) == 0 {
|
||
// SMB默认凭据
|
||
credentials = []Credential{
|
||
{Username: "", Password: ""},
|
||
{Username: "administrator", Password: ""},
|
||
{Username: "administrator", Password: "admin"},
|
||
{Username: "administrator", Password: "password"},
|
||
{Username: "administrator", Password: "123456"},
|
||
{Username: "admin", Password: "admin"},
|
||
{Username: "guest", Password: ""},
|
||
{Username: "root", Password: ""},
|
||
{Username: "root", Password: "root"},
|
||
}
|
||
}
|
||
|
||
// 逐个测试凭据
|
||
for _, cred := range credentials {
|
||
// 检查Context是否被取消
|
||
select {
|
||
case <-ctx.Done():
|
||
return &ScanResult{
|
||
Success: false,
|
||
Service: "smb",
|
||
Error: ctx.Err(),
|
||
}
|
||
default:
|
||
}
|
||
|
||
// 测试凭据
|
||
if p.testCredential(ctx, info, cred) {
|
||
// SMB认证成功
|
||
var successMsg string
|
||
if common.Domain != "" {
|
||
successMsg = fmt.Sprintf("SMB %s 弱密码 %s\\%s:%s", target, common.Domain, cred.Username, cred.Password)
|
||
} else {
|
||
successMsg = fmt.Sprintf("SMB %s 弱密码 %s:%s", target, cred.Username, cred.Password)
|
||
}
|
||
common.LogSuccess(successMsg)
|
||
|
||
return &ScanResult{
|
||
Success: true,
|
||
Service: "smb",
|
||
Username: cred.Username,
|
||
Password: cred.Password,
|
||
}
|
||
}
|
||
}
|
||
|
||
// 所有凭据都失败
|
||
return &ScanResult{
|
||
Success: false,
|
||
Service: "smb",
|
||
Error: fmt.Errorf("未发现弱密码"),
|
||
}
|
||
}
|
||
|
||
|
||
// testCredential 测试单个凭据
|
||
func (p *SmbPlugin) testCredential(ctx context.Context, info *common.HostInfo, cred Credential) bool {
|
||
options := smb.Options{
|
||
Host: info.Host,
|
||
Port: 445,
|
||
User: cred.Username,
|
||
Password: cred.Password,
|
||
Domain: common.Domain,
|
||
Workstation: "",
|
||
}
|
||
|
||
// 设置超时
|
||
timeoutCtx, cancel := context.WithTimeout(ctx, time.Duration(common.Timeout)*time.Second)
|
||
defer cancel()
|
||
|
||
// 在协程中执行连接测试
|
||
resultChan := make(chan bool, 1)
|
||
go func() {
|
||
session, err := smb.NewSession(options, false)
|
||
if err == nil {
|
||
defer session.Close()
|
||
resultChan <- session.IsAuthenticated
|
||
} else {
|
||
resultChan <- false
|
||
}
|
||
}()
|
||
|
||
// 等待结果或超时
|
||
select {
|
||
case result := <-resultChan:
|
||
return result
|
||
case <-timeoutCtx.Done():
|
||
return false
|
||
case <-ctx.Done():
|
||
return false
|
||
}
|
||
}
|
||
|
||
// getShares 获取共享列表
|
||
func (p *SmbPlugin) getShares(ctx context.Context, info *common.HostInfo, creds Credential) []string {
|
||
options := smb.Options{
|
||
Host: info.Host,
|
||
Port: 445,
|
||
User: creds.Username,
|
||
Password: creds.Password,
|
||
Domain: common.Domain,
|
||
Workstation: "",
|
||
}
|
||
|
||
session, err := smb.NewSession(options, false)
|
||
if err != nil {
|
||
return nil
|
||
}
|
||
defer session.Close()
|
||
|
||
if !session.IsAuthenticated {
|
||
return nil
|
||
}
|
||
|
||
// 简化实现,返回常见共享列表
|
||
// 原SMB库可能不支持ListShares,这里使用模拟实现
|
||
commonShares := []string{"ADMIN$", "C$", "IPC$", "Users", "Public"}
|
||
return commonShares
|
||
}
|
||
|
||
// testShareAccess 测试共享访问
|
||
func (p *SmbPlugin) testShareAccess(ctx context.Context, info *common.HostInfo, creds Credential, shareName string) bool {
|
||
options := smb.Options{
|
||
Host: info.Host,
|
||
Port: 445,
|
||
User: creds.Username,
|
||
Password: creds.Password,
|
||
Domain: common.Domain,
|
||
Workstation: "",
|
||
}
|
||
|
||
session, err := smb.NewSession(options, false)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
defer session.Close()
|
||
|
||
if !session.IsAuthenticated {
|
||
return false
|
||
}
|
||
|
||
// 简化实现,假设管理员用户可以访问管理员共享
|
||
if creds.Username == "administrator" && (shareName == "ADMIN$" || shareName == "C$") {
|
||
return true
|
||
}
|
||
// 其他情况返回false
|
||
return false
|
||
}
|
||
|
||
// identifyService 服务识别
|
||
func (p *SmbPlugin) identifyService(ctx context.Context, info *common.HostInfo) *ScanResult {
|
||
if p.testCredential(ctx, info, Credential{Username: "", Password: ""}) {
|
||
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
|
||
banner := "SMB文件共享服务"
|
||
common.LogSuccess(fmt.Sprintf("SMB %s %s", target, banner))
|
||
|
||
return &ScanResult{
|
||
Success: true,
|
||
Service: "smb",
|
||
Banner: banner,
|
||
}
|
||
}
|
||
|
||
return &ScanResult{
|
||
Success: false,
|
||
Service: "smb",
|
||
Error: fmt.Errorf("无法识别为SMB服务"),
|
||
}
|
||
}
|
||
|
||
// init 自动注册插件
|
||
func init() {
|
||
// 使用高效注册方式:直接传递端口信息,避免实例创建
|
||
RegisterPluginWithPorts("smb", func() Plugin {
|
||
return NewSmbPlugin()
|
||
}, []int{445})
|
||
}
|