service_matcher-go/ldap/ldap.go

102 lines
2.4 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package ldap
import (
"math/rand"
"time"
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
ber "gopkg.in/asn1-ber.v1"
)
const (
ApplicationBindRequest = 0
ApplicationBindResponse = 1
)
type LDAPMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
reqID int64
}
2018-10-23 04:31:25 +00:00
func (m *LDAPMatcher) Key(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
return "LDAP"
}
2018-10-23 04:31:25 +00:00
func (m *LDAPMatcher) Type(matchCtx *osm.MatchCtx) string {
2018-09-12 04:26:27 +00:00
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"
}
2018-09-12 04:26:27 +00:00
2018-09-03 13:36:57 +00:00
func (m *LDAPMatcher) Name(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
return "LDAP"
}
2018-09-03 13:41:28 +00:00
func (m *LDAPMatcher) IsPrePacket() bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 13:36:57 +00:00
func (m *LDAPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 13:36:57 +00:00
func (m *LDAPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
2018-09-03 06:42:56 +00:00
if packet == nil || !packet.Valid() {
2018-08-15 07:17:18 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 07:23:25 +00:00
p := ber.DecodePacket(packet.Buffer)
2018-08-13 07:48:32 +00:00
2018-09-03 09:48:58 +00:00
if nil == p || nil == p.Children || len(p.Children) <= 1 {
2018-09-03 03:43:12 +00:00
return osm.NotMatchedError()
}
2018-08-13 07:48:32 +00:00
respID, ok := p.Children[0].Value.(int64)
if !ok {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 13:36:57 +00:00
if respID != m.reqID {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
if p.Children[1].Tag != ApplicationBindResponse {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
return nil
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
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)
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(p.Bytes(), len(p.Bytes())))
2018-08-13 07:48:32 +00:00
return m
}