fscan/plugins/services/memcached.go
ZacharyZcR 36f0e5076d refactor: 重构Memcached和RabbitMQ插件使用统一发包控制
- 修改Memcached插件,在TCP连接和服务识别中添加发包控制
- 修改RabbitMQ插件,在AMQP连接、HTTP连接和管理接口中添加发包控制
- 统一包计数逻辑,确保TCP连接成功和失败都正确计数
- 保持现有缓存服务和消息队列检测功能
2025-09-02 11:52:59 +00:00

154 lines
3.4 KiB
Go

package services
import (
"context"
"fmt"
"net"
"strings"
"time"
"github.com/shadow1ng/fscan/common"
"github.com/shadow1ng/fscan/plugins"
)
type MemcachedPlugin struct {
plugins.BasePlugin
}
func NewMemcachedPlugin() *MemcachedPlugin {
return &MemcachedPlugin{
BasePlugin: plugins.NewBasePlugin("memcached"),
}
}
func (p *MemcachedPlugin) Scan(ctx context.Context, info *common.HostInfo) *ScanResult {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
if common.DisableBrute {
return p.identifyService(ctx, info)
}
conn := p.connectToMemcached(ctx, info)
if conn == nil {
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf("Memcached服务连接失败"),
}
}
defer conn.Close()
if p.testBasicCommand(conn) {
common.LogSuccess(fmt.Sprintf("Memcached %s 未授权访问", target))
return &ScanResult{
Success: true,
Service: "memcached",
Banner: "未授权访问",
}
}
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf("Memcached服务连接失败"),
}
}
func (p *MemcachedPlugin) connectToMemcached(ctx context.Context, info *common.HostInfo) net.Conn {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
// 检查发包限制
if canSend, reason := common.CanSendPacket(); !canSend {
common.LogError(fmt.Sprintf("Memcached连接 %s 受限: %s", target, reason))
return nil
}
timeout := time.Duration(common.Timeout) * time.Second
conn, err := net.DialTimeout("tcp", target, timeout)
if err != nil {
common.IncrementTCPFailedPacketCount()
return nil
}
common.IncrementTCPSuccessPacketCount()
conn.SetDeadline(time.Now().Add(timeout))
return conn
}
func (p *MemcachedPlugin) testBasicCommand(conn net.Conn) bool {
timeout := time.Duration(common.Timeout) * time.Second
conn.SetWriteDeadline(time.Now().Add(timeout))
if _, err := conn.Write([]byte("version\r\n")); err != nil {
return false
}
conn.SetReadDeadline(time.Now().Add(timeout))
response := make([]byte, 1024)
n, err := conn.Read(response)
if err != nil {
return false
}
responseStr := string(response[:n])
return strings.Contains(responseStr, "VERSION") || strings.Contains(responseStr, "memcached")
}
func (p *MemcachedPlugin) identifyService(ctx context.Context, info *common.HostInfo) *ScanResult {
target := fmt.Sprintf("%s:%s", info.Host, info.Ports)
// 检查发包限制
if canSend, reason := common.CanSendPacket(); !canSend {
common.LogError(fmt.Sprintf("Memcached识别 %s 受限: %s", target, reason))
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf("发包受限: %s", reason),
}
}
conn := p.connectToMemcached(ctx, info)
if conn == nil {
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf("无法连接到Memcached服务"),
}
}
defer conn.Close()
banner := "Memcached"
if p.testBasicCommand(conn) {
common.LogSuccess(fmt.Sprintf("Memcached %s %s", target, banner))
return &ScanResult{
Success: true,
Service: "memcached",
Banner: banner,
}
}
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf("无法识别为Memcached服务"),
}
}
func init() {
// 使用高效注册方式:直接传递端口信息,避免实例创建
RegisterPluginWithPorts("memcached", func() Plugin {
return NewMemcachedPlugin()
}, []int{11211, 11212, 11213})
}