elastalert icon indicating copy to clipboard operation
elastalert copied to clipboard

Unable to match the string with " _"

Open sathishdsgithub opened this issue 9 years ago • 18 comments

@Qmando

I'm trying to match specific string for the following field but it's not matching. Please assist. Not sure if this is because of the _ character in the field ?

filter:
  - term:
        srx-rt-flow.raw: "RT_FLOW_SESSION_DENY"

sathishdsgithub avatar Jul 12 '17 16:07 sathishdsgithub

My field looks like below

srx-rt-flow : RT_FLOW_SESSION_DENY

sathishdsgithub avatar Jul 12 '17 16:07 sathishdsgithub

Are you sure your index has .raw fields? Try

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

Qmando avatar Jul 12 '17 17:07 Qmando

@Qmando

Please find my message from elasticsearch index. I'm trying to match

"srx-rt-flow": "RT_FLOW_SESSION_DENY",

{
"_index": "graylog_0",
"_type": "message",
"_id": "8caccd10-65f4-11e7-a486-000c298681bd",
"_version": 1,
"_score": 1,
"_source": {
"gl2_remote_ip": "192.168.96.141",
"srx-destination-address": "17.200.72.4",
"gl2_remote_port": 44654,
"source": "192.168.96.141",
"gl2_source_input": "594bf4b34c5eedc98b64409f",
"srx-ip-address": "10.2.0.20",
"service-name": "None",
"srx-destination-port": "17072",
"srx-source-zone-name": "corporate",
"gl2_source_node": "6e3e4964-4439-4cbb-8e49-0baf1a006532",
"srx-protocol-id": "17",
"timestamp": "2017-07-11 04:51:06.443",
"level": -1,
"streams": [
"000000000000000000000001"
],
"message": "Apr 3 20:34:18 10.212.0.20 1 2017-04-03T20:34:17.602Z  RT_FLOW - RT_FLOW_SESSION_DENY [[email protected] source-address="10.15.166.30" source-port="56453" destination-address="170.200.72.4" destination-port="17072" service-name="None" protocol-id="17" icmp-type="0" policy-name="default-deny" source-zone-name="corporate" destination-zone-name="highwinds-inet" application="UNKNOWN" nested-application="UNKNOWN" username="N/A" roles="N/A" packet-incoming-interface="xe-4/0/1.10" encrypted="UNKNOWN" reason="policy deny"]",
"src-icmp-type": "0",
"srx-source-port": "56453",
"srx-policy-name": "default-deny",
"srx-rt-flow": "RT_FLOW_SESSION_DENY",
"srx-destination-zone-name": "highwinds-inet",
"facility": "Unknown",
"srx-source-address": "10.15.166.30"
}
}

sathishdsgithub avatar Jul 12 '17 17:07 sathishdsgithub

@Qmando

Tried many combination but nothing worked.

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

- term:
     srx-rt-flow: "RT_FLOW_SESSION_DENY"


sathishdsgithub avatar Jul 12 '17 17:07 sathishdsgithub

Paste the full rule YAML? There could be other issues like doc_type or index.

Also, how are you testing this? Are you sure the document exists in the time period queried?

Qmando avatar Jul 12 '17 17:07 Qmando

@Qmando

Please find my complete YAML rule .

# ElastAlert Rule Name

name: SRX Port Scan Detection between

# ElasticSearch host and port details

es_host: 192.168.96.1

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: cardinality


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

timeframe:

    seconds: 60

# Define the fields

fields:

- srx-source-address
- srx-destination-address
- srx-source-zone-name
- srx-rt-flow
- srx-policy-name


# Look for Specific RT FLOW SESSION SESSION_DENY

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


# Look for Specific Zone and Whitelist IP address

filter:
 - and:
   - not:
       terms:
         srx-source-zone-name: ["Internet", "EXTERNAL"]
   - not:
       terms:
         srx-source-address:   ["10.18.109.59", "10.10.10.10", "20.20.20.20", "30.30.30.30"]


# Combine multiple events and aggreagate the event for 2 minutes

aggregation:

   minutes: 2

# Aggregate the events based on the specfic field parameter

aggregation_key: 'srx-source-address'

############### Email Alert Configuration SMTP #############################

alert:
 - "email"
smtp_host: smtp.gmail.com
smtp_port: 465
smtp_ssl : true
smtp_auth_file: /usr/share/elasticsearch/smtp_auth_file.yml

# Email will be sent to bewlo Rcpt

email:

  -  "[email protected]"

  # Email will contain following Subject TAG and Body message


alert_subject: "Port Scan Detection between SRX Source {0} and SRX Destination {1}"
alert_subject_args:
  - srx-source-address
  - srx-destination-address

alert_text_type: exclude_fields

alert_text: |

  This is a test email please ignore. ElastAlert has detected suspicious activity .At this time, network port scan has been detected. Do some thing about it.

    Source address: {}

    Source port: {}

    Destination address: {}

    Destination port: {}

    Time: {}

    No of hits: {}

    No of Matches: {}

    Firewall Logs: {}

