Example #1
0
        static void Main(string[] args)
        {
            NdisApiDotNet ndisapi = new NdisApiDotNet(null);

            Console.ResetColor();

            Console.CancelKeyPress += HandleCancelKeyPress;

            if (!ndisapi.IsDriverLoaded())
            {
                Console.WriteLine("WinpkFilter driver is not loaded. Exiting.");
                return;
            }

            var vpnAdapter    = NetworkAdapterHelper.GetVpnAdapter(ndisapi);
            var normalAdapter = NetworkAdapterHelper.GetNormalAdapter(ndisapi);

            var tableList = IpHelperWrapper.GetTcpConnections("firefox");

            foreach (var line in tableList)
            {
                Console.WriteLine($"Source: {line.Local.Address} Port: {line.Local.Port} -> Destination: {line.Remote.Address} Port: {line.Remote.Port}");
            }

            Console.ReadLine();

            LoadOutFilter(vpnAdapter.Handle, tableList);
            LoadInFilter(normalAdapter.Handle, tableList);

            LoadFilterEverythingElseFilter(vpnAdapter.Handle);
            LoadFilterEverythingElseFilter(normalAdapter.Handle);

            var loaded = ndisapi.SetPacketFilterTable(filterList);

            outWorker.DoWork += (s, e) => { TreatOUTPacketsVPNtoNormal(ndisapi, vpnAdapter, normalAdapter); };

            inWorker.DoWork += (s, e) => { TreatINPacketsNormaltoVPN(ndisapi, normalAdapter, vpnAdapter); };

            Console.WriteLine("-- Filtering started");

            outWorker.RunWorkerAsync();
            inWorker.RunWorkerAsync();

            Console.ReadLine();

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

            DumpStaticFilters(ndisapi);

            Console.WriteLine("Stopped!");
            Console.ReadLine();
        }
Example #2
0
        private static bool LoadIpv4DnsFilter(IntPtr adapterHandle)
        {
            var filterList = new List <StaticFilter>(3);

            //
            // Initialize static filters
            //

            // 1.Outgoing DNS requests filter: REDIRECT OUT UDP packets with destination PORT 53
            filterList.Add(
                new StaticFilter(
                    adapterHandle,
                    PACKET_FLAG.PACKET_FLAG_ON_SEND,
                    StaticFilter.FILTER_PACKET_ACTION.FILTER_PACKET_REDIRECT,
                    StaticFilter.STATIC_FILTER_FIELDS.NETWORK_LAYER_VALID | StaticFilter.STATIC_FILTER_FIELDS.TRANSPORT_LAYER_VALID,
                    null,
                    new IpAddressFilter(
                        AddressFamily.InterNetwork,
                        IpAddressFilter.IP_FILTER_FIELDS.IP_FILTER_PROTOCOL,
                        null,
                        null,
                        (byte)IPProtocolType.UDP
                        ),
                    new TcpUdpFilter(
                        TcpUdpFilter.TCPUDP_FILTER_FIELDS.TCPUDP_DEST_PORT,
                        new TcpUdpFilter.PortRange {
                startRange = 0, endRange = 0
            },
                        new TcpUdpFilter.PortRange {
                startRange = 53, endRange = 53
            },
                        0)
                    ));

            // 2.Incoming DNS requests filter: REDIRECT IN UDP packets with source PORT 53
            filterList.Add(
                new StaticFilter(
                    adapterHandle,
                    PACKET_FLAG.PACKET_FLAG_ON_RECEIVE,
                    StaticFilter.FILTER_PACKET_ACTION.FILTER_PACKET_REDIRECT,
                    StaticFilter.STATIC_FILTER_FIELDS.NETWORK_LAYER_VALID | StaticFilter.STATIC_FILTER_FIELDS.TRANSPORT_LAYER_VALID,
                    null,
                    new IpAddressFilter(
                        AddressFamily.InterNetwork,
                        IpAddressFilter.IP_FILTER_FIELDS.IP_FILTER_PROTOCOL,
                        null,
                        null,
                        (byte)IPProtocolType.UDP
                        ),
                    new TcpUdpFilter(
                        TcpUdpFilter.TCPUDP_FILTER_FIELDS.TCPUDP_SRC_PORT,
                        new TcpUdpFilter.PortRange {
                startRange = 53, endRange = 53
            },
                        new TcpUdpFilter.PortRange {
                startRange = 0, endRange = 0
            },
                        0)
                    ));

            // 3.Pass over everything else
            filterList.Add(
                new StaticFilter(
                    adapterHandle,
                    PACKET_FLAG.PACKET_FLAG_ON_SEND_RECEIVE,
                    StaticFilter.FILTER_PACKET_ACTION.FILTER_PACKET_PASS,
                    0,
                    null,
                    null,
                    null
                    ));

            // Load static filter into the driver
            return(ndisapi.SetPacketFilterTable(filterList));
        }
