service_matcher-go/snmp/v2/snmpv2.go

159 lines
2.8 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package v2
import (
"encoding/asn1"
"math/rand"
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
type snmpv2 struct {
Version int
Community []byte
Data struct {
RequestID int32
ErrorStatus int
ErrorIndex int
Bindings []binding
} `asn1:"tag:0"`
}
type response struct {
ID int32
ErrorStatus int
ErrorIndex int
Bindings []binding
}
type binding struct {
Name asn1.ObjectIdentifier
Value asn1.RawValue
}
var (
null = asn1.RawValue{Class: 0, Tag: 5}
noSuchObject = asn1.RawValue{Class: 2, Tag: 0}
noSuchInstance = asn1.RawValue{Class: 2, Tag: 1}
endOfMibView = asn1.RawValue{Class: 2, Tag: 2}
)
type SNMPMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-09-03 13:36:57 +00:00
2018-08-13 07:48:32 +00:00
requestID int32
}
2018-10-23 04:31:25 +00:00
func (s *SNMPMatcher) Key(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
return "SNMP"
}
2018-10-23 04:31:25 +00:00
func (m *SNMPMatcher) Type(matchCtx *osm.MatchCtx) string {
2018-09-12 04:26:27 +00:00
return "MONITORING"
}
func (m *SNMPMatcher) Vendor(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *SNMPMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *SNMPMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *SNMPMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
2018-09-03 13:36:57 +00:00
func (s *SNMPMatcher) Name(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
return "SNMP"
}
2018-09-03 13:41:28 +00:00
func (s *SNMPMatcher) IsPrePacket() bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 13:36:57 +00:00
func (s *SNMPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (s *SNMPMatcher) 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
}
var p struct {
Version int
Community []byte
Data struct {
RequestID int32
ErrorStatus int
ErrorIndex int
Bindings []binding
} `asn1:"tag:2"`
}
2018-09-03 07:23:25 +00:00
if _, err := asn1.Unmarshal(packet.Buffer, &p); err != nil {
2018-08-13 07:48:32 +00:00
return err
}
resp := &response{p.Data.RequestID, p.Data.ErrorStatus, p.Data.ErrorIndex, p.Data.Bindings}
if s.requestID != resp.ID {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
if len(resp.Bindings) == 0 {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
for _, binding := range resp.Bindings {
if len(binding.Value.Bytes) <= 0 {
continue
}
// if binding.Name.String() == "1.3.6.1.2.1.1.5.0" {
2018-09-03 13:36:57 +00:00
matchCtx.SetAttribute(binding.Name.String(), string(binding.Value.Bytes))
2018-08-13 07:48:32 +00:00
// }
}
return nil
}
func (s *SNMPMatcher) IsSend(port int) bool {
if 161 == port {
return true
}
return false
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.UDPMatcher {
2018-08-13 07:48:32 +00:00
m := &SNMPMatcher{}
m.requestID = rand.Int31()
p := snmpv2{}
p.Version = 0
p.Community = []byte("test1252serc")
p.Data.RequestID = m.requestID
p.Data.Bindings = []binding{
binding{
Name: []int{1, 3, 6, 1, 2, 1, 1, 5, 0},
Value: null,
},
binding{
Name: []int{1, 3, 6, 1, 2, 1, 1, 1, 0},
Value: null,
},
}
buf, _ := asn1.Marshal(p)
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(buf, len(buf)))
2018-08-13 07:48:32 +00:00
return m
}