From ce43ef35507d1aee690d9f9d7d1dcfecf5fc213d Mon Sep 17 00:00:00 2001 From: "insanity@loafle.com" Date: Tue, 11 Apr 2017 19:44:17 +0900 Subject: [PATCH] pgsql matcher bug fix --- matcher/pgsql/pgsql.go | 40 +++++++++++++++++++++++++++++++------ matcher/pgsql/pgsql_test.go | 2 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/matcher/pgsql/pgsql.go b/matcher/pgsql/pgsql.go index 403d12b..55d769a 100644 --- a/matcher/pgsql/pgsql.go +++ b/matcher/pgsql/pgsql.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "loafle.com/overflow/commons_go/matcher/packet" "loafle.com/overflow/commons_go/model/scaninfo" + "strings" ) const ( @@ -40,6 +41,18 @@ type pgsqlErrResponse struct { Message [53]byte } +type pgsqlErrResponse2 struct { + ResponseType uint8 + Len [4]byte + Data [128]byte + //Severity [6]byte + //_ byte + //Code [6]byte + //_ byte + //Message [53]byte +} + + type PostgreSQLMatcher struct { packets []*packet.Packet } @@ -133,7 +146,7 @@ func (t *PostgreSQLMatcher) Match(index int, packet *packet.Packet, info scaninf reader := new(bytes.Buffer) reader.Write(packet.Buffer) - pg := pgsqlErrResponse{} + pg := pgsqlErrResponse2{} if err := binary.Read(reader, binary.BigEndian, &pg); err != nil { return false } @@ -142,14 +155,29 @@ func (t *PostgreSQLMatcher) Match(index int, packet *packet.Packet, info scaninf return false } - severity := string(pg.Severity[1:]) - if severity != "FATAL" && severity != "ERROR" { + length := binary.BigEndian.Uint32(pg.Len[:]) + if length + 1 != uint32(packet.Len) { + return false; + } + + data := string(pg.Data[:]) + splits := strings.Split(data, "\x00") + + var findSeverity bool = false + var findErrorCode bool = false + for _, s := range splits { + if strings.Contains(s, "FATAL") { + findSeverity = true; + } + if strings.Contains(s, "28000") { + findErrorCode = true; + } + } + + if !findSeverity || !findErrorCode { return false } - if string(pg.Code[1:]) != "28000" { - return false - } return true diff --git a/matcher/pgsql/pgsql_test.go b/matcher/pgsql/pgsql_test.go index afc9279..5530423 100644 --- a/matcher/pgsql/pgsql_test.go +++ b/matcher/pgsql/pgsql_test.go @@ -11,7 +11,7 @@ import ( func TestPG(t *testing.T) { m := NewPostgreSQLMatcher() - conn, err := net.Dial("tcp", "192.168.1.88:5432") //107 + conn, err := net.Dial("tcp", "192.168.1.106:5432") //107 if err != nil { t.Error(err) return