elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

New feature: Blacklist (or whitelist) type with file inclusion

Open chicco27 opened this issue 10 years ago • 20 comments

For better use of blacklist and withelist type shuld be possible to use file lists. When the blackist contais a lot of fileds drill down the file may be not easy, it's better to use a file separate form the rule file and include the list of fileds with inlude option for example o similar.

ex. `include:

  • blackilist_file.yaml`

chicco27 avatar Jun 13 '16 15:06 chicco27

@chicco27

I would like to apply a method to whitelist IP address and specific string. Currently, I have a cardinality rule and I would like to do whitelist on few strings and IP address by combining the rule as shown below. The rule runs without any error but it's not working as expected. Can someone assist me is this a right method to combine the rule for whitelist?

Main Rule

type: cardinality
filter:
 - terms:
     rule_name:
         -  whitelist-zone  --> Rule name for whitelisting zone
         -  whitelisti-ip  --> Rule name for whitelisting IP address

whitelist-zone

root@ubuntu:/tmp/elastalert# more whitelist-zone.yaml
# ElastAlert Rule Name

name: whitelist-zone

# ElasticSearch host and port details

es_host: 192.168.96.141

es_port: 9200

# ElasticSearch Index Name

index: graylog_0

# Timestamp added to overcome ES graylog parsing issue

timestamp_field: timestamp
timestamp_type: custom
timestamp_format: '%Y-%m-%d %H:%M:%S.%f'
timestamp_format_expr: 'ts[:23] + ts[26:]'


doc_type: message

#writeback_index: elastalert_status

# Network Port scan aggregation Rule 50 counts in 60 Seconds

type: whitelist

compare_key: srx-source-zone-name

whitelist:
 - "!file /opt/whitelist-zone.txt"

whitelisti-ip


root@ubuntu:/tmp/elastalert# more whitelist-ip.yaml
# ElastAlert Rule Name

name: whitelisti-ip

# ElasticSearch host and port details

es_host: 192.168.96.141

es_port: 9200

# ElasticSearch Index Name

index: graylog_0

# Timestamp added to overcome ES graylog parsing issue

timestamp_field: timestamp
timestamp_type: custom
timestamp_format: '%Y-%m-%d %H:%M:%S.%f'
timestamp_format_expr: 'ts[:23] + ts[26:]'


doc_type: message

#writeback_index: elastalert_status

# Network Port scan aggregation Rule 50 counts in 60 Seconds

type: whitelist

compare_key: srx-source-address

whitelist:
  - "!file /opt/whitelist-IP.txt"

Files


root@ubuntu:/tmp/elastalert# cat /opt/whitelist-IP.txt
10.18.109.59
10.10.10.10
20.20.20.20
30.30.30.30
root@ubuntu:/tmp/elastalert# cat /opt/whitelist-zone.txt
EXTERNAL
Internet
root@ubuntu:/tmp/elastalert#

sathishdsgithub avatar Jul 14 '17 12:07 sathishdsgithub

http://elastalert.readthedocs.io/en/latest/ruletypes.html?highlight=import#import

Qmando avatar Jul 14 '17 17:07 Qmando

@Qmando

Thanks!!!

Please confirm the below mentioned format is correct ?

import:

Rule name of whitelist

- whitelist-ip.yaml
- whitelist-zone.yaml 

sathishdsgithub avatar Jul 14 '17 17:07 sathishdsgithub

In some rule.yaml

name: blah blah
import: whitelist-stuff.yaml

in whitelist-stuff.yaml

whitelist:
 - "10.0.0.1"
 - "1.2.3.4"

You can only import one file at the moment. Though, that file can also import things.

The imported file will also overwrite any options in the first rule, so you can't have two whitelists and make them merge together.

Qmando avatar Jul 14 '17 17:07 Qmando

@Qmando

Apologize to annoy persistently, I have followed the above-mentioned steps, but I'm not able to obtain my requirements. Am I missing something in rule format? Appreciate your help .

Please find the steps below

  • I have created a rule name test-srx.yaml and specified the import: whitelist-zone.yam within the rule.

Main rule test-srx.yaml

root@ubuntu:/tmp/elastalert# more test-srx.yaml

name: SRX Port Scan Detection

es_host: 192.168.96.141

es_port: 9200

index: graylog_0

