This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ElasticDetector.py
executable file
·191 lines (160 loc) · 6.6 KB
/
ElasticDetector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 - 2018 Bernat Mut <[email protected]>.
#
# This file is part of Alienvault/OSSIM
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# GLOBAL IMPORTS
#
from elasticsearch import Elasticsearch
from elasticsearch.client import IndicesClient
import logging
import time
class ElasticDetector(object):
def __init__(self, es_host, plugin_name, store_index='ossim_index',
verify_certs=True, windows_size=50, credentials=None):
if not verify_certs:
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
urllib3.disable_warnings(UserWarning)
self._es = Elasticsearch([es_host], verify_certs=verify_certs, http_auth=credentials)
self._store_index = store_index
self.plugin_name = plugin_name
self.rule_name = ""
self.plugin_sid = 1
self._create_index()
self._scroll_time = '10m'
self._windows_size = windows_size
def _create_index(self):
es_index = IndicesClient(self._es)
if es_index.exists(self._store_index):
logging.info('Index ' + self._store_index + ' already exists. Skipping index creation.')
return None
es_mapping = {
"mappings": {
'last_runtime': {
'properties': {
'plugin_name': {'index': 'not_analyzed', 'type': 'string'},
'rule_name': {'index': 'not_analyzed', 'type': 'string'},
'plugin_sid': {'index': 'not_analyzed', 'type': 'long'},
'@timestamp': {'format': 'dateOptionalTime||epoch_millis', 'type': 'date'}
}
}
}
}
self._es.indices.create(self._store_index, body=es_mapping)
time.sleep(1)
def delete_store_index(self):
self._es.indices.delete(index=self._store_index)
def clean_store_index(self):
query = {
"query": {
"query_string": {
"query": "rule_name:{0} AND plugin_name:{1} AND plugin_sid: {2}".format(
self.rule_name, self.plugin_name, self.plugin_sid)
}
},
}
self._es.delete_by_query(self._store_index, doc_type='last_runtime', query=query)
def insert_timestamp(self, delete_older=True):
current_timestamp = self._get_current_timestamp()
if delete_older:
self.delete_store_index()
logging.debug("timestamp {}".format(current_timestamp))
self._es.index(self._store_index, doc_type='last_runtime',
body={'@timestamp': self._get_current_timestamp(), 'plugin_name': self.plugin_name,
'rule_name': self.rule_name, 'plugin_sid': self.plugin_sid})
@staticmethod
def _get_current_timestamp(offset_seconds=0):
ts_epoch = round((time.time() + offset_seconds) * 1000)
return int(ts_epoch)
def get_last_timestamp(self):
query = {
"query": {
"query_string": {
"query": "rule_name:{0} AND plugin_name:{1} AND plugin_sid: {2}".format(
self.rule_name, self.plugin_name, self.plugin_sid)
}
},
"sort": {'@timestamp': {'order': 'desc'}}
}
res = self._es.search(index=self._store_index, body=query, size=1)
hits = res['hits']['hits']
logging.info("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
logging.info(hit)
if res['hits']['total'] > 0:
return int(hits[0]['_source']['@timestamp'])
else:
return self._get_current_timestamp(-3600)
def get_matches_since(self, data_index, timestamp, query):
logging.debug('timestamp _get_matches_since: {}'.format(timestamp))
query = {"query": {
"bool": {
"must": [
{
"query_string": {
"analyze_wildcard": True,
"query": query
}
},
{
"range": {
"@timestamp": {
"gte": timestamp,
"lte": self._get_current_timestamp(),
"format": "epoch_millis"
}
}
}
],
"must_not": [
]
}
}
}
ds_count = self._windows_size
skip = 0
while ds_count == self._windows_size:
res = self._es.search(index=data_index, body=query, size=self._windows_size, from_=skip)
skip += self._windows_size
ds_count = len(res['hits']['hits'])
for doc in res['hits']['hits']:
yield doc
def do_something(self, data_index="*", query="*", fields=None, timestamp=None):
if not timestamp:
timestamp = self.get_last_timestamp()
documents = self.get_matches_since(data_index=data_index, timestamp=timestamp, query=query)
for doc in documents:
logging.info(doc)
logging.info("")
source = DotAccessibleDict(doc['_source'])
group = []
for field in fields:
group.append(source[field])
logging.info(group)
break
logging.info("timestamp {}".format(timestamp))
class DotAccessibleDict(object):
def __init__(self, data):
self._data = data
def __str__(self):
return str(self._data)
def __getitem__(self, name):
val = self._data
for key in name.split('.'):
val = val[key]
return val