Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var hexRE = regexp.MustCompile(`(?i)(\|(?:\s*[a-f0-9]{2}\s*)+\|)`)
var escapeRE = regexp.MustCompile(`([()+.'\\])`)

// escapeContent matches escaped special characters.
var escapeContent = regexp.MustCompile(`\\([\\;":])`)
var escapeContent = regexp.MustCompile(`\\([\\;":|\[\]+ .])`)

// metaSplitRE matches string in metadata.
var metaSplitRE = regexp.MustCompile(`,\s*`)
Expand Down Expand Up @@ -386,7 +386,7 @@ func containsUnescaped(s string) bool {
for _, b := range s {
if esc {
switch b {
case '\\', ';', '"', ':':
case '\\', ';', '"', ':', '|', '[', ']', '+', '.', ' ':
esc = false
default:
return true
Expand Down Expand Up @@ -630,13 +630,22 @@ func (r *Rule) option(key item, l *lexer) error {
"dce_iface", "dce_opnum", "dce_stub_data",
"asn1"}):
nextItem := l.nextItem()
negate := false
if nextItem.typ == itemNot {
negate = true
nextItem = l.nextItem()
}
if nextItem.typ != itemOptionValue {
return fmt.Errorf("no valid value for %s tag", key.value)
}
if r.Tags == nil {
r.Tags = make(map[string]string)
}
r.Tags[key.value] = nextItem.value
if negate {
r.Tags[key.value] = "!" + nextItem.value
} else {
r.Tags[key.value] = nextItem.value
}
case inSlice(key.value, []string{"sameip", "tls.store", "ftpbounce"}):
r.Statements = append(r.Statements, key.value)
case inSlice(key.value, tlsTags):
Expand Down
59 changes: 59 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,31 @@ func TestParseRule(t *testing.T) {
},
},
},
{
name: "negated tags",
rule: `alert tcp any any -> any any (msg:"test"; fragbits:!M; flags:!F; tos:!0; ipopts:!rr; sid:1; rev:1;)`,
want: &Rule{
Action: "alert",
Protocol: "tcp",
Source: Network{
Nets: []string{"any"},
Ports: []string{"any"},
},
Destination: Network{
Nets: []string{"any"},
Ports: []string{"any"},
},
SID: 1,
Revision: 1,
Description: "test",
Tags: map[string]string{
"fragbits": "!M",
"flags": "!F",
"tos": "!0",
"ipopts": "!rr",
},
},
},
{
name: "commented rule content",
rule: `#alert udp $HOME_NET any -> $EXTERNAL_NET any (sid:1337; msg:"foo"; content:"AA"; rev:2;)`,
Expand Down Expand Up @@ -2139,6 +2164,25 @@ func TestParseRule(t *testing.T) {
},
},
},
{
name: "content match with escaped pipe and characters",
rule: `alert tcp any any -> any any (msg:"test"; content:"\|test\| \[abc\] \+ \. \ "; sid:1; rev:1;)`,
want: &Rule{
Action: "alert",
Protocol: "tcp",
Source: Network{Nets: []string{"any"}, Ports: []string{"any"}},
Destination: Network{Nets: []string{"any"}, Ports: []string{"any"}},
SID: 1,
Revision: 1,
Description: "test",
Matchers: []orderedMatcher{
&Content{
DataPosition: pktData,
Pattern: []byte("|test| [abc] + . "),
},
},
},
},
// Errors
{
name: "invalid action",
Expand Down Expand Up @@ -2364,6 +2408,21 @@ func TestContainsUnescaped(t *testing.T) {
input: `\\\\\\\\\;`,
want: false,
},
{
name: "escaped pipe",
input: `\|`,
want: false,
},
{
name: "escaped bracket",
input: `\[`,
want: false,
},
{
name: "escaped plus dot space",
input: `\+\.\ `,
want: false,
},
} {
got := containsUnescaped(tt.input)
if got != tt.want {
Expand Down
Loading