alert_text_args:

 - srx-source-address

 - srx-source-port

 - srx-destination-address

 - srx-destination-port

 - timestamp

 - num_hits

 - num_matches

 - message

sathishdsgithub avatar Jul 12 '17 17:07 sathishdsgithub

My logs are sent to the graylog which stores on ES index graylog_0 . Elastalert queries from the graylog_0 index.

sathishdsgithub avatar Jul 12 '17 17:07 sathishdsgithub

You've got two filter sections. The second one just overwrites the first one. You need to combine them.

Qmando avatar Jul 12 '17 18:07 Qmando

filter:
 - query_string:
      query: "srx-rt-flow: RT_FLOW_SESSION_DENY"
 - not:
       terms:
         srx-source-zone-name: ["Internet", "EXTERNAL"]
 - not:
      terms:
         srx-source-address:   ["10.18.109.59", "10.10.10.10", "20.20.20.20", "30.30.30.30"]

Qmando avatar Jul 12 '17 18:07 Qmando

@Qmando

I tried to combine them but still it's not matching. Should I use query_string or term to match srx-rt-flow ?

PLEASE let me know if the below one is correct?

filter:

  • and:
    • not: terms: srx-source-zone-name: ["Internet", "EXTERNAL"]
    • not: terms: srx-source-address: ["10.18.109.59
      • term: srx-rt-flow: "RT_FLOW_SESSION_DENY"

sathishdsgithub avatar Jul 12 '17 18:07 sathishdsgithub

@Qmando

Let me try the below and update you. Thanks again

filter:

  • query_string: query: "srx-rt-flow: RT_FLOW_SESSION_DENY"
  • not: terms: srx-source-zone-name: ["Internet", "EXTERNAL"]
  • not: terms: srx-source-address: ["10.18.109.59", "10.10.10.10", "20.20.20.20", "30.30.30.30"]

sathishdsgithub avatar Jul 12 '17 18:07 sathishdsgithub

how are you testing this? Have you tried elastalert-test-rule --count-only --days=7 rule.yaml

Also, what version of elasticsearch?

Qmando avatar Jul 12 '17 20:07 Qmando

@Qmando

My elasticsearch version is 2.x as shown below. I test by sending the text file log to the Syslog port using netcat command. That way I can test in my VM before testing in real-time production logs.

cat logs.txt | nc 192.168.96.141 8855

root@ubuntu:~/SRX_log# curl -XGET http://192.168.96.141:9200
{
  "name" : "En Sabah Nur",
  "cluster_name" : "graylog",
  "cluster_uuid" : "KSma-dJxTVW97Jwm0ttlRA",
  "version" : {
    "number" : "2.4.5",
    "build_hash" : "c849dd13904f53e63e88efc33b2ceeda0b6a1276",
    "build_timestamp" : "2017-04-24T16:18:17Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.4"
  },
  "tagline" : "You Know, for Search"
}

sathishdsgithub avatar Jul 13 '17 03:07 sathishdsgithub

@Qmando

I did a test and it works as expected Thanks !!!!. I need to do more test, will confirm the final status by end of the day.

Meanwhile, Is there any specific condition in YAML rule that we should only use one filter rather using the below format? and how the filter matching is happening in YAML ? is there any sequence of steps followed during the yaml rule execution? just curious to know

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


# Look for Specific Zone and Whitelist IP address

filter:
 - and:
   - not:
       terms:
         srx-source-zone-name: ["Internet", "EXTERNAL"]

sathishdsgithub avatar Jul 13 '17 03:07 sathishdsgithub

You can't have two keys with the same name in YAML. The last one overrides the previous ones. That's just part of the spec.

>>> yaml.load('''
... filter: foo
... filter: bar''')
{'filter': 'bar'}

Qmando avatar Jul 13 '17 17:07 Qmando

@Qmando

Thanks for your valuable reply .

Just to clarify in the below rule i don't see two keys with the same name in YAML. Can you specify which key repeats twice?

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


# Look for Specific Zone and Whitelist IP address

filter:
 - and:
   - not:
       terms:
         srx-source-zone-name: ["Internet", "EXTERNAL"]

sathishdsgithub avatar Jul 13 '17 17:07 sathishdsgithub

filter appears twice. Exactly like I JUST showed in my comment.

>>> yaml.load('''
... filter:
...  - query_string:
...       query: "srx-rt-flow: RT_FLOW_SESSION_DENY"
...
...
... # Look for Specific Zone and Whitelist IP address
...
... filter:
...  - and:
...    - not:
...        terms:
...          srx-source-zone-name: ["Internet", "EXTERNAL"]
... ''')
{'filter': [{'and': [{'not': {'terms': {'srx-source-zone-name': ['Internet', 'EXTERNAL']}}}]}]}
>>>

Qmando avatar Jul 13 '17 18:07 Qmando

@Qmando

Sorry, I understood now. Thanks

sathishdsgithub avatar Jul 13 '17 18:07 sathishdsgithub