service_matcher-go/ldap/ldap.go
crusader bce329e44e ing
2018-10-23 13:31:25 +09:00

102 lines
2.4 KiB
Go

package ldap
import (
"math/rand"
"time"
osm "git.loafle.net/overflow/service_matcher-go"
ber "gopkg.in/asn1-ber.v1"
)
const (
ApplicationBindRequest = 0
ApplicationBindResponse = 1
)
type LDAPMatcher struct {
osm.Matchers
reqID int64
}
func (m *LDAPMatcher) Key(matchCtx *osm.MatchCtx) string {
return "LDAP"
}
func (m *LDAPMatcher) Type(matchCtx *osm.MatchCtx) string {
return "DIRECTORY"
}
func (m *LDAPMatcher) Vendor(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *LDAPMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *LDAPMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *LDAPMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *LDAPMatcher) Name(matchCtx *osm.MatchCtx) string {
return "LDAP"
}
func (m *LDAPMatcher) IsPrePacket() bool {
return false
}
func (m *LDAPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *LDAPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
p := ber.DecodePacket(packet.Buffer)
if nil == p || nil == p.Children || len(p.Children) <= 1 {
return osm.NotMatchedError()
}
respID, ok := p.Children[0].Value.(int64)
if !ok {
return osm.NotMatchedError()
}
if respID != m.reqID {
return osm.NotMatchedError()
}
if p.Children[1].Tag != ApplicationBindResponse {
return osm.NotMatchedError()
}
return nil
}
func NewMatcher() osm.Matcher {
m := &LDAPMatcher{}
rand.Seed(time.Now().UnixNano())
m.reqID = rand.Int63n(1000)
p := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
p.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, m.reqID, "MessageID"))
bindRequest := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
bindRequest.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 2, "Version"))
bindRequest.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "LOAFLEOVERFLOW", "User Name"))
bindRequest.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, 0, "LOAFLEOVERFLOW", "Password"))
p.AppendChild(bindRequest)
m.AddPacket(osm.NewPacket(p.Bytes(), len(p.Bytes())))
return m
}