-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESTools.py
More file actions
executable file
·368 lines (293 loc) · 9.2 KB
/
Copy pathESTools.py
File metadata and controls
executable file
·368 lines (293 loc) · 9.2 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/python
host = "127.0.0.1"
from elasticsearch import Elasticsearch
import sys
import base64
import time
import datetime
def del_by_query(es):
if len(sys.argv)<6:
print("Usage:<host><cmd><index><docType><field><value>")
sys.exit(-1)
index = sys.argv[3]
doc = sys.argv[4]
field = sys.argv[5]
value = sys.argv[6]
query = {
"query": {
"term" : { field: value }
}
}
print(query)
print(es.count(index,doc,query)['count'])
es.delete_by_query(index,query,doc)
def del_es_index(es):
if len(sys.argv)<4:
print("Usage: <host> <cmd> <index>")
sys.exit(-1)
index = sys.argv[3]
indices = es.indices.get(index).keys()
for ind in indices:
es.indices.delete(ind)
def find_terms_topN(es):
if len(sys.argv)<8:
print("Usage:<host><cmd><index><doctype><field><topN><isDesc>")
sys.exit(-1)
index = sys.argv[3]
doc = sys.argv[4]
field = sys.argv[5]
topN = int(sys.argv[6])
orderS ="asc"
if sys.argv[7] == "true":
orderS = "desc"
query = {
"query": {
"match_all": {}
},
"size":0,
"aggs": {
"group_terms": {
"terms": {"field": field,
"order" : { "_count" : orderS},
"size":topN
}
}
}
}
results = es.search(index,doc,query)['aggregations']['group_terms']['buckets']
for item in results:
print(item['key'])
def get_http_unusual_server_and_port(es):
index = "log_stream_session_tcp_*"
doc = "esdatabase_doc"
query = {
"query":{
"bool":{
"must_not":[
{"term":{"dstPort":80}},
{"term":{"dstPort":8080}}
],
"must":{
"term":{"app.uprotocol":"http"}
}
}
}
}
count = es.count(index,doc,query)['count']
if count == 0:
print("No HTTP Servers Info that port is not 80 and 8080!")
else:
aggs = {
"group_by_dstIP":{
"terms":{
"field":"dstIP",
"order":{"_count":"desc"},
"size":count
},
"aggs":{
"group_by_dstPort":{
"terms":{
"field":"dstPort",
"order":{"_count":"desc"},
"size":count
}
}
}
}
}
query['size']=0
query['aggs']=aggs
results = es.search(index,doc,query)['aggregations']['group_by_dstIP']['buckets']
for ipkey in results:
ip = ipkey['key']
ports = "[ "
for port in ipkey['group_by_dstPort']['buckets']:
ports= ports+str(port['key'])+" "
ports= ports+"]"
print(ip+" "+ports)
def get_mail_user_passwd(es):
#from tcp stream session get pop3 username and passwd
stream_index = "log_stream_session_tcp_*"
mail_index = "log_tcp_session_mail_*"
doc = "esdatabase_doc"
userList = set()
stream_query = {
"query":{
"bool":{
"must":[
{"term":{"app.uprotocol":"pop3"}},
{"range":{"reqDataSize":{"gte":10}}}
]
}
}
}
count = es.count(stream_index,doc,stream_query)['count']
if count>0:
stream_query['from']=0
stream_query['size']=count
hits = es.search(stream_index,doc,stream_query)['hits']['hits']
for hit in hits:
src = hit['_source']
reqData = src['reqData']
text = base64.b64decode(reqData)
userIndex = text.find('USER')
passIndex = text.find('PASS')
if userIndex != -1 and passIndex != -1:
userName = text[userIndex+5:passIndex-2]
passStart = text[passIndex+5:]
passEndIndex = passStart.find('\n')
passEndIndexR = passStart.find('\r')
pssInd = passEndIndex
if passEndIndexR!=-1:
pssInd = passEndIndexR
passwd = passStart[0:pssInd]
userList.add(userName+' '+passwd)
#from mail session
mail_query = {
"query":{
"match_all":{}
}
}
count = es.count(mail_index,doc,mail_query)['count']
if count>0:
mail_query['size'] = 0
mail_query['aggs'] = {
"group_by_user":{
"terms":{
"field":"userName",
"size":count
},
"aggs":{
"group_by_passwd":{
"terms":{
"field":"passwd",
'size':count
}
}
}
}
}
mail_results = es.search(mail_index,doc,mail_query)['aggregations']['group_by_user']['buckets']
for userNameKey in mail_results:
user = userNameKey['key']
for passwd in userNameKey['group_by_passwd']['buckets']:
pas = passwd['key']
if len(pas)>=2 and pas[0]=='"' and pas[len(pas)-1]=='"':
pas=pas[1:len(pas)-1]
userList.add(user+' '+pas)
for user in userList:
print(user)
def count(es):
index = None
if len(sys.argv)>=4:
index = sys.argv[3]
print(es.count(index)['count'])
def catIndices(es):
index = "*"
if len(sys.argv)>=4:
index = sys.argv[3]
results = es.cat.indices(index,v=True)
print(results)
def getIndices(es):
index = "*"
if len(sys.argv)>=4:
index = sys.argv[3]
return es.indices.get(index).keys()
def getClosedIndices(es):
index = "*"
if len(sys.argv)>=4:
index = sys.argv[3]
results = es.cat.indices(index).split("\n")
mres = []
for k in results:
if len(k)==0 or k.find('close')==-1:
continue
i = 0
ksplits = list(filter(lambda s:False if len(s) == 0 else True, k.split(' ')))
for ki in ksplits:
if ki == 'close':
mres.append(ksplits[i+1])
break
i=i+1
return mres
def getOpenedIndices(es):
index = "*"
if len(sys.argv)>=4:
index = sys.argv[3]
results = es.cat.indices(index).split("\n")
mres = []
for k in results:
if len(k)==0 or k.find('open')==-1:
continue
i = 0
ksplits = list(filter(lambda s:False if len(s) == 0 else True, k.split(' ')))
for ki in ksplits:
if ki == 'open':
mres.append(ksplits[i+1])
break
i=i+1
return mres
def openIndices(es):
index = "*"
if len(sys.argv)>=4:
index = sys.argv[3]
indices = getClosedIndices(es)
for k in indices:
es.indices.open(k,timeout='30m')
def closeIndices(es):
index = "*"
if len(sys.argv)>=4:
index = sys.argv[3]
indices = getOpenedIndices(es)
for k in indices:
es.indices.close(k,timeout='30m')
def getTimeFromIndex(index):
dstr = index[index.rfind('_')+1:]
ta = time.strptime(dstr, "%Y.%m.%d")
return int(time.mktime(ta))
def getTimeFromDateNumber(dn):
dayAgo = (datetime.datetime.now() - datetime.timedelta(days = dn))
return int(time.mktime(dayAgo.timetuple()))
def isOldIndex(index,oldTime):
indexTime = getTimeFromIndex(index)
return indexTime<=oldTime
def closeIndicesWithDate(es):
if len(sys.argv)<5:
print('Usage: <host> <cmd> <index> <dateNumber>')
sys.exit(-1)
index = sys.argv[3]
dn = int(sys.argv[4])
oldTime = getTimeFromDateNumber(dn)
indices = getOpenedIndices(es)
for ind in indices:
if isOldIndex(ind,oldTime):
es.indices.close(ind,timeout='30m')
def handle_cmd(es,cmd):
if cmd == "del":
del_es_index(es)
elif cmd == "term_topN":
find_terms_topN(es)
elif cmd == "http_servers":
get_http_unusual_server_and_port(es)
elif cmd == 'mail_users':
get_mail_user_passwd(es)
elif cmd == 'count':
count(es)
elif cmd == 'del_by_query':
del_by_query(es)
elif cmd == 'cat':
catIndices(es)
elif cmd == 'open':
openIndices(es)
elif cmd == 'close':
closeIndices(es)
elif cmd == 'closeWithDate':
closeIndicesWithDate(es)
if __name__ == '__main__':
if len(sys.argv)<3:
print("Usage: <host> <cmd> [args]")
sys.exit(-1)
host = sys.argv[1]
cmd = sys.argv[2]
es = Elasticsearch(host)
handle_cmd(es,cmd)