2017-01-26 23:38:53 +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/.
|
2017-01-26 23:42:59 +03:00
|
|
|
# Copyright (c) 2017 Mozilla Corporation
|
2017-01-26 23:38:53 +03:00
|
|
|
|
|
|
|
from lib.alerttask import AlertTask
|
2017-01-27 00:33:38 +03:00
|
|
|
from query_models import SearchQuery, TermMatch, PhraseMatch
|
2017-01-26 23:38:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class AlertOpenPortViolation(AlertTask):
|
|
|
|
def main(self):
|
|
|
|
search_query = SearchQuery(hours=4)
|
|
|
|
|
|
|
|
search_query.add_must([
|
2018-03-03 00:29:30 +03:00
|
|
|
TermMatch('category', 'open_port_policy_violation'),
|
|
|
|
PhraseMatch('tags', 'open_port_policy_violation')
|
2017-01-26 23:38:53 +03:00
|
|
|
])
|
|
|
|
|
|
|
|
self.filtersManual(search_query)
|
|
|
|
|
|
|
|
# Search aggregations on field 'sourceipaddress', keep X samples of
|
|
|
|
# events at most
|
|
|
|
self.searchEventsAggregated('details.destinationipaddress', samplesLimit=100)
|
|
|
|
# alert when >= X matching events in an aggregation
|
|
|
|
self.walkAggregations(threshold=1)
|
|
|
|
|
|
|
|
# Set alert properties
|
|
|
|
def onAggregation(self, aggreg):
|
|
|
|
# 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
|
|
|
|
category = 'open_port_policy_violation'
|
2017-06-07 23:30:48 +03:00
|
|
|
tags = ['open_port_policy_violation', 'openportpagerduty']
|
2017-01-26 23:38:53 +03:00
|
|
|
severity = 'CRITICAL'
|
|
|
|
|
2017-01-27 00:33:38 +03:00
|
|
|
summary = ('{0} unauthorized open port(s) on {1} ('.format(aggreg['count'], aggreg['value']))
|
|
|
|
for event in aggreg['events'][:5]:
|
|
|
|
summary += str(event['_source']['details']['destinationport']) + ' '
|
|
|
|
summary += ')'
|
2017-01-26 23:38:53 +03:00
|
|
|
|
|
|
|
# Create the alert object based on these properties
|
|
|
|
return self.createAlertDict(summary, category, tags, aggreg['events'], severity)
|