elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

range not working within filter query_string

Open sathishdsgithub opened this issue 8 years ago • 9 comments

@Qmando

The below filter parameter is not working. Am I missing anything here?

filter:
 - query_string:
       query: "srx-rt-flow: RT_FLOW_SESSION_DENY"
 - range
        destination-port:
                     from: 0
                        to: 1024
- query:
        query_string:
          query: "NOT destination-port: (80 OR 443 OR 53)"
   - query:
        query_string:
           query: "AND destination-port: (1433 OR 3306 OR 3389 OR 5900)"

sathishdsgithub avatar Mar 16 '18 09:03 sathishdsgithub

Exactly as you posted it, this isn't even valid yaml. You are missing a : after range on line 4. The indentation of to is also wrong, it needs to line up with from. Same with the final query. And you don't need an AND at the start of a query string.

Qmando avatar Mar 16 '18 19:03 Qmando

@Qmando

I have configured filter with correct indentation. I do not get any error my rule is running fine. But the filter is not working as expected.

My requirement is to look only for the destination port between 0 to 1024. But i get alert for ports > 1024 example 10050 destination port etc..

Please find the es_debug trace for reference.

[root@elastalert]# more /tmp/debugg
curl -XGET 'http://localhost:9200/?pretty' -d ''
curl -XGET 'http://localhost:9200/?pretty' -d ''
curl -H 'Content-Type: application/json' -XGET 'http://localhost:9200/elastalert_status/elastalert/_search?pretty&size=1000' -d '{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "alert_time": {
            "from": "2018-03-17T08:44:38.458508Z",
            "to": "2018-03-19T08:44:38.458528Z"
          }
        }
      },
      "must": {
        "query_string": {
          "query": "!_exists_:aggregate_id AND alert_sent:false"
        }
      }
    }
  },
  "sort": {
    "alert_time": {
      "order": "asc"
    }
  }
}'
curl -H 'Content-Type: application/json' -XGET 'http://localhost:9200/appliance_*/_search?pretty&_source_include=srx-source-address%2Ctimestamp%2Csrx-destination-port%2C%2A%2Csrx-source-ad
dress%2Csrx-destination-port&ignore_unavailable=true&scroll=30s&size=25000' -d '{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "timestamp": {
                  "gt": "2018-03-19 08:44:38.458",
                  "lte": "2018-03-19 08:44:38.466"
                }
              }
            },
            {
              "range": {
                "srx-destination-port": {
                  "from": 0,
                  "to": 1024
                }
              }
            },
            {
              "query_string": {
                "query": "NOT srx-source-address: (10.1.1.1 OR 10.1.2.3 OR 9.9.9.9)"
              }
            },
            {
              "query_string": {
                "query": "NOT srx-source-address: (20.1.1.1 OR 20.1.2.3 OR 90.9.9.9)"
              }
            },
            {
              "query_string": {
                "query": "NOT srx-source-zone-name: (zone1 OR zone2 OR  zone 3)"
              }
            },
            {
              "query_string": {
                "query": "NOT srx-source-address: (120.1.1.1 OR 220.1.2.3 OR 190.19.9.9)"
              }
            },
            {
              "query_string": {
                "query": "srx-rt-flow: RT_FLOW_SESSION_DENY"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "timestamp": {
        "order": "asc"
      }
    }
  ]

My rules looks something like below

doc_type: message


# Internal Outbreak Aggregation

type: cardinality

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

timeframe:

      minutes: 10

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

import:
        - filter-1
        - filter-2
        - filter-3
        - filter-4
        - port-range

Inside port-range file

 more Rule_folder/port-range
filter:
   - range:
        srx-destination-port:
           from: 0
           to: 1024

sathishdsgithub avatar Mar 19 '18 09:03 sathishdsgithub

@Qmando @Dmitry1987

Please provide your assistance on the above issue. Following range filter is not working as expected. The filter should look only for the port range between 0 TO 1024 . I even treid with gt, lte, regex no luck.

filter:
   - range:
        srx-destination-port:
           from: 0
           to: 1024

sathishdsgithub avatar Mar 20 '18 13:03 sathishdsgithub

Is srx-destination-port mapped as an number?

You've got the raw query, mess around with it until you figure it out. This isn't an elastalert issue.

Qmando avatar Mar 20 '18 17:03 Qmando

@Qmando

Yes srx-destination-port mapped as number

