2017-10-28 19:00:18 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-10-29 11:52:59 +01:00
|
|
|
"""
|
2020-03-29 22:34:30 +02:00
|
|
|
Reference analyzer script for NetFlow Python package.
|
2019-03-31 20:51:34 +02:00
|
|
|
This file belongs to https://github.com/bitkeks/python-netflow-v9-softflowd.
|
2017-10-29 11:52:59 +01:00
|
|
|
|
2020-04-24 16:34:37 +02:00
|
|
|
Copyright 2016-2020 Dominik Pataky <software+pynetflow@dpataky.eu>
|
2017-10-29 11:52:59 +01:00
|
|
|
Licensed under MIT License. See LICENSE.
|
|
|
|
"""
|
2017-10-28 19:00:18 +02:00
|
|
|
|
2019-10-05 09:06:06 +02:00
|
|
|
import argparse
|
|
|
|
import contextlib
|
|
|
|
import functools
|
2019-11-03 11:59:28 +01:00
|
|
|
import gzip
|
2017-10-28 19:00:18 +02:00
|
|
|
import ipaddress
|
|
|
|
import json
|
2019-11-03 11:59:28 +01:00
|
|
|
import logging
|
|
|
|
import os.path
|
2017-10-29 11:52:59 +01:00
|
|
|
import socket
|
2019-10-05 09:06:06 +02:00
|
|
|
import sys
|
2020-04-24 16:34:37 +02:00
|
|
|
from collections import namedtuple
|
|
|
|
from datetime import datetime
|
2018-02-20 12:09:10 +01:00
|
|
|
|
2020-03-19 18:07:32 +01:00
|
|
|
IP_PROTOCOLS = {
|
|
|
|
1: "ICMP",
|
|
|
|
6: "TCP",
|
|
|
|
17: "UDP",
|
|
|
|
58: "ICMPv6"
|
|
|
|
}
|
|
|
|
|
2019-10-05 09:06:06 +02:00
|
|
|
Pair = namedtuple('Pair', ['src', 'dest'])
|
2020-03-29 22:14:45 +02:00
|
|
|
|
2019-11-03 11:59:28 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2020-03-29 22:14:45 +02:00
|
|
|
ch = logging.StreamHandler()
|
|
|
|
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
logger.addHandler(ch)
|
2017-10-29 11:52:59 +01:00
|
|
|
|
|
|
|
|
2020-03-19 18:07:32 +01:00
|
|
|
def printv(message, *args_, **kwargs):
|
|
|
|
if args.verbose:
|
|
|
|
print(message.format(*args_, **kwargs))
|
2020-01-20 17:01:50 +01:00
|
|
|
|
|
|
|
|
2019-11-03 11:59:28 +01:00
|
|
|
@functools.lru_cache(maxsize=None)
|
2020-03-19 18:07:32 +01:00
|
|
|
def resolve_hostname(ip: str) -> str:
|
|
|
|
if args.no_dns:
|
|
|
|
# If no DNS resolution is requested, simply return the IP string
|
|
|
|
return ip
|
|
|
|
# else resolve the IP address to a hostname and return the hostname
|
2019-10-05 09:06:06 +02:00
|
|
|
return socket.getfqdn(ip)
|
2017-10-29 11:52:59 +01:00
|
|
|
|
2019-10-17 05:23:51 +02:00
|
|
|
|
|
|
|
def fallback(d, keys):
|
|
|
|
for k in keys:
|
2019-10-31 17:55:48 +01:00
|
|
|
if k in d:
|
2019-10-17 05:23:51 +02:00
|
|
|
return d[k]
|
|
|
|
raise KeyError(", ".join(keys))
|
|
|
|
|
|
|
|
|
2020-03-19 18:07:32 +01:00
|
|
|
def human_size(size_bytes):
|
|
|
|
# Calculate a human readable size of the flow
|
|
|
|
if size_bytes < 1024:
|
|
|
|
return "%dB" % size_bytes
|
|
|
|
elif size_bytes / 1024. < 1024:
|
|
|
|
return "%.2fK" % (size_bytes / 1024.)
|
2020-04-24 16:34:37 +02:00
|
|
|
elif size_bytes / 1024. ** 2 < 1024:
|
|
|
|
return "%.2fM" % (size_bytes / 1024. ** 2)
|
2020-03-19 18:07:32 +01:00
|
|
|
else:
|
2020-04-24 16:34:37 +02:00
|
|
|
return "%.2fG" % (size_bytes / 1024. ** 3)
|
2020-03-19 18:07:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def human_duration(seconds):
|
|
|
|
# Calculate human readable duration times
|
|
|
|
if seconds < 60:
|
|
|
|
# seconds
|
|
|
|
return "%d sec" % seconds
|
|
|
|
if seconds / 60 > 60:
|
|
|
|
# hours
|
2020-04-24 16:34:37 +02:00
|
|
|
return "%d:%02d.%02d hours" % (seconds / 60 ** 2, seconds % 60 ** 2 / 60, seconds % 60)
|
2020-03-19 18:07:32 +01:00
|
|
|
# minutes
|
|
|
|
return "%02d:%02d min" % (seconds / 60, seconds % 60)
|
|
|
|
|
|
|
|
|
2017-10-29 11:52:59 +01:00
|
|
|
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
|
2019-10-05 09:06:06 +02:00
|
|
|
does NOT have to mean that 'src' was the initiator of the connection.
|
2017-10-29 11:52:59 +01:00
|
|
|
"""
|
2020-04-24 16:34:37 +02:00
|
|
|
|
2017-10-29 11:52:59 +01:00
|
|
|
def __init__(self, flow1, flow2):
|
2019-10-05 09:06:06 +02:00
|
|
|
if not flow1 or not flow2:
|
|
|
|
raise Exception("A connection requires two flows")
|
|
|
|
|
2019-10-17 05:23:51 +02:00
|
|
|
# Assume the size that sent the most data is the source
|
2019-10-31 17:35:15 +01:00
|
|
|
# TODO: this might not always be right, maybe use earlier timestamp?
|
2019-10-17 05:23:51 +02:00
|
|
|
size1 = fallback(flow1, ['IN_BYTES', 'IN_OCTETS'])
|
|
|
|
size2 = fallback(flow2, ['IN_BYTES', 'IN_OCTETS'])
|
|
|
|
if size1 >= size2:
|
2017-10-29 11:52:59 +01:00
|
|
|
src = flow1
|
|
|
|
dest = flow2
|
|
|
|
else:
|
|
|
|
src = flow2
|
|
|
|
dest = flow1
|
|
|
|
|
2019-11-03 15:58:40 +01:00
|
|
|
# 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
|
2019-10-05 09:06:06 +02:00
|
|
|
ips = self.get_ips(src)
|
2017-10-29 11:52:59 +01:00
|
|
|
self.src = ips.src
|
|
|
|
self.dest = ips.dest
|
2019-10-17 05:23:51 +02:00
|
|
|
self.src_port = fallback(src, ['L4_SRC_PORT', 'SRC_PORT'])
|
2019-10-31 17:35:15 +01:00
|
|
|
self.dest_port = fallback(dest, ['L4_DST_PORT', 'DST_PORT'])
|
2019-10-17 05:23:51 +02:00
|
|
|
self.size = fallback(src, ['IN_BYTES', 'IN_OCTETS'])
|
2017-10-29 11:52:59 +01:00
|
|
|
|
2017-10-29 19:38:33 +01:00
|
|
|
# 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
|
2019-10-05 09:06:06 +02:00
|
|
|
# TODO: Should be handled in the collection phase
|
2020-04-24 16:34:37 +02:00
|
|
|
self.duration = (2 ** 32 - src['FIRST_SWITCHED']) + src['LAST_SWITCHED']
|
2017-10-29 19:38:33 +01:00
|
|
|
|
2017-10-29 11:52:59 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return "<Connection from {} to {}, size {}>".format(
|
|
|
|
self.src, self.dest, self.human_size)
|
|
|
|
|
2019-10-05 09:06:06 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_ips(flow):
|
|
|
|
# IPv4
|
2019-11-03 13:30:50 +01:00
|
|
|
if flow.get('IP_PROTOCOL_VERSION') == 4 or \
|
|
|
|
'IPV4_SRC_ADDR' in flow or 'IPV4_DST_ADDR' in flow:
|
2019-10-05 09:06:06 +02:00
|
|
|
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'])
|
|
|
|
)
|
|
|
|
|
2017-10-29 11:52:59 +01:00
|
|
|
@property
|
|
|
|
def human_size(self):
|
2020-03-19 18:07:32 +01:00
|
|
|
return human_size(self.size)
|
2017-10-29 11:52:59 +01:00
|
|
|
|
2017-10-29 19:38:33 +01:00
|
|
|
@property
|
|
|
|
def human_duration(self):
|
|
|
|
duration = self.duration // 1000 # uptime in milliseconds, floor it
|
2020-03-19 18:07:32 +01:00
|
|
|
return human_duration(duration)
|
2017-10-29 19:38:33 +01:00
|
|
|
|
2017-10-29 11:52:59 +01:00
|
|
|
@property
|
|
|
|
def hostnames(self):
|
|
|
|
# Resolve the IPs of this flows to their hostname
|
2019-10-05 09:06:06 +02:00
|
|
|
src_hostname = resolve_hostname(self.src.compressed)
|
|
|
|
dest_hostname = resolve_hostname(self.dest.compressed)
|
2017-10-29 19:38:33 +01:00
|
|
|
return Pair(src_hostname, dest_hostname)
|
2017-10-29 11:52:59 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def service(self):
|
|
|
|
# Resolve ports to their services, if known
|
2019-11-03 15:58:40 +01:00
|
|
|
default = "({} {})".format(self.src_port, self.dest_port)
|
2019-10-05 09:06:06 +02:00
|
|
|
with contextlib.suppress(OSError):
|
|
|
|
return socket.getservbyport(self.src_port)
|
|
|
|
with contextlib.suppress(OSError):
|
|
|
|
return socket.getservbyport(self.dest_port)
|
2019-11-03 15:58:40 +01:00
|
|
|
return default
|
|
|
|
|
|
|
|
@property
|
|
|
|
def total_packets(self):
|
|
|
|
return self.src_flow["IN_PKTS"] + self.dest_flow["IN_PKTS"]
|
2019-10-05 09:06:06 +02:00
|
|
|
|
|
|
|
|
2020-03-29 22:14:45 +02:00
|
|
|
if __name__ == "netflow.analyzer":
|
|
|
|
logger.error("The analyzer is currently meant to be used as a CLI tool only.")
|
|
|
|
logger.error("Use 'python3 -m netflow.analyzer -h' in your console for additional help.")
|
|
|
|
|
2019-10-05 09:06:06 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Output a basic analysis of NetFlow data")
|
2020-03-19 18:07:32 +01:00
|
|
|
parser.add_argument("-f", "--file", dest="file", type=str, default=sys.stdin,
|
2019-10-05 09:06:06 +02:00
|
|
|
help="The file to analyze (defaults to stdin if not provided)")
|
2020-03-19 18:07:32 +01:00
|
|
|
parser.add_argument("-p", "--packets", dest="packets_threshold", type=int, default=10,
|
2019-11-03 15:58:40 +01:00
|
|
|
help="Number of packets representing the lower bound in connections to be processed")
|
2020-03-19 18:07:32 +01:00
|
|
|
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
|
2020-01-20 17:01:50 +01:00
|
|
|
help="Enable verbose output.")
|
2020-03-19 18:07:32 +01:00
|
|
|
parser.add_argument("--match-host", dest="match_host", type=str, default=None,
|
|
|
|
help="Filter output by matching on the given host (matches source or destination)")
|
2020-03-29 22:14:45 +02:00
|
|
|
parser.add_argument("-n", "--no-dns", dest="no_dns", action="store_true",
|
|
|
|
help="Disable DNS resolving of IP addresses")
|
2019-10-05 09:06:06 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-03-19 18:07:32 +01:00
|
|
|
# Sanity check for IP address
|
|
|
|
if args.match_host:
|
|
|
|
try:
|
|
|
|
match_host = ipaddress.ip_address(args.match_host)
|
2023-08-19 10:57:01 +02:00
|
|
|
except ValueError:
|
2020-03-19 18:07:32 +01:00
|
|
|
exit("IP address '{}' is neither IPv4 nor IPv6".format(args.match_host))
|
|
|
|
|
2019-11-03 11:59:28 +01:00
|
|
|
# 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:
|
2020-03-29 22:14:45 +02:00
|
|
|
logger.warning("The line does not have exactly one timestamp key: \"{}\"".format(line.keys()))
|
2019-11-03 11:59:28 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
ts = list(entry)[0] # timestamp from key
|
|
|
|
except KeyError:
|
|
|
|
logger.error("Saved line \"{}\" has no timestamp key!".format(line))
|
|
|
|
continue
|
|
|
|
|
2020-04-01 11:55:45 +02:00
|
|
|
if "header" not in entry[ts]:
|
|
|
|
logger.error("No header dict in entry {}".format(ts))
|
|
|
|
raise ValueError
|
|
|
|
|
2020-03-31 22:47:23 +02:00
|
|
|
if entry[ts]["header"]["version"] == 10:
|
|
|
|
logger.warning("Skipped IPFIX entry, because analysis of IPFIX is not yet implemented")
|
|
|
|
continue
|
|
|
|
|
2019-11-03 11:59:28 +01:00
|
|
|
data[ts] = entry[ts]
|
2019-10-05 09:06:06 +02:00
|
|
|
|
2019-11-03 11:59:28 +01:00
|
|
|
# Go through data and dissect every flow saved inside the dump
|
2019-11-03 15:58:40 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2020-01-20 17:01:50 +01:00
|
|
|
first_line = True # print header line before first line
|
|
|
|
|
2019-10-05 09:06:06 +02:00
|
|
|
for key in sorted(data):
|
|
|
|
timestamp = datetime.fromtimestamp(float(key)).strftime("%Y-%m-%d %H:%M.%S")
|
2019-11-03 13:57:06 +01:00
|
|
|
client = data[key]["client"]
|
|
|
|
flows = data[key]["flows"]
|
2019-11-03 15:58:40 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# Find the peer for this connection
|
2020-03-19 18:07:32 +01:00
|
|
|
if "IPV4_SRC_ADDR" in flow or flow.get("IP_PROTOCOL_VERSION") == 4:
|
2019-11-03 15:58:40 +01:00
|
|
|
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"]
|
|
|
|
|
2020-03-19 18:07:32 +01:00
|
|
|
# Match on host filter passed in as argument
|
|
|
|
if args.match_host and not any([local_peer == args.match_host, remote_peer == args.match_host]):
|
|
|
|
# If a match_host is given but neither local_peer nor remote_peer match
|
|
|
|
continue
|
|
|
|
|
|
|
|
if first_switched not in pending:
|
|
|
|
pending[first_switched] = {}
|
|
|
|
|
|
|
|
# Match peers
|
2019-11-03 15:58:40 +01:00
|
|
|
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
|
2019-10-05 09:06:06 +02:00
|
|
|
continue
|
2019-11-03 15:58:40 +01:00
|
|
|
|
2020-01-20 17:01:50 +01:00
|
|
|
if first_line:
|
2020-04-24 16:34:37 +02:00
|
|
|
print("{:19} | {:14} | {:8} | {:9} | {:7} | Involved hosts".format("Timestamp", "Service", "Size",
|
|
|
|
"Duration", "Packets"))
|
2020-01-20 17:01:50 +01:00
|
|
|
print("-" * 100)
|
|
|
|
first_line = False
|
|
|
|
|
2020-03-19 18:07:32 +01:00
|
|
|
print("{timestamp} | {service:<14} | {size:8} | {duration:9} | {packets:7} | "
|
2020-04-24 16:34:37 +02:00
|
|
|
"Between {src_host} ({src}) and {dest_host} ({dest})"
|
2019-11-03 15:58:40 +01:00
|
|
|
.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:
|
2020-04-24 16:34:37 +02:00
|
|
|
print("{skipped} connections skipped, because they had less than {skipped_threshold} packets "
|
|
|
|
"(this value can be set with the -p flag).".format(skipped=skipped, skipped_threshold=skipped_threshold))
|
2020-01-20 17:01:50 +01:00
|
|
|
|
|
|
|
if not args.verbose:
|
|
|
|
# Exit here if no debugging session was wanted
|
|
|
|
exit(0)
|
2019-11-03 15:58:40 +01:00
|
|
|
|
|
|
|
if len(pending) > 0:
|
2020-04-24 16:34:37 +02:00
|
|
|
print("\nThere are {pending} first_switched entries left in the pending dict!".format(pending=len(pending)))
|
2019-11-03 15:58:40 +01:00
|
|
|
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
|
|
|
|
|
2020-03-19 18:07:32 +01:00
|
|
|
src = flow.get("IPV4_SRC_ADDR") or flow.get("IPV6_SRC_ADDR")
|
|
|
|
src_host = resolve_hostname(src)
|
2020-04-24 16:34:37 +02:00
|
|
|
src_text = "{}".format(src) if src == src_host else "{} ({})".format(src_host, src)
|
2020-03-19 18:07:32 +01:00
|
|
|
dst = flow.get("IPV4_DST_ADDR") or flow.get("IPV6_DST_ADDR")
|
|
|
|
dst_host = resolve_hostname(dst)
|
2020-04-24 16:34:37 +02:00
|
|
|
dst_text = "{}".format(dst) if dst == dst_host else "{} ({})".format(dst_host, dst)
|
2020-03-19 18:07:32 +01:00
|
|
|
proto = flow["PROTOCOL"]
|
|
|
|
size = flow["IN_BYTES"]
|
|
|
|
packets = flow["IN_PKTS"]
|
|
|
|
src_port = flow.get("L4_SRC_PORT", 0)
|
|
|
|
dst_port = flow.get("L4_DST_PORT", 0)
|
|
|
|
|
2020-04-24 16:34:37 +02:00
|
|
|
print("From {src_text}:{src_port} to {dst_text}:{dst_port} with "
|
|
|
|
"proto {proto} and size {size}"
|
|
|
|
" ({packets} packets)".format(src_text=src_text, src_port=src_port, dst_text=dst_text,
|
|
|
|
dst_port=dst_port, proto=IP_PROTOCOLS.get(proto, 'UNKNOWN'),
|
|
|
|
size=human_size(size), packets=packets))
|
2019-11-03 15:58:40 +01:00
|
|
|
|
|
|
|
if all_noise:
|
2020-01-20 17:01:50 +01:00
|
|
|
print("They were all noise!")
|