예제 #1
0
        /// <summary>
        /// Starts a pass thru thread.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="waitHandles">The wait handles.</param>
        /// <param name="networkAdapters">The network adapters.</param>
        /// <param name="waitHandlesManualResetEvents">The wait handles manual reset events.</param>
        private static void PassThruThread(NdisApi filter, WaitHandle[] waitHandles, IReadOnlyList <NetworkAdapter> networkAdapters, IReadOnlyList <ManualResetEvent> waitHandlesManualResetEvents)
        {
            var ndisApiHelper = new NdisApiHelper();

            var ethPackets = ndisApiHelper.CreateEthMRequest();

            while (true)
            {
                var handle = WaitHandle.WaitAny(waitHandles);
                ethPackets.AdapterHandle = networkAdapters[handle].Handle;

                while (filter.ReadPackets(ref ethPackets))
                {
                    var packets = ethPackets.Packets;
                    for (int i = 0; i < ethPackets.PacketsCount; i++)
                    {
                        var ethPacket = packets[i].GetEthernetPacket(ndisApiHelper);
                        if (ethPacket.PayloadPacket is IPv4Packet iPv4Packet)
                        {
                            if (iPv4Packet.PayloadPacket is TcpPacket tcpPacket)
                            {
                                Console.WriteLine($"{iPv4Packet.SourceAddress}:{tcpPacket.SourcePort} -> {iPv4Packet.DestinationAddress}:{tcpPacket.DestinationPort}.");
                            }
                        }
                    }

                    filter.SendPackets(ref ethPackets);
                    ethPackets.PacketsCount = 0;
                }

                waitHandlesManualResetEvents[handle].Reset();
            }
        }
예제 #2
0
        static void Main()
        {
            var filter = NdisApi.Open();  // check driver available

            if (!filter.IsValid)
            {
                throw new ApplicationException("Драйвер не найден");
            }
            Console.WriteLine($"Версия сетевого драйвера: {filter.GetVersion()}");
            // Создать и установить событие для адаптеров
            var waitHandlesCollection = new List <ManualResetEvent>();
            // Creating network adapter list
            var tcpAdapters = new List <NetworkAdapter>();

            foreach (var networkAdapter in filter.GetNetworkAdapters())
            {
                if (networkAdapter.IsValid)
                {
                    var success = filter.SetAdapterMode(networkAdapter,
                                                        NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_TUNNEL |
                                                        NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_LOOPBACK_FILTER |
                                                        NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_LOOPBACK_BLOCK);
                    var manualResetEvent = new ManualResetEvent(false);
                    success &= filter.SetPacketEvent(networkAdapter,
                                                     manualResetEvent.SafeWaitHandle);
                    if (success)
                    {
                        Console.WriteLine($"Добавлен адаптер: {networkAdapter.FriendlyName}");
                        // Добавление адаптеров в список
                        waitHandlesCollection.Add(manualResetEvent);
                        tcpAdapters.Add(networkAdapter);
                    }
                }
            }
            var waitHandlesManualResetEvents =
                waitHandlesCollection.Cast <ManualResetEvent>().ToArray();
            var waitHandles = waitHandlesCollection.Cast <WaitHandle>().ToArray();

            Console.Write("Press ENTER to start");
            ConsoleKeyInfo keyInfo = Console.ReadKey();

            if (keyInfo.Key == ConsoleKey.Enter)
            {
                var t1 = Task.Factory.StartNew(() => Threadripper.PassThruThread(filter, waitHandles, tcpAdapters.ToArray(), waitHandlesManualResetEvents));
                Task.WaitAll(t1);
                Console.Read();
            }
        }
