-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPython_Elastic.py
More file actions
executable file
·181 lines (164 loc) · 4.79 KB
/
Python_Elastic.py
File metadata and controls
executable file
·181 lines (164 loc) · 4.79 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
#!/usr/bin/env python
from __future__ import print_function
from elasticsearch import Elasticsearch
import logging
from dateutil.parser import parse as parse_date
"""
Querying Elastic search for threat data
Most of this is copied from:
http://blog.arisetyo.com/getting-started-with-python-elasticsearch-client/
"""
EHOST='HOST'
INDEX='INDEX'
##### Print The Generic Hits
def print_hits(results, facet_masks={}):
" Simple utility function to print results of a search query. "
print('=' * 80)
print('Total %d found in %dms' % (results['hits']['total'], results['took']))
if results['hits']['hits']:
print('-' * 80)
for hit in results['hits']['hits']:
# get created date for a repo and fallback to authored_date for a commit
created_at = parse_date(hit['_source'].get('created_at', hit['_source']['@timestamp']))
print('/%s/%s/%s (%s): %s' % (
hit['_index'], hit['_type'], hit['_id'],
created_at.strftime('%Y-%m-%d'),
hit['_source']['message'].replace('\n', ' ')))
for facet, mask in facet_masks.items():
print('-' * 80)
for d in results['facets'][facet]['terms']:
print(mask % d)
print('=' * 80)
print()
##### Print The SSH Counts
def print_ssh_counts(results, facet_masks={}):
" Simple utility function to print results of a search query. "
print('=' * 80)
print('SSH Bruteforcers')
if results['hits']['hits']:
print('-' * 80)
for hit in results['hits']['hits']:
print(hit)
# get created date for a repo and fallback to authored_date for a commit
created_at = parse_date(hit['_source'].get('created_at', hit['_source']['@timestamp']))
print('/%s/%s/%s (%s): %s' % (
hit['_index'], hit['_type'], hit['_id'],
created_at.strftime('%Y-%m-%d'),
hit['_source']['message'].replace('\n', ' ')))
for facet, mask in facet_masks.items():
print('-' * 80)
for d in results['facets'][facet]['terms']:
print(mask % d)
print('=' * 80)
print()
##### Print The SSH Usernames
def print_ssh_usernames(lresult, facet_masks={}):
" Simple utility function to print results of a search query. "
print('=' * 80)
print('SSH Usernames and Passwords')
print('-' * 80)
for results in lresult:
for hit in results['hits']['hits']:
#print(hit['_source']['user'],'\n')
# get created date for a repo and fallback to authored_date for a commit
created_at = parse_date(hit['_source'].get('created_at', hit['_source']['@timestamp']))
try:
print('%s: %s' % (hit['_source']['user'],hit['_source']['pass']))
except:
pass
for facet, mask in facet_masks.items():
print('-' * 80)
for d in results['facets'][facet]['terms']:
print(mask % d)
print('=' * 80)
print()
# by default we don't sniff, ever
es = Elasticsearch(host=EHOST, port=9200)
##### GET The SSH Counts
result = es.search(
index=INDEX,
body={
'size': 0,
'query': {
'filtered': {
'filter': {
'query': {
'filtered': {
'filter': {
'term': {
'_type': 'SSHPOT_sshlog'
}
}
}
}
}
}
},
'facets': {
'terms': {
'terms': {
'field': 'id.orig_h',
'order': 'count'
}
}
}
}
)
print_ssh_counts(result,{'terms': '%(term)15s: %(count)3d'})
##### Print GET Usernames and Passwords
result = es.search(
index=INDEX,
body={
'size': 0,
'query': {
'filtered': {
'filter': {
'query': {
'filtered': {
'filter': {
'term': {
'_type': 'SSHPOT_sshlog'
}
}
}
}
}
}
}
}
)
size = result['hits']['total']
"""how many times to run the loop each loop has 500 results"""
qsize = 500
lresult = []
if size == 0:
pass
elif size <= qsize:
itertations = 1
else:
iterations = (size/qsize)+1
for i in range(0,iterations):
start = (qsize*i)
result = es.search(
index=INDEX,
body={
'from': start, 'size': qsize,
'query': {
'filtered': {
'filter': {
'query': {
'filtered': {
'filter': {
'term': {
'_type': 'SSHPOT_sshlog'
}
}
}
}
}
}
}
}
)
lresult.append(result)
print_ssh_usernames(lresult)