Пример #1
0
    async def _add_ubridge_ethernet_connection(self, bridge_name, ethernet_interface, block_host_traffic=False):
        """
        Creates a connection with an Ethernet interface in uBridge.

        :param bridge_name: bridge name in uBridge
        :param ethernet_interface: Ethernet interface name
        :param block_host_traffic: block network traffic originating from the host OS (Windows only)
        """

        if sys.platform.startswith("linux") and block_host_traffic is False:
            # on Linux we use RAW sockets by default excepting if host traffic must be blocked
            await self._ubridge_send('bridge add_nio_linux_raw {name} "{interface}"'.format(name=bridge_name, interface=ethernet_interface))
        elif sys.platform.startswith("win"):
            # on Windows we use Winpcap/Npcap
            windows_interfaces = interfaces()
            npf_id = None
            source_mac = None
            for interface in windows_interfaces:
                # Winpcap/Npcap uses a NPF ID to identify an interface on Windows
                if "netcard" in interface and ethernet_interface in interface["netcard"]:
                    npf_id = interface["id"]
                    source_mac = interface["mac_address"]
                elif ethernet_interface in interface["name"]:
                    npf_id = interface["id"]
                    source_mac = interface["mac_address"]
            if npf_id:
                await self._ubridge_send('bridge add_nio_ethernet {name} "{interface}"'.format(name=bridge_name,
                                                                                                    interface=npf_id))
            else:
                raise NodeError("Could not find NPF id for interface {}".format(ethernet_interface))

            if block_host_traffic:
                if source_mac:
                    await self._ubridge_send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=bridge_name, mac=source_mac))
                    log.info('PCAP filter applied on "{interface}" for source MAC {mac}'.format(interface=ethernet_interface, mac=source_mac))
                else:
                    log.warning("Could not block host network traffic on {} (no MAC address found)".format(ethernet_interface))
        else:
            # on other platforms we just rely on the pcap library
            await self._ubridge_send('bridge add_nio_ethernet {name} "{interface}"'.format(name=bridge_name, interface=ethernet_interface))
            source_mac = None
            for interface in interfaces():
                if interface["name"] == ethernet_interface:
                    source_mac = interface["mac_address"]
            if source_mac:
                await self._ubridge_send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=bridge_name, mac=source_mac))
                log.info('PCAP filter applied on "{interface}" for source MAC {mac}'.format(interface=ethernet_interface, mac=source_mac))
Пример #2
0
    def _add_ubridge_ethernet_connection(self, bridge_name, ethernet_interface, block_host_traffic=False):
        """
        Creates a connection with an Ethernet interface in uBridge.

        :param bridge_name: bridge name in uBridge
        :param ethernet_interface: Ethernet interface name
        :param block_host_traffic: block network traffic originating from the host OS (Windows only)
        """

        if sys.platform.startswith("linux") and block_host_traffic is False:
            # on Linux we use RAW sockets by default excepting if host traffic must be blocked
            yield from self._ubridge_send('bridge add_nio_linux_raw {name} "{interface}"'.format(name=bridge_name, interface=ethernet_interface))
        elif sys.platform.startswith("win"):
            # on Windows we use Winpcap/Npcap
            windows_interfaces = interfaces()
            npf_id = None
            source_mac = None
            for interface in windows_interfaces:
                # Winpcap/Npcap uses a NPF ID to identify an interface on Windows
                if "netcard" in interface and ethernet_interface in interface["netcard"]:
                    npf_id = interface["id"]
                    source_mac = interface["mac_address"]
                elif ethernet_interface in interface["name"]:
                    npf_id = interface["id"]
                    source_mac = interface["mac_address"]
            if npf_id:
                yield from self._ubridge_send('bridge add_nio_ethernet {name} "{interface}"'.format(name=bridge_name,
                                                                                                    interface=npf_id))
            else:
                raise NodeError("Could not find NPF id for interface {}".format(ethernet_interface))

            if block_host_traffic:
                if source_mac:
                    yield from self._ubridge_send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=bridge_name, mac=source_mac))
                    log.info('PCAP filter applied on "{interface}" for source MAC {mac}'.format(interface=ethernet_interface, mac=source_mac))
                else:
                    log.warning("Could not block host network traffic on {} (no MAC address found)".format(ethernet_interface))
        else:
            # on other platforms we just rely on the pcap library
            yield from self._ubridge_send('bridge add_nio_ethernet {name} "{interface}"'.format(name=bridge_name, interface=ethernet_interface))
            source_mac = None
            for interface in interfaces():
                if interface["name"] == ethernet_interface:
                    source_mac = interface["mac_address"]
            if source_mac:
                yield from self._ubridge_send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=bridge_name, mac=source_mac))
                log.info('PCAP filter applied on "{interface}" for source MAC {mac}'.format(interface=ethernet_interface, mac=source_mac))
