Exemplo n.º 1
0
    def __init__(self, name, personality, ethernet, actions, services, binds):
        """Function initializes a network device
        Args:
            name : name of the device
            personality : nmap personality of the device
            ethernet : ethernet address of the device
            actions : default actions of the device for packets of certain protocols
            services : detailed actions for protocols and port  numbers
            binds : ip addresses the devices
        """
        logger.debug('Creating device %s on IPs %s', name, binds)
        self.name = name
        self.personality = personality
        self.mac = ethernet
        try:
            self.ethernet = [int(i, 16) for i in self.mac.split(':')]
        except BaseException:
            logger.exception('Exception: MAC conversion for device %s failed: %s', self.name, self.mac)
            sys.exit(1)
        self.ethernet = tuple(self.ethernet)
        self.action_dictionary = actions
        self.service_list = services
        self.bind_list = binds

        self.protocol_mapping = (
            ('icmp', 1, ICMPHandler()),  # IP_PROTO_ICMP
            ('tcp', 6, TCPHandler()),  # IP_PROTO_TCP
            ('udp', 17, UDPHandler())  # IP_PROTO_UDP
        )
        self.metadata = {
            'ip_id': 0,  # IP ID
            'ip_id_delta': 0,
            'cip_id': 0,  # CLOSED IP ID
            'cip_id_delta': 0,
            'icmp_id': 0,  # ICMP ID
            'icmp_id_delta': 0,
            'tcp_isn': 0,  # TCP ISN
            'tcp_isn_delta': 0,
            'tcp_isn_gcd': 0,
            'tcp_isn_dev': 0,
            'tcp_ts': 0,  # TCP TS
            'tcp_ts_delta': 0
        }
        self.ip_id_generator()
        self.tcp_isn_generator()
        self.tcp_ts_generator()
        # script can return IP()/ICMP() -> see impacket bug #4870
        self.decoder = ImpactDecoder.IPDecoder()
        self.decoder_icmp = ImpactDecoder.IPDecoderForICMP()
Exemplo n.º 2
0
 def __init__(self, interface, network, default, elements, loggers,
              tunnels):
     """Function initialized the dipatcher
     Args:
         interface : name of the network interface to listen
         network : networkx graph representation of the network
         default : default template
         elements : elements of the network
         loggers : instances of the logger modules
         tunnels : tunnel configuration
     """
     self.interface = interface
     self.mac = netifaces.ifaddresses(
         self.interface)[netifaces.AF_LINK][0]['addr']
     self.network = network
     try:
         post('http://localhost:8080/network',
              json=dumps(json_graph.node_link_data(self.network)))
     except:
         logger.exception('Exception: Cannot connect to local server.')
     self.default = default
     self.devices, self.routes, self.externals = elements
     self.hpfeeds, self.dblogger = loggers
     self.tunnels = tunnels
     self.packet_queue = dict()
     self.entry_points = list()
     self.unreach_list = list()
     self.pcapy_object = pcapy.open_live(self.interface, 65535, 1, 10)
     self.decoder = ImpactDecoder.EthDecoder()
     self.ip_decoder = ImpactDecoder.IPDecoder()
     self.ip_icmp_decoder = ImpactDecoder.IPDecoderForICMP()
     self.mac_set = set([self.mac])
     for d in self.devices:
         if len(d.mac):
             self.mac_set.add(d.mac)
     for r in self.routes:
         if r.entry:
             self.entry_points.append(r)
         self.unreach_list.extend(r.unreach_list)
     logger.info('Started dispatcher listening on interface %s',
                 self.interface)
     while True:
         try:
             (hdr, pkt) = self.pcapy_object.next()
             self.callback(hdr, pkt)
         except KeyboardInterrupt:
             return