Skip to content

Commit faeed35

Browse files
Merge pull request #1398 from Andrew15-5/empty-host
Add ability to match bare IP requests (empty host name)
2 parents 576b8c9 + c428587 commit faeed35

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

daemon/rule/operator.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,16 @@ func (o *Operator) Compile() error {
111111
return nil
112112
}
113113

114-
// the only operator Type that can have the Data field empty is List.
115-
if o.Type != List && o.Operand != OpTrue && o.Data == "" {
114+
// The only operator Type that can have the Data field empty are:
115+
// Simple, Regexp, List.
116+
// For List, because it uses List field and not Data field.
117+
// For Simple and Regexp, because it can be useful to match on some
118+
// operands that can in practice be equal to an empty string. This is the
119+
// case, for example, when a request has a "bare" IP instead of a domain
120+
// name, therefore DstHost field will be empty. You can match empty string
121+
// with simple comparison or the "^$" regexp pattern.
122+
if !(o.Type == Simple || o.Type == Regexp || o.Type == List) &&
123+
o.Operand != OpTrue && o.Data == "" {
116124
return fmt.Errorf("Operand %s cannot be empty (%s)", o.Operand, o.Type)
117125
}
118126

@@ -343,7 +351,7 @@ func (o *Operator) Match(con *conman.Connection, hasChecksums bool) bool {
343351
return false
344352
} else if o.Operand == OpProcessCmd {
345353
return o.cb(strings.Join(con.Process.Args, " "))
346-
} else if o.Operand == OpDstHost && con.DstHost != "" {
354+
} else if o.Operand == OpDstHost {
347355
return o.cb(con.DstHost)
348356
} else if o.Operand == OpDstIP {
349357
return o.cb(con.DstIP.String())

daemon/rule/operator_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,47 @@ func TestRaceNewOperatorListsDomainsRegexp(t *testing.T) {
740740

741741
restoreConnection()
742742
}
743+
744+
func TestNewOperatorRegexpBareIpNoHostName(t *testing.T) {
745+
t.Log("Test NewOperator() regex bare IP (no host name)")
746+
var dummyList []Operator
747+
748+
conn.DstHost = ""
749+
750+
opRE, err := NewOperator(Regexp, true, OpDstHost, "^$", dummyList)
751+
if err != nil {
752+
t.Error("NewOperator regexp.case-sensitive.err should be nil: ", err)
753+
t.Fail()
754+
}
755+
if err = opRE.Compile(); err != nil {
756+
t.Fail()
757+
}
758+
if opRE.Match(conn, false) == false {
759+
t.Error("Test NewOperator() RE sensitive match:", conn.DstHost)
760+
t.Fail()
761+
}
762+
763+
restoreConnection()
764+
}
765+
766+
func TestNewOperatorSimpleBareIpNoHostName(t *testing.T) {
767+
t.Log("Test NewOperator() simple bare IP (no host name)")
768+
var dummyList []Operator
769+
770+
conn.DstHost = ""
771+
772+
opSimple, err := NewOperator(Simple, true, OpDstHost, "", dummyList)
773+
if err != nil {
774+
t.Error("NewOperator simple.case-sensitive.err should be nil: ", err)
775+
t.Fail()
776+
}
777+
if err = opSimple.Compile(); err != nil {
778+
t.Fail()
779+
}
780+
if opSimple.Match(conn, false) == false {
781+
t.Error("Test NewOperator() simple sensitive match:", conn.DstHost)
782+
t.Fail()
783+
}
784+
785+
restoreConnection()
786+
}

0 commit comments

Comments
 (0)