timestamp_field: timestamp
timestamp_type: custom
timestamp_format: '%Y-%m-%d %H:%M:%S.%f'
timestamp_format_expr: 'ts[:23] + ts[26:]'

doc_type: message

type: cardinality

query_key: [srx-source-address, srx-destination-address]
cardinality_field: srx-destination-port
max_cardinality: 50

timeframe:

    seconds: 60
filter:
 - query_string:
      query: "srx-rt-flow: RT_FLOW_SESSION_DENY"

import: whitelist-zone.yaml

aggregation:

   minutes: 2

whitelist-zone.yaml


root@ubuntu:/tmp/elastalert# more whitelist-zone.yaml

name: whitelist zone

es_host: 192.168.96.141

es_port: 9200

index: graylog_0

timestamp_field: timestamp
timestamp_type: custom
timestamp_format: '%Y-%m-%d %H:%M:%S.%f'
timestamp_format_expr: 'ts[:23] + ts[26:]'

doc_type: message

type: whitelist

compare_key: srx-source-zone-name

ignore_null: true

whitelist:

 - "EXTERNAL"
 - "Internet"

config.yaml

#rules_folder: example_rules
rules_folder: Network_scan

root@ubuntu:/tmp/elastalert# ls -ls Network_scan
total 8
4 -rw-r--r-- 1 root root 2479 Jul 15 10:38 test-srx.yaml
4 -rw-r--r-- 1 root root  504 Jul 15 10:38 whitelist-zone.yaml

Running config.yaml Produces the below error message.


root@ubuntu:/tmp/elastalert# python -m elastalert.elastalert  --start NOW --verbose --config config.yaml
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/tmp/elastalert/elastalert/elastalert.py", line 1730, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/tmp/elastalert/elastalert/elastalert.py", line 1724, in main
    client = ElastAlerter(args)
  File "/tmp/elastalert/elastalert/elastalert.py", line 111, in __init__
    self.conf = load_rules(self.args)
  File "elastalert/config.py", line 457, in load_rules
    raise EAException('Error loading file %s: %s' % (rule_file, e))
elastalert.util.EAException: Error loading file Network_scan/whitelist-zone.yaml: Invalid Rule file: Network_scan/whitelist-zone.yaml
{'whitelist': ['EXTERNAL', 'Internet'], 'rule_file': 'Network_scan/whitelist-zone.yaml'} is valid under each of {'properties': {'type': {'pattern': '[.]'}}, 'title': 'Custom Rule from Module'}, {'properties': {'type': {'enum': ['any']}}, 'title': 'Any'}