Пример #3
0
    def _add_ubridge_connection(self, nio, adapter_number):
        """
        Creates a connection in uBridge.

        :param nio: NIO instance
        :param adapter_number: adapter number
        """

        block_host_traffic = self.manager.config.get_section_config("VMware").getboolean("block_host_traffic", False)
        vnet = "ethernet{}.vnet".format(adapter_number)
        if vnet not in self._vmx_pairs:
            raise VMwareError("vnet {} not in VMX file".format(vnet))
        yield from self._ubridge_hypervisor.send("bridge create {name}".format(name=vnet))
        vmnet_interface = os.path.basename(self._vmx_pairs[vnet])
        if sys.platform.startswith("linux"):
            yield from self._ubridge_hypervisor.send('bridge add_nio_linux_raw {name} "{interface}"'.format(name=vnet,
                                                                                                            interface=vmnet_interface))
        elif sys.platform.startswith("win"):
            windows_interfaces = interfaces()
            npf = None
            source_mac = None
            for interface in windows_interfaces:
                if "netcard" in interface and vmnet_interface in interface["netcard"]:
                    npf = interface["id"]
                    source_mac = interface["mac_address"]
                elif vmnet_interface in interface["name"]:
                    npf = interface["id"]
                    source_mac = interface["mac_address"]
            if npf:
                yield from self._ubridge_hypervisor.send('bridge add_nio_ethernet {name} "{interface}"'.format(name=vnet,
                                                                                                               interface=npf))
            else:
                raise VMwareError("Could not find NPF id for VMnet interface {}".format(vmnet_interface))

            if block_host_traffic:
                if source_mac:
                    yield from self._ubridge_hypervisor.send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=vnet,
                                                                                                                          mac=source_mac))
                else:
                    log.warn("Could not block host network traffic on {} (no MAC address found)".format(vmnet_interface))

        elif sys.platform.startswith("darwin"):
            yield from self._ubridge_hypervisor.send('bridge add_nio_fusion_vmnet {name} "{interface}"'.format(name=vnet,
                                                                                                               interface=vmnet_interface))
        else:
            yield from self._ubridge_hypervisor.send('bridge add_nio_ethernet {name} "{interface}"'.format(name=vnet,
                                                                                                           interface=vmnet_interface))

        if isinstance(nio, NIOUDP):
            yield from self._ubridge_hypervisor.send('bridge add_nio_udp {name} {lport} {rhost} {rport}'.format(name=vnet,
                                                                                                                lport=nio.lport,
                                                                                                                rhost=nio.rhost,
                                                                                                                rport=nio.rport))

        if nio.capturing:
            yield from self._ubridge_hypervisor.send('bridge start_capture {name} "{pcap_file}"'.format(name=vnet,
                                                                                                        pcap_file=nio.pcap_output_file))

        yield from self._ubridge_hypervisor.send('bridge start {name}'.format(name=vnet))
Пример #4
0
def test_interfaces():
    # This test should pass on all platforms without crash
    interface_list = interfaces()
    assert isinstance(interface_list, list)
    for interface in interface_list:
        if interface["name"].startswith("vmnet"):
            assert interface["special"]

        assert "id" in interface
        assert "name" in interface
        assert "ip_address" in interface
        assert "mac_address" in interface
        if sys.platform.startswith("win"):
            assert "netcard" in interface
        assert "type" in interface
        assert "netmask" in interface
