2015-02-26 21:42:51 +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
|
2016-08-19 23:40:51 +03:00
|
|
|
from query_models import SearchQuery, TermMatch, ExistsMatch, TermsMatch
|
2016-08-03 23:40:10 +03:00
|
|
|
|
2015-02-26 21:42:51 +03:00
|
|
|
|
|
|
|
class AlertMultipleIntelHits(AlertTask):
|
|
|
|
def main(self):
|
2016-08-03 23:40:10 +03:00
|
|
|
search_query = SearchQuery(minutes=2)
|
|
|
|
|
|
|
|
search_query.add_must([
|
2017-10-21 01:30:17 +03:00
|
|
|
TermMatch('category', 'bro'),
|
|
|
|
TermMatch('source', 'intel'),
|
2017-01-12 01:03:29 +03:00
|
|
|
ExistsMatch('details.seenindicator'),
|
2016-08-19 23:40:51 +03:00
|
|
|
TermsMatch('hostname', ['sensor1', 'sensor2', 'sensor3'])
|
2016-08-03 23:40:10 +03:00
|
|
|
])
|
|
|
|
|
|
|
|
self.filtersManual(search_query)
|
2015-02-26 21:42:51 +03:00
|
|
|
|
|
|
|
# Search aggregations on field 'seenindicator', keep X samples of events at most
|
2015-05-27 23:23:42 +03:00
|
|
|
self.searchEventsAggregated('details.seenindicator', samplesLimit=10)
|
2015-02-26 21:42:51 +03:00
|
|
|
# alert when >= X matching events in an aggregation
|
|
|
|
self.walkAggregations(threshold=10)
|
|
|
|
|
|
|
|
# Set alert properties
|
2015-05-27 23:23:42 +03:00
|
|
|
def onAggregation(self, aggreg):
|
2015-02-26 21:42:51 +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
|
|
|
|
category = 'bro'
|
|
|
|
tags = ['nsm,bro,intel']
|
|
|
|
severity = 'NOTICE'
|
|
|
|
hostname = aggreg['events'][0]['_source']['hostname']
|
|
|
|
|
|
|
|
summary = '{0} {1} {2} on {3}'.format(aggreg['count'], hostname, ' Bro intel match for indicator:', aggreg['value'])
|
2015-05-27 23:23:42 +03:00
|
|
|
|
2015-02-26 21:42:51 +03:00
|
|
|
summary += ' sample hosts that hit it: '
|
|
|
|
for e in aggreg['events'][:3]:
|
|
|
|
if 'details' in e['_source'].keys() \
|
|
|
|
and 'sourceipaddress' in e['_source']['details'].keys() \
|
|
|
|
and 'seenwhere' in e['_source']['details'].keys():
|
|
|
|
interestingaddres = ''
|
|
|
|
# someone talking to a bad guy, I want to know who
|
|
|
|
# someone resolving bad guy's domain name, I want to know who
|
|
|
|
# bad guy talking to someone, I want to know to whom
|
|
|
|
if 'Conn::IN_RESP' in e['_source']['details']['seenwhere'] \
|
|
|
|
or 'HTTP::IN_HOST_HEADER' in e['_source']['details']['seenwhere'] \
|
|
|
|
or 'DNS::IN_REQUEST' in e['_source']['details']['seenwhere']:
|
|
|
|
interestingaddres = e['_source']['details']['sourceipaddress']
|
|
|
|
elif 'Conn::IN_ORIG' in e['_source']['details']['seenwhere'] \
|
|
|
|
or 'HTTP::IN_X_CLUSTER_CLIENT_IP_HEADER' in e['_source']['details']['seenwhere'] \
|
|
|
|
or 'HTTP::IN_X_FORWARDED_FOR_HEADER' in e['_source']['details']['seenwhere']:
|
|
|
|
interestingaddres = e['_source']['details']['destinationipaddress']
|
|
|
|
|
|
|
|
summary += '{0} in {1} '.format(interestingaddres, e['_source']['details']['seenwhere'])
|
|
|
|
|
|
|
|
# Create the alert object based on these properties
|
|
|
|
return self.createAlertDict(summary, category, tags, aggreg['events'], severity)
|