diff --git a/plugins/services/ldap.go b/plugins/services/ldap.go index 79deb1b..933f386 100644 --- a/plugins/services/ldap.go +++ b/plugins/services/ldap.go @@ -19,28 +19,30 @@ func NewLDAPPlugin() *LDAPPlugin { } } - - -func (p *LDAPPlugin) Scan(ctx context.Context, info *common.HostInfo) *ScanResult { +func (p *LDAPPlugin) Scan(ctx context.Context, info *common.HostInfo) *plugins.Result { target := fmt.Sprintf("%s:%s", info.Host, info.Ports) if common.DisableBrute { return p.identifyService(ctx, info) } - credentials := GenerateCredentials("ldap") - if len(credentials) == 0 { - return &ScanResult{ - Success: false, - Service: "ldap", - Error: fmt.Errorf("没有可用的测试凭据"), - } - } - + credentials := plugins.GenerateCredentials("ldap") + for _, cred := range credentials { + // 检查上下文是否已取消 + select { + case <-ctx.Done(): + return &plugins.Result{ + Success: false, + Service: "ldap", + Error: ctx.Err(), + } + default: + } + if p.testCredential(ctx, info, cred) { common.LogSuccess(fmt.Sprintf("LDAP %s %s:%s", target, cred.Username, cred.Password)) - return &ScanResult{ + return &plugins.Result{ Success: true, Service: "ldap", Username: cred.Username, @@ -49,15 +51,14 @@ func (p *LDAPPlugin) Scan(ctx context.Context, info *common.HostInfo) *ScanResul } } - return &ScanResult{ + return &plugins.Result{ Success: false, Service: "ldap", Error: fmt.Errorf("未发现弱密码"), } } - -func (p *LDAPPlugin) testCredential(ctx context.Context, info *common.HostInfo, cred Credential) bool { +func (p *LDAPPlugin) testCredential(ctx context.Context, info *common.HostInfo, cred plugins.Credential) bool { conn, err := p.connectLDAP(ctx, info, cred) if err != nil { return false @@ -71,7 +72,7 @@ func (p *LDAPPlugin) testCredential(ctx context.Context, info *common.HostInfo, return false } -func (p *LDAPPlugin) connectLDAP(ctx context.Context, info *common.HostInfo, creds Credential) (*ldaplib.Conn, error) { +func (p *LDAPPlugin) connectLDAP(ctx context.Context, info *common.HostInfo, creds plugins.Credential) (*ldaplib.Conn, error) { target := fmt.Sprintf("%s:%s", info.Host, info.Ports) if info.Ports == "636" { @@ -80,17 +81,12 @@ func (p *LDAPPlugin) connectLDAP(ctx context.Context, info *common.HostInfo, cre return ldaplib.Dial("tcp", target) } - - - - - -func (p *LDAPPlugin) identifyService(ctx context.Context, info *common.HostInfo) *ScanResult { +func (p *LDAPPlugin) identifyService(ctx context.Context, info *common.HostInfo) *plugins.Result { target := fmt.Sprintf("%s:%s", info.Host, info.Ports) - conn, err := p.connectLDAP(ctx, info, Credential{}) + conn, err := p.connectLDAP(ctx, info, plugins.Credential{}) if err != nil { - return &ScanResult{ + return &plugins.Result{ Success: false, Service: "ldap", Error: err, @@ -100,7 +96,7 @@ func (p *LDAPPlugin) identifyService(ctx context.Context, info *common.HostInfo) banner := "LDAP" common.LogSuccess(fmt.Sprintf("LDAP %s %s", target, banner)) - return &ScanResult{ + return &plugins.Result{ Success: true, Service: "ldap", Banner: banner, @@ -108,8 +104,7 @@ func (p *LDAPPlugin) identifyService(ctx context.Context, info *common.HostInfo) } func init() { - // 使用高效注册方式:直接传递端口信息,避免实例创建 - RegisterPluginWithPorts("ldap", func() Plugin { + plugins.RegisterWithPorts("ldap", func() plugins.Plugin { return NewLDAPPlugin() }, []int{389, 636, 3268, 3269}) } \ No newline at end of file