Пример #5
0
    def find_bridge_interface(self, vmnet_interface):
        """
        Find the bridge interface that is used for the vmnet interface in VMware.
        """

        if vmnet_interface in self._vmnets_info.keys():
            subnet = self._vmnets_info[vmnet_interface].get("subnet", None)
            netmask = self._vmnets_info[vmnet_interface].get("netmask", None)
            if subnet and netmask:
                for interface in interfaces():
                    try:
                        network = ipaddress.ip_network(f"{subnet}/{netmask}")
                        ip = ipaddress.ip_address(interface["ip_address"])
                    except ValueError:
                        continue
                    if ip in network:
                        return interface["name"]
        return None
Пример #6
0
    def _get_vmnet_interfaces_ubridge():

        vmnet_interfaces = []
        for interface in interfaces():
            if sys.platform.startswith("win"):
                if "netcard" in interface:
                    windows_name = interface["netcard"]
                else:
                    windows_name = interface["name"]
                match = re.search("(VMnet[0-9]+)", windows_name)
                if match:
                    vmnet = match.group(1)
                    if vmnet not in ("VMnet1", "VMnet8"):
                        vmnet_interfaces.append(vmnet)
            elif interface["name"].startswith("vmnet"):
                vmnet = interface["name"]
                if vmnet not in ("vmnet1", "vmnet8"):
                    vmnet_interfaces.append(interface["name"])
        return vmnet_interfaces
Пример #7
0
    def _get_vmnet_interfaces_ubridge():

        vmnet_interfaces = []
        for interface in interfaces():
            if sys.platform.startswith("win"):
                if "netcard" in interface:
                    windows_name = interface["netcard"]
                else:
                    windows_name = interface["name"]
                match = re.search("(VMnet[0-9]+)", windows_name)
                if match:
                    vmnet = match.group(1)
                    if vmnet not in ("VMnet1", "VMnet8"):
                        vmnet_interfaces.append(vmnet)
            elif interface["name"].startswith("vmnet"):
                vmnet = interface["name"]
                if vmnet not in ("vmnet1", "vmnet8"):
                    vmnet_interfaces.append(interface["name"])
        return vmnet_interfaces
