-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathdialects.py
More file actions
324 lines (261 loc) · 7.72 KB
/
dialects.py
File metadata and controls
324 lines (261 loc) · 7.72 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2018 Fedele Mantuano (https://www.linkedin.com/in/fmantuano/)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
"""
This module is based on Analysis of SMTP dialects
(https://sissden.eu/blog/analysis-of-smtp-dialects)
"""
import datetime
import logging
import re
from operator import itemgetter
try:
from elasticsearch import Elasticsearch
from elasticsearch import ElasticsearchException
except ImportError:
raise ImportError("To use dialects module, you must use Elasticsearch")
try:
from modules.attachments import fingerprints
except ImportError:
from ...modules.attachments import fingerprints
log = logging.getLogger(__name__)
DIALECT_CLIENT_REGX_SORTED = [
(re.compile(r'(?:ehlo|helo)\s*', re.I), 0),
(re.compile(r'mail\s+from\s*:?\s*', re.I), 1),
(re.compile(r'rcpt\s+to\s*:?\s*', re.I), 2),
(re.compile(r'^[\b\s]*data[\b\s]*$', re.I), 3),
(re.compile(r'^[\b\s]*quit[\b\s]*$', re.I), 4),
]
# This query gets the code in postfix index
query_code = """
{
"query": {
"term": {
"message_id.keyword": {
"value": "%(message_id)s"
}
}
}
}
"""
# This query gets the client in postfix index
query_client = """
{
"query": {
"bool": {
"filter": {
"term": {
"tags": "client"
}
},
"must": [
{
"term": {
"code.keyword": {
"value": "%(code)s"
}
}
}
]
}
}
}
"""
# This query gets all communication from client and server
query_dialect = """
{
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": "%(timestamp)s||-30s",
"lte": "%(timestamp)s||+10s"
}
}
},
{
"term": {
"client_ip.keyword": {
"value": "%(client_ip)s"
}
}
},
{
"term": {
"client_name.keyword": {
"value": "%(client_name)s"
}
}
},
{
"term": {
"tags": {
"value": "dialect"
}
}
}
]
}
},
"sort": [
{
"@timestamp": {
"order": "asc"
}
}
]
}
"""
def get_elastic_indices(index_prefix="postfix-"):
"""
This function gets an Elastisearch index prefix
and returns a string comma-separated of indices of
yesterday and today.
The indices must be prefix-YEAR.MONTH.DAY
(postfix-2018.09.07)
Keyword Arguments:
index_prefix {str} -- prefix of Elasticsearch
indices (default: {"postfix-"})
Returns:
{str} -- list comma-separated of indices
"""
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
days = [today, yesterday]
indices = ",".join(["postfix-{}".format(
i.strftime("%Y.%m.%d")) for i in days])
return indices
def get_messages(message_id, elastic_server, index_prefix, max_size=100):
"""
This function returns a list with all SMTP messages between
client and server (dialect)
Arguments:
message_id {string} -- email message-id header
elastic_server {list/string} -- Elasticsearch server
index_prefix {string} -- prefix of Postfix index
Keyword Arguments:
max_size {int} -- Max size of messages to save (default: {100})
Returns:
list -- List tuples of SMTP messages. Every tuple has actor and message
"""
try:
# get indices to query
indices = get_elastic_indices(index_prefix)
# connect to Elasticsearch
es = Elasticsearch(hosts=elastic_server)
# From message_id get code of comunication from client and server
r = es.search(
index=indices,
body=query_code % {"message_id": message_id},
ignore_unavailable=True)
code = r["hits"]["hits"][0]["_source"]["code"]
timestamp = r["hits"]["hits"][0]["_source"]["@timestamp"]
log.debug("The code of {!r} is {!r}".format(message_id, code))
# From code get client (ip and name)
r = es.search(
index=indices,
body=query_client % {"code": code},
ignore_unavailable=True)
client_ip = r["hits"]["hits"][0]["_source"]["client_ip"]
client_name = r["hits"]["hits"][0]["_source"]["client_name"]
# From client get dialects
r = es.search(
index=indices,
body=query_dialect % {
"timestamp": timestamp,
"client_ip": client_ip,
"client_name": client_name},
size=max_size,
ignore_unavailable=True)
messages = [(i["_source"]["actor"],
i["_source"]["dialect"]) for i in r["hits"]["hits"]]
except ElasticsearchException:
log.exception(
"Failed query Elasticsearch for dialect: {!r}".format(message_id))
except IndexError:
log.debug("message-id {!r} not found".format(message_id))
else:
return messages
def get_messages_str(messages):
"""
From messeges list returns a string with all
conversation between client and server
Arguments:
messages {list} -- list of messages from get_messages
Returns:
str -- string of conversation
"""
messages_str = ""
for i in messages:
messages_str += "{}: {}\n".format(i[0], i[1])
return messages_str.strip("\n\t ")
def get_dialect(messages):
"""
This function gets only the client parts related
the SMTP command
Arguments:
messages {list} -- list of messages from get_messages
Returns:
list -- List of commands of client
"""
dialect = set()
for j in DIALECT_CLIENT_REGX_SORTED:
for i in messages:
if i[0] == "client":
r = j[0].findall(i[1])
if r:
dialect.add((r[0], j[1]))
else:
dialect = sorted(dialect, key=itemgetter(1))
return [i[0] for i in dialect]
def get_dialect_str(dialect):
"""
From list of dialect returns a string
Arguments:
dialect {list} -- output of get_dialect
Returns:
str -- string of client commands
"""
return " ".join(dialect)
def get_dialect_fingerprints(dialect):
"""
Given a dialect list returns the hashes of its string
version
Arguments:
dialect {list} -- output of get_dialect
Returns:
namedtuple -- fingerprints md5, sha1, sha256, sha512, ssdeep
"""
dialect_str = get_dialect_str(dialect)
return fingerprints(dialect_str)
def make_dialect_report(
message_id,
elastic_server,
index_prefix,
max_size=100
):
messages = get_messages(message_id, elastic_server, index_prefix, max_size)
if messages:
communication = get_messages_str(messages)
dialect = get_dialect(messages)
dialect_str = get_dialect_str(dialect)
report = {
"communication": communication,
"dialect": dialect_str}
report["md5"], report["sha1"], report["sha256"], \
report["sha512"], report["ssdeep"] = get_dialect_fingerprints(
dialect)
return report