forked from MatthewClarkMay/geoip-attack-map
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataServer_v2.py
385 lines (338 loc) · 14 KB
/
DataServer_v2.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from elasticsearch import Elasticsearch
from flask import Flask, render_template
import json
import redis
import datetime
import time
import pickle
import tornado.ioloop
import tornado.web
import tornado.websocket
app = Flask(__name__)
es = Elasticsearch('127.0.0.1:64298')
es2 = Elasticsearch('snorlax.true.nl:9200')
redis_ip = '127.0.0.1'
redis_instance = None
dst_ip = "87.233.192.218"
dst_lat = 52.305610
dst_long = 4.932533
event_count = 1
ips_tracked = {}
ports = {}
ip_to_code = {}
countries_tracked = {}
continent_tracked = {}
service_rgb = {
'FTP': '#ff0000',
'SSH': '#ff8000',
'TELNET': '#ffff00',
'EMAIL': '#80ff00',
'WHOIS': '#00ff00',
'DNS': '#00ff80',
'HTTP': '#00ffff',
'HTTPS': '#0080ff',
'VNC': '#0000ff',
'SNMP': '#8000ff',
'SMB': '#bf00ff',
'AUTH': '#ff00ff',
'RDP': '#ff0060',
'SIP': '#ff0000',
'ICMP': '#ffcccc',
'OTHER': '#6600cc'
}
def connect_redis(redis_ip):
r = redis.StrictRedis(host=redis_ip, port=6379, db=0)
return r
def get_honeypot_data():
anti_dedup = []
processed_data = []
banned_src = ["8.8.8.8","87.233.192.218"]
time_last_request = datetime.datetime.utcnow()
while True:
tmp = str(time_last_request).split(" ")
ES_query = {"query": {
"bool": {
"must": {
"range": {
"@timestamp": {
"gte": tmp[0] + "T" + tmp[1]
}
}
}
}
}
}
res = es.search(index="logstash-*", size=100, body=ES_query)
# res2 = es2.search(index="logstash-*", size=100, body=ES_query)
# print res2
hits = res['hits']
# hits.update(res2['hits'])
#print str(datetime.datetime.now() - time_last_request) + " Got ES1 "+ str(res['hits']['total']) #+ "and ES2 "+ str(res2['hits']['total']) + " Hits:"
if len(hits['hits']) != 0:
time_last_request = datetime.datetime.utcnow()
for hit in hits['hits']:
try:
#print json.dumps(hit)
if not (hit["_id"] in anti_dedup or hit["_source"]["src_ip"] in banned_src):
process_datas = process_data(hit)
if process_datas != None:
processed_data.append(process_datas)
except:
pass
if len(processed_data) != 0:
push(processed_data)
processed_data = []
time.sleep(1)
#exit()
def process_data(hit):
global dst_ip,dst_lat,dst_long
alert = {}
alert["as_org"] = hit["_source"]["geoip"].get("as_org", "")
alert["country"] = hit["_source"]["geoip"].get("country_name", "")
alert["country_code"] = hit["_source"]["geoip"].get("country_code2", "")
alert["continent_code"] = hit["_source"]["geoip"].get("continent_code", "")
alert["dst_lat"] = dst_lat
alert["dst_long"] = dst_long
alert["dst_ip"] = dst_ip
alert["event_time"] = str(hit["_source"]["@timestamp"][0:10]) + " " + str(hit["_source"]["@timestamp"][11:19])
alert["iso_code"] = hit["_source"]["geoip"]["country_code2"]
alert["latitude"] = hit["_source"]["geoip"]["latitude"]
alert["longitude"] = hit["_source"]["geoip"]["longitude"]
#print hit["_source"]["type"]
# if hit["_source"]["type"] == "NGINX":
# alert["src_ip"] == ""
# if hit["_source"]["type"] == "Cowrie":
# alert["detect_source"] = "Cowrie"
# alert["dst_port"] = "unknown"
# alert["msg_type"] = hit["_source"]["message"]
# alert["protocol"] = "OTHER"
# alert["src_ip"] = hit["_source"]["src_ip"]
# alert["src_port"] = "0"
# if "SSH" in hit["_source"]["message"] or ":2222" in hit["_source"]["message"] or ":22" in hit["_source"]["message"] or "SSH" in hit["_source"]["system"]:
# alert["dst_port"] = 22
# alert["protocol"] = "SSH"
# if "Telnet" in hit["_source"]["system"]:
# alert["dst_port"] = 23
# alert["protocol"] = "Telnet"
# if alert["dst_port"] == "unknown":
# print "unknown"
# print hit
# time.sleep(5)
#
# elif hit["_source"]["type"] == "Glastopf":
# alert["detect_source"] = "Glastopf"
# alert["dst_port"] = hit["_source"]["dest_port"]
# alert["msg_type"] = hit["_source"]["message"]
# alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
# alert["src_ip"] = hit["_source"]["src_ip"]
# alert["src_port"] = "0"
# elif hit["_source"]["type"] == "Dionaea":
# alert["detect_source"] = "Dionaea"
# alert["dst_port"] = hit["_source"]["dest_port"]
# alert["msg_type"] = str(hit["_source"]["connection"]["protocol"]) + str(hit["_source"]["connection"]["type"])
# alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
# #alert["protocol"] = str(hit["_source"]["connection"]["protocol"]) + ""
# alert["src_ip"] = hit["_source"]["src_ip"]
# alert["src_port"] = "0"
# elif hit["_source"]["type"] == "Honeytrap":
# alert["detect_source"] = "Honeytrap"
# alert["dst_port"] = hit["_source"]["dest_port"]
# alert["msg_type"] = hit["_source"].get("payload", 1)
# alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
# alert["src_ip"] = hit["_source"]["src_ip"]
# alert["src_port"] = "0"
# elif hit["_source"]["type"] == "Vnclowpot":
# alert["detect_source"] = "Vnclowpot"
# alert["dst_port"] = hit["_source"]["dest_port"]
# alert["msg_type"] = hit["_source"].get("payload", 1)
# alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
# alert["src_ip"] = hit["_source"]["src_ip"]
# alert["src_port"] = hit["_source"]["src_port"]
# elif hit["_source"]["type"] == "Rdpy":
# alert["detect_source"] = "Rdpy"
# alert["dst_port"] = hit["_source"]["dest_port"]
# alert["msg_type"] = hit["_source"].get("message", 1)
# alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
# alert["src_ip"] = hit["_source"]["src_ip"]
# alert["src_port"] = hit["_source"]["src_port"]
# elif hit["_source"]["type"] == "Heralding":
# print json.dumps(hit)
# alert["detect_source"] = "Heralding"
# alert["dst_port"] = hit["_source"]["dest_port"]
# alert["msg_type"] = hit["_source"].get("message", 1)
# alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
# alert["src_ip"] = hit["_source"]["src_ip"]
# alert["src_port"] = hit["_source"]["src_port"]
# elif hit["_source"]["type"] == "log":
# pass
print hit["_source"]["type"]
if hit["_source"]["type"] == "Mailoney":
alert["detect_source"] = "Mailoney"
alert["dst_port"] = hit["_source"]["dest_port"]
alert["msg_type"] = hit["_source"].get("message", 1)
alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
alert["src_ip"] = hit["_source"]["src_ip"]
alert["src_port"] = hit["_source"]["src_port"]
elif hit["_source"]["type"] == "Heralding":
alert["detect_source"] = "Heralding"
alert["dst_port"] = hit["_source"]["dest_port"]
alert["msg_type"] = hit["_source"].get("ip_rep", 1)
alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
print alert["protocol"],hit["_source"]["dest_port"]
alert["src_ip"] = hit["_source"]["src_ip"]
alert["src_port"] = hit["_source"]["src_port"]
elif hit["_source"]["type"] == "Rdpy":
alert["detect_source"] = "Rdpy"
alert["dst_port"] = hit["_source"]["dest_port"]
alert["msg_type"] = hit["_source"].get("message", 1)
alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
alert["src_ip"] = hit["_source"]["src_ip"]
alert["src_port"] = hit["_source"]["src_port"]
elif hit["_source"]["type"] == "Tanner":
alert["detect_source"] = "Tanner"
alert["dst_port"] = hit["_source"]["dest_port"]
alert["msg_type"] = hit["_source"]["path"]
alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
alert["src_ip"] = hit["_source"]["src_ip"]
alert["src_port"] = hit["_source"]["src_port"]
elif hit["_source"]["type"] == "Honeypy":
alert["detect_source"] = "Honeypy"
alert["dst_port"] = hit["_source"]["dest_port"]
alert["msg_type"] = hit["_source"]["dest_port"]
alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
alert["src_ip"] = hit["_source"]["src_ip"]
alert["src_port"] = "0"
elif hit["_source"]["type"] == "Cowrie":
try:
if hit["_source"]["dest_port"] == 23 or hit["_source"]["dest_port"] == 2323:
alert["dst_port"] = 23
alert["protocol"] = "TELNET"
elif "SSH" in hit["_source"]["message"] or ":2222" in hit["_source"]["message"] or ":22" in hit["_source"]["message"] or "SSH" in hit["_source"]["system"]:
alert["dst_port"] = 22
alert["protocol"] = "SSH"
alert["detect_source"] = "Cowrie"
alert["src_port"] = "0"
alert["msg_type"] = hit["_source"].get("message", 1)
alert["src_ip"] = hit["_source"]["src_ip"]
except:
pass
#print json.dumps(hit)
elif hit["_source"]["type"] == "Dionaea":
alert["detect_source"] = "Dionaea"
alert["dst_port"] = hit["_source"]["dest_port"]
alert["msg_type"] = hit["_source"].get("message", 1)
alert["protocol"] = port_to_type(hit["_source"]["dest_port"])
alert["src_ip"] = hit["_source"]["src_ip"]
alert["src_port"] = hit["_source"]["src_port"]
else:
print "no type matched", json.dumps(hit)
#time.sleep(5)
#time.sleep(5)
# print hit
return
if not alert["src_ip"] == "":
alert["color"] = service_rgb[alert["protocol"].upper()]
return alert
else:
print "SRC IP EMPTY"
def port_to_type(port):
port = int(port)
try:
if port == 80 or port == 8080:
return "HTTP"
if port == 21:
return "FTP"
if port == 22 :
return "SSH"
if port == 23 or port == 2223:
return "TELNET"
if port == 25 or port == 143 or port == 110 or port == 993 or port == 995:
return "EMAIL"
if port == 53:
return "DNS"
if port == 443 or port == 4443 or port == 8443:
return "HTTPS"
if port == 5900:
return "VNC"
if port == 161 or port == 1900:
return "SNMP"
if port == 445:
return "SMB"
if port == 3389: #or port == 5900:
return "RDP"
if port == 5060: #or port == 5900:
return "SIP"
else:
return "OTHER"
except:
return "OTHER"
def push(alerts):
global ips_tracked,continent_tracked,countries_tracked, ip_to_code, ports, event_count
redis_instance = connect_redis(redis_ip)
for alert in alerts:
ips_tracked[alert["src_ip"]] = ips_tracked.get(alert["src_ip"], 1) + 1
continent_tracked[alert["continent_code"]] = ips_tracked.get(alert["continent_code"], 1) + 1
countries_tracked[alert["country"]] = countries_tracked.get(alert["country"], 1) + 1
ip_to_code[alert["src_ip"]] = alert["iso_code"]
ports[alert["dst_port"]] = ports.get(alert["dst_port"], 0)+ 1
json_data = {
"protocol": alert["protocol"],
"color": alert["color"],
"postal_code": "null",
"iso_code": alert["iso_code"],
"continent": "South America",
"type3": "source:"+alert["detect_source"]+" port: "+str(alert["dst_port"]),
"type2": alert["dst_port"],
"city": alert["as_org"],
"ips_tracked": {
"170.246.70.172": 1
},
"src_port": alert["src_port"],
"event_time": alert["event_time"],
"src_lat": alert["latitude"],
"src_ip": alert["src_ip"],
"continents_tracked": {
"Europe": 30
},
"type": "Traffic",
"country_to_code": {
"Brazil": "BR"
},
"dst_long": alert["dst_long"],
"continent_code": "SA",
"dst_lat": alert["dst_lat"],
"ip_to_code": {
"170.246.70.172": "BR"
},
"countries_tracked": {
"Brazil": 25
},
"event_count": event_count,
"country": alert["country"],
"src_long": alert["longitude"],
"unknowns": {
},
"dst_port": alert["dst_port"],
"dst_ip": "172.23.0.2"
}
json_data["ips_tracked"] = ips_tracked
event_count+=1
json_data["ip_to_code"] = ip_to_code
#json_data["continents_tracked"] = continent_tracked
json_data["countries_tracked"] = countries_tracked
tmp = json.dumps(json_data)
time.sleep(0.2)
#print tmp
redis_instance.publish('attack-map-production', tmp)
if __name__ == '__main__':
try:
while True:
# get_honeypot_data()
try:
get_honeypot_data()
except:
print "failed"
time.sleep(5)
except KeyboardInterrupt:
print('\nSHUTTING DOWN')
exit()