Example #3
0
        static void Main(string[] args)
        {
            NdisApiDotNet ndisapi = new NdisApiDotNet(null);

            Console.ResetColor();

            Console.CancelKeyPress += HandleCancelKeyPress;

            if (!ndisapi.IsDriverLoaded())
            {
                Console.WriteLine("WinpkFilter driver is not loaded. Exiting.");
                return;
            }

            var vpnAdapter = NetworkAdapterHelper.GetAdapterById(ndisapi, vpnAdapterID);

            vpnIP         = NetworkAdapterHelper.GetAdapterLocalIP(vpnAdapterID);
            vpnMacAddress = NetworkAdapterHelper.GetAdapterPhisicalAddress(vpnAdapterID);

            var normalAdapter = NetworkAdapterHelper.GetAdapterById(ndisapi, normalAdapterID);

            localIp         = NetworkAdapterHelper.GetAdapterLocalIP(normalAdapterID);
            localMacAddress = NetworkAdapterHelper.GetAdapterPhisicalAddress(normalAdapterID);
            Console.WriteLine($"======================================================================================");
            Console.WriteLine($"Found Ethernet Adapter MAC: {localMacAddress} IP: {localIp}");
            Console.WriteLine($"Found VPN Adapter      MAC: {vpnMacAddress} IP: {vpnIP}");
            Console.WriteLine($"======================================================================================");

            if (vpnIP == null)
            {
                throw new Exception("VPN not connected");
            }

            var tableList = IpHelperWrapper.GetTcpConnections("firefox");

            Console.WriteLine();
            Console.WriteLine($"==============================FireFox sockets=========================================");
            Console.WriteLine($"======================================================================================");
            foreach (var line in tableList)
            {
                Console.WriteLine($"Source: {line.Local.Address} Port: {line.Local.Port} -> Destination: {line.Remote.Address} Port: {line.Remote.Port}");
            }
            Console.WriteLine($"======================================================================================");

            Console.ReadLine();

            LoadOutFilter(vpnAdapter.Handle, tableList);
            LoadInFilter(normalAdapter.Handle, tableList);

            //LoadTESTInFilter(normalAdapter.Handle, tableList);
            //LoadTESTOutFilter(normalAdapter.Handle, tableList);

            //LoadTESTInFilter(vpnAdapter.Handle, tableList);

            LoadFilterEverythingElseFilter(vpnAdapter.Handle);
            LoadFilterEverythingElseFilter(normalAdapter.Handle);

            var loaded = ndisapi.SetPacketFilterTable(filterList);

            outWorker.DoWork += (s, e) => { TreatOUTPacketsVPNtoNormal(ndisapi, vpnAdapter, normalAdapter); };

            inWorker.DoWork += (s, e) => { TreatINPacketsNormaltoVPN(ndisapi, normalAdapter, vpnAdapter); };

            Console.WriteLine("-- Filtering started");

            outWorker.RunWorkerAsync();
            inWorker.RunWorkerAsync();

            Console.ReadLine();

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

            DumpStaticFilters(ndisapi);

            Console.WriteLine("Stopped!");
            Console.ReadLine();
        }