2014-08-15 10:11:50 +00:00
|
|
|
#!/usr/bin/python
|
2014-08-16 07:01:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-08-15 10:11:50 +00:00
|
|
|
'''
|
2014-08-16 07:01:16 +00:00
|
|
|
Author: Volker Schwicking, vs@heg.com
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
Salt tcpdumper to visualize whats happening network-wise on
|
|
|
|
the salt-master. It uses pcapy and inspects all incoming networks
|
|
|
|
packets and filters only the ones relevant to salt-communication.
|
|
|
|
|
|
|
|
based on: http://oss.coresecurity.com/projects/pcapy.html
|
|
|
|
|
|
|
|
$ salt-tcpdump.py -n 2
|
|
|
|
|
|
|
|
Will print the overall tcp-status of tcp-connections to salts
|
|
|
|
default ports in a two second interval.
|
|
|
|
|
|
|
|
|
|
|
|
$ salt-tcpdump.py -I -n 2
|
|
|
|
|
|
|
|
Will print the number of IPs making new connections to salts
|
|
|
|
default ports.
|
|
|
|
|
|
|
|
|
|
|
|
$ salt-tcpdump.py -I -n 2 -i eth1
|
|
|
|
|
|
|
|
Same as before but on eth1 instead of the default eth0.
|
|
|
|
|
|
|
|
Rough equivalents to this script could be:
|
|
|
|
|
|
|
|
For Port 4505
|
|
|
|
tcpdump "tcp[tcpflags] & tcp-syn != 0" and port 4505 and "tcp[tcpflags] & tcp-ack == 0"
|
|
|
|
|
|
|
|
For Port 4506
|
|
|
|
tcpdump "tcp[tcpflags] & tcp-syn != 0" and port 4506 and "tcp[tcpflags] & tcp-ack == 0"
|
|
|
|
'''
|
2017-04-04 12:11:54 +00:00
|
|
|
# pylint: disable=resource-leakage
|
2015-04-14 21:54:38 +00:00
|
|
|
# Import Python Libs
|
2014-11-24 03:06:22 +00:00
|
|
|
from __future__ import absolute_import, print_function
|
2014-08-15 10:11:50 +00:00
|
|
|
import socket
|
2014-08-15 11:03:26 +00:00
|
|
|
from struct import unpack
|
2017-03-06 14:20:20 +00:00
|
|
|
import pcapy # pylint: disable=import-error,3rd-party-module-not-gated
|
2014-08-15 10:11:50 +00:00
|
|
|
import sys
|
2015-04-27 08:11:29 +00:00
|
|
|
import argparse # pylint: disable=minimum-python-version
|
2014-08-15 10:11:50 +00:00
|
|
|
import time
|
|
|
|
|
2014-08-16 07:01:16 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
class ArgParser(object):
|
2014-08-15 11:03:26 +00:00
|
|
|
'''
|
|
|
|
Simple Argument-Parser class
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
def __init__(self):
|
2014-08-15 11:03:26 +00:00
|
|
|
'''
|
|
|
|
Init the Parser
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
self.main_parser = argparse.ArgumentParser()
|
2014-08-15 11:03:26 +00:00
|
|
|
self.add_args()
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
def add_args(self):
|
|
|
|
'''
|
|
|
|
Add new arguments
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
self.main_parser.add_argument('-i',
|
|
|
|
type=str,
|
|
|
|
default='eth0',
|
|
|
|
dest='iface',
|
|
|
|
required=False,
|
2014-08-16 07:24:46 +00:00
|
|
|
help=('the interface to dump the'
|
2014-08-15 11:03:26 +00:00
|
|
|
'master runs on(default:eth0)'))
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
self.main_parser.add_argument('-n',
|
|
|
|
type=int,
|
|
|
|
default=5,
|
|
|
|
dest='ival',
|
|
|
|
required=False,
|
2014-08-15 11:03:26 +00:00
|
|
|
help=('interval for printing stats '
|
|
|
|
'(default:5)'))
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
self.main_parser.add_argument('-I',
|
|
|
|
type=bool,
|
|
|
|
default=False,
|
|
|
|
const=True,
|
|
|
|
nargs='?',
|
|
|
|
dest='only_ip',
|
|
|
|
required=False,
|
2014-08-15 11:03:26 +00:00
|
|
|
help=('print unique IPs making new '
|
|
|
|
'connections with SYN set'))
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
def parse_args(self):
|
|
|
|
'''
|
|
|
|
parses and returns the given arguments in a namespace object
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
return self.main_parser.parse_args()
|
|
|
|
|
2014-08-16 07:01:16 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
class PCAPParser(object):
|
|
|
|
'''
|
|
|
|
parses a network packet on given device and
|
|
|
|
returns source, target, source_port and dest_port
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, iface):
|
|
|
|
self.iface = iface
|
|
|
|
|
|
|
|
def run(self):
|
2014-08-15 11:03:26 +00:00
|
|
|
'''
|
|
|
|
main loop for the packet-parser
|
|
|
|
'''
|
|
|
|
# open device
|
2014-08-15 10:11:50 +00:00
|
|
|
# Arguments here are:
|
|
|
|
# device
|
|
|
|
# snaplen (maximum number of bytes to capture _per_packet_)
|
|
|
|
# promiscious mode (1 for true)
|
|
|
|
# timeout (in milliseconds)
|
2014-08-16 07:01:16 +00:00
|
|
|
cap = pcapy.open_live(self.iface, 65536, 1, 0)
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
l_time = None
|
|
|
|
|
2014-08-16 07:24:46 +00:00
|
|
|
while 1:
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
packet_data = {
|
2014-08-16 07:01:16 +00:00
|
|
|
'ip': {},
|
|
|
|
'tcp': {}
|
2014-08-15 10:11:50 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 20:04:02 +00:00
|
|
|
(header, packet) = cap.next() # pylint: disable=incompatible-py3-code
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
eth_length, eth_protocol = self.parse_ether(packet)
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
# Parse IP packets, IP Protocol number = 8
|
2014-08-16 07:01:16 +00:00
|
|
|
if eth_protocol == 8:
|
2014-11-24 03:06:22 +00:00
|
|
|
# Parse IP header
|
|
|
|
# take first 20 characters for the ip header
|
2014-08-15 10:11:50 +00:00
|
|
|
version_ihl, version, ihl, iph_length, ttl, protocol, s_addr, d_addr = self.parse_ip(packet, eth_length)
|
|
|
|
packet_data['ip']['s_addr'] = s_addr
|
|
|
|
packet_data['ip']['d_addr'] = d_addr
|
|
|
|
|
2014-11-24 03:06:22 +00:00
|
|
|
# TCP protocol
|
2014-08-16 07:01:16 +00:00
|
|
|
if protocol == 6:
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
source_port, dest_port, flags, data = self.parse_tcp(packet, iph_length, eth_length)
|
|
|
|
packet_data['tcp']['d_port'] = dest_port
|
|
|
|
packet_data['tcp']['s_port'] = source_port
|
|
|
|
packet_data['tcp']['flags'] = flags
|
|
|
|
packet_data['tcp']['data'] = data
|
|
|
|
yield packet_data
|
|
|
|
|
|
|
|
def parse_ether(self, packet):
|
|
|
|
'''
|
|
|
|
parse ethernet_header and return size and protocol
|
|
|
|
'''
|
|
|
|
eth_length = 14
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
eth_header = packet[:eth_length]
|
2014-08-16 07:01:16 +00:00
|
|
|
eth = unpack('!6s6sH', eth_header)
|
2014-08-15 10:11:50 +00:00
|
|
|
eth_protocol = socket.ntohs(eth[2])
|
|
|
|
return eth_length, eth_protocol
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
def parse_ip(self, packet, eth_length):
|
|
|
|
'''
|
|
|
|
parse ip_header and return all ip data fields
|
|
|
|
'''
|
2014-11-24 03:06:22 +00:00
|
|
|
# Parse IP header
|
|
|
|
# take first 20 characters for the ip header
|
2014-08-15 10:11:50 +00:00
|
|
|
ip_header = packet[eth_length:20+eth_length]
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-11-24 03:06:22 +00:00
|
|
|
# now unpack them:)
|
2014-08-16 07:01:16 +00:00
|
|
|
iph = unpack('!BBHHHBBH4s4s', ip_header)
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
version_ihl = iph[0]
|
|
|
|
version = version_ihl >> 4
|
|
|
|
ihl = version_ihl & 0xF
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
iph_length = ihl * 4
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
ttl = iph[5]
|
|
|
|
protocol = iph[6]
|
2014-08-15 11:03:26 +00:00
|
|
|
s_addr = socket.inet_ntoa(iph[8])
|
|
|
|
d_addr = socket.inet_ntoa(iph[9])
|
|
|
|
|
2014-08-16 07:24:46 +00:00
|
|
|
return [version_ihl,
|
|
|
|
version,
|
|
|
|
ihl,
|
|
|
|
iph_length,
|
|
|
|
ttl,
|
|
|
|
protocol,
|
|
|
|
s_addr,
|
2014-08-15 11:03:26 +00:00
|
|
|
d_addr]
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
def parse_tcp(self, packet, iph_length, eth_length):
|
|
|
|
'''
|
2014-08-16 07:24:46 +00:00
|
|
|
parse tcp_data and return source_port,
|
2014-08-15 10:11:50 +00:00
|
|
|
dest_port and actual packet data
|
|
|
|
'''
|
2014-08-15 11:03:26 +00:00
|
|
|
p_len = iph_length + eth_length
|
|
|
|
tcp_header = packet[p_len:p_len+20]
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-11-24 03:06:22 +00:00
|
|
|
# now unpack them:)
|
2014-08-16 07:01:16 +00:00
|
|
|
tcph = unpack('!H HLLBBHHH', tcp_header)
|
2014-08-15 10:11:50 +00:00
|
|
|
# H H L L B B H H H
|
|
|
|
# 2b 2b 4b 4b 1b 1b 2b 2b 2b
|
|
|
|
# sport dport seq ack res flags win chk up
|
|
|
|
# (22, 36513, 3701969065, 2346113113, 128, 24, 330, 33745, 0)
|
|
|
|
source_port = tcph[0]
|
|
|
|
|
|
|
|
dest_port = tcph[1]
|
|
|
|
sequence = tcph[2]
|
|
|
|
acknowledgement = tcph[3]
|
|
|
|
doff_reserved = tcph[4]
|
|
|
|
tcph_length = doff_reserved >> 4
|
|
|
|
tcp_flags = tcph[5]
|
|
|
|
|
|
|
|
h_size = eth_length + iph_length + tcph_length * 4
|
|
|
|
data_size = len(packet) - h_size
|
|
|
|
|
|
|
|
data = packet[h_size:]
|
|
|
|
|
|
|
|
return source_port, dest_port, tcp_flags, data
|
|
|
|
|
2014-08-16 07:01:16 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
class SaltNetstat(object):
|
2014-08-15 11:03:26 +00:00
|
|
|
'''
|
|
|
|
Reads /proc/net/tcp and returns all connections
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
def proc_tcp(self):
|
2014-08-16 07:24:46 +00:00
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
Read the table of tcp connections & remove header
|
|
|
|
'''
|
2014-08-15 11:03:26 +00:00
|
|
|
with open('/proc/net/tcp', 'r') as tcp_f:
|
|
|
|
content = tcp_f.readlines()
|
2014-08-15 10:11:50 +00:00
|
|
|
content.pop(0)
|
2014-08-16 07:24:46 +00:00
|
|
|
return content
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
def hex2dec(self, hex_s):
|
|
|
|
'''
|
|
|
|
convert hex to dezimal
|
|
|
|
'''
|
|
|
|
return str(int(hex_s, 16))
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
def ip(self, hex_s):
|
|
|
|
'''
|
|
|
|
convert into readable ip
|
|
|
|
'''
|
|
|
|
ip = [(self.hex2dec(hex_s[6:8])),
|
|
|
|
(self.hex2dec(hex_s[4:6])),
|
|
|
|
(self.hex2dec(hex_s[2:4])),
|
|
|
|
(self.hex2dec(hex_s[0:2]))]
|
2014-08-15 10:11:50 +00:00
|
|
|
return '.'.join(ip)
|
|
|
|
|
|
|
|
def remove_empty(self, array):
|
2014-08-15 11:03:26 +00:00
|
|
|
'''
|
|
|
|
create new list without empty entries
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
return [x for x in array if x != '']
|
|
|
|
|
|
|
|
def convert_ip_port(self, array):
|
2014-08-15 11:03:26 +00:00
|
|
|
'''
|
|
|
|
hex_ip:hex_port to str_ip:str_port
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
host, port = array.split(':')
|
|
|
|
return self.ip(host), self.hex2dec(port)
|
|
|
|
|
|
|
|
def run(self):
|
2014-08-15 11:03:26 +00:00
|
|
|
'''
|
|
|
|
main loop for netstat
|
|
|
|
'''
|
2014-08-16 07:24:46 +00:00
|
|
|
while 1:
|
2014-08-15 10:11:50 +00:00
|
|
|
ips = {
|
2014-08-16 07:01:16 +00:00
|
|
|
'ips/4505': {},
|
|
|
|
'ips/4506': {}
|
2014-08-15 10:11:50 +00:00
|
|
|
}
|
|
|
|
content = self.proc_tcp()
|
|
|
|
|
|
|
|
for line in content:
|
|
|
|
line_array = self.remove_empty(line.split(' '))
|
|
|
|
l_host, l_port = self.convert_ip_port(line_array[1])
|
|
|
|
r_host, r_port = self.convert_ip_port(line_array[2])
|
|
|
|
if l_port == '4505':
|
|
|
|
if r_host not in ips['ips/4505']:
|
|
|
|
ips['ips/4505'][r_host] = 0
|
|
|
|
ips['ips/4505'][r_host] += 1
|
|
|
|
if l_port == '4506':
|
|
|
|
if r_host not in ips['ips/4506']:
|
|
|
|
ips['ips/4506'][r_host] = 0
|
|
|
|
ips['ips/4506'][r_host] += 1
|
|
|
|
|
|
|
|
yield (len(ips['ips/4505']), len(ips['ips/4506']))
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
def filter_new_cons(packet):
|
|
|
|
'''
|
|
|
|
filter packets by there tcp-state and
|
|
|
|
returns codes for specific states
|
|
|
|
'''
|
2014-08-15 11:03:26 +00:00
|
|
|
flags = []
|
2014-08-15 10:11:50 +00:00
|
|
|
TCP_FIN = 0x01
|
|
|
|
TCP_SYN = 0x02
|
|
|
|
TCP_RST = 0x04
|
|
|
|
TCP_PSH = 0x08
|
|
|
|
TCP_ACK = 0x10
|
|
|
|
TCP_URG = 0x20
|
|
|
|
TCP_ECE = 0x40
|
|
|
|
TCP_CWK = 0x80
|
|
|
|
|
|
|
|
if packet['tcp']['flags'] & TCP_FIN:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('FIN')
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['flags'] & TCP_SYN:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('SYN')
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['flags'] & TCP_RST:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('RST')
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['flags'] & TCP_PSH:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('PSH')
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['flags'] & TCP_ACK:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('ACK')
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['flags'] & TCP_URG:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('URG')
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['flags'] & TCP_ECE:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('ECE')
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['flags'] & TCP_CWK:
|
2014-08-15 11:03:26 +00:00
|
|
|
flags.append('CWK')
|
2014-08-15 10:11:50 +00:00
|
|
|
else:
|
2014-11-24 03:06:22 +00:00
|
|
|
print("UNKNOWN PACKET")
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
if packet['tcp']['d_port'] == 4505:
|
|
|
|
# track new connections
|
2014-08-15 11:03:26 +00:00
|
|
|
if 'SYN' in flags and len(flags) == 1:
|
2014-08-15 10:11:50 +00:00
|
|
|
return 10
|
|
|
|
# track closing connections
|
2014-08-15 11:03:26 +00:00
|
|
|
elif 'FIN' in flags:
|
2014-08-15 10:11:50 +00:00
|
|
|
return 12
|
2014-08-16 07:24:46 +00:00
|
|
|
|
2014-08-15 10:11:50 +00:00
|
|
|
elif packet['tcp']['d_port'] == 4506:
|
|
|
|
# track new connections
|
2014-08-15 11:03:26 +00:00
|
|
|
if 'SYN' in flags and len(flags) == 1:
|
2014-08-15 10:11:50 +00:00
|
|
|
return 100
|
|
|
|
# track closing connections
|
2014-08-15 11:03:26 +00:00
|
|
|
elif 'FIN' in flags:
|
2014-08-15 10:11:50 +00:00
|
|
|
return 120
|
|
|
|
# packet does not match requirements
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2014-08-16 07:01:16 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
def main():
|
|
|
|
'''
|
|
|
|
main loop for whole script
|
|
|
|
'''
|
2014-08-15 10:11:50 +00:00
|
|
|
# passed parameters
|
2014-08-15 11:03:26 +00:00
|
|
|
args = vars(ArgParser().parse_args())
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
# reference timer for printing in intervals
|
|
|
|
r_time = 0
|
|
|
|
|
|
|
|
# the ports we want to monitor
|
|
|
|
ports = [4505, 4506]
|
|
|
|
|
2014-11-24 03:06:22 +00:00
|
|
|
print("Sniffing device {0}".format(args['iface']))
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
stat = {
|
2014-08-16 07:01:16 +00:00
|
|
|
'4506/new': 0,
|
|
|
|
'4506/est': 0,
|
|
|
|
'4506/fin': 0,
|
|
|
|
'4505/new': 0,
|
|
|
|
'4505/est': 0,
|
|
|
|
'4505/fin': 0,
|
|
|
|
'ips/4505': 0,
|
|
|
|
'ips/4506': 0
|
2014-08-15 10:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if args['only_ip']:
|
2017-03-31 10:40:40 +00:00
|
|
|
print(
|
2014-08-15 11:03:26 +00:00
|
|
|
'IPs making new connections '
|
|
|
|
'(ports:{0}, interval:{1})'.format(ports,
|
|
|
|
args['ival'])
|
2017-03-31 10:40:40 +00:00
|
|
|
)
|
2014-08-16 07:24:46 +00:00
|
|
|
else:
|
2017-03-31 10:40:40 +00:00
|
|
|
print(
|
2014-08-15 11:03:26 +00:00
|
|
|
'Salt-Master Network Status '
|
|
|
|
'(ports:{0}, interval:{1})'.format(ports,
|
|
|
|
args['ival'])
|
2017-03-31 10:40:40 +00:00
|
|
|
)
|
2014-08-15 10:11:50 +00:00
|
|
|
try:
|
2014-08-16 07:24:46 +00:00
|
|
|
while 1:
|
2014-08-15 10:11:50 +00:00
|
|
|
s_time = int(time.time())
|
|
|
|
|
2014-11-24 03:06:22 +00:00
|
|
|
packet = next(PCAPParser(args['iface']).run())
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
p_state = filter_new_cons(packet)
|
|
|
|
|
|
|
|
ips_auth = []
|
|
|
|
ips_push = []
|
|
|
|
|
|
|
|
# new connection to 4505
|
|
|
|
if p_state == 10:
|
2014-08-15 11:03:26 +00:00
|
|
|
stat['4505/new'] += 1
|
2014-08-15 10:11:50 +00:00
|
|
|
if packet['ip']['s_addr'] not in ips_auth:
|
|
|
|
ips_auth.append(packet['ip']['s_addr'])
|
|
|
|
# closing connection to 4505
|
|
|
|
elif p_state == 12:
|
2014-08-15 11:03:26 +00:00
|
|
|
stat['4505/fin'] += 1
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
# new connection to 4506
|
|
|
|
elif p_state == 100:
|
2014-08-15 11:03:26 +00:00
|
|
|
stat['4506/new'] += 1
|
2014-08-15 10:11:50 +00:00
|
|
|
if packet['ip']['s_addr'] not in ips_push:
|
|
|
|
ips_push.append(packet['ip']['s_addr'])
|
|
|
|
# closing connection to 4506
|
|
|
|
elif p_state == 120:
|
2014-08-15 11:03:26 +00:00
|
|
|
stat['4506/fin'] += 1
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
# get the established connections to 4505 and 4506
|
|
|
|
# these would only show up in tcpdump if data is transferred
|
2014-08-15 11:03:26 +00:00
|
|
|
# but then with different flags (PSH, etc.)
|
2014-11-24 03:06:22 +00:00
|
|
|
stat['4505/est'], stat['4506/est'] = next(SaltNetstat().run())
|
2014-08-15 10:11:50 +00:00
|
|
|
|
|
|
|
# print only in intervals
|
2014-08-15 10:15:49 +00:00
|
|
|
if (s_time % args['ival']) == 0:
|
2014-08-15 10:11:50 +00:00
|
|
|
# prevent printing within the same second
|
|
|
|
if r_time != s_time:
|
|
|
|
if args['only_ip']:
|
|
|
|
msg = 'IPs/4505: {0}, IPs/4506: {1}'.format(len(ips_auth),
|
|
|
|
len(ips_push))
|
|
|
|
else:
|
2014-08-15 11:03:26 +00:00
|
|
|
msg = "4505=>[ est: {0}, ".format(stat['4505/est'])
|
|
|
|
msg += "new: {0}/s, ".format(stat['4505/new'] / args['ival'])
|
|
|
|
msg += "fin: {0}/s ] ".format(stat['4505/fin'] / args['ival'])
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
msg += " 4506=>[ est: {0}, ".format(stat['4506/est'])
|
|
|
|
msg += "new: {0}/s, ".format(stat['4506/new'] / args['ival'])
|
|
|
|
msg += "fin: {0}/s ]".format(stat['4506/fin'] / args['ival'])
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-11-24 03:06:22 +00:00
|
|
|
print(msg)
|
2014-08-15 10:11:50 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
# reset the so far collected stats
|
|
|
|
for item in stat:
|
|
|
|
stat[item] = 0
|
2014-08-15 10:11:50 +00:00
|
|
|
r_time = s_time
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
2014-08-16 07:24:46 +00:00
|
|
|
sys.exit(1)
|
2014-08-15 11:03:26 +00:00
|
|
|
|
2018-12-21 00:19:01 +00:00
|
|
|
|
2014-08-15 11:03:26 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|