elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

Query works perfectly in Kibana - but doesn’t work at all in Elastalert

Open SecOpsWarrior opened this issue 6 years ago • 5 comments

I have a little query im using to detect powershell and some other things being run. To keep it from being noisy, I have some regex to ignore certain powershell files generated whenever a script is kicked off by our RMM. It works PERFECTLY in Kibana when I test it, but does not do the same filtering job in Elastalert, and I get flooded with the alerts that should be filtered out.

Any ideas why this works great in ES but not in Elastalert?

ES QUERY:

GET _search
{
  "query": {
    "query_string": {
      "query": "(file_name.keyword:(C\\:\\Windows\\Temp\\* OR C\\:\\Temp\\*OR *\\Client\\* OR C\\:\\PerfLogs\\* OR *\\Users\\Public\\* OR C\\:\\Users\\Default\\* OR *.ps1 OR *.vbs OR *.bat OR *.chm OR *.xml OR *.txt OR *.jsp OR *.jspx OR *.asp OR *.aspx OR *.php OR *.war) NOT file_name.keyword:(/.*windows.temp.[a-z0-9_]+\\.[a-z0-9]{3}\\.ps1/ OR /.*users[a-z0-9\\]+temp.[a-z0-9]+\\.[a-z0-9]{3}\\.ps1/))"
    } 
  }
}

Elastalert rule (doesn't filter the NOT at all and alerts come pouring in)

alert:
- debug

description: Detects an Antivirus alert in a highly relevant file path or with a relevant file name

filter:
- query:
    query_string:
      query: (file_name.keyword:(C\\:\\Windows\\Temp\\* OR C\\:\\Temp\\*OR *\\Client\\* OR C\\:\\PerfLogs\\* OR *\\Users\\Public\\* OR C\\:\\Users\\Default\\* OR *.ps1 OR *.vbs OR *.bat OR *.chm OR *.xml OR *.txt OR *.jsp OR *.jspx OR *.asp OR *.aspx OR *.php OR *.war) NOT file_name.keyword:(/.*windows.temp.[a-z0-9_]+\\.[a-z0-9]{3}\\.ps1/ OR /.*users[a-z0-9\\]+temp.[a-z0-9]+\\.[a-z0-9]{3}\\.ps1/))

index: logs-*
name: Antivirus-Relevant-File-Paths-Alerts_0
priority: 2
realert:
  minutes: 0
type: any

EXAMPLE file_name that should be filtered, but still throws an alert:

"file_name" : """c:\windows\temp\5ttwu3tm.enp.ps1""",

SecOpsWarrior avatar Jan 28 '20 05:01 SecOpsWarrior

Aren't you missing AND in the filter clause before NOT? Here:

NOT file_name.keyword:

=>

AND NOT file_name.keyword:

nickbabkin avatar Jan 29 '20 16:01 nickbabkin

Tried it both ways actually (same result - I believe the AND is implied), but even if so, why would it work in kibana but not in elastalert?

SecOpsWarrior avatar Jan 29 '20 16:01 SecOpsWarrior

Figured it out. For some reason, using parens to enclose the query in elastalert wasnt properly sending the whole query, it was only grabbing the first condition. Changing the elastalert query to the below resolved the issue. Does anyone have an idea why this is? Bug or expected behavior?

filter:
- query:
    query_string:
      query: "file_name.keyword:(C\\:\\Windows\\Temp\\* OR C\\:\\Temp\\*OR *\\Client\\* OR C\\:\\PerfLogs\\* OR *\\Users\\Public\\* OR C\\:\\Users\\Default\\* OR *.ps1 OR *.vbs OR *.bat OR *.chm OR *.xml OR *.txt OR *.jsp OR *.jspx OR *.asp OR *.aspx OR *.php OR *.war) NOT file_name.keyword:(/.*windows.temp.[a-z0-9_]+\\.[a-z0-9]{3}\\.ps1/ OR /.*users[a-z0-9\\]+temp.[a-z0-9]+\\.[a-z0-9]{3}\\.ps1/)"

SecOpsWarrior avatar Jan 31 '20 21:01 SecOpsWarrior

Hi @SecOpsWarrior, Are you tunning helk-elastalert rules? Can I ask some questions about not triggering rules?

tgocoh avatar Feb 05 '20 02:02 tgocoh

I am. Ask away. I may not be of much help, but I'll answer what I can :)

SecOpsWarrior avatar Feb 05 '20 04:02 SecOpsWarrior