elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

Wildcard Windows directories in queries/terms not working

Open josephbleroy opened this issue 8 years ago • 7 comments

I'm working on writing some security specific queries. So far I've been able to get some wildcard queries working properly, such as:

# WORKS
# https://raw.githubusercontent.com/Neo23x0/sigma/master/rules/windows/sysmon/sysmon_outlook_shell.yml
filter:
- query:
   query_string:
      query: "(event_data.ParentImage:*OUTLOOK.EXE) AND (event_data.Image:*cmd.exe OR event_data.Image:*powershell.exe OR event_data.Image:*wscript.exe OR event_data.Image:*cscript.exe OR event_data.Image:*sh.exe OR event_data.Image:*bash.exe OR event_data.Image:*schtasks.exe)"

But I would like to focus on writing a query that includes Windows directories, using the following logic:

- query:
   query_string:
      query: "(event_id:1) AND (event_data.Image:C:\Intel\Logs\* OR event_data.Image:C:\Users\Default\*)"

So far nothing is working. This is likely due to my limited experience with Elasticsearch DSL, not ElastAlert itself.

Any guidance or direction is greatly appreciated!

Thank you.

josephbleroy avatar Oct 12 '18 01:10 josephbleroy

Try match query instead of querystring

prashanttct07 avatar Oct 18 '18 12:10 prashanttct07

Try match query instead of querystring

@prashanttct07 Thanks, I'll give it a try and let you know if it works or not.

josephbleroy avatar Oct 22 '18 08:10 josephbleroy

I wasn't able to get the query syntax working correctly, however I did get it sort of working by using the following syntax:

filter:
- term:
      event_id: "1"
- wildcard:
       event_data.Image: '\C:\\Intel\\Logs\\*'

However, I'd like to setup multiple wildcards using OR logic so that whenever something is executed from any of the directories an alert will be sent:

filter:
- term:
      event_id: "1"
- wildcard:
       event_data.Image: '\C:\\Intel\\Logs\\*'
       event_data.Image: '\C:\\Windows\\Fonts\\*'
       event_data.Image: '\C:\\Windows\\Debug\\*'

Any ideas?

josephbleroy avatar Oct 23 '18 07:10 josephbleroy

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

assuming you use the default analyzer, this should work

filter:
- term:
     event_id: "1"
 - query_string:
      query: "\"C/Intel/Logs\" OR \"C/Windows/Debug\" OR \"C/Windows/Fonts\""
      default_field: event_data.Image

Qmando avatar Oct 23 '18 18:10 Qmando

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

assuming you use the default analyzer, this should work

filter:
- term:
     event_id: "1"
 - query_string:
      query: "\"C/Intel/Logs\" OR \"C/Windows/Debug\" OR \"C/Windows/Fonts\""
      default_field: event_data.Image

@Qmando Thank you for your reply. The example you provided is able to execute, however it doesn't return any results.

If I run this query in Kibana, I receive 3 results:

(event_id:1) AND ((event_data.Image:C\:\\Windows\\debug\\*) OR (event_data.Image:C\:\\Intel\\Logs\\*) OR (event_data.Image:C\:\\Users\\Public\\*))

I'm running Elasticsearch 6.4.0 without any significant modifications. The only thing I enabled from Kibana was enabling query features (auto complete). Do you think this could be the issue?

I'm wondering if this is my lack of query dsl experience or perhaps something more...

josephbleroy avatar Oct 24 '18 01:10 josephbleroy

Well, without quotes, a query string is basically a bunch of ORed together single words. That is, if you have event_data.Image: C\:\\Windows\\debug\\*, this is actually similar to doing event_data.Image: C OR event_data.Image: Windows OR event_data.Image: debug. The slashes don't actually matter because of the analyzer, and the wildcard doesn't actually matter since "Foo bar" matches "foo bar baz etc" anyway.

Do those three results match your three path options? If so, I'm a bit surprised that the one I gave doesn't return any results (obviously remove \s if you are trying it in kibana, because thats YAML escaping).

All that being said, I'm basing this off the default analyzer in ES2, and if your field is non-analyzed, that goes out the window. You can check that in the mapping. Anyway, anything you can put into Kibana that works for you can be put into a query_string filter in elastalert, so if that works, just use that.

Qmando avatar Oct 24 '18 01:10 Qmando

All that being said, I'm basing this off the default analyzer in ES2, and if your field is non-analyzed, that goes out the window.

This is most likely the reason. I believe that I'm using dynamic mapping for several fields. I'm using the default winlogbeat.template.json file to create the index mapping for winlogbeat-6.4.0-*:

        {
          "event_data": {
            "mapping": {
              "type": "keyword"
            },
            "match_mapping_type": "string",
            "path_match": "event_data.*"
          }
        },

josephbleroy avatar Oct 24 '18 01:10 josephbleroy