Files
fscan/plugins/services/memcached.go
T
ZacharyZcR 77827bef66 修复凭证测试器计数器、消除panic、补齐i18n
- credential_tester: testCredentialWithRetry返回ErrorType,修复网络错误计数器永久不递增的bug
- scanner: os.Exit(1)改为return,defer Cleanup可正常执行
- probe_parser: 5处panic改为error返回,调用链透传到init()
- common库: parsers/initialize/network/session共17处硬编码中文改用i18n
- services插件: 18个文件115处硬编码中文改用i18n
- locale: 补齐service/parser/network相关~25个中英文键
2026-05-18 04:55:20 +08:00

152 lines
3.6 KiB
Go

//go:build plugin_memcached || !plugin_selective
package services
import (
"context"
"fmt"
"net"
"time"
"github.com/shadow1ng/fscan/common"
"github.com/shadow1ng/fscan/common/i18n"
"github.com/shadow1ng/fscan/plugins"
)
// MemcachedPlugin Memcached扫描插件
type MemcachedPlugin struct {
plugins.BasePlugin
}
func NewMemcachedPlugin() *MemcachedPlugin {
return &MemcachedPlugin{
BasePlugin: plugins.NewBasePlugin("memcached"),
}
}
func (p *MemcachedPlugin) Scan(ctx context.Context, info *common.HostInfo, session *common.ScanSession) *ScanResult {
config := session.Config
target := info.Target()
if config.DisableBrute {
return p.identifyService(ctx, info, session)
}
// 检测未授权访问
if result := p.testUnauthorizedAccess(ctx, info, session); result != nil && result.Success {
common.LogVuln(i18n.Tr("memcached_unauth", target))
return result
}
// Memcached通常不需要认证,如果上面检测失败则服务可能不可用
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf("无法访问Memcached服务"),
}
}
// testUnauthorizedAccess 测试Memcached未授权访问
func (p *MemcachedPlugin) testUnauthorizedAccess(ctx context.Context, info *common.HostInfo, session *common.ScanSession) *ScanResult {
conn := p.connectToMemcached(ctx, info, session)
if conn == nil {
return nil
}
defer func() { _ = conn.Close() }()
if p.testBasicCommand(conn, session.Config) {
return &ScanResult{
Type: plugins.ResultTypeVuln,
Success: true,
Service: "memcached",
Banner: i18n.GetText("service_unauthorized"),
}
}
return nil
}
func (p *MemcachedPlugin) connectToMemcached(ctx context.Context, info *common.HostInfo, session *common.ScanSession) net.Conn {
target := info.Target()
timeout := session.Config.Timeout
connChan := make(chan net.Conn, 1)
go func() {
conn, err := session.DialTCP(ctx, "tcp", target, timeout)
if err != nil {
connChan <- nil
return
}
_ = conn.SetDeadline(time.Now().Add(timeout))
connChan <- conn
}()
select {
case conn := <-connChan:
return conn
case <-ctx.Done():
go func() {
conn := <-connChan
if conn != nil {
_ = conn.Close()
}
}()
return nil
}
}
func (p *MemcachedPlugin) testBasicCommand(conn net.Conn, config *common.Config) bool {
_ = conn.SetWriteDeadline(time.Now().Add(config.Timeout))
if _, err := conn.Write([]byte("version\r\n")); err != nil {
return false
}
_ = conn.SetReadDeadline(time.Now().Add(config.Timeout))
response := make([]byte, 1024)
n, err := conn.Read(response)
if err != nil {
return false
}
responseStr := string(response[:n])
return common.ContainsAny(responseStr, "VERSION", "memcached")
}
func (p *MemcachedPlugin) identifyService(ctx context.Context, info *common.HostInfo, session *common.ScanSession) *ScanResult {
target := info.Target()
conn := p.connectToMemcached(ctx, info, session)
if conn == nil {
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf("无法连接到Memcached服务"),
}
}
defer func() { _ = conn.Close() }()
if p.testBasicCommand(conn, session.Config) {
banner := "Memcached"
common.LogSuccess(i18n.Tr("memcached_service", target, banner))
return &ScanResult{
Type: plugins.ResultTypeService,
Success: true,
Service: "memcached",
Banner: banner,
}
}
return &ScanResult{
Success: false,
Service: "memcached",
Error: fmt.Errorf(i18n.Tr("service_not_identified", "Memcached")),
}
}
func init() {
RegisterPluginWithPorts("memcached", func() Plugin {
return NewMemcachedPlugin()
}, []int{11211, 11212, 11213})
}