-
Notifications
You must be signed in to change notification settings - Fork 0
/
snort_api.py
137 lines (115 loc) · 4.35 KB
/
snort_api.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
import os, threading, socket, json
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
from Queue import Queue
from time import time
from datetime import datetime
from subprocess import call
class ThreadPoolMixIn(ThreadingMixIn):
numThreads = 100
allow_reuse_address = True
def serve_forever(self):
self.requests = Queue(self.numThreads)
for x in range(self.numThreads):
t = threading.Thread(target = self.process_request_thread)
t.setDaemon(1)
t.start()
while True:
self.handle_request()
self.server_close()
def process_request_thread(self):
while True:
ThreadingMixIn.process_request_thread(self, *self.requests.get())
def handle_request(self):
try:
request, client_address = self.get_request()
except socket.error:
return
if self.verify_request(request, client_address):
self.requests.put((request, client_address))
class Handler(BaseHTTPRequestHandler):
def get_alerts(self,ip,port,time_interval):
dt_now = datetime.now()
alerts = {"alerts":[]}
ips = next(os.walk('/var/log/snort'))[1]
if ip in ips:
ip_file_path = '/var/log/snort/'+ip + '/'
alert_files = [f for f in os.listdir(ip_file_path) if os.path.isfile(ip_file_path + f) and f.split('-')[-1]==port]
if alert_files:
for alert_file in alert_files:
with open(ip_file_path + alert_file,'r') as f:
lines = f.readlines()
for i in range(len(lines)):
line = lines[i]
title = line.strip()
if title.startswith('[**]') and title.endswith('[**]'):
potential_alert = {"message":title[5:-5], "timestamp":"", "src_ip":"", "src_port":"", "dst_ip":"", "dst_port":""}
try:
next_line = lines[i+1].strip()
next_line_items = next_line.split(' ')
timestamp = datetime.strptime(str(dt_now.year)+'/'+next_line_items[0],'%Y/%m/%d-%H:%M:%S.%f')
potential_alert['src_ip'] = next_line_items[1].split(':')[0]
potential_alert['src_port'] = next_line_items[1].split(':')[1]
potential_alert['dst_ip'] = next_line_items[3].split(':')[0]
potential_alert['dst_port'] = next_line_items[3].split(':')[1]
if (datetime.now() - timestamp).total_seconds < time_interval:
potential_alert['timestamp'] = str(timestamp)
alerts["alerts"].append(potential_alert)
except Exception, e:
print e
return alerts
def get_rules(self):
with open('/etc/snort/rules/local.rules','r') as f:
lines = f.readlines()
rules = {"rules": []}
for line in lines:
if line.startswith('alert'):
rules['rules'].append(line.strip())
return rules
def update_rules(self,rules)
with open('/etc/snort/rules/local.rules','w') as f:
for rule in rules['rules']:
f.write(rule + '\n')
return 0
def do_GET(self):
try:
path_items = self.path.split('/')
if path_items[1] == 'get_alerts':
ip = path_items[2]
port = path_items[3]
time_interval = path_items[4]
alerts = self.get_alerts(ip,port,time_interval)
self.send_response(200)
self.end_headers()
data = json.dumps(alerts)
self.wfile.write(data.encode())
elif path_items[1] == 'get_rules':
self.send_response(200)
self.end_headers()
rules = self.get_rules()
data = json.dumps(rules)
self.wfile.write(data.encode())
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
try:
path_items = self.path.split('/')
if path_items[1] == 'update_rules':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
rules = json.loads(post_data)
update_rules(rules)
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
class ThreadedHTTPServer(ThreadPoolMixIn, HTTPServer):
pass
def start_snort():
print('Starting snort!')
call(['snort','-d','-K','ascii'])
if __name__ == '__main__':
t = threading.Thread(target = start_snort)
t.setDaemon(1)
t.start()
print ('Starting server, use <Ctrl-C> to stop')
server = ThreadedHTTPServer(('', 8080), Handler)
server.serve_forever()