forked from bitkeks/python-netflow-v9-softflowd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.py
executable file
·263 lines (217 loc) · 9.53 KB
/
analyzer.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
#!/usr/bin/env python3
"""
Example analyzing script for saved exports (by main.py, as JSON).
This file belongs to https://github.com/bitkeks/python-netflow-v9-softflowd.
Copyright 2017-2019 Dominik Pataky <[email protected]>
Licensed under MIT License. See LICENSE.
"""
import argparse
from collections import namedtuple
import contextlib
from datetime import datetime
import functools
import gzip
import ipaddress
import json
import logging
import os.path
import socket
import sys
Pair = namedtuple('Pair', ['src', 'dest'])
logger = logging.getLogger(__name__)
@functools.lru_cache(maxsize=None)
def resolve_hostname(ip):
return socket.getfqdn(ip)
def fallback(d, keys):
for k in keys:
if k in d:
return d[k]
raise KeyError(", ".join(keys))
class Connection:
"""Connection model for two flows.
The direction of the data flow can be seen by looking at the size.
'src' describes the peer which sends more data towards the other. This
does NOT have to mean that 'src' was the initiator of the connection.
"""
def __init__(self, flow1, flow2):
if not flow1 or not flow2:
raise Exception("A connection requires two flows")
# Assume the size that sent the most data is the source
# TODO: this might not always be right, maybe use earlier timestamp?
size1 = fallback(flow1, ['IN_BYTES', 'IN_OCTETS'])
size2 = fallback(flow2, ['IN_BYTES', 'IN_OCTETS'])
if size1 >= size2:
src = flow1
dest = flow2
else:
src = flow2
dest = flow1
# TODO: this next approach uses the lower port as the service identifier
# port1 = fallback(flow1, ['L4_SRC_PORT', 'SRC_PORT'])
# port2 = fallback(flow2, ['L4_SRC_PORT', 'SRC_PORT'])
#
# src = flow1
# dest = flow2
# if port1 > port2:
# src = flow2
# dest = flow1
self.src_flow = src
self.dest_flow = dest
ips = self.get_ips(src)
self.src = ips.src
self.dest = ips.dest
self.src_port = fallback(src, ['L4_SRC_PORT', 'SRC_PORT'])
self.dest_port = fallback(dest, ['L4_DST_PORT', 'DST_PORT'])
self.size = fallback(src, ['IN_BYTES', 'IN_OCTETS'])
# Duration is given in milliseconds
self.duration = src['LAST_SWITCHED'] - src['FIRST_SWITCHED']
if self.duration < 0:
# 32 bit int has its limits. Handling overflow here
# TODO: Should be handled in the collection phase
self.duration = (2**32 - src['FIRST_SWITCHED']) + src['LAST_SWITCHED']
def __repr__(self):
return "<Connection from {} to {}, size {}>".format(
self.src, self.dest, self.human_size)
@staticmethod
def get_ips(flow):
# IPv4
if flow.get('IP_PROTOCOL_VERSION') == 4 or \
'IPV4_SRC_ADDR' in flow or 'IPV4_DST_ADDR' in flow:
return Pair(
ipaddress.ip_address(flow['IPV4_SRC_ADDR']),
ipaddress.ip_address(flow['IPV4_DST_ADDR'])
)
# IPv6
return Pair(
ipaddress.ip_address(flow['IPV6_SRC_ADDR']),
ipaddress.ip_address(flow['IPV6_DST_ADDR'])
)
@property
def human_size(self):
# Calculate a human readable size of the traffic
if self.size < 1024:
return "%dB" % self.size
elif self.size / 1024. < 1024:
return "%.2fK" % (self.size / 1024.)
elif self.size / 1024.**2 < 1024:
return "%.2fM" % (self.size / 1024.**2)
else:
return "%.2fG" % (self.size / 1024.**3)
@property
def human_duration(self):
duration = self.duration // 1000 # uptime in milliseconds, floor it
if duration < 60:
# seconds
return "%d sec" % duration
if duration / 60 > 60:
# hours
return "%d:%02d.%02d hours" % (duration / 60**2, duration % 60**2 / 60, duration % 60)
# minutes
return "%02d:%02d min" % (duration / 60, duration % 60)
@property
def hostnames(self):
# Resolve the IPs of this flows to their hostname
src_hostname = resolve_hostname(self.src.compressed)
dest_hostname = resolve_hostname(self.dest.compressed)
return Pair(src_hostname, dest_hostname)
@property
def service(self):
# Resolve ports to their services, if known
default = "({} {})".format(self.src_port, self.dest_port)
if self.src_port > 10000:
return default
with contextlib.suppress(OSError):
return socket.getservbyport(self.src_port)
with contextlib.suppress(OSError):
return socket.getservbyport(self.dest_port)
return default
@property
def total_packets(self):
return self.src_flow["IN_PKTS"] + self.dest_flow["IN_PKTS"]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Output a basic analysis of NetFlow data")
parser.add_argument('-f', '--file', dest='file', type=str, default=sys.stdin,
help="The file to analyze (defaults to stdin if not provided)")
parser.add_argument('-p', '--packets', dest='packets_threshold', type=int, default=10,
help="Number of packets representing the lower bound in connections to be processed")
args = parser.parse_args()
# Using a file and using stdin differ in their further usage for gzip.open
file = args.file
mode = "rb" # reading files
if file != sys.stdin and not os.path.exists(file):
exit("File {} does not exist!".format(file))
if file == sys.stdin:
file = sys.stdin.buffer
mode = "rt" # reading from stdin
data = {}
with gzip.open(file, mode) as gzipped:
# "for line in" lazy-loads all lines in the file
for line in gzipped:
entry = json.loads(line)
if len(entry.keys()) != 1:
logger.warning("Line \"{}\" does not have exactly one timestamp key.")
try:
ts = list(entry)[0] # timestamp from key
except KeyError:
logger.error("Saved line \"{}\" has no timestamp key!".format(line))
continue
data[ts] = entry[ts]
# Go through data and dissect every flow saved inside the dump
# The following dict holds flows which are looking for a peer, to analyze a duplex 'Connection'.
# For each flow, the destination address is looked up. If the peer is not in the list of pending peers,
# insert this flow, waiting for its peer. If found, take the waiting peer and create a Connection object.
pending = {}
skipped = 0
skipped_threshold = args.packets_threshold
for key in sorted(data):
timestamp = datetime.fromtimestamp(float(key)).strftime("%Y-%m-%d %H:%M.%S")
client = data[key]["client"]
flows = data[key]["flows"]
for flow in sorted(flows, key=lambda x: x["FIRST_SWITCHED"]):
first_switched = flow["FIRST_SWITCHED"]
if first_switched - 1 in pending:
# TODO: handle fitting, yet mismatching (here: 1 second) pairs
pass
if first_switched not in pending:
pending[first_switched] = {}
# Find the peer for this connection
if flow["IP_PROTOCOL_VERSION"] == 4:
local_peer = flow["IPV4_SRC_ADDR"]
remote_peer = flow["IPV4_DST_ADDR"]
else:
local_peer = flow["IPV6_SRC_ADDR"]
remote_peer = flow["IPV6_DST_ADDR"]
if remote_peer in pending[first_switched]:
# The destination peer put itself into the pending dict, getting and removing entry
peer_flow = pending[first_switched].pop(remote_peer)
if len(pending[first_switched]) == 0:
del pending[first_switched]
else:
# Flow did not find a matching, pending peer - inserting itself
pending[first_switched][local_peer] = flow
continue
con = Connection(flow, peer_flow)
if con.total_packets < skipped_threshold:
skipped += 1
continue
print("{timestamp}: {service:<14} | {size:8} | {duration:9} | {packets:5} | Between {src_host} ({src}) and {dest_host} ({dest})" \
.format(timestamp=timestamp, service=con.service.upper(), src_host=con.hostnames.src, src=con.src,
dest_host=con.hostnames.dest, dest=con.dest, size=con.human_size, duration=con.human_duration,
packets=con.total_packets))
if skipped > 0:
print(f"{skipped} connections skipped, because they had less than {skipped_threshold} packets.")
if len(pending) > 0:
print(f"There are {len(pending)} first_switched entries left in the pending dict!")
all_noise = True
for first_switched, flows in sorted(pending.items(), key=lambda x: x[0]):
for peer, flow in flows.items():
# Ignore all pings, SYN scans and other noise to find only those peers left over which need a fix
if flow["IN_PKTS"] < skipped_threshold:
continue
all_noise = False
if flow["IP_PROTOCOL_VERSION"] == 4:
print(first_switched, peer, flow["IPV4_DST_ADDR"], flow["IN_PKTS"])
else:
print(first_switched, peer, flow["IPV6_DST_ADDR"], flow["IN_PKTS"])
if all_noise:
print("They were all noise!")