service_matcher-go/ssh/ssh.go

86 lines
1.7 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package ssh
import (
"bufio"
"bytes"
"strings"
csm "git.loafle.net/commons/service_matcher-go"
)
type SSHMatcher struct {
csm.Matchers
meta csm.Metadata
}
func (ssh *SSHMatcher) Key() string {
return "SSH"
}
func (ssh *SSHMatcher) Name() string {
name := "SSH"
if v, ok := ssh.meta["softwareversion"]; ok {
name = name + " (" + v + ")"
}
return name
}
func (ssh *SSHMatcher) Meta() csm.Metadata {
return ssh.meta
}
func (ssh *SSHMatcher) IsPrePacket() bool {
return true
}
func (ssh *SSHMatcher) HasResponse(index int) bool {
return true
}
func (ssh *SSHMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool {
return false
}
func (ssh *SSHMatcher) Match(info csm.MatchInfo, index int, packet *csm.Packet) error {
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
return csm.NoPacketReceivedError()
}
// SSH-protoversion-softwareversion SP comments CR LF
// e.g. ) SSH-2.0-OpenSSH_7.5p1 Ubuntu-10ubuntu0.1\n
scanner := bufio.NewScanner(bytes.NewReader(packet.Buffer))
for scanner.Scan() {
exchange := scanner.Text()
if !strings.HasPrefix(exchange, "SSH") {
return csm.NotMatchedError()
}
temp := strings.Split(exchange, " ")
versions := strings.Split(temp[0], "-")
protoversion := versions[1]
softwareversion := versions[2]
if strings.HasPrefix(protoversion, "1") || strings.HasPrefix(protoversion, "2") {
ssh.meta["protoversion"] = protoversion
ssh.meta["softwareversion"] = softwareversion
if len(temp) > 1 {
ssh.meta["comments"] = temp[1]
}
return nil
}
break
}
return csm.NotMatchedError()
}
func NewMatcher() csm.Matcher {
m := &SSHMatcher{}
m.meta = csm.NewMetadata()
return m
}