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

- 新增common/network.go统一网络操作包装器 - 重构MySQL/FTP/SSH/SNMP插件使用统一包装器 - 简化发包控制逻辑,避免重复代码 - 为未来代理、重试等功能扩展奠定基础
140 lines
3.0 KiB
Go
140 lines
3.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
ftplib "github.com/jlaffaye/ftp"
|
|
"github.com/shadow1ng/fscan/common"
|
|
"github.com/shadow1ng/fscan/plugins"
|
|
)
|
|
|
|
type FTPPlugin struct {
|
|
plugins.BasePlugin
|
|
}
|
|
|
|
func NewFTPPlugin() *FTPPlugin {
|
|
return &FTPPlugin{
|
|
BasePlugin: plugins.NewBasePlugin("ftp"),
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (p *FTPPlugin) Scan(ctx context.Context, info *common.HostInfo) *ScanResult {
|
|
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
|
|
|
|
if common.DisableBrute {
|
|
return p.identifyService(info)
|
|
}
|
|
|
|
credentials := GenerateCredentials("ftp")
|
|
if len(credentials) == 0 {
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "ftp",
|
|
Error: fmt.Errorf("没有可用的测试凭据"),
|
|
}
|
|
}
|
|
|
|
for _, cred := range credentials {
|
|
if conn := p.testCredential(ctx, info, cred); conn != nil {
|
|
conn.Quit()
|
|
common.LogSuccess(fmt.Sprintf("FTP %s %s:%s", target, cred.Username, cred.Password))
|
|
return &ScanResult{
|
|
Success: true,
|
|
Service: "ftp",
|
|
Username: cred.Username,
|
|
Password: cred.Password,
|
|
}
|
|
}
|
|
}
|
|
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "ftp",
|
|
Error: fmt.Errorf("未发现弱密码"),
|
|
}
|
|
}
|
|
|
|
|
|
func (p *FTPPlugin) testCredential(ctx context.Context, info *common.HostInfo, cred Credential) *ftplib.ServerConn {
|
|
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
|
|
timeout := time.Duration(common.Timeout) * time.Second
|
|
|
|
// 检查发包限制
|
|
if canSend, reason := common.CanSendPacket(); !canSend {
|
|
common.LogError(fmt.Sprintf("FTP连接 %s 受限: %s", target, reason))
|
|
return nil
|
|
}
|
|
|
|
conn, err := ftplib.DialTimeout(target, timeout)
|
|
if err == nil {
|
|
// 计数成功连接
|
|
common.IncrementTCPSuccessPacketCount()
|
|
} else {
|
|
// 计数失败连接
|
|
common.IncrementTCPFailedPacketCount()
|
|
}
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
err = conn.Login(cred.Username, cred.Password)
|
|
if err != nil {
|
|
conn.Quit()
|
|
return nil
|
|
}
|
|
|
|
return conn
|
|
}
|
|
|
|
|
|
|
|
func (p *FTPPlugin) identifyService(info *common.HostInfo) *ScanResult {
|
|
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
|
|
timeout := time.Duration(common.Timeout) * time.Second
|
|
|
|
// 检查发包限制
|
|
if canSend, reason := common.CanSendPacket(); !canSend {
|
|
common.LogError(fmt.Sprintf("FTP服务识别 %s 受限: %s", target, reason))
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "ftp",
|
|
Error: fmt.Errorf("发包受限: %s", reason),
|
|
}
|
|
}
|
|
|
|
conn, err := ftplib.DialTimeout(target, timeout)
|
|
if err == nil {
|
|
// 计数成功连接
|
|
common.IncrementTCPSuccessPacketCount()
|
|
} else {
|
|
// 计数失败连接
|
|
common.IncrementTCPFailedPacketCount()
|
|
}
|
|
if err != nil {
|
|
return &ScanResult{
|
|
Success: false,
|
|
Service: "ftp",
|
|
Error: err,
|
|
}
|
|
}
|
|
defer conn.Quit()
|
|
|
|
banner := "FTP"
|
|
common.LogSuccess(fmt.Sprintf("FTP %s %s", target, banner))
|
|
return &ScanResult{
|
|
Success: true,
|
|
Service: "ftp",
|
|
Banner: banner,
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
// 使用高效注册方式:直接传递端口信息,避免实例创建
|
|
RegisterPluginWithPorts("ftp", func() Plugin {
|
|
return NewFTPPlugin()
|
|
}, []int{21, 2121, 990})
|
|
} |