Пример #8
0
    async def create_nio(self, node, nio_settings):
        """
        Creates a new NIO.

        :param node: Dynamips node instance
        :param nio_settings: information to create the NIO

        :returns: a NIO object
        """

        nio = None
        if nio_settings["type"] == "nio_udp":
            lport = nio_settings["lport"]
            rhost = nio_settings["rhost"]
            rport = nio_settings["rport"]
            try:
                info = socket.getaddrinfo(rhost, rport, socket.AF_UNSPEC, socket.SOCK_DGRAM, 0, socket.AI_PASSIVE)
                if not info:
                    raise DynamipsError("getaddrinfo returns an empty list on {}:{}".format(rhost, rport))
                for res in info:
                    af, socktype, proto, _, sa = res
                    with socket.socket(af, socktype, proto) as sock:
                        sock.connect(sa)
            except OSError as e:
                raise DynamipsError("Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e))
            nio = NIOUDP(node, lport, rhost, rport)
            nio.filters = nio_settings.get("filters", {})
            nio.suspend = nio_settings.get("suspend", False)
        elif nio_settings["type"] == "nio_generic_ethernet":
            ethernet_device = nio_settings["ethernet_device"]
            if sys.platform.startswith("win"):
                # replace the interface name by the GUID on Windows
                windows_interfaces = interfaces()
                npf_interface = None
                for interface in windows_interfaces:
                    if interface["name"] == ethernet_device:
                        npf_interface = interface["id"]
                if not npf_interface:
                    raise DynamipsError("Could not find interface {} on this host".format(ethernet_device))
                else:
                    ethernet_device = npf_interface
            if not is_interface_up(ethernet_device):
                raise aiohttp.web.HTTPConflict(text="Ethernet interface {} is down".format(ethernet_device))
            nio = NIOGenericEthernet(node.hypervisor, ethernet_device)
        elif nio_settings["type"] == "nio_linux_ethernet":
            if sys.platform.startswith("win"):
                raise DynamipsError("This NIO type is not supported on Windows")
            ethernet_device = nio_settings["ethernet_device"]
            nio = NIOLinuxEthernet(node.hypervisor, ethernet_device)
        elif nio_settings["type"] == "nio_tap":
            tap_device = nio_settings["tap_device"]
            nio = NIOTAP(node.hypervisor, tap_device)
            if not is_interface_up(tap_device):
                # test after the TAP interface has been created (if it doesn't exist yet)
                raise aiohttp.web.HTTPConflict(text="TAP interface {} is down".format(tap_device))
        elif nio_settings["type"] == "nio_unix":
            local_file = nio_settings["local_file"]
            remote_file = nio_settings["remote_file"]
            nio = NIOUNIX(node.hypervisor, local_file, remote_file)
        elif nio_settings["type"] == "nio_vde":
            control_file = nio_settings["control_file"]
            local_file = nio_settings["local_file"]
            nio = NIOVDE(node.hypervisor, control_file, local_file)
        elif nio_settings["type"] == "nio_null":
            nio = NIONull(node.hypervisor)
        else:
            raise aiohttp.web.HTTPConflict(text="NIO of type {} is not supported".format(nio_settings["type"]))

        await nio.create()
        return nio
Пример #9
0
def test_interfaces():
    # This test should pass on all platforms without crash
    assert isinstance(interfaces(), list)
Пример #10
0
    def _add_ubridge_connection(self, nio, adapter_number):
        """
        Creates a connection in uBridge.

        :param nio: NIO instance
        :param adapter_number: adapter number
        """

        block_host_traffic = self.manager.config.get_section_config("VMware").getboolean("block_host_traffic", False)
        vnet = "ethernet{}.vnet".format(adapter_number)
        if vnet not in self._vmx_pairs:
            raise VMwareError("vnet {} not in VMX file".format(vnet))
        yield from self._ubridge_hypervisor.send("bridge create {name}".format(name=vnet))
        vmnet_interface = os.path.basename(self._vmx_pairs[vnet])
        if sys.platform.startswith("linux"):
            yield from self._ubridge_hypervisor.send('bridge add_nio_linux_raw {name} "{interface}"'.format(name=vnet,
                                                                                                            interface=vmnet_interface))
        elif sys.platform.startswith("win"):
            windows_interfaces = interfaces()
            npf = None
            source_mac = None
            for interface in windows_interfaces:
                if "netcard" in interface and vmnet_interface in interface["netcard"]:
                    npf = interface["id"]
                    source_mac = interface["mac_address"]
                elif vmnet_interface in interface["name"]:
                    npf = interface["id"]
                    source_mac = interface["mac_address"]
            if npf:
                yield from self._ubridge_hypervisor.send('bridge add_nio_ethernet {name} "{interface}"'.format(name=vnet,
                                                                                                               interface=npf))
            else:
                raise VMwareError("Could not find NPF id for VMnet interface {}".format(vmnet_interface))

            if block_host_traffic:
                if source_mac:
                    yield from self._ubridge_hypervisor.send('bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=vnet,
                                                                                                                          mac=source_mac))
                else:
                    log.warn("Could not block host network traffic on {} (no MAC address found)".format(vmnet_interface))

        elif sys.platform.startswith("darwin"):
            yield from self._ubridge_hypervisor.send('bridge add_nio_fusion_vmnet {name} "{interface}"'.format(name=vnet,
                                                                                                               interface=vmnet_interface))
        else:
            yield from self._ubridge_hypervisor.send('bridge add_nio_ethernet {name} "{interface}"'.format(name=vnet,
                                                                                                           interface=vmnet_interface))

        if isinstance(nio, NIOUDP):
            yield from self._ubridge_hypervisor.send('bridge add_nio_udp {name} {lport} {rhost} {rport}'.format(name=vnet,
                                                                                                                lport=nio.lport,
                                                                                                                rhost=nio.rhost,
                                                                                                                rport=nio.rport))
        elif isinstance(nio, NIOTAP):
            yield from self._ubridge_hypervisor.send('bridge add_nio_tap {name} {tap}'.format(name=vnet, tap=nio.tap_device))

        if nio.capturing:
            yield from self._ubridge_hypervisor.send('bridge start_capture {name} "{pcap_file}"'.format(name=vnet,
                                                                                                        pcap_file=nio.pcap_output_file))

        yield from self._ubridge_hypervisor.send('bridge start {name}'.format(name=vnet))
