Esempio n. 1
0
        /// <summary>
        /// Returns a list of devices
        /// </summary>
        /// <param name="address">
        /// A <see cref="IPAddress"/>
        /// </param>
        /// <param name="port">
        /// A <see cref="int"/>
        /// </param>
        /// <param name="remoteAuthentication">
        /// A <see cref="RemoteAuthentication"/>
        /// </param>
        /// <returns>
        /// A <see cref="List<WinPcapDevice>"/>
        /// </returns>
        public static List <WinPcapDevice> Devices(IPAddress address,
                                                   int port,
                                                   RemoteAuthentication remoteAuthentication)
        {
            var source = new IPEndPoint(address, port);

            return(BuildDeviceList(PcapInterface.GetAllPcapInterfaces(source, remoteAuthentication)));
        }
Esempio n. 2
0
 public void PcapInterfaceNullAuthTest(
     [ValueSource(nameof(NullAuthCredentials))] RemoteAuthentication credentials
     )
 {
     using (new RemotePcapServer(NullAuthArgs))
     {
         var list = PcapInterface.GetAllPcapInterfaces(LoopbackSource, credentials);
         CollectionAssert.IsNotEmpty(list);
     }
 }
Esempio n. 3
0
 public void NpcapDeviceListNullAuthTest()
 {
     using (new RemotePcapServer(NullAuthArgs))
     {
         var auth     = new RemoteAuthentication(AuthenticationTypes.Null, null, null);
         var loopback = new IPEndPoint(IPAddress.Loopback, 2002);
         CollectionAssert.IsNotEmpty(PcapInterface.GetAllPcapInterfaces(loopback, auth));
         CollectionAssert.IsNotEmpty(PcapInterface.GetAllPcapInterfaces(loopback, null));
     }
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            if ((args.Length < 1) || (args.Length > 2))
            {
                PrintUsage("NpcapRemoteCapture");
                return;
            }

            // ensure that a remote capture daemon has been started by running
            // 'rpcapd.exe' on the remote server. By default port 2003 is used for the server

            var ipAddress = System.Net.IPAddress.Parse(args[0]);
            var port      = rpcapDefaultPort;

            if (args.Length == 2)
            {
                port = Int32.Parse(args[1]);
            }

            var remoteInterfaces = PcapInterface.GetAllPcapInterfaces(new IPEndPoint(ipAddress, port), null);

            foreach (var dev in remoteInterfaces)
            {
                Console.WriteLine("device: {0}", dev.ToString());
            }

            // open the device for capture
            using var device = new LibPcapLiveDevice(remoteInterfaces[0]);

            device.OnPacketArrival += new PacketArrivalEventHandler(dev_OnPacketArrival);

            device.Open(new DeviceConfiguration {
                ReadTimeout = 500
            });

            Console.WriteLine();
            Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
                              device.Description);

            // Start the capturing process
            device.StartCapture();

            // Wait for 'Enter' from the user.
            Console.ReadLine();

            // Stop the capturing process
            device.StopCapture();

            Console.WriteLine("-- Capture stopped.");

            // Print out the device statistics
            Console.WriteLine(device.Statistics.ToString());
        }
Esempio n. 5
0
        public void TestPcapTapExchange()
        {
            var nic = TunnelDevice.GetTunnelInterfaces().First();

            using var tapDevice = GetTunnelDevice(nic);
            // Open TAP device first to ensure the virutal device is connected
            tapDevice.Open();
            // Wait for interface to be fully up
            Thread.Sleep(1000);
            var pcapInterface = PcapInterface.GetAllPcapInterfaces()
                                .First(pIf => pIf.FriendlyName == nic.Name);

            using var pcapDevice = new LibPcapLiveDevice(pcapInterface);
            PcapDeviceTest.CheckExchange(tapDevice, pcapDevice);
        }
