This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSessionParser.py
278 lines (205 loc) · 8.26 KB
/
SessionParser.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
#
# License:
#
# Copyright (c) 2003-2006 ossim.net
# Copyright (c) 2007-2014 AlienVault
# All rights reserved.
#
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
# You may not use, modify or distribute this program under any other version
# of the GNU General Public License.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA
#
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL-2'.
#
# Otherwise you can read it here: http://www.gnu.org/licenses/gpl-2.0.txt
#
#
# GLOBAL IMPORTS
#
import re
from sets import Set
import sgmllib
import socket
class SessionParser(sgmllib.SGMLParser):
def parse(self, s):
"Parse the given string 's'."
self.feed(s)
self.close()
def __init__(self, verbose=0):
sgmllib.SGMLParser.__init__(self, verbose)
self.starting_description = 0
self.inside_table_element = 0
self.inside_table_row = 0
self.in_session = 0
self.td_count = 0
self.seen = 0
self.sessions = []
self.session_num = 0
self.queried_ip = ""
self.dst_port_group = {}
self.src_port_group = {}
self.source_ip = ""
self.source_port = ""
self.dest_ip = ""
self.dest_port = ""
self.data_sent = 0
self.data_rcvd = 0
self.active_since = ""
self.last_seen = ""
self.duration = 0
self.inactive = 0
def tosecs(self, data):
# example: 27 sec
if data.endswith('sec'):
data = data.split(' ', 1)[0]
# examples: 1 day 1:52:27
# 2 days 4:56:23
elif data.__contains__('day'):
result = re.findall('(\d+) days? (\S+)', data)
(days, time) = result[0]
seconds = int(days) * 86400
dt = time.split(':')
seconds += int(dt.pop())
try:
seconds += seconds + int(dt.pop()) * 60
seconds += seconds + int(dt.pop()) * 3600
except IndexError:
pass
data = str(seconds)
# example: 1:52:27
else:
dt = data.split(':')
seconds = int(dt.pop())
try:
seconds += int(dt.pop()) * 60
seconds += int(dt.pop()) * 3600
except IndexError:
pass
data = str(seconds)
return data
def start_td(self, attributes):
self.td_count += 1
self.inside_table_element = 1
def end_td(self):
self.inside_table_element = 0
self.seen = 0
def start_tr(self, attributes):
self.inside_table_row = 1
def end_tr(self):
self.inside_table_row = 0
self.td_count = 0
if self.in_session and self.source_ip:
self.session_num += 1
tmp = "%s:%s --> %s:%s (%f %f) duration: %s" % (self.source_ip,self.source_port,self.dest_ip,self.dest_port, self.data_sent, self.data_rcvd, self.duration)
self.sessions.append(tmp)
def start_a(self, attributes):
self.inside_table_row = 1
for name, value in attributes:
if name == "href" and self.in_session:
matches = re.findall("\d+\.\d+\.\d+\.\d+",value)
if len(matches) > 0:
if self.td_count is 1:
self.source_ip = matches[0]
elif self.td_count is 2:
self.dest_ip = matches[0]
def handle_data(self, data):
if self.queried_ip is "":
matches = re.findall("(\d+\.\d+\.\d+\.\d+)",data)
if len(matches) > 0:
self.queried_ip = matches[0]
if data.__contains__("Active TCP/UDP Sessions"):
self.in_session = 1
if data.__contains__("The color of the host"):
self.sessions.append("NumSessions:%d" % int(self.session_num))
self.in_session = 0
source_sessions = 0
dest_sessions = 0
for sess in self.sessions:
src_str = "^%s:.*" % self.queried_ip
dst_str = "^\d+\.\d+\.\d+\.\d+:\S+\s+-->\s+%s:.*" % self.queried_ip
src_sess = "^%s:\S+\s+-->\s+(\d+\.\d+\.\d+\.\d+):(\S+)" % self.queried_ip
dst_sess = "^(\d+\.\d+\.\d+\.\d+):\S+\s+-->\s+%s:(\S+)" % self.queried_ip
if re.findall(src_str, sess):
source_sessions += 1
if re.findall(dst_str, sess):
dest_sessions += 1
matches = re.findall(src_sess, sess)
if len(matches) > 0:
if matches[0][1] in self.src_port_group:
self.src_port_group[matches[0][1]].add(matches[0][0])
else:
self.src_port_group[matches[0][1]] = Set()
self.src_port_group[matches[0][1]].add(matches[0][0])
matches = re.findall(dst_sess, sess)
if len(matches) > 0:
if matches[0][1] in self.dst_port_group:
self.dst_port_group[matches[0][1]].add(matches[0][0])
else:
self.dst_port_group[matches[0][1]] = Set()
self.dst_port_group[matches[0][1]].add(matches[0][0])
self.sessions.append("SessionsAsSource:%s" % source_sessions)
self.sessions.append("SessionsAsDest:%s" % dest_sessions)
for port in self.src_port_group:
self.sessions.append("UniqPort%sAsSourceSessions:%d" % (port, len(self.src_port_group[port])))
for port in self.dst_port_group:
self.sessions.append("UniqPort%sAsDestSessions:%d" % (port, len(self.dst_port_group[port])))
if self.inside_table_element and self.in_session:
if self.td_count <= 2:
matches = re.findall(":(\w+)",data)
if len(matches) > 0:
try:
port = socket.getservbyname(matches[0])
except:
port = matches[0]
if self.td_count is 1:
self.source_port = port
elif self.td_count is 2:
self.dest_port = port
elif self.td_count is 3:
if self.seen == 0:
self.data_sent = float(data)
self.seen = 1
else:
if data.__contains__("KB"):
self.data_sent *= 1024
elif data.__contains__("MB"):
self.data_sent *= 1024 * 1024
elif data.__contains__("GB"):
self.data_sent *= 1024 * 1024 * 1024
self.seen = 0
elif self.td_count is 4:
if self.seen == 0:
self.data_rcvd = float(data)
self.seen = 1
else:
if data.__contains__("KB"):
self.data_rcvd *= 1024
elif data.__contains__("MB"):
self.data_rcvd *= 1024 * 1024
elif data.__contains__("GB"):
self.data_rcvd *= 1024 * 1024 * 1024
self.seen = 0
elif self.td_count is 5:
self.active_since = data
elif self.td_count is 6:
self.last_seen = data
elif self.td_count is 7:
self.duration = self.tosecs(data)
elif self.td_count is 8:
self.inactive = self.tosecs(data)
def get_sessions(self):
return self.sessions