Exemplo n.º 1
0
def get_params(argv):
    """

        Get CLI params provided by user
        Args:
            argv: CLI params
        Returns:
            cap: pcap object
            position: packet number to read
            load_apps: apps to load
            filters_file: filters
    """
    # Default Values
    input_filter, filters_file, dev, captured_file = '', '', 'eth0', ''
    save_file = ''
    topology_file = "./docs/topology.json"
    load_apps = dict()

    opts = read_params(argv)

    for option, param in opts:
        if option in ['-p']:
            PrintingOptions().set_full_headers()
        elif option in ['-f', '--pcap-filter']:
            input_filter = param
        elif option in ['-F', '--filters-file']:
            filters_file = param
        elif option in ['-i', '--interface']:
            dev = param
        elif option in ['-r', '--captured-file']:
            captured_file = param
        elif option in ['-o', '--print-ovs']:
            PrintingOptions().set_print_ovs()
        elif option in ['-T', '--topology-file']:
            topology_file = param
        elif option in ['-c', '--no-colors']:
            PrintingOptions().set_no_color()
        elif option in ['-q', '--no-output']:
            PrintingOptions().set_no_print()
        elif option in ['-O', '--oess-fvd']:
            load_apps['oess_fvd'] = param
        elif option in ['-S', '--enable-statistics']:
            load_apps['statistics'] = 0
        elif option in ['-I', '--enable-influx']:
            load_apps['influx'] = 0
        elif option in ['-w', '--save-to-file']:
            save_file = param
        elif option in ['-v', '--version']:
            print('OpenFlow Sniffer version %s' % VERSION)
            sys.exit(0)
        elif option in ['-N', '--notify-via-slack']:
            load_apps['notifications'] = param
        else:
            usage(argv[0])

    cap, position = start_capture(captured_file, input_filter, dev)

    return cap, position, load_apps, filters_file, topology_file, save_file
Exemplo n.º 2
0
 def __init__(self, log_file):
     """
         Initialize two main descriptors: terminal and log.
         Terminal will be responsible to print to the CLI and
         Log will be responsible to print to file.
         If user provides a -q (quiet), but provides a -w,
         nothing will be printed on terminal.
     """
     if PrintingOptions().is_quiet():
         self.terminal = None  # Dont Print to Terminal
         PrintingOptions().set_no_quiet()
     else:
         self.terminal = sys.stdout  # Print to Terminal
     self.log = RotateFile(log_file)  # Print to File
Exemplo n.º 3
0
def yellow(string):
    """
        If PrintingOptions().is_colored, prints string
        in the color YELLOW

        Args:
            string: text to be printed
    """
    if not PrintingOptions().is_colored():
        return string
    return colored(string, 'yellow')
Exemplo n.º 4
0
def blue(string):
    """
        If PrintingOptions().is_colored, prints string
        in the color BLUE

        Args:
            string: text to be printed
    """
    if not PrintingOptions().is_colored():
        return string
    return colored(string, 'blue')
Exemplo n.º 5
0
def green(string):
    """
        If PrintingOptions().is_colored, prints string
        in the color GREEN

        Args:
            string: text to be printed
    """
    if not PrintingOptions().is_colored():
        return string
    return colored(string, 'green')
Exemplo n.º 6
0
def red(string):
    """
        If PrintingOptions().colors, prints string
        in the color RED

        Args:
            string: text to be printed
    """
    if PrintingOptions().colors is False:
        return string
    return colored(string, 'red')
Exemplo n.º 7
0
def green(string):
    """
        If PrintingOptions().colors, prints string
        in the color GREEN

        Args:
            string: text to be printed
    """
    if PrintingOptions().colors is False:
        return string
    return colored(string, 'green')
Exemplo n.º 8
0
def red(string):
    """
        If PrintingOptions().is_colored, prints string
        in the color RED

        Args:
            string: text to be printed
    """
    if not PrintingOptions().is_colored():
        return string
    return colored(string, 'red')