Esempio n. 6
0
        public void PwdAuthTest()
        {
            try
            {
                if (!TestUser.Create())
                {
                    Assert.Inconclusive("Please rerun the test as administrator.");
                }
                var goodCred = new RemoteAuthentication(AuthenticationTypes.Password, TestUser.Username, TestUser.Password);
                var badCred  = new RemoteAuthentication(AuthenticationTypes.Password, "foo", "bar");
                using (new RemotePcapServer(PwdAuthArgs))
                {
                    var pcapIfs  = PcapInterface.GetAllPcapInterfaces("rpcap://localhost/", goodCred);
                    var loopback = new IPEndPoint(IPAddress.Loopback, 2002);

                    // using rpcap with LibPcapLiveDevice should be possible
                    using var device = new LibPcapLiveDevice(pcapIfs[0]);

                    // repassing the auth to Open() should be optional
                    device.Open();
                    Assert.IsTrue(device.Opened);
                    device.Close();

                    Assert.Throws <PcapException>(
                        () => device.Open(StrictConfig(new DeviceConfiguration
                    {
                        Mode = DeviceModes.NoCaptureRemote,
                        // Setting Immediate increase code coverage
                        // Does not affect overall function of the test
                        // Immediate would be translated to DeviceModes.MaxResponsiveness
                        Immediate   = true,
                        ReadTimeout = 1,
                        Credentials = badCred
                    })),
                        "Credentials provided to Open() method takes precedence"
                        );

                    Assert.Throws <PcapException>(
                        () => PcapInterface.GetAllPcapInterfaces("rpcap://localhost/", badCred)
                        );
                }
            }
            finally
            {
                TestUser.Delete();
            }
        }
Esempio n. 7
0
        public void PwdAuthTest()
        {
            try
            {
                if (!TestUser.Create())
                {
                    Assert.Inconclusive("Please rerun the test as administrator.");
                }
                var goodCred = new RemoteAuthentication(AuthenticationTypes.Password, TestUser.Username, TestUser.Password);
                var badCred  = new RemoteAuthentication(AuthenticationTypes.Password, "foo", "bar");
                using (new RemotePcapServer(PwdAuthArgs))
                {
                    var pcapIfs      = PcapInterface.GetAllPcapInterfaces("rpcap://localhost/", goodCred);
                    var npcapDevices = NpcapDeviceList.Devices(IPAddress.Loopback, NpcapDeviceList.RpcapdDefaultPort, goodCred);
                    CollectionAssert.IsNotEmpty(npcapDevices);

                    var devices = new PcapDevice[] {
                        // using NpcapDevice
                        npcapDevices[0],
                        // using rpcap with LibPcapLiveDevice should be possible
                        new LibPcapLiveDevice(pcapIfs[0])
                    };
                    foreach (var device in devices)
                    {
                        // repassing the auth to Open() should be optional
                        device.Open();
                        Assert.IsTrue(device.Opened);
                        device.Close();
                    }

                    Assert.Throws <PcapException>(
                        () => npcapDevices[0].Open(OpenFlags.NoCaptureRemote, 1, badCred),
                        "Credentials provided to Open() method takes precedence"
                        );

                    Assert.Throws <PcapException>(
                        () => PcapInterface.GetAllPcapInterfaces("rpcap://localhost/", badCred)
                        );
                }
            }
            finally
            {
                TestUser.Delete();
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Find the first Ethernet adapter that is actually connected to something
        /// </summary>
        /// <returns></returns>
        internal static LibPcapLiveDevice GetPcapDevice()
        {
            var nics = NetworkInterface.GetAllNetworkInterfaces();

            foreach (var inf in PcapInterface.GetAllPcapInterfaces())
            {
                var friendlyName = inf.FriendlyName ?? string.Empty;
                if (friendlyName.ToLower().Contains("loopback") || friendlyName == "any")
                {
                    continue;
                }
                if (friendlyName == "virbr0-nic")
                {
                    // Semaphore CI have this interface, and it's always down
                    // OperationalStatus does not detect it correctly
                    continue;
                }
                var nic = nics.FirstOrDefault(ni => ni.Name == friendlyName);
                if (nic?.OperationalStatus != OperationalStatus.Up)
                {
                    continue;
                }
                using var device = new LibPcapLiveDevice(inf);
                LinkLayers link;
                try
                {
                    device.Open();
                    link = device.LinkType;
                }
                catch (PcapException ex)
                {
                    Console.WriteLine(ex);
                    continue;
                }

                if (link == LinkLayers.Ethernet)
                {
                    return(device);
                }
            }
            throw new InvalidOperationException("No ethernet pcap supported devices found, are you running" +
                                                " as a user with access to adapters (root on Linux)?");
        }
Esempio n. 9
0
 public static List <WinPcapDevice> Devices()
 {
     return(BuildDeviceList(PcapInterface.GetAllPcapInterfaces()));
 }