예제 #3
0
        private static void Main()
        {
            var filter = NdisApi.Open();

            if (!filter.IsValid)
            {
                throw new ApplicationException("Cannot load driver.");
            }


            Console.WriteLine($"Loaded driver: {filter.GetVersion()}.");

            // Create and set event for the adapters.
            var waitHandlesCollection = new List <ManualResetEvent>();
            var tcpAdapters           = new List <NetworkAdapter>();

            foreach (var networkAdapter in filter.GetNetworkAdapters())
            {
                if (networkAdapter.IsValid)
                {
                    var success = filter.SetAdapterMode(networkAdapter,
                                                        NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_TUNNEL |
                                                        NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_LOOPBACK_FILTER |
                                                        NdisApiDotNet.Native.NdisApi.MSTCP_FLAGS.MSTCP_FLAG_LOOPBACK_BLOCK);

                    var manualResetEvent = new ManualResetEvent(false);

                    success &= filter.SetPacketEvent(networkAdapter, manualResetEvent.SafeWaitHandle);

                    if (success)
                    {
                        Console.WriteLine($"Added {networkAdapter.FriendlyName}.");

                        waitHandlesCollection.Add(manualResetEvent);
                        tcpAdapters.Add(networkAdapter);
                    }
                }
            }

            var waitHandlesManualResetEvents = waitHandlesCollection.Cast <ManualResetEvent>().ToArray();
            var waitHandles = waitHandlesCollection.Cast <WaitHandle>().ToArray();

            var t1 = Task.Factory.StartNew(() => PassThruThread(filter, waitHandles, tcpAdapters.ToArray(), waitHandlesManualResetEvents));

            Task.WaitAll(t1);

            Console.Read();
        }
예제 #4
0
        public static void PassThruThread(NdisApi filter, WaitHandle[] waitHandles, IReadOnlyList <NetworkAdapter> networkAdapters, IReadOnlyList <ManualResetEvent> waitHandlesManualResetEvents)
        {
            int    port          = 0;
            string ip            = "";
            var    ndisApiHelper = new NdisApiHelper();
            var    ethRequest    = ndisApiHelper.CreateEthRequest();

            Console.WriteLine("\nChoose the mode");
            //Console.WriteLine("\n1. Show all connections");
            Console.WriteLine("\n1. Close connections by source port" +
                              "\n2. Close connections by source IP" +
                              "\n3. Close connections by destination port" +
                              "\n4. Close connections by destination IP" +
                              "\n5. Close all connections");
            var mode = int.Parse(Console.ReadLine());

            if (mode == 1 | mode == 3)
            {
                Console.WriteLine("\nEnter a port:");
                port = int.Parse(Console.ReadLine());
            }
            if (mode == 2 | mode == 4)
            {
                Console.WriteLine("\nEnter an IP:");
                ip = (Console.ReadLine());
            }

            while (true)
            {
                var handle = WaitHandle.WaitAny(waitHandles);
                ethRequest.AdapterHandle = networkAdapters[handle].Handle;
                while (filter.ReadPacket(ref ethRequest))
                {
                    var ethPacket = ethRequest.Packet.GetEthernetPacket(ndisApiHelper);
                    if (ethPacket.PayloadPacket is IPv4Packet iPv4Packet)
                    {
                        if (iPv4Packet.PayloadPacket is TcpPacket tcpPacket)
                        {
                            if (mode == 1 && tcpPacket.SourcePort == port)
                            {
                                Console.WriteLine($"{iPv4Packet.SourceAddress}:{tcpPacket.SourcePort} -> { iPv4Packet.DestinationAddress}:{ tcpPacket.DestinationPort}.");
                                continue;
                            }
                            if (mode == 3 && tcpPacket.DestinationPort == port)
                            {
                                Console.WriteLine($"{iPv4Packet.SourceAddress}:{tcpPacket.SourcePort} -> { iPv4Packet.DestinationAddress}:{ tcpPacket.DestinationPort}.");
                                continue;
                            }
                            if (mode == 2 && iPv4Packet.SourceAddress == IPAddress.Parse(ip))
                            {
                                Console.WriteLine($"{iPv4Packet.SourceAddress}:{tcpPacket.SourcePort} -> { iPv4Packet.DestinationAddress}:{ tcpPacket.DestinationPort}.");
                                continue;
                            }

                            if (mode == 4 && iPv4Packet.DestinationAddress == IPAddress.Parse(ip))
                            {
                                Console.WriteLine($"{iPv4Packet.SourceAddress}:{tcpPacket.SourcePort} -> { iPv4Packet.DestinationAddress}:{ tcpPacket.DestinationPort}.");
                                continue;
                            }
                            if (mode == 5)
                            {
                                Console.WriteLine($"{iPv4Packet.SourceAddress}:{tcpPacket.SourcePort} -> { iPv4Packet.DestinationAddress}:{ tcpPacket.DestinationPort}.");
                                continue;
                            }
                        }
                    }
                    filter.SendPacket(ref ethRequest);
                }
                waitHandlesManualResetEvents[handle].Reset();
            }
        }