Failed validating 'oneOf' in schema:
    {'$schema': 'http://json-schema.org/draft-04/schema#',
     'definitions': {'arrayOfStrings': {'items': {'type': 'string'},
                                        'type': ['string', 'array']},
                     'arrayOfStringsOrOtherArrays': {'items': {'type': ['string',
                                                                        'array']},

sathishdsgithub avatar Jul 15 '17 06:07 sathishdsgithub

@Qmando

Need your assistance on the above issue #572

sathishdsgithub avatar Jul 17 '17 09:07 sathishdsgithub

@Qmando @Dmitry1987

I tried a different combination but still no luck. Not sure what I'm missing here.

As per the document Ifimport: whitelist-zone.yamlspecified it includes all the settings from this YAML file. This allows common config options to be shared.

Also, it was mentioned that imported files that aren’t complete rules should not have a .yml or .yaml suffix so that ElastAlert doesn’t treat them as rules. I have tried to remove the.yamlextension and tried, but still, the rule is not whitelisting the specified string.

I have attached the rule for reference.

test-srx.zip

In addition to the above, i also tried the below options but no luck

filter:
 - query_string:
       query: "srx-rt-flow: RT_FLOW_SESSION_DENY"
   and:
    filters:
         - [whitelist-zone.yaml]

I see the below error

WARNING:elasticsearch:GET http://192.168.96.141:9200/graylog_0/_search?_source_include=srx-source-address%2Ctimestamp%2Csrx-destination-address%2C%2A%2Csrx-source-address%2Csrx-destination-address&ignore_unavailable=true&scroll=30s&size=10000 [status:400 request:0.014s]
ERROR:root:Error running query: TransportError(400, u'search_phase_execution_exception', u'No query registered for [filters]')
INFO:elastalert:Ran SRX Port Scan Detection from 2017-07-18 13:33 IST to 2017-07-18 13:33 IST: 0 query hits (0 already seen), 0 matches, 1 alerts sent
INFO:elastalert:Sleeping for 3.085389 seconds


WARNING:elasticsearch:GET http://192.168.96.141:9200/graylog_0/_search?_source_include=srx-source-address%2Ctimestamp%2Csrx-destination-address%2C%2A%2Csrx-source-address%2Csrx-destination-address&ignore_unavailable=true&scroll=30s&size=10000 [status:400 request:0.009s]
ERROR:root:Error running query: TransportError(400, u'search_phase_execution_exception', u'No query registered for [filters]')
INFO:elastalert:Ran SRX Port Scan Detection from 2017-07-18 13:30 IST to 2017-07-18 13:35 IST: 0 query hits (0 already seen), 0 matches, 0 alerts sent
INFO:elastalert:Sleeping for 9.961991 seconds

sathishdsgithub avatar Jul 18 '17 06:07 sathishdsgithub

@Qmando

I tried the other way and it worked now. However, I'm not able to use two compare_key in same white list rule as shown below. Any options to achieve this

Below one works with single compare_key : srx-source-zone-name

root@ubuntu:~/SRX_log# more /tmp/elastalert/Network_scan/whitelist-zone.yaml

name: whitelist zone and ip
import: test-srx

type: whitelist

compare_key: srx-source-zone-name

ignore_null: true

whitelist:

  - "!file /opt/whitelist-zone.txt"

Below one with two compare_key: [srx-source-zone-name, srx-source-address] is not working

name: whitelist zone and ip
import: test-srx

type: whitelist

compare_key: [srx-source-zone-name,  srx-source-address]

ignore_null: true

whitelist:

  - "!file /opt/whitelist-zone.txt"
  - "!file /opt/whitelist-ip.txt"


root@ubuntu:/tmp/elastalert/Network_scan# more /opt/whitelist-zone.txt
EXTERNAL
Internet

root@ubuntu:/tmp/elastalert/Network_scan# more /opt/whitelist-ip.txt
"10.18.109.59"

sathishdsgithub avatar Jul 18 '17 09:07 sathishdsgithub

@sathishdsgithub What is "the other way" which worked? We've tried a bunch of different ways to import a whitelist.yaml which imports a txt file, and it just ignores it.

Prinzka avatar Mar 01 '18 13:03 Prinzka

@Prinzka
If you want to use import, you can only use one. It should look like this https://github.com/Yelp/elastalert/issues/572#issuecomment-315424522 except you shouldn't use .yaml for the imported file (dumb bug)

Yes, its a very anaemic feature right now.

Qmando avatar Mar 01 '18 19:03 Qmando

@Qmando

We're only using one import file at a time. Yeah it's not great, but if we can chain them we can make it work. But we are not able to make it so that we can use a whitelist in a frequency type elastalert.

We've tried:

Frequency rule import -> <A yaml file of whitelist type with the whitelist inside the same file. ALso tried with stripping the .yaml.> Result is that the whitelist is ignored, frequency rule still fires as normal

Frequency rule import -> <A yaml file of whitelist type with the whitelist being "!file /bla/somefile.txt ALso tried with stripping the .yaml.> -> /bla/somefile.txt having the whitelisted computer names in it. Again, result is that the whitelist is ignored, frequency rule remains functional.

We verified that the !file in the whitelist works by just running a whitelist yaml directly. However, we want a frequency rule type which we can feed a central file to use for its whitelist. That sounds like it should be possible, however in practice it is not happening.

Prinzka avatar Mar 01 '18 20:03 Prinzka

You got !file to work? I'm suspect of that. It's not part of pyyaml that I know of.

the whitelist setting only works with type: whitelist, not with frequency. If you want to do that, convert your whitelist into elasticsearch terms.

Here's an example:

type: frequency
filter:
 - not:
      terms:
        ip_adddress:
          - 127.0.0.1
          - 10.0.0.1
          - 8.8.8.8

Qmando avatar Mar 01 '18 20:03 Qmando

!file works but only inside a whitelist type rule. Your comment https://github.com/Yelp/elastalert/issues/572#issuecomment-315424522 suggests that we can import a whitelist file/rule into our frequency rule. And @sathishdsgithub is implying he got that to work. We want to be able to use central whitelist files, not have to actually edit every rule that uses it every time there's a change.

I've tried a bunch of different variations with using terms in the filter. However, using "- not:" inside "filter:" in any form is causing it to error out.

Prinzka avatar Mar 01 '18 20:03 Prinzka

You are using Elasticsearch 5 or 6? You need to do query strings instead unfortunately.

filter:
 query_string:
   query: "NOT (127.0.0.1 OR 10.0.0.1 OR 8.8.8.8)"
   default_field: ip_address

Also, so you're completely right about the !file thing. It was added from somebody's pull request last year. I can't even keep track of the features anymore 😢

Qmando avatar Mar 01 '18 20:03 Qmando

We're using ES 6. We're using query strings for the actual query in the rules. What we would like to do is not have to put our whitelist into the actual query in the rule itself. What we're looking for is some way to use an external file with a whitelist, in a non-whitelist rule. So that we can have a central file that multiple rules can use and so that it's more easily edited when it comes to temporary whitelists.

The posts in here suggest that we can, but it doesn't seem to function for us.

Prinzka avatar Mar 12 '18 13:03 Prinzka

@Prinzka

  • You have to create a file without *.yaml in the name of the file (example: simply create whitelist-ip-address) and put your whitelist IP address in below format and save the file in the same rule folder
filter:
   - query:
        query_string:
          query: "NOT srx-source-address: (10.10.0.1 OR 10.1.1.1 )"

  • Use import: to import the file to your rule . You have to mention the import option in your rule.yaml file Example:
import:
  - whitelist-ip-address

You can use the same whitelist file in multiple rules

sathishdsgithub avatar Mar 12 '18 13:03 sathishdsgithub

Hello All,

We are working on elastalert rules. Type blacklist not taking wildcard in blacklist files that means when i enter blacklist: -"!file /blacklist-file"

In blacklist file we have several list of blacklist text rows like below:

blacklist-file content: 10.18.109.59 10.18.109.51 10.18.109.52 10.18.109.53 10.18.109.55

Requirement we need to add wildcards inside blacklist textfile which is not working currently.

example requirement below: blacklist-file content: 10.18.109.5*

Could you please help us on achieving requirement?

Thank you, Prabhakar.

Prabhakarg1 avatar Jul 10 '18 19:07 Prabhakarg1

If you need to use wildcards/regular expressions, you need to do it in the filter.

type: any
filter:
- query:
     query_string:
       query: "ip_address: /10.18.109.5.*/"

By the way, https://github.com/Yelp/elastalert/pull/1753 is now a thing, so the !file tricks are not needed anymore.

Qmando avatar Jul 10 '18 20:07 Qmando

@Qmando

Thank you so much for quick response. I have just provided example which has sample ips but in real there are many ip_address in blacklist file where as we need to use wildcards inside blacklist-file. In elastic search these are 10.18. is a dynamic variable we do not know when this will get changed to what. So we need wildcard to search those in elastic search. Since these are many we cannot use type any is there any other solution that you can propose. I am giving blacklist examples below

blacklist-file content: 10.18.109.59 10.18.109.51 10.18.109.52 10.18.109.53 10.18.109.55 10.20.107.22 So many IP address available in which last four characters are getting changed so we thought of using regular expression instead of adding multiple entries inside file. This file also contains some of the unique IP address in huge number.

Can we use both blacklist file and query_string in blacklist type rule file?

As per your advise it seems like "!file /blacklist-file" is not required not I have gone through #1753 did not see documentation that explains more about the new change.

Could you please provide if you have documentation link related to this.

Thank you, Prabhakar.

Prabhakarg1 avatar Jul 11 '18 12:07 Prabhakarg1

"Since these are many we cannot use type any" This doesn't make sense to me. Type any with a filter is the same as blacklist. You can have as many regexs as you want.

filter:
- query:
     query_string:
       query: "ip_address: /10.18\\..*/ OR ip_address: /10.20\\..*/ OR ip_address: /10.55\\.*/"

Regarding the #1753 change, it's actually completely automatic. If you are using a blacklist or whitelist, the filters will be automatically updated. This just makes the elasticsearch queries much smaller and more efficient. If you want multiple rules to inherit the same blacklist/whitelist, you still need to use !file.

Qmando avatar Jul 11 '18 18:07 Qmando