Skip to content

Commit

Permalink
Merge pull request #258 from draios/better-list-substitution
Browse files Browse the repository at this point in the history
Better list substitution
  • Loading branch information
mstemm committed Jun 30, 2017
2 parents 42e5035 + eecc927 commit 3349dec
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 1 deletion.
62 changes: 62 additions & 0 deletions test/falco_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,68 @@ trace_files: !mux
- rules/override_nested_list.yaml
trace_file: trace_files/cat_write.scap

list_substring:
detect: False
rules_file:
- rules/list_substring.yaml
trace_file: trace_files/cat_write.scap

list_sub_front:
detect: True
detect_level: WARNING
rules_file:
- rules/list_sub_front.yaml
trace_file: trace_files/cat_write.scap

list_sub_mid:
detect: True
detect_level: WARNING
rules_file:
- rules/list_sub_mid.yaml
trace_file: trace_files/cat_write.scap

list_sub_end:
detect: True
detect_level: WARNING
rules_file:
- rules/list_sub_end.yaml
trace_file: trace_files/cat_write.scap

list_sub_bare:
detect: True
detect_level: WARNING
rules_file:
- rules/list_sub_bare.yaml
trace_file: trace_files/cat_write.scap

list_sub_whitespace:
detect: True
detect_level: WARNING
rules_file:
- rules/list_sub_whitespace.yaml
trace_file: trace_files/cat_write.scap

list_order:
detect: True
detect_level: WARNING
rules_file:
- rules/list_order.yaml
trace_file: trace_files/cat_write.scap

macro_order:
detect: True
detect_level: WARNING
rules_file:
- rules/macro_order.yaml
trace_file: trace_files/cat_write.scap

rule_order:
detect: True
detect_level: WARNING
rules_file:
- rules/rule_order.yaml
trace_file: trace_files/cat_write.scap

invalid_rule_output:
exit_status: 1
stderr_contains: "Runtime error: Error loading rules:.* Invalid output format 'An open was seen %not_a_real_field': 'invalid formatting token not_a_real_field'. Exiting."
Expand Down
14 changes: 14 additions & 0 deletions test/rules/list_order.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- list: cat_binaries
items: [not_cat]

- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name in (cat_binaries)

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
11 changes: 11 additions & 0 deletions test/rules/list_sub_bare.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name=cat_binaries

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
11 changes: 11 additions & 0 deletions test/rules/list_sub_end.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name in (ls, cat_binaries)

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
11 changes: 11 additions & 0 deletions test/rules/list_sub_front.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name in (cat_binaries, ps)

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
11 changes: 11 additions & 0 deletions test/rules/list_sub_mid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name in (ls, cat_binaries, ps)

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
11 changes: 11 additions & 0 deletions test/rules/list_sub_whitespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name= cat_binaries or proc.name=nopey

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
8 changes: 8 additions & 0 deletions test/rules/list_substring.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- list: my_list
items: ['"one string"']

- rule: my_rule
desc: my description
condition: evt.type=open and fd.name in (file_my_list)
output: my output
priority: INFO
14 changes: 14 additions & 0 deletions test/rules/macro_order.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name in (not_cat)

- macro: is_cat
condition: proc.name in (cat_binaries)

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
17 changes: 17 additions & 0 deletions test/rules/rule_order.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- list: cat_binaries
items: [cat]

- macro: is_cat
condition: proc.name in (cat_binaries)

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and proc.name=not_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING

- rule: open_from_cat
desc: A process named cat does an open
condition: evt.type=open and is_cat
output: "An open was seen (command=%proc.cmdline)"
priority: WARNING
7 changes: 6 additions & 1 deletion userspace/engine/lua/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,12 @@ end
function compiler.compile_filter(name, source, macro_defs, list_defs)

for name, items in pairs(list_defs) do
source = string.gsub(source, name, table.concat(items, ", "))
local begin_name_pat = "^("..name..")([%s(),=])"
local mid_name_pat = "([%s(),=])("..name..")([%s(),=])"
local end_name_pat = "([%s(),=])("..name..")$"
source = string.gsub(source, begin_name_pat, table.concat(items, ", ").."%2")
source = string.gsub(source, mid_name_pat, "%1"..table.concat(items, ", ").."%3")
source = string.gsub(source, end_name_pat, "%1"..table.concat(items, ", "))
end

local ast, error_msg = parser.parse_filter(source)
Expand Down

0 comments on commit 3349dec

Please sign in to comment.