diff --git a/activedirectory/activedirectory.go b/activedirectory/activedirectory.go index 5c780a6..8185ebc 100644 --- a/activedirectory/activedirectory.go +++ b/activedirectory/activedirectory.go @@ -177,23 +177,19 @@ func (m *ActiveDirectoryMatcher) Key() string { return "ACTIVEDIRECTORY" } -func (m *ActiveDirectoryMatcher) Name() string { +func (m *ActiveDirectoryMatcher) Name(matchCtx *osm.MatchCtx) string { return "ActiveDirectory" } -func (m *ActiveDirectoryMatcher) Meta() osm.Metadata { - return nil -} - -func (m *ActiveDirectoryMatcher) IsPrePacket() bool { +func (m *ActiveDirectoryMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (m *ActiveDirectoryMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *ActiveDirectoryMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (m *ActiveDirectoryMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *ActiveDirectoryMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() diff --git a/activedirectory/activedirectory_test.go b/activedirectory/activedirectory_test.go index 6cb277a..64c543e 100644 --- a/activedirectory/activedirectory_test.go +++ b/activedirectory/activedirectory_test.go @@ -18,18 +18,19 @@ func TestAD(t *testing.T) { } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { + matchCtx := osm.NewMatchCtx("192.168.1.10", 10389) - pack := m.Packet(i) + for i := 0; i < m.PacketCount(matchCtx); i++ { + + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) p := osm.NewPacket(bytes, n) - if err := m.Match(nil, i, p); err != nil { + if err := m.Match(matchCtx, i, p); err != nil { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) } diff --git a/cassandra/cassandra.go b/cassandra/cassandra.go index e2f39a4..6ab35f8 100644 --- a/cassandra/cassandra.go +++ b/cassandra/cassandra.go @@ -23,27 +23,23 @@ func (m *CassandraMatcher) Key() string { return "CASSANDRA" } -func (m *CassandraMatcher) Name() string { +func (m *CassandraMatcher) Name(matchCtx *osm.MatchCtx) string { return "Cassandra" } -func (m *CassandraMatcher) Meta() osm.Metadata { - return nil -} - -func (m *CassandraMatcher) IsPrePacket() bool { +func (m *CassandraMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (m *CassandraMatcher) HasResponse(index int) bool { +func (m *CassandraMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (m *CassandraMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *CassandraMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (m *CassandraMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *CassandraMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() diff --git a/cassandra/cassandra_test.go b/cassandra/cassandra_test.go index ddbee17..9f4eedb 100644 --- a/cassandra/cassandra_test.go +++ b/cassandra/cassandra_test.go @@ -18,9 +18,11 @@ func TestCassandra(t *testing.T) { } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { + matchCtx := osm.NewMatchCtx("192.168.1.103", 8080) - pack := m.Packet(i) + for i := 0; i < m.PacketCount(matchCtx); i++ { + + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) @@ -30,5 +32,5 @@ func TestCassandra(t *testing.T) { t.Error(err) } } - t.Log(m.Meta()) + t.Log(matchCtx) } diff --git a/dns/dns.go b/dns/dns.go index dedac3d..4843992 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -41,31 +41,27 @@ type DNSMatcher struct { osm.Matchers } -func (t *DNSMatcher) Key() string { +func (m *DNSMatcher) Key() string { return "DNS" } -func (t *DNSMatcher) Name() string { +func (m *DNSMatcher) Name(matchCtx *osm.MatchCtx) string { return "DNS" } -func (t *DNSMatcher) Meta() osm.Metadata { - return nil -} - -func (t *DNSMatcher) IsPrePacket() bool { +func (m *DNSMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (t *DNSMatcher) HasResponse(index int) bool { +func (m *DNSMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (t *DNSMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *DNSMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (t *DNSMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *DNSMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() } @@ -114,7 +110,7 @@ func (t *DNSMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) er return nil } -func (t *DNSMatcher) IsSend(port int) bool { +func (m *DNSMatcher) IsSend(port int) bool { if 53 == port { return true } diff --git a/dns/dns_test.go b/dns/dns_test.go index 03d91bc..ef070b6 100644 --- a/dns/dns_test.go +++ b/dns/dns_test.go @@ -10,15 +10,20 @@ import ( func TestDns(t *testing.T) { m := NewMatcher() - conn, _ := net.Dial("udp", "192.168.1.254:53") - + conn, err := net.Dial("udp", "192.168.1.254:53") + if err != nil { + t.Error(err) + return + } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { + matchCtx := osm.NewMatchCtx("192.168.1.254", 53) + + for i := 0; i < m.PacketCount(matchCtx); i++ { if m.IsSend(53) != true { t.Error("not port") } - pack := m.Packet(i) + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) @@ -29,5 +34,5 @@ func TestDns(t *testing.T) { } } - t.Log(m.Meta()) + t.Log(matchCtx) } diff --git a/elasticsearch/elasticsearch.go b/elasticsearch/elasticsearch.go index 981d8f6..fb3509a 100644 --- a/elasticsearch/elasticsearch.go +++ b/elasticsearch/elasticsearch.go @@ -11,38 +11,33 @@ import ( type ElasticSearchMatcher struct { osm.Matchers - meta osm.Metadata } -func (es *ElasticSearchMatcher) Key() string { +func (m *ElasticSearchMatcher) Key() string { return "ELASTICSEARCH" } -func (es *ElasticSearchMatcher) Name() string { +func (m *ElasticSearchMatcher) Name(matchCtx *osm.MatchCtx) string { name := "ElasticSearch" - if v, ok := es.meta["number"]; ok { - name = name + " (" + v + ")" + if v, ok := matchCtx.GetAttribute("number"); ok { + name = name + " (" + v.(string) + ")" } return name } -func (es *ElasticSearchMatcher) Meta() osm.Metadata { - return es.meta -} - -func (es *ElasticSearchMatcher) IsPrePacket() bool { +func (m *ElasticSearchMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (es *ElasticSearchMatcher) HasResponse(index int) bool { +func (m *ElasticSearchMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (es *ElasticSearchMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *ElasticSearchMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (es *ElasticSearchMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *ElasticSearchMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -86,34 +81,34 @@ func (es *ElasticSearchMatcher) Match(info osm.MatchInfo, index int, packet *osm if strings.HasPrefix(content, "{") && strings.HasSuffix(content, "}") { return osm.NotMatchedError() } - es.parseJson(content) - if _, ok := es.meta["cluster_name"]; !ok { + m.parseJson(matchCtx, content) + if _, ok := matchCtx.GetAttribute("cluster_name"); !ok { return osm.NotMatchedError() } - if _, ok := es.meta["cluster_uuid"]; !ok { + if _, ok := matchCtx.GetAttribute("cluster_uuid"); !ok { return osm.NotMatchedError() } return nil } -func (es *ElasticSearchMatcher) parseJson(jsonstr string) error { +func (m *ElasticSearchMatcher) parseJson(matchCtx *osm.MatchCtx, jsonstr string) error { jsonMap := make(map[string]interface{}) err := json.Unmarshal([]byte(jsonstr), &jsonMap) if err != nil { return err } - es.dumpMap(jsonMap) + m.dumpMap(matchCtx, jsonMap) return nil } -func (es *ElasticSearchMatcher) dumpMap(m map[string]interface{}) { - for k, v := range m { +func (m *ElasticSearchMatcher) dumpMap(matchCtx *osm.MatchCtx, jsonMap map[string]interface{}) { + for k, v := range jsonMap { if mv, ok := v.(map[string]interface{}); ok { - es.dumpMap(mv) + m.dumpMap(matchCtx, mv) } else { s, ok := v.(string) if ok { - es.meta[k] = s + matchCtx.SetAttribute(k, s) } } } @@ -122,7 +117,6 @@ func (es *ElasticSearchMatcher) dumpMap(m map[string]interface{}) { func NewMatcher() osm.Matcher { m := &ElasticSearchMatcher{} - m.meta = osm.NewMetadata() reqStr := "GET / HTTP/1.1\r\n\r\n" byte := make([]byte, len(reqStr)) diff --git a/elasticsearch/elasticsearch_test.go b/elasticsearch/elasticsearch_test.go index 6000c87..f3c6374 100644 --- a/elasticsearch/elasticsearch_test.go +++ b/elasticsearch/elasticsearch_test.go @@ -14,11 +14,12 @@ func TestES(t *testing.T) { if err != nil { t.Errorf("ERR %s", err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("localhost", 9200) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -30,6 +31,6 @@ func TestES(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/ftp/ftp.go b/ftp/ftp.go index 7bedc23..a2b4ac1 100644 --- a/ftp/ftp.go +++ b/ftp/ftp.go @@ -27,34 +27,29 @@ const ( type FTPMatcher struct { osm.Matchers - meta osm.Metadata } -func (ftp *FTPMatcher) Key() string { +func (m *FTPMatcher) Key() string { return "FTP" } -func (ftp *FTPMatcher) Name() string { +func (m *FTPMatcher) Name(matchCtx *osm.MatchCtx) string { return "FTP" } -func (ftp *FTPMatcher) Meta() osm.Metadata { - return ftp.meta -} - -func (ftp *FTPMatcher) IsPrePacket() bool { +func (m *FTPMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return true } -func (ftp *FTPMatcher) HasResponse(index int) bool { +func (m *FTPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (ftp *FTPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *FTPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (ftp *FTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *FTPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -69,7 +64,7 @@ func (ftp *FTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) if index == 0 { switch code { case statusNewConnectOK, statusReadyServer: - ftp.meta["comment"] = str[4:] + matchCtx.SetAttribute("comment", str[4:]) return nil } } else if index == 1 { @@ -83,9 +78,7 @@ func (ftp *FTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) } func NewMatcher() osm.Matcher { - m := &FTPMatcher{} - m.meta = osm.NewMetadata() quitStr := "QUIT\r\n" quitByte := make([]byte, len(quitStr)) diff --git a/ftp/ftp_test.go b/ftp/ftp_test.go index 1f1cc6f..3e430e5 100644 --- a/ftp/ftp_test.go +++ b/ftp/ftp_test.go @@ -20,8 +20,10 @@ func TestFTP(t *testing.T) { } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.229", 21) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -34,8 +36,8 @@ func TestFTP(t *testing.T) { return } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } func TestFTPS(t *testing.T) { @@ -61,8 +63,10 @@ func TestFTPS(t *testing.T) { } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.201", 990) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -74,6 +78,6 @@ func TestFTPS(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/http/http.go b/http/http.go index dddf206..2f19975 100644 --- a/http/http.go +++ b/http/http.go @@ -8,38 +8,33 @@ import ( type HTTPMatcher struct { osm.Matchers - meta osm.Metadata } -func (h *HTTPMatcher) Key() string { +func (m *HTTPMatcher) Key() string { return "HTTP" } -func (h *HTTPMatcher) Name() string { +func (m *HTTPMatcher) Name(matchCtx *osm.MatchCtx) string { name := "HTTP" - if v, ok := h.meta["server"]; ok { - name = name + " (" + v + ")" + if v, ok := matchCtx.GetAttribute("server"); ok { + name = name + " (" + v.(string) + ")" } return name } -func (h *HTTPMatcher) Meta() osm.Metadata { - return h.meta -} - -func (h *HTTPMatcher) IsPrePacket() bool { +func (m *HTTPMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (h *HTTPMatcher) HasResponse(index int) bool { +func (m *HTTPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (h *HTTPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *HTTPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (h *HTTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *HTTPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -68,9 +63,9 @@ func (h *HTTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) e } } - h.meta["protocol_version"] = protocol + matchCtx.SetAttribute("protocol_version", protocol) if len(serverName) > 0 { - h.meta["server"] = serverName + matchCtx.SetAttribute("server", serverName) } return nil @@ -79,7 +74,6 @@ func (h *HTTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) e func NewMatcher() osm.Matcher { m := &HTTPMatcher{} - m.meta = osm.NewMetadata() reqStr := "GET / HTTP/1.1\r\n\r\n" byte := make([]byte, len(reqStr)) diff --git a/http/http_test.go b/http/http_test.go index 69e1094..4a4ba9f 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -16,11 +16,12 @@ func TestHTTP(t *testing.T) { if err != nil { t.Errorf("ERR %s", err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("localhost", 8000) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -32,8 +33,8 @@ func TestHTTP(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } func TestHTTPS(t *testing.T) { @@ -55,11 +56,12 @@ func TestHTTPS(t *testing.T) { if err != nil { t.Errorf("ERR %s", err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.1", 443) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -71,6 +73,6 @@ func TestHTTPS(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/imap/imap.go b/imap/imap.go index fb702d4..443cfe1 100644 --- a/imap/imap.go +++ b/imap/imap.go @@ -13,31 +13,27 @@ type IMAPMatcher struct { osm.Matchers } -func (i *IMAPMatcher) Key() string { +func (m *IMAPMatcher) Key() string { return "IMAP" } -func (i *IMAPMatcher) Name() string { +func (m *IMAPMatcher) Name(matchCtx *osm.MatchCtx) string { return "IMAP" } -func (i *IMAPMatcher) Meta() osm.Metadata { - return nil -} - -func (i *IMAPMatcher) IsPrePacket() bool { +func (m *IMAPMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return true } -func (i *IMAPMatcher) HasResponse(index int) bool { +func (m *IMAPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (i *IMAPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *IMAPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (i *IMAPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *IMAPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { switch index { case 0: diff --git a/imap/imap_test.go b/imap/imap_test.go index 1d4f19b..9ec4223 100644 --- a/imap/imap_test.go +++ b/imap/imap_test.go @@ -9,7 +9,7 @@ import ( osm "git.loafle.net/overflow/service_matcher-go" ) -func ImapRun(client net.Conn, t *testing.T) { +func ImapRun(matchCtx *osm.MatchCtx, conn net.Conn, t *testing.T) { lm := NewMatcher() @@ -21,51 +21,46 @@ func ImapRun(client net.Conn, t *testing.T) { //ipport = port.Host.Ip + ":" + string(port.Port) // //fmt.Println(ipport) - //client, _ := net.Dial("tcp", ipport) + //conn, _ := net.Dial("tcp", ipport) - //defer client.Close() + //defer conn.Close() bytett := make([]byte, 1024) - rr, _ := client.Read(bytett) + rr, _ := conn.Read(bytett) - bb := lm.Match(nil, 0, osm.NewPacket(bytett, rr)) - - if bb { + if err := lm.Match(matchCtx, 0, osm.NewPacket(bytett, rr)); nil == err { t.Log("good!") } - fmt.Println(lm.PacketCount()) + fmt.Println(lm.PacketCount(matchCtx)) - for ii := 0; ii < lm.PacketCount(); ii++ { + for ii := 0; ii < lm.PacketCount(matchCtx); ii++ { - pack := lm.Packet(ii) + pack := lm.Packet(matchCtx, ii) //fmt.Println(pack) - client.Write(pack.Buffer) + conn.Write(pack.Buffer) bytes := make([]byte, 1024) - read, _ := client.Read(bytes) + read, _ := conn.Read(bytes) fmt.Println(cap(bytes)) //fmt.Println(bytes) - b := lm.Match(nil, ii+1, osm.NewPacket(bytes, read)) - - if b { + if err := lm.Match(nil, ii+1, osm.NewPacket(bytes, read)); nil == err { t.Log("send Good!") } - } } func TestIMapTls(t *testing.T) { - conn, _ := tls.Dial( + conn, err := tls.Dial( "tcp", "192.168.1.15:993", &tls.Config{ @@ -73,23 +68,31 @@ func TestIMapTls(t *testing.T) { ServerName: "192.168.1.15", }, ) - + if err != nil { + t.Errorf("ERR %s", err) + } defer conn.Close() - ImapRun(conn, t) + matchCtx := osm.NewMatchCtx("192.168.1.15", 993) + + ImapRun(matchCtx, conn, t) } func TestIMapNormal(t *testing.T) { - client, err := net.Dial("tcp", "192.168.1.15:143") + conn, err := net.Dial("tcp", "192.168.1.15:143") if err != nil { t.Fatal(err) } + if err != nil { + t.Errorf("ERR %s", err) + } + defer conn.Close() - defer client.Close() + matchCtx := osm.NewMatchCtx("192.168.1.15", 143) - ImapRun(client, t) + ImapRun(matchCtx, conn, t) } @@ -105,45 +108,44 @@ func TestImap(t *testing.T) { //ipport = port.Host.Ip + ":" + port.Port_ fmt.Println(ipport) - client, _ := net.Dial("tcp", ipport) + conn, err := net.Dial("tcp", ipport) + if err != nil { + t.Errorf("ERR %s", err) + } + defer conn.Close() - defer client.Close() + matchCtx := osm.NewMatchCtx("192.168.1.15", 993) bytett := make([]byte, 1024) - rr, _ := client.Read(bytett) + rr, _ := conn.Read(bytett) //bb := lm.Match(0, osm.NewPacket(bytett, rr), scanInfo) - bb := lm.Match(nil, 0, osm.NewPacket(bytett, rr)) - - if bb { + if err := lm.Match(nil, 0, osm.NewPacket(bytett, rr)); nil == err { t.Log("good!") } - fmt.Println(lm.PacketCount()) + fmt.Println(lm.PacketCount(matchCtx)) - for ii := 0; ii < lm.PacketCount(); ii++ { + for ii := 0; ii < lm.PacketCount(matchCtx); ii++ { - pack := lm.Packet(ii) + pack := lm.Packet(matchCtx, ii) //fmt.Println(pack) - client.Write(pack.Buffer) + conn.Write(pack.Buffer) bytes := make([]byte, 1024) - read, _ := client.Read(bytes) + read, _ := conn.Read(bytes) fmt.Println(cap(bytes)) //fmt.Println(bytes) - b := lm.Match(nil, ii+1, osm.NewPacket(bytes, read)) - - if b { + if err := lm.Match(nil, ii+1, osm.NewPacket(bytes, read)); nil == err { t.Log("send Good!") } - } //t.Log(scanInfo) } diff --git a/ldap/ldap.go b/ldap/ldap.go index 32cbef8..86da204 100644 --- a/ldap/ldap.go +++ b/ldap/ldap.go @@ -18,27 +18,23 @@ type LDAPMatcher struct { reqID int64 } -func (l *LDAPMatcher) Key() string { +func (m *LDAPMatcher) Key() string { return "LDAP" } -func (l *LDAPMatcher) Name() string { +func (m *LDAPMatcher) Name(matchCtx *osm.MatchCtx) string { return "LDAP" } -func (l *LDAPMatcher) Meta() osm.Metadata { - return nil -} - -func (l *LDAPMatcher) IsPrePacket() bool { +func (m *LDAPMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (l *LDAPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *LDAPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (l *LDAPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *LDAPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -53,7 +49,7 @@ func (l *LDAPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) e if !ok { return osm.NotMatchedError() } - if respID != l.reqID { + if respID != m.reqID { return osm.NotMatchedError() } diff --git a/ldap/ldap_test.go b/ldap/ldap_test.go index 67d23e8..c19aa98 100644 --- a/ldap/ldap_test.go +++ b/ldap/ldap_test.go @@ -18,11 +18,12 @@ func TestLdap(t *testing.T) { if err != nil { t.Errorf("ERR %s", err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.10", 10389) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -34,8 +35,8 @@ func TestLdap(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } func TestBer(t *testing.T) { diff --git a/lpd/lpd.go b/lpd/lpd.go index 81bfd76..b3024d0 100644 --- a/lpd/lpd.go +++ b/lpd/lpd.go @@ -6,34 +6,29 @@ import ( type LPDMatcher struct { osm.Matchers - meta osm.Metadata } -func (l *LPDMatcher) Key() string { +func (m *LPDMatcher) Key() string { return "LPD" } -func (l *LPDMatcher) Meta() osm.Metadata { - return l.meta -} - -func (l *LPDMatcher) Name() string { +func (m *LPDMatcher) Name(matchCtx *osm.MatchCtx) string { return "LPD (Printer)" } -func (l *LPDMatcher) IsPrePacket() bool { +func (m *LPDMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (l *LPDMatcher) HasResponse(index int) bool { +func (m *LPDMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (l *LPDMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *LPDMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (l *LPDMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *LPDMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -47,7 +42,6 @@ func (l *LPDMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) er } func NewMatcher() osm.Matcher { - m := &LPDMatcher{} reqStr := "GET / HTTP/1.1\r\n\r\n" rbyte := make([]byte, len(reqStr)) diff --git a/lpd/lpd_test.go b/lpd/lpd_test.go index a3d3320..6307ddc 100644 --- a/lpd/lpd_test.go +++ b/lpd/lpd_test.go @@ -14,11 +14,12 @@ func TestLDP(t *testing.T) { if err != nil { t.Errorf("ERR %s", err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.99", 515) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -30,7 +31,7 @@ func TestLDP(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/matcher.go b/matcher.go index 12d5b86..653da63 100644 --- a/matcher.go +++ b/matcher.go @@ -4,22 +4,14 @@ import "fmt" type Matcher interface { Key() string - Name() string + Name(matchCtx *MatchCtx) string - Meta() Metadata + IsPrePacket(matchCtx *MatchCtx) bool + PacketCount(matchCtx *MatchCtx) int + Packet(matchCtx *MatchCtx, index int) *Packet + HasResponse(matchCtx *MatchCtx, index int) bool - IsPrePacket() bool - PacketCount() int - Packet(index int) *Packet - HasResponse(index int) bool - - Match(info MatchInfo, index int, packet *Packet) error -} - -type Metadata map[string]string - -func NewMetadata() Metadata { - return make(map[string]string) + Match(matchCtx *MatchCtx, index int, packet *Packet) error } type UDPMatcher interface { @@ -31,15 +23,15 @@ type Matchers struct { packets []*Packet } -func (m *Matchers) PacketCount() int { +func (m *Matchers) PacketCount(matchCtx *MatchCtx) int { return len(m.packets) } -func (m *Matchers) Packet(index int) *Packet { +func (m *Matchers) Packet(matchCtx *MatchCtx, index int) *Packet { return m.packets[index] } -func (m *Matchers) HasResponse(index int) bool { +func (m *Matchers) HasResponse(matchCtx *MatchCtx, index int) bool { return len(m.packets)-1 > index } @@ -47,28 +39,39 @@ func (m *Matchers) AddPacket(packet *Packet) { m.packets = append(m.packets, packet) } -type MatchInfo interface { - IP() string - Port() int +type MatchCtx struct { + address string + port int + attributes map[string]interface{} } -type simpleMatchInfo struct { - ip string - port int +func (mc *MatchCtx) Address() string { + return mc.address } -func (mi *simpleMatchInfo) IP() string { - return mi.ip +func (mc *MatchCtx) Port() int { + return mc.port } -func (mi *simpleMatchInfo) Port() int { - return mi.port +func (mc *MatchCtx) GetAttribute(key string) (value interface{}, ok bool) { + value, ok = mc.attributes[key] + + return } -func NewMatchInfo(ip string, port int) MatchInfo { - return &simpleMatchInfo{ - ip: ip, - port: port, +func (mc *MatchCtx) SetAttribute(key string, value interface{}) { + mc.attributes[key] = value +} + +func (mc *MatchCtx) InitAttribute() { + mc.attributes = make(map[string]interface{}) +} + +func NewMatchCtx(address string, port int) *MatchCtx { + return &MatchCtx{ + address: address, + port: port, + attributes: make(map[string]interface{}), } } diff --git a/mongodb/mongodb.go b/mongodb/mongodb.go index 608ad3e..9689231 100644 --- a/mongodb/mongodb.go +++ b/mongodb/mongodb.go @@ -48,34 +48,29 @@ type OP_reply struct { type MongoDBMatcher struct { osm.Matchers - meta osm.Metadata } func (m *MongoDBMatcher) Key() string { return "MONGODB" } -func (m *MongoDBMatcher) Name() string { +func (m *MongoDBMatcher) Name(matchCtx *osm.MatchCtx) string { return "MongoDB" } -func (m *MongoDBMatcher) Meta() osm.Metadata { - return m.meta -} - -func (m *MongoDBMatcher) IsPrePacket() bool { +func (m *MongoDBMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (m *MongoDBMatcher) HasResponse(index int) bool { +func (m *MongoDBMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (m *MongoDBMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *MongoDBMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (m *MongoDBMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *MongoDBMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -108,7 +103,6 @@ func (m *MongoDBMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet func NewMatcher() osm.Matcher { mm := &MongoDBMatcher{} - mm.meta = osm.NewMetadata() tempBuf := new(bytes.Buffer) binary.Write(tempBuf, binary.BigEndian, OP_request{}) diff --git a/mongodb/mongodb_test.go b/mongodb/mongodb_test.go index 487e4e0..c38a2f1 100644 --- a/mongodb/mongodb_test.go +++ b/mongodb/mongodb_test.go @@ -10,16 +10,20 @@ import ( func TestMongoNor(t *testing.T) { - conn, _ := net.Dial("tcp", "192.168.1.229:27036") - + conn, err := net.Dial("tcp", "192.168.1.229:27036") + if err != nil { + t.Errorf("ERR %s", err) + } defer conn.Close() - MongoRun(conn, t) + matchCtx := osm.NewMatchCtx("192.168.1.229", 27036) + + MongoRun(matchCtx, conn, t) } func TestMongoTLS(t *testing.T) { - conn, _ := tls.Dial( + conn, err := tls.Dial( "tcp", "192.168.1.229:27036", &tls.Config{ @@ -27,19 +31,23 @@ func TestMongoTLS(t *testing.T) { ServerName: "192.168.1.229", }, ) - + if err != nil { + t.Errorf("ERR %s", err) + } defer conn.Close() - MongoRun(conn, t) + matchCtx := osm.NewMatchCtx("192.168.1.229", 27036) + + MongoRun(matchCtx, conn, t) } -func MongoRun(conn net.Conn, t *testing.T) { +func MongoRun(matchCtx *osm.MatchCtx, conn net.Conn, t *testing.T) { m := NewMatcher() - for i := 0; i < m.PacketCount(); i++ { + for i := 0; i < m.PacketCount(matchCtx); i++ { - pack := m.Packet(i) + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) @@ -50,6 +58,6 @@ func MongoRun(conn net.Conn, t *testing.T) { } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/mysql/mysql.go b/mysql/mysql.go index d3ec011..bb76a18 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -11,37 +11,32 @@ import ( type MySqlMatcher struct { osm.Matchers - meta osm.Metadata } func (m *MySqlMatcher) Key() string { return "MYSQL" } -func (m *MySqlMatcher) Name() string { +func (m *MySqlMatcher) Name(matchCtx *osm.MatchCtx) string { name := "MySQL" - if v, ok := m.meta["version"]; ok { - if strings.Contains(v, "MariaDB") { + if v, ok := matchCtx.GetAttribute("version"); ok { + if strings.Contains(v.(string), "MariaDB") { name = "MariaDB" } - name = name + " (" + v + ")" + name = name + " (" + v.(string) + ")" } return name } -func (m *MySqlMatcher) Meta() osm.Metadata { - return m.meta -} - -func (m *MySqlMatcher) IsPrePacket() bool { +func (m *MySqlMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return true } -func (m *MySqlMatcher) HasResponse(index int) bool { +func (m *MySqlMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (m *MySqlMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *MySqlMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } @@ -55,7 +50,7 @@ type serverSettings struct { keepalive int64 } -func (m *MySqlMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *MySqlMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() } @@ -76,7 +71,7 @@ func (m *MySqlMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) if err != nil { return osm.NotMatchedError() } - m.meta["version"] = string(slice) + matchCtx.SetAttribute("version", string(slice)) pos += len(slice) + 1 p.threadID = bytesToUint32(packet.Buffer[pos : pos+4]) @@ -105,6 +100,6 @@ func bytesToUint32(b []byte) (n uint32) { func NewMatcher() osm.Matcher { m := &MySqlMatcher{} - m.meta = osm.NewMetadata() + return m } diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index 54ad37a..bc083ba 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -11,10 +11,14 @@ func TestMySql(t *testing.T) { m := NewMatcher() - conn, _ := net.Dial("tcp", "192.168.1.201:23306") - + conn, err := net.Dial("tcp", "192.168.1.201:23306") + if err != nil { + t.Errorf("ERR %s", err) + } defer conn.Close() + matchCtx := osm.NewMatchCtx("192.168.1.201", 23306) + bytes := make([]byte, 1024) n, _ := conn.Read(bytes) p := osm.NewPacket(bytes, n) @@ -22,7 +26,7 @@ func TestMySql(t *testing.T) { if err := m.Match(nil, 0, p); err != nil { t.Error(err) } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/nbss/nbss.go b/nbss/nbss.go index bb92924..c5d0b04 100644 --- a/nbss/nbss.go +++ b/nbss/nbss.go @@ -29,31 +29,27 @@ type NBSSMatcher struct { osm.Matchers } -func (t *NBSSMatcher) Key() string { +func (m *NBSSMatcher) Key() string { return "NBSS" } -func (t *NBSSMatcher) Name() string { +func (m *NBSSMatcher) Name(matchCtx *osm.MatchCtx) string { return "NBSS" } -func (t *NBSSMatcher) Meta() osm.Metadata { - return nil -} - -func (t *NBSSMatcher) IsPrePacket() bool { +func (m *NBSSMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (t *NBSSMatcher) HasResponse(index int) bool { +func (m *NBSSMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (t *NBSSMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *NBSSMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (t *NBSSMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *NBSSMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() diff --git a/nbss/nbss_test.go b/nbss/nbss_test.go index 123fc78..828c340 100644 --- a/nbss/nbss_test.go +++ b/nbss/nbss_test.go @@ -11,13 +11,17 @@ func TestNBSS(t *testing.T) { m := NewMatcher() - conn, _ := net.Dial("tcp", "192.168.1.102:139") - + conn, err := net.Dial("tcp", "192.168.1.102:139") + if err != nil { + t.Errorf("ERR %s", err) + } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { + matchCtx := osm.NewMatchCtx("192.168.1.102", 139) - pack := m.Packet(i) + for i := 0; i < m.PacketCount(matchCtx); i++ { + + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) @@ -27,6 +31,6 @@ func TestNBSS(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/oracle/oracle.go b/oracle/oracle.go index 6402049..6140e7c 100644 --- a/oracle/oracle.go +++ b/oracle/oracle.go @@ -11,30 +11,26 @@ type OracleMatcher struct { osm.Matchers } -func (o *OracleMatcher) Key() string { +func (m *OracleMatcher) Key() string { return "ORACLE" } -func (o *OracleMatcher) Name() string { +func (m *OracleMatcher) Name(matchCtx *osm.MatchCtx) string { return "Oracle" } -func (o *OracleMatcher) Meta() osm.Metadata { - return nil -} - -func (o *OracleMatcher) IsPrePacket() bool { +func (m *OracleMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (o *OracleMatcher) HasResponse(index int) bool { +func (m *OracleMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (o *OracleMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *OracleMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (o *OracleMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *OracleMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil { return osm.NotMatchedError() @@ -71,7 +67,7 @@ func (o *OracleMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) return osm.NotMatchedError() } - var dataLen int = int(refuse.Data_len) + dataLen := int(refuse.Data_len) if dataLen != packet.Len-12 { // if dataLen != packet.Len-22 { // morformed packet error not user not service return osm.NotMatchedError() @@ -130,8 +126,8 @@ func NewMatcher() osm.Matcher { conDataStr := "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.30)(PORT=1521))(CONNECT_DATA=(CID=(PROGRAM=JDBC Thin Client)(HOST=__jdbc__)(USER=loafle.match))(SERVICE_NAME=oracle.loafle.com1)))" //conDataStr := "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.30)(PORT=1521))(CONNECT_DATA=(CID=(PROGRAM=JDBC Thin Client)(HOST=__jdbc__)(USER=Jackdaw))(SERVICE_NAME=oracle.loafle.co1m)))" - connect_data := make([]byte, len(conDataStr)) - copy(connect_data, conDataStr) + connectData := make([]byte, len(conDataStr)) + copy(connectData, conDataStr) hpBuf := new(bytes.Buffer) binary.Write(hpBuf, binary.BigEndian, hp) @@ -147,7 +143,7 @@ func NewMatcher() osm.Matcher { copy(sendByte[0:], hpBt) copy(sendByte[len(hpBt):], bcBt) - copy(sendByte[len(hpBt)+len(bcBt):], connect_data) + copy(sendByte[len(hpBt)+len(bcBt):], connectData) m.AddPacket(osm.NewPacket(sendByte, byteSize)) diff --git a/oracle/oracle_test.go b/oracle/oracle_test.go index ee75f2a..864679d 100644 --- a/oracle/oracle_test.go +++ b/oracle/oracle_test.go @@ -9,36 +9,38 @@ import ( func TestOracle(t *testing.T) { - lm := NewOracleMatcher() + lm := NewMatcher() //port := types.NewPort("1521", types.NewHost("192.168.1.30"), types.TYPE_TCP) //scanInfo := scaninfo.NewServiceScanInfo(port) //var ipport string //ipport = port.Host.Ip + ":" + string(port.Port) - client, _ := net.Dial("tcp", "192.168.1.15:1521") + conn, err := net.Dial("tcp", "192.168.1.15:1521") + if err != nil { + t.Errorf("ERR %s", err) + } + defer conn.Close() - defer client.Close() + matchCtx := osm.NewMatchCtx("192.168.1.15", 1521) - t.Log(lm.PacketCount()) + t.Log(lm.PacketCount(matchCtx)) - for ii := 0; ii < lm.PacketCount(); ii++ { + for ii := 0; ii < lm.PacketCount(matchCtx); ii++ { - pack := lm.Packet(ii) + pack := lm.Packet(matchCtx, ii) t.Log(pack) - client.Write(pack.Buffer) + conn.Write(pack.Buffer) bytes := make([]byte, 1024) - read, _ := client.Read(bytes) + read, _ := conn.Read(bytes) t.Log(bytes) - b := lm.Match(ii, osm.NewPacket(bytes, read), nil) - - if b { + if err := lm.Match(matchCtx, ii, osm.NewPacket(bytes, read)); nil == err { t.Log("Good") } diff --git a/pop/pop.go b/pop/pop.go index ce271a4..83f122a 100644 --- a/pop/pop.go +++ b/pop/pop.go @@ -12,31 +12,27 @@ type POPMatcher struct { osm.Matchers } -func (p *POPMatcher) Key() string { +func (m *POPMatcher) Key() string { return "POP3" } -func (p *POPMatcher) Name() string { +func (m *POPMatcher) Name(matchCtx *osm.MatchCtx) string { return "POP3" } -func (p *POPMatcher) Meta() osm.Metadata { - return nil -} - -func (p *POPMatcher) IsPrePacket() bool { +func (m *POPMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return true } -func (p *POPMatcher) HasResponse(index int) bool { +func (m *POPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (p *POPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *POPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (p *POPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *POPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { switch index { case 0: diff --git a/pop/pop_test.go b/pop/pop_test.go index e83f627..0f67cf2 100644 --- a/pop/pop_test.go +++ b/pop/pop_test.go @@ -11,7 +11,7 @@ import ( ) func TestPopTLS(t *testing.T) { - conn, _ := tls.Dial( + conn, err := tls.Dial( "tcp", "192.168.1.15:995", &tls.Config{ @@ -19,25 +19,33 @@ func TestPopTLS(t *testing.T) { ServerName: "192.168.1.15", }, ) - + if err != nil { + t.Errorf("ERR %s", err) + } defer conn.Close() - pop3Run(conn, t) + matchCtx := osm.NewMatchCtx("192.168.1.15", 995) + + pop3Run(matchCtx, conn, t) } func TestPopNor(t *testing.T) { - client, _ := net.Dial("tcp", "192.168.1.15:110") + conn, err := net.Dial("tcp", "192.168.1.15:110") + if err != nil { + t.Errorf("ERR %s", err) + } + defer conn.Close() - defer client.Close() + matchCtx := osm.NewMatchCtx("192.168.1.15", 110) - pop3Run(client, t) + pop3Run(matchCtx, conn, t) } -func pop3Run(client net.Conn, t *testing.T) { +func pop3Run(matchCtx *osm.MatchCtx, conn net.Conn, t *testing.T) { - lm := NewPOPMatcher() + lm := NewMatcher() //port := types.NewPort("110", types.NewHost("192.168.1.215"), types.TYPE_TCP) //scanInfo := scaninfo.NewServiceScanInfo(port) @@ -48,33 +56,29 @@ func pop3Run(client net.Conn, t *testing.T) { bytett := make([]byte, 1024) - read, _ := client.Read(bytett) + read, _ := conn.Read(bytett) - bb := lm.Match(0, osm.NewPacket(bytett, read), nil) - - if bb { + if err := lm.Match(matchCtx, 0, osm.NewPacket(bytett, read)); nil == err { t.Log("good!") } - fmt.Println(lm.PacketCount()) + fmt.Println(lm.PacketCount(matchCtx)) - for ii := 0; ii < lm.PacketCount(); ii++ { + for ii := 0; ii < lm.PacketCount(matchCtx); ii++ { - pack := lm.Packet(ii) + pack := lm.Packet(matchCtx, ii) //fmt.Println(pack) - client.Write(pack.Buffer) + conn.Write(pack.Buffer) bytes := make([]byte, 1024) - rr, _ := client.Read(bytes) + rr, _ := conn.Read(bytes) //fmt.Println(bytes) - b := lm.Match(ii+1, osm.NewPacket(bytes, rr), nil) - - if b { + if err := lm.Match(matchCtx, ii+1, osm.NewPacket(bytes, rr)); nil == err { t.Log("send Good!") } diff --git a/postgresql/postgresql.go b/postgresql/postgresql.go index fba5ebd..e2b0cff 100644 --- a/postgresql/postgresql.go +++ b/postgresql/postgresql.go @@ -39,34 +39,29 @@ type pgsqlErrResponse struct { type PostgreSQLMatcher struct { osm.Matchers - meta osm.Metadata } -func (p *PostgreSQLMatcher) Key() string { +func (m *PostgreSQLMatcher) Key() string { return "POSTGRESQL" } -func (p *PostgreSQLMatcher) Name() string { +func (m *PostgreSQLMatcher) Name(matchCtx *osm.MatchCtx) string { return "PostgreSQL" } -func (p *PostgreSQLMatcher) Meta() osm.Metadata { - return p.meta -} - -func (p *PostgreSQLMatcher) IsPrePacket() bool { +func (m *PostgreSQLMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (p *PostgreSQLMatcher) HasResponse(index int) bool { +func (m *PostgreSQLMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (p *PostgreSQLMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *PostgreSQLMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return true } -func (p *PostgreSQLMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *PostgreSQLMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -92,8 +87,8 @@ func (p *PostgreSQLMatcher) Match(info osm.MatchInfo, index int, packet *osm.Pac data := string(pg.Data[:]) splits := strings.Split(data, "\x00") - var findSeverity bool = false - var findErrorCode bool = false + findSeverity := false + findErrorCode := false for _, s := range splits { if strings.Contains(s, "FATAL") { findSeverity = true diff --git a/postgresql/postgresql_test.go b/postgresql/postgresql_test.go index 6a63c78..cb99f5d 100644 --- a/postgresql/postgresql_test.go +++ b/postgresql/postgresql_test.go @@ -17,9 +17,11 @@ func TestPG(t *testing.T) { } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { + matchCtx := osm.NewMatchCtx("192.168.1.229", 5432) - pack := m.Packet(i) + for i := 0; i < m.PacketCount(matchCtx); i++ { + + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) @@ -29,6 +31,6 @@ func TestPG(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/redis/redis.go b/redis/redis.go index 74b2c2b..7f9147d 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -13,46 +13,43 @@ const REDIS_QUIT string = "*1\r\n$4\r\nQUIT\r\n" type RedisMatcher struct { osm.Matchers - meta osm.Metadata - - protected bool } -func (r *RedisMatcher) Key() string { +func (m *RedisMatcher) Key() string { return "REDIS" } -func (r *RedisMatcher) Name() string { +func (m *RedisMatcher) Name(matchCtx *osm.MatchCtx) string { name := "Redis" - if r.protected { - return name + " (protected)" + + if v, ok := matchCtx.GetAttribute("protected"); ok { + if v.(bool) { + return name + " (protected)" + } } - if v, ok := r.meta["redis_mode"]; ok { - name = name + " " + v + + if v, ok := matchCtx.GetAttribute("redis_mode"); ok { + name = name + " " + v.(string) } - if v, ok := r.meta["redis_version"]; ok { - name = name + " (" + v + ")" + if v, ok := matchCtx.GetAttribute("redis_version"); ok { + name = name + " (" + v.(string) + ")" } return name } -func (r *RedisMatcher) Meta() osm.Metadata { - return r.meta -} - -func (r *RedisMatcher) IsPrePacket() bool { +func (m *RedisMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (r *RedisMatcher) HasResponse(index int) bool { +func (m *RedisMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (r *RedisMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *RedisMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (r *RedisMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *RedisMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -80,16 +77,21 @@ func (r *RedisMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) return nil } } - r.protected = r.checkProtectedMode(packet) - if r.protected { + + protected := m.checkProtectedMode(packet) + + matchCtx.SetAttribute("protected", protected) + if protected { return nil } case 1: // INFO + response := string(packet.Bytes()) - info := string(packet.Bytes()) - if !r.protected { - r.parseInfo(info) + if v, ok := matchCtx.GetAttribute("protected"); ok { + if !v.(bool) { + m.parseResponse(matchCtx, response) + } } return nil case 2: @@ -104,7 +106,7 @@ func (r *RedisMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) return osm.NotMatchedError() } -func (r *RedisMatcher) checkProtectedMode(packet *osm.Packet) bool { +func (m *RedisMatcher) checkProtectedMode(packet *osm.Packet) bool { var ( compareSign = "-" compareMsg = "DENIED" @@ -128,13 +130,11 @@ func (r *RedisMatcher) checkProtectedMode(packet *osm.Packet) bool { return false } - r.protected = true - return true } -func (r *RedisMatcher) parseInfo(info string) { - scanner := bufio.NewScanner(strings.NewReader(info)) +func (m *RedisMatcher) parseResponse(matchCtx *osm.MatchCtx, response string) { + scanner := bufio.NewScanner(strings.NewReader(response)) for scanner.Scan() { line := scanner.Text() if strings.Compare(line, "") == 0 { @@ -143,23 +143,25 @@ func (r *RedisMatcher) parseInfo(info string) { if len(line) > 0 && strings.Contains(line, ":") { kv := strings.Split(line, ":") if len(kv[0]) > 0 && len(kv[1]) > 0 { - r.meta[kv[0]] = kv[1] + matchCtx.SetAttribute(kv[0], kv[1]) } } } } -func (r *RedisMatcher) PacketCount() int { - if r.protected { - return 1 +func (m *RedisMatcher) PacketCount(matchCtx *osm.MatchCtx) int { + if v, ok := matchCtx.GetAttribute("protected"); ok { + if v.(bool) { + return 1 + } } + return 3 } func NewMatcher() osm.Matcher { m := &RedisMatcher{} - m.meta = osm.NewMetadata() m.AddPacket(osm.NewPacket([]byte(REDIS_PING), len(REDIS_PING))) m.AddPacket(osm.NewPacket([]byte(REDIS_INFO), len(REDIS_INFO))) diff --git a/redis/redis_test.go b/redis/redis_test.go index 1250a12..d136755 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -16,11 +16,17 @@ func TestRedisMatcher(t *testing.T) { m := NewMatcher() - conn, _ := net.Dial("tcp", ADDR) + conn, err := net.Dial("tcp", ADDR) + if err != nil { + t.Error(err) + return + } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.229", 6379) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -33,7 +39,7 @@ func TestRedisMatcher(t *testing.T) { return } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/rmi/rmi.go b/rmi/rmi.go index d602a92..3fb0aff 100644 --- a/rmi/rmi.go +++ b/rmi/rmi.go @@ -31,31 +31,27 @@ type RMIMatcher struct { osm.Matchers } -func (r *RMIMatcher) Key() string { +func (m *RMIMatcher) Key() string { return "RMI" } -func (r *RMIMatcher) Name() string { +func (m *RMIMatcher) Name(matchCtx *osm.MatchCtx) string { return "RMI" } -func (r *RMIMatcher) Meta() osm.Metadata { - return nil -} - -func (r *RMIMatcher) IsPrePacket() bool { +func (m *RMIMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (r *RMIMatcher) HasResponse(index int) bool { +func (m *RMIMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (r *RMIMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *RMIMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (r *RMIMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *RMIMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() diff --git a/rmi/rmi_test.go b/rmi/rmi_test.go index d1a59f8..132d23b 100644 --- a/rmi/rmi_test.go +++ b/rmi/rmi_test.go @@ -18,8 +18,10 @@ func TestRMIMatcher_Match(t *testing.T) { } defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.102", 9840) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -31,6 +33,6 @@ func TestRMIMatcher_Match(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/smb/smb.go b/smb/smb.go index 152cb7b..5351402 100644 --- a/smb/smb.go +++ b/smb/smb.go @@ -52,31 +52,27 @@ type SMBMatcher struct { osm.Matchers } -func (t *SMBMatcher) Key() string { +func (m *SMBMatcher) Key() string { return "SMB" } -func (t *SMBMatcher) Name() string { +func (m *SMBMatcher) Name(matchCtx *osm.MatchCtx) string { return "SMB" } -func (t *SMBMatcher) Meta() osm.Metadata { - return nil -} - -func (t *SMBMatcher) IsPrePacket() bool { +func (m *SMBMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (t *SMBMatcher) HasResponse(index int) bool { +func (m *SMBMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (t *SMBMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *SMBMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (t *SMBMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *SMBMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() diff --git a/smb/smb_test.go b/smb/smb_test.go index a45a323..7bafff4 100644 --- a/smb/smb_test.go +++ b/smb/smb_test.go @@ -16,16 +16,17 @@ func TestSMBMatcher(t *testing.T) { m := NewMatcher() conn, err := net.Dial("tcp", ADDR) - if err != nil { - t.Fatal(err) + t.Error(err) + return } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { + matchCtx := osm.NewMatchCtx("192.168.1.101", 445) - pack := m.Packet(i) + for i := 0; i < m.PacketCount(matchCtx); i++ { + + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) @@ -36,6 +37,6 @@ func TestSMBMatcher(t *testing.T) { t.Error(err) } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/smtp/smtp.go b/smtp/smtp.go index 8e3a984..ac996a9 100644 --- a/smtp/smtp.go +++ b/smtp/smtp.go @@ -10,31 +10,27 @@ type SmtpMatcher struct { osm.Matchers } -func (t *SmtpMatcher) Key() string { +func (m *SmtpMatcher) Key() string { return "SMTP" } -func (t *SmtpMatcher) Name() string { +func (m *SmtpMatcher) Name(matchCtx *osm.MatchCtx) string { return "SMTP" } -func (t *SmtpMatcher) Meta() osm.Metadata { - return nil -} - -func (t *SmtpMatcher) IsPrePacket() bool { +func (m *SmtpMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return true } -func (t *SmtpMatcher) HasResponse(index int) bool { +func (m *SmtpMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (t *SmtpMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *SmtpMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (t *SmtpMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *SmtpMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil { return osm.NoPacketReceivedError() } diff --git a/smtp/smtp_test.go b/smtp/smtp_test.go index eddcc8f..96bc91d 100644 --- a/smtp/smtp_test.go +++ b/smtp/smtp_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + osm "git.loafle.net/overflow/service_matcher-go" "github.com/stretchr/testify/assert" ) @@ -19,35 +20,41 @@ func TestSMTPTLS(t *testing.T) { ServerName: "192.168.1.215", }, ) - if err != nil { - t.Log(err) + t.Error(err) return } + defer conn.Close() + + matchCtx := osm.NewMatchCtx("192.168.1.215", 465) b := make([]byte, 1024) - check(t, b, conn, "", "220") - check(t, b, conn, "helo test\r\n", "250") - check(t, b, conn, "quit\r\n", "221") + check(matchCtx, t, b, conn, "", "220") + check(matchCtx, t, b, conn, "helo test\r\n", "250") + check(matchCtx, t, b, conn, "quit\r\n", "221") - conn.Close() } func TestSMTP(t *testing.T) { - conn, _ := net.Dial("tcp", "192.168.1.15:25") + conn, err := net.Dial("tcp", "192.168.1.15:25") + if err != nil { + t.Error(err) + return + } + defer conn.Close() + + matchCtx := osm.NewMatchCtx("192.168.1.15", 25) b := make([]byte, 1024) - check(t, b, conn, "", "220") - check(t, b, conn, "helo test\r\n", "250") - check(t, b, conn, "quit\r\n", "221") - - conn.Close() + check(matchCtx, t, b, conn, "", "220") + check(matchCtx, t, b, conn, "helo test\r\n", "250") + check(matchCtx, t, b, conn, "quit\r\n", "221") } -func check(t *testing.T, b []byte, conn net.Conn, cmd string, compare string) { +func check(matchCtx *osm.MatchCtx, t *testing.T, b []byte, conn net.Conn, cmd string, compare string) { if cmd != "" { wlen, _ := conn.Write([]byte(cmd)) diff --git a/snmp/v2/snmpv2.go b/snmp/v2/snmpv2.go index 01be818..e1927c6 100644 --- a/snmp/v2/snmpv2.go +++ b/snmp/v2/snmpv2.go @@ -39,32 +39,27 @@ var ( type SNMPMatcher struct { osm.Matchers + requestID int32 - meta osm.Metadata } func (s *SNMPMatcher) Key() string { return "SNMP" } -func (s *SNMPMatcher) Name() string { +func (s *SNMPMatcher) Name(matchCtx *osm.MatchCtx) string { return "SNMP" } -func (s *SNMPMatcher) Meta() osm.Metadata { - - return s.meta -} - -func (s *SNMPMatcher) IsPrePacket() bool { +func (s *SNMPMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (s *SNMPMatcher) HasResponse(index int) bool { +func (s *SNMPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (s *SNMPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (s *SNMPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -100,7 +95,7 @@ func (s *SNMPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) e continue } // if binding.Name.String() == "1.3.6.1.2.1.1.5.0" { - s.meta[binding.Name.String()] = string(binding.Value.Bytes) + matchCtx.SetAttribute(binding.Name.String(), string(binding.Value.Bytes)) // } } @@ -119,8 +114,6 @@ func (s *SNMPMatcher) IsSend(port int) bool { func NewMatcher() osm.UDPMatcher { m := &SNMPMatcher{} - m.meta = osm.NewMetadata() - m.requestID = rand.Int31() p := snmpv2{} diff --git a/snmp/v2/snmpv2_test.go b/snmp/v2/snmpv2_test.go index 97fb75a..235a3bd 100644 --- a/snmp/v2/snmpv2_test.go +++ b/snmp/v2/snmpv2_test.go @@ -18,7 +18,9 @@ func TestSNMPv2(t *testing.T) { } defer conn.Close() - pack := m.Packet(0) + matchCtx := osm.NewMatchCtx("192.168.1.99", 161) + + pack := m.Packet(matchCtx, 0) conn.Write(pack.Buffer) bytes := make([]byte, 1024) @@ -28,5 +30,5 @@ func TestSNMPv2(t *testing.T) { if err := m.Match(nil, 0, p); err != nil { t.Error(err) } - t.Log(m.Meta()) + t.Log(matchCtx) } diff --git a/sqlserver/sqlserver.go b/sqlserver/sqlserver.go index 63e4469..e2b0d2b 100644 --- a/sqlserver/sqlserver.go +++ b/sqlserver/sqlserver.go @@ -68,34 +68,30 @@ type SQLServerMatcher struct { isSSL bool } -func (t *SQLServerMatcher) Key() string { +func (m *SQLServerMatcher) Key() string { return "SQLSERVER" } -func (t *SQLServerMatcher) Name() string { - if t.isSSL { +func (m *SQLServerMatcher) Name(matchCtx *osm.MatchCtx) string { + if m.isSSL { return "SQL Server (SSL)" } return "SQL Server" } -func (t *SQLServerMatcher) Meta() osm.Metadata { - return nil -} - -func (t *SQLServerMatcher) IsPrePacket() bool { +func (m *SQLServerMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (t *SQLServerMatcher) HasResponse(index int) bool { +func (m *SQLServerMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (t *SQLServerMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *SQLServerMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (t *SQLServerMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *SQLServerMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil { return osm.NoPacketReceivedError() @@ -104,30 +100,30 @@ func (t *SQLServerMatcher) Match(info osm.MatchInfo, index int, packet *osm.Pack reader := new(bytes.Buffer) reader.Write(packet.Buffer) - m := mssqlResponse{} + res := mssqlResponse{} if err := binary.Read(reader, binary.BigEndian, &m); err != nil { return osm.NotMatchedError() } - if m.Type_ != HEADER_TYPE_RESPONSE { + if res.Type_ != HEADER_TYPE_RESPONSE { return osm.NotMatchedError() } - if m.Length != uint16(packet.Len) { + if res.Length != uint16(packet.Len) { return osm.NotMatchedError() } - switch m.PreLoginResp.Msg[m.Length-9 : m.Length-8][0] { + switch res.PreLoginResp.Msg[res.Length-9 : res.Length-8][0] { case 0: return nil case 1: - t.isSSL = true + matchCtx.SetAttribute("isSSL", true) return nil case 2: return nil case 3: - t.isSSL = true + matchCtx.SetAttribute("isSSL", true) return nil default: return osm.NotMatchedError() diff --git a/sqlserver/sqlserver_test.go b/sqlserver/sqlserver_test.go index 892cabc..a71841d 100644 --- a/sqlserver/sqlserver_test.go +++ b/sqlserver/sqlserver_test.go @@ -13,11 +13,16 @@ import ( */ func TestSqlNor(t *testing.T) { - conn, _ := net.Dial("tcp", "192.168.1.16:11433") - + conn, err := net.Dial("tcp", "192.168.1.16:11433") + if err != nil { + t.Error(err) + return + } defer conn.Close() - sqlServerRun(conn, t) + matchCtx := osm.NewMatchCtx("192.168.1.16", 11433) + + sqlServerRun(matchCtx, conn, t) } @@ -41,19 +46,19 @@ func TestSqlNor(t *testing.T) { // sqlServerRun(conn, t) //} -func sqlServerRun(conn net.Conn, t *testing.T) { +func sqlServerRun(matchCtx *osm.MatchCtx, conn net.Conn, t *testing.T) { m := NewMatcher() - for i := 0; i < m.PacketCount(); i++ { + for i := 0; i < m.PacketCount(matchCtx); i++ { - pack := m.Packet(i) + pack := m.Packet(matchCtx, i) conn.Write(pack.Buffer) bytes := make([]byte, 1024) n, _ := conn.Read(bytes) p := osm.NewPacket(bytes, n) - if m.Match(nil, i, p) { + if err := m.Match(matchCtx, i, p); nil != err { t.Log(m.Key()) return } diff --git a/ssh/ssh.go b/ssh/ssh.go index ea7054b..5780c02 100644 --- a/ssh/ssh.go +++ b/ssh/ssh.go @@ -10,38 +10,33 @@ import ( type SSHMatcher struct { osm.Matchers - meta osm.Metadata } -func (ssh *SSHMatcher) Key() string { +func (m *SSHMatcher) Key() string { return "SSH" } -func (ssh *SSHMatcher) Name() string { +func (m *SSHMatcher) Name(matchCtx *osm.MatchCtx) string { name := "SSH" - if v, ok := ssh.meta["softwareversion"]; ok { - name = name + " (" + v + ")" + if v, ok := matchCtx.GetAttribute("softwareversion"); ok { + name = name + " (" + v.(string) + ")" } return name } -func (ssh *SSHMatcher) Meta() osm.Metadata { - return ssh.meta -} - -func (ssh *SSHMatcher) IsPrePacket() bool { +func (m *SSHMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return true } -func (ssh *SSHMatcher) HasResponse(index int) bool { +func (m *SSHMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (ssh *SSHMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *SSHMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (ssh *SSHMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *SSHMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() @@ -64,11 +59,11 @@ func (ssh *SSHMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) softwareversion := versions[2] if strings.HasPrefix(protoversion, "1") || strings.HasPrefix(protoversion, "2") { - ssh.meta["protoversion"] = protoversion - ssh.meta["softwareversion"] = softwareversion + matchCtx.SetAttribute("protoversion", protoversion) + matchCtx.SetAttribute("softwareversion", softwareversion) if len(temp) > 1 { - ssh.meta["comments"] = temp[1] + matchCtx.SetAttribute("comments", temp[1]) } return nil } @@ -80,6 +75,6 @@ func (ssh *SSHMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) func NewMatcher() osm.Matcher { m := &SSHMatcher{} - m.meta = osm.NewMetadata() + return m } diff --git a/ssh/ssh_test.go b/ssh/ssh_test.go index 81bf256..0a5a5cd 100644 --- a/ssh/ssh_test.go +++ b/ssh/ssh_test.go @@ -15,11 +15,12 @@ func TestSSH(t *testing.T) { if err != nil { t.Error(err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.10", 22) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -32,6 +33,6 @@ func TestSSH(t *testing.T) { return } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/telnet/telnet.go b/telnet/telnet.go index c61e705..7e132a5 100644 --- a/telnet/telnet.go +++ b/telnet/telnet.go @@ -16,31 +16,27 @@ type TelnetMatcher struct { osm.Matchers } -func (tel *TelnetMatcher) Key() string { +func (m *TelnetMatcher) Key() string { return "TELNET" } -func (tel *TelnetMatcher) Name() string { +func (m *TelnetMatcher) Name(matchCtx *osm.MatchCtx) string { return "Telnet" } -func (tel *TelnetMatcher) Meta() osm.Metadata { - return nil -} - -func (tel *TelnetMatcher) IsPrePacket() bool { +func (m *TelnetMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return true } -func (tel *TelnetMatcher) HasResponse(index int) bool { +func (m *TelnetMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (tel *TelnetMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *TelnetMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (tel *TelnetMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *TelnetMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil || !packet.Valid() { return osm.NoPacketReceivedError() diff --git a/telnet/telnet_test.go b/telnet/telnet_test.go index a57d1f2..efb975c 100644 --- a/telnet/telnet_test.go +++ b/telnet/telnet_test.go @@ -15,11 +15,12 @@ func TestTelnetMatcher_Match(t *testing.T) { if err != nil { t.Error(err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.201", 23) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -32,7 +33,7 @@ func TestTelnetMatcher_Match(t *testing.T) { return } } - t.Log(m.Name()) - t.Log(m.Meta()) + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) } diff --git a/wmi/wmi.go b/wmi/wmi.go index a39a7ed..3db2a47 100644 --- a/wmi/wmi.go +++ b/wmi/wmi.go @@ -19,34 +19,29 @@ const ( type WMIMatcher struct { osm.Matchers - meta osm.Metadata } -func (w *WMIMatcher) Key() string { +func (m *WMIMatcher) Key() string { return "WMI" } -func (w *WMIMatcher) Name() string { +func (m *WMIMatcher) Name(matchCtx *osm.MatchCtx) string { return "WMI" } -func (w *WMIMatcher) Meta() osm.Metadata { - return w.meta -} - -func (w *WMIMatcher) IsPrePacket() bool { +func (m *WMIMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool { return false } -func (w *WMIMatcher) HasResponse(index int) bool { +func (m *WMIMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool { return true } -func (w *WMIMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool { +func (m *WMIMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool { return false } -func (w *WMIMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { +func (m *WMIMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error { if packet == nil { return osm.NoPacketReceivedError() diff --git a/wmi/wmi_test.go b/wmi/wmi_test.go index f9df46b..3b53500 100644 --- a/wmi/wmi_test.go +++ b/wmi/wmi_test.go @@ -16,11 +16,12 @@ func TestWMI(t *testing.T) { if err != nil { t.Error(err) } - defer conn.Close() - for i := 0; i < m.PacketCount(); i++ { - _, err := conn.Write(m.Packet(i).Buffer) + matchCtx := osm.NewMatchCtx("192.168.1.200", 135) + + for i := 0; i < m.PacketCount(matchCtx); i++ { + _, err := conn.Write(m.Packet(matchCtx, i).Buffer) if err != nil { t.Error(err) } @@ -33,7 +34,6 @@ func TestWMI(t *testing.T) { return } } - t.Log(m.Name()) - t.Log(m.Meta()) - + t.Log(m.Name(matchCtx)) + t.Log(matchCtx) }