Exemplo n.º 9
0
def cyan(string):
    """
        If PrintingOptions().colors, prints string
        in the color CYAN

        Args:
            string: text to be printed
    """
    if PrintingOptions().colors is False:
        return string
    return colored(string, 'cyan')
Exemplo n.º 10
0
def yellow(string):
    """
        If PrintingOptions().colors, prints string
        in the color YELLOW

        Args:
            string: text to be printed
    """
    if PrintingOptions().colors is False:
        return string
    return colored(string, 'yellow')
Exemplo n.º 11
0
def blue(string):
    """
        If PrintingOptions().colors, prints string
        in the color BLUE

        Args:
            string: text to be printed
    """
    if PrintingOptions().colors is False:
        return string
    return colored(string, 'blue')
Exemplo n.º 12
0
def cyan(string):
    """
        If PrintingOptions().is_colored, prints string
        in the color CYAN

        Args:
            string: text to be printed
    """
    if not PrintingOptions().is_colored():
        return string
    return colored(string, 'cyan')
Exemplo n.º 13
0
 def __init__(self):
     self.printing_options = PrintingOptions()
     self.sanitizer = Sanitizer()
     self.oft = None
     self.stats = None
     self.cap = None
     self.packet_number = None
     self.load_apps = []
     self.packet_count = 1
     self.topo_reader = TopoReader()
     self.ofp_proxy = None
     self.load_config()
Exemplo n.º 14
0
    def process_filters(self, filters_file):
        """
            If -F file is provided, read the file and import all filters.
            Args:
                filters_file: file with filters

        """
        if len(filters_file) == 0:
            return
        configs = self.read_file(filters_file)
        if len(configs) != 0:
            PrintingOptions().filters = 1
            self.allowed_of_versions = configs['allowed_of_versions']
            self.filters = configs['filters']
Exemplo n.º 15
0
def filter_msg(msg):
    """
        This method will be the core of all filters. Any new filter comes here
    Args:
        msg: OFMessage class
    Returns:
        False: Don't filter packet
        True: Filter it (don't print)
    """

    if PrintingOptions().is_quiet():
        # Don't print anything. Used in conjunction with some apps.
        return True

    if not PrintingOptions().has_filters():
        # User hasn't selected CLI option -F
        return False

    # Filter per OF Version
    if filter_of_version(msg):
        return True

    # Filter per OF Message Type
    if filter_of_type(msg):
        return True

    # Filter Ethertypes from PacketIn/Out messages
    if ethertype_filters(msg):
        return True

    # Filter PacketIn/Out based on DPID and Port
    if dpid_filters(msg):
        return True

    # Don't filter
    return False
Exemplo n.º 16
0
 def __init__(self):
     self.printing_options = PrintingOptions()
     self.sanitizer = Sanitizer()
     self.oft = None
     self.stats = None
     self.influx = None
     self.notifications = None
     self.trigger_event = threading.Event()
     self.cap = None
     self.packet_number = None
     self.load_apps = dict()
     self.packet_count = 1
     self.topo_reader = TopoReader()
     self.save_to_file = None
     self.ofp_proxy = None
     self.load_config()
Exemplo n.º 17
0
def print_headers(pkt, overwrite_min=0):
    """
        Print TCP/IP header. It uses command line option -p
            to print 'mininal' or 'full' headers
    Args:
        pkt: OFMessage class
        overwrite_min: in case of problems, overwrite user definition
    """
    if PrintingOptions().min == 1 and overwrite_min == 0:
        print_minimal(pkt.position, pkt.l1.time, pkt.l1.caplen, pkt.l3, pkt.l4)
    else:
        print_position(pkt.position)
        print_layer1(pkt.l1.time, pkt.l1.caplen, pkt.l1.truncate)
        print_layer2(pkt.l2)
        print_layer3(pkt.l3)
        print_tcp(pkt.l4)