~wesmason/conn-check/configs-unicorns

« back to all changes in this revision

Viewing changes to conn_check_configs/unicorns.py

  • Committer: Wes Mason
  • Date: 2015-07-09 23:52:09 UTC
  • Revision ID: wesley.mason@canonical.com-20150709235209-hzb0cpx2uaape050
Grab protocol/port/dest

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
from __future__ import print_function
4
4
 
5
5
import argparse
 
6
from impacket.ImpactPacket import TCP, UDP
6
7
from impacket.ImpactDecoder import EthDecoder
7
8
import pcap
8
9
from sys import argv, exit, stdout, stderr
10
11
import yaml
11
12
 
12
13
 
 
14
PROTOCOLS = {
 
15
    TCP.protocol: 'tcp',
 
16
    UDP.protocol: 'udp',
 
17
}
 
18
 
 
19
 
13
20
class PcapTimeout(Exception):
14
21
    pass
15
22
 
53
60
        if (time.time() - start) >= to:
54
61
            raise PcapTimeout
55
62
 
56
 
        packet = EthDecoder().decode(_data)
57
 
        data.append(packet)
 
63
        ip = EthDecoder().decode(_data).child()
 
64
        tp = ip.child()
 
65
        proto = ip.get_ip_p()
 
66
 
 
67
        if proto not in PROTOCOLS:
 
68
            return
 
69
 
 
70
        protocol = PROTOCOLS[proto]
 
71
        port = tp.get_th_dport() if protocol == 'tcp' else tp.get_uh_dport()
 
72
        data.append({
 
73
            'protocol': protocol,
 
74
            'to': ip.get_ip_dst(),
 
75
            'port': port,
 
76
        })
58
77
 
59
78
    # -1 max number of packets, e.g. keep going forever or until we kill
60
79
    # the loop ourselves