service_matcher-go/ssh/ssh.go

86 lines
1.6 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package ssh
import (
"bufio"
"bytes"
"strings"
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 SSHMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
meta osm.Metadata
2018-08-13 07:48:32 +00:00
}
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
}
2018-08-15 07:17:18 +00:00
func (ssh *SSHMatcher) Meta() osm.Metadata {
2018-08-13 07:48:32 +00:00
return ssh.meta
}
func (ssh *SSHMatcher) IsPrePacket() bool {
return true
}
func (ssh *SSHMatcher) HasResponse(index int) bool {
return true
}
2018-08-15 07:17:18 +00:00
func (ssh *SSHMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-08-15 07:17:18 +00:00
func (ssh *SSHMatcher) Match(info osm.MatchInfo, 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
}
// SSH-protoversion-softwareversion SP comments CR LF
// e.g. ) SSH-2.0-OpenSSH_7.5p1 Ubuntu-10ubuntu0.1\n
2018-09-03 07:23:25 +00:00
scanner := bufio.NewScanner(bytes.NewReader(packet.Buffer))
2018-08-13 07:48:32 +00:00
for scanner.Scan() {
exchange := scanner.Text()
if !strings.HasPrefix(exchange, "SSH") {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
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
}
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &SSHMatcher{}
2018-08-15 07:17:18 +00:00
m.meta = osm.NewMetadata()
2018-08-13 07:48:32 +00:00
return m
}