'''elastalert]# curl -XGET "http://10.50.XX:9200/firewalls_*/message/_search?pretty" { "took" : 3078, "timed_out" : false, "num_reduce_phases" : 2, "_shards" : { "total" : 600, "successful" : 600, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1528782218, "max_score" : 1.0, "hits" : [ { "_index" : "firewalls_1390", "_type" : "message", "_id" : "df84f177-d150-11e7-8201-1866dae8a558", "_score" : 1.0, "_source" : { "srx-nat-source-address" : "10.50.XX", "gl2_remote_ip" : "10.191.XX", "srx-destination-address" : "10.15.XX", "gl2_remote_port" : 35353, "source" : "10.42.XX", "gl2_source_input" : "57fd0f0aa345205031b18652", "srx-ip-address" : "10.42.XX", "srx-destination-port" : "161", "srx-nat-source-port" : "42282", "srx-source-zone-name" : "T_XX", "gl2_source_node" : "1037c649-ea01-42cb-9cb0-1f462b6e7618", "srx-reason" : "idle Timeout", "srx-service-name" : "None", "srx-source-nat-rule-name" : "N/A", "srx-src-nat-rule-type" : "N/A", "srx-roles" : "N/A", "srx-packets-from-client" : "38", "srx-protocol-id" : "17", "timestamp" : "2017-11-24 19:51:33.000", "srx-application" : "UNKNOWN", "srx-session-id-32" : "10690079", "srx-nat-destination-port" : "161", "srx-destination-nat-rule-name" : "N/A", "level" : 6, "srx-packets-from-server" : "38", "streams" : [ "57fd0f28a345205031b18676" ], "srx-username" : "N/A", "TCHInput" : "SRX-12208-TCP", "srx-bytes-from-client" : "3524", "srx-dst-nat-rule-type" : "N/A", "message" : "10.42.XX Nov 24 19:51:31 10.42.255.255 1 2017-11-24T19:51:30.677Z RT_FLOW - RT_FLOW_SESSION_CLOSE [[email protected] reason="idle Timeout" source-address="10.50.XX" source-port="42282" destination-address="10.15.XX1" destination-port="161" service-name="None" nat-source-address="10.50.XX" nat-source-port="42282" nat-destination-address="10.15.XX" nat-destination-port="161" src-nat-rule-type="N/A" src-nat-rule-name="N/A" dst-nat-rule-type="N/A" dst-nat-rule-name="N/A" protocol-id="17" policy-name="G_XX (global)" source-zone-name="T_SHARED" destination-zone-name="CORPORATE" session-id-32="10690079" packets-from-client="38" bytes-from-client="3524" packets-from-server="38" bytes-from-server="5030" elapsed-time="63" application="UNKNOWN" nested-application="UNKNOWN" username="N/A" roles="N/A" packet-incoming-interface="reth0.217" encrypted="UNKNOWN"]", "srx-bytes-from-server" : "5030", "srx-elapsed-time" : "63", "srx-nat-destination-address" : "10.XX", "srx-encrypted" : "UNKNOWN", "srx-source-port" : "42282", "srx-policy-name" : "G_NETWORK_XX(global)", "srx-rt-flow" : "RT_FLOW_SESSION_CLOSE", "srx-destination-zone-name" : "CORPORATE", "facility" : "user-level", "srx-nested-application" : "UNKNOWN", "srx-source-address" : "10.50.XX", "srx-packet-incoming-interface" : "reth0.217" } },'''

sathishdsgithub avatar Mar 20 '18 18:03 sathishdsgithub

"srx-destination-port" : "161", "161" That's not a number, that's a string.

You'll need to adjust the mapping for range to work.

Qmando avatar Mar 20 '18 18:03 Qmando

@Qmando

Is there any alternative options available to create a filter to match the string with range options..can we use regex ?

sathishdsgithub avatar Mar 20 '18 19:03 sathishdsgithub

You could definitely use regex, but you are on your own to create it.

Qmando avatar Mar 20 '18 19:03 Qmando

@Qmando

Just wanted to share.

This might be helpful for someone. We can use regex to achieve port ranges from 0 TO 1024 using the below filter. I tested and it works!!

filter:
 - query_string:
       query: "srx-rt-flow: RT_FLOW_SESSION_DENY"
 - regexp:
       srx-destination-port: "([0-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|10[01][0-9]|102[0-4] OR 3306 OR 3389 OR 1433)"

sathishdsgithub avatar Mar 28 '18 06:03 sathishdsgithub