Пример #11
0
    def network_interfaces(request, response):

        network_interfaces = interfaces()
        response.json(network_interfaces)
Пример #12
0
    def create_nio(self, node, nio_settings):
        """
        Creates a new NIO.

        :param node: Dynamips node instance
        :param nio_settings: information to create the NIO

        :returns: a NIO object
        """

        nio = None
        if nio_settings["type"] == "nio_udp":
            lport = nio_settings["lport"]
            rhost = nio_settings["rhost"]
            rport = nio_settings["rport"]
            try:
                info = socket.getaddrinfo(rhost, rport, socket.AF_UNSPEC,
                                          socket.SOCK_DGRAM, 0,
                                          socket.AI_PASSIVE)
                if not info:
                    raise DynamipsError(
                        "getaddrinfo returns an empty list on {}:{}".format(
                            rhost, rport))
                for res in info:
                    af, socktype, proto, _, sa = res
                    with socket.socket(af, socktype, proto) as sock:
                        sock.connect(sa)
            except OSError as e:
                raise DynamipsError(
                    "Could not create an UDP connection to {}:{}: {}".format(
                        rhost, rport, e))
            nio = NIOUDP(node, lport, rhost, rport,
                         nio_settings.get("filters", {}))
        elif nio_settings["type"] == "nio_generic_ethernet":
            ethernet_device = nio_settings["ethernet_device"]
            if sys.platform.startswith("win"):
                # replace the interface name by the GUID on Windows
                windows_interfaces = interfaces()
                npf_interface = None
                for interface in windows_interfaces:
                    if interface["name"] == ethernet_device:
                        npf_interface = interface["id"]
                if not npf_interface:
                    raise DynamipsError(
                        "Could not find interface {} on this host".format(
                            ethernet_device))
                else:
                    ethernet_device = npf_interface
            if not is_interface_up(ethernet_device):
                raise aiohttp.web.HTTPConflict(
                    text="Ethernet interface {} is down".format(
                        ethernet_device))
            nio = NIOGenericEthernet(node.hypervisor, ethernet_device)
        elif nio_settings["type"] == "nio_linux_ethernet":
            if sys.platform.startswith("win"):
                raise DynamipsError(
                    "This NIO type is not supported on Windows")
            ethernet_device = nio_settings["ethernet_device"]
            nio = NIOLinuxEthernet(node.hypervisor, ethernet_device)
        elif nio_settings["type"] == "nio_tap":
            tap_device = nio_settings["tap_device"]
            nio = NIOTAP(node.hypervisor, tap_device)
            if not is_interface_up(tap_device):
                # test after the TAP interface has been created (if it doesn't exist yet)
                raise aiohttp.web.HTTPConflict(
                    text="TAP interface {} is down".format(tap_device))
        elif nio_settings["type"] == "nio_unix":
            local_file = nio_settings["local_file"]
            remote_file = nio_settings["remote_file"]
            nio = NIOUNIX(node.hypervisor, local_file, remote_file)
        elif nio_settings["type"] == "nio_vde":
            control_file = nio_settings["control_file"]
            local_file = nio_settings["local_file"]
            nio = NIOVDE(node.hypervisor, control_file, local_file)
        elif nio_settings["type"] == "nio_null":
            nio = NIONull(node.hypervisor)
        else:
            raise aiohttp.web.HTTPConflict(
                text="NIO of type {} is not supported".format(
                    nio_settings["type"]))

        yield from nio.create()
        return nio
Пример #13
0
    def network_interfaces(request, response):

        network_interfaces = interfaces()
        response.json(network_interfaces)