2018-10-31 20:54:58 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
# Copyright (c) 2014 Mozilla Corporation
|
|
|
|
|
|
|
|
|
|
|
|
from lib.alerttask import AlertTask
|
2018-10-31 22:11:31 +03:00
|
|
|
from mozdef_util.query_models import SearchQuery, TermMatch, QueryStringMatch, ExistsMatch, PhraseMatch, WildcardMatch
|
2018-10-31 20:54:58 +03:00
|
|
|
|
|
|
|
|
2018-10-31 21:53:25 +03:00
|
|
|
class TEMPLATE_ALERT_CLASSNAME(AlertTask):
|
2018-10-31 20:54:58 +03:00
|
|
|
def main(self):
|
2018-10-31 22:11:31 +03:00
|
|
|
# Create a query to look back the last 20 minutes
|
2018-10-31 20:54:58 +03:00
|
|
|
search_query = SearchQuery(minutes=20)
|
2018-10-31 22:11:31 +03:00
|
|
|
|
|
|
|
# Add search terms to our query
|
2018-10-31 20:54:58 +03:00
|
|
|
search_query.add_must([
|
2018-10-31 22:11:31 +03:00
|
|
|
TermMatch('category', 'helloworld'),
|
|
|
|
ExistsMatch('details.sourceipaddress'),
|
2018-10-31 20:54:58 +03:00
|
|
|
])
|
|
|
|
|
|
|
|
self.filtersManual(search_query)
|
2018-10-31 21:53:25 +03:00
|
|
|
# Search aggregations on field 'sourceipaddress'
|
|
|
|
# keep X samples of events at most
|
2018-10-31 20:54:58 +03:00
|
|
|
self.searchEventsAggregated('details.sourceipaddress', samplesLimit=10)
|
2018-10-31 21:53:25 +03:00
|
|
|
# alert when >= X matching events in an aggregation
|
2018-10-31 20:54:58 +03:00
|
|
|
self.walkAggregations(threshold=1)
|
|
|
|
|
2018-10-31 21:53:25 +03:00
|
|
|
# Set alert properties
|
2018-10-31 20:54:58 +03:00
|
|
|
def onAggregation(self, aggreg):
|
2018-10-31 21:53:25 +03:00
|
|
|
# aggreg['count']: number of items in the aggregation, ex: number of failed login attempts
|
|
|
|
# aggreg['value']: value of the aggregation field, ex: toto@example.com
|
|
|
|
# aggreg['events']: list of events in the aggregation
|
2018-10-31 22:11:31 +03:00
|
|
|
category = 'hellocategory'
|
|
|
|
tags = ['hello', 'world']
|
2018-10-31 20:54:58 +03:00
|
|
|
severity = 'WARNING'
|
2018-10-31 22:11:31 +03:00
|
|
|
summary = "My first alert!"
|
2018-10-31 20:54:58 +03:00
|
|
|
|
2018-10-31 21:53:25 +03:00
|
|
|
# Create the alert object based on these properties
|
2018-10-31 20:54:58 +03:00
|
|
|
return self.createAlertDict(summary, category, tags, aggreg['events'], severity)
|