Пример #1
0
        private void Calculate()
        {
            IsCalculationRunning = true;

            IPNetwork subnet1 = IPNetwork.Parse(Subnet1);
            IPNetwork subnet2 = IPNetwork.Parse(Subnet2);

            IPNetwork subnet = subnet1.Supernet(subnet2);

            NetworkAddress = subnet.Network;
            Broadcast      = subnet.Broadcast;
            Subnetmask     = subnet.Netmask;
            CIDR           = subnet.Cidr;
            IPAddresses    = subnet.Total;
            FirstIPAddress = subnet.FirstUsable;
            LastIPAddress  = subnet.LastUsable;
            Hosts          = subnet.Usable;

            IsResultVisible = true;

            AddSubnet1ToHistory(Subnet1);
            AddSubnet2ToHistory(Subnet2);

            IsCalculationRunning = false;
        }
Пример #2
0
        /*
         * 2020-07-23 14:58:26 Routes: 0.0.0.0/0, 10.4.10.1/32, 10.4.10.4/30
         * 2020-07-23 14:58:26 Routes excluded: 62.210.26.0/24, 192.168.1.173/24
         * fe80:0:0:0:f660:e2ff:fef0:23ee/64
         * 2020-07-23 14:58:26 VpnService routes installed: 0.0.0.0/3, 32.0.0.0/4,
         * 48.0.0.0/5, 56.0.0.0/6, 60.0.0.0/7, 62.0.0.0/9, 62.128.0.0/10,
         * 62.192.0.0/12, 62.208.0.0/15, 62.210.0.0/20, 62.210.16.0/21,
         * 62.210.24.0/23, 62.210.27.0/24, 62.210.28.0/22, 62.210.32.0/19,
         * 62.210.64.0/18, 62.210.128.0/17, 62.211.0.0/16, 62.212.0.0/14,
         * 62.216.0.0/13, 62.224.0.0/11, 63.0.0.0/8, 64.0.0.0/2, 128.0.0.0/2,
         * 192.0.0.0/9, 192.128.0.0/11, 192.160.0.0/13, 192.168.0.0/24,
         * 192.168.2.0/23, 192.168.4.0/22, 192.168.8.0/21, 192.168.16.0/20,
         * 192.168.32.0/19, 192.168.64.0/18, 192.168.128.0/17, 192.169.0.0/16,
         * 192.170.0.0/15, 192.172.0.0/14, 192.176.0.0/12, 192.192.0.0/10,
         * 193.0.0.0/8, 194.0.0.0/7,
         * 196.0.0.0/6, 200.0.0.0/5, 208.0.0.0/4, 224.0.0.0/3*/
        static void Main(string[] args)
        {
            var excluded = new List <IPNetwork>();
            var exclude1 = IPNetwork.Parse("62.210.26.0/24");
            var exclude2 = IPNetwork.Parse("192.168.1.173/24");

            excluded.Add(exclude1);
            excluded.Add(exclude2);
            excluded.Add(IPNetwork.Parse("224.0.0.0/3"));

            var actualNet = new List <IPNetwork>()
            {
                IPNetwork.Parse("0.0.0.0/0")
            };

            var newRoutes = new List <IPNetwork>();
            var removed   = new List <IPNetwork>();

            foreach (var route in excluded)
            {
                newRoutes.Clear();
                removed.Clear();
                foreach (var main in actualNet)
                {
                    if (main.Overlap(route) == false)
                    {
                        continue;
                    }
                    removed.Add(main);
                    RecursiveAdd(main, route, newRoutes);
                    //var current = main;
                    //while (current.Cidr < 32)
                    //{
                    //    if (route.Contains(current))
                    //        break;
                    //    var splitted = Split(current);
                    //    var firstOverLap = route.Overlap(splitted[0]);
                    //    var secondOverlap = route.Overlap(splitted[1]);

                    //    if (firstOverLap && secondOverlap)
                    //    {
                    //        break;
                    //    }
                    //    else if (firstOverLap)
                    //    {
                    //        current = splitted[0];
                    //        newRoutes.Add(splitted[1]);
                    //    }
                    //    else if (secondOverlap)
                    //    {
                    //        current = splitted[1];
                    //        newRoutes.Add(splitted[0]);
                    //    }
                    //}
                }
                var allNew     = string.Join(",", newRoutes.Select(x => x.Network));
                var allRemoved = string.Join(",", removed.Select(x => x.Network));
                Console.WriteLine($"New {allNew}");
                Console.WriteLine($"Removed {allRemoved}");
                removed.ForEach(x => actualNet.Remove(x));
                actualNet.AddRange(newRoutes);
            }
            var all         = IPNetwork.Supernet(actualNet.ToArray());
            var stringLinst = new List <string>();

            foreach (var dt in all)
            {
                var m = $"{dt.Network}/{dt.Cidr}";
                stringLinst.Add(m);
                Console.WriteLine(m);
            }

            foreach (var item in stringLinst)
            {
                if (!target.Contains(item))
                {
                    Console.WriteLine("Error List: " + item);
                }
            }

            foreach (var item in target)
            {
                if (!stringLinst.Contains(item))
                {
                    Console.WriteLine("Error Target: " + item);
                }
            }

            Console.ReadLine();
        }
Пример #3
0
        static void Main(string[] args)
        {
            if (args.Length != 1 || inputDirectory(args) != true)
            {
                helpRequested();
            }
            try
            {
                DirectoryInfo diTop      = new DirectoryInfo(args[0]);
                List <string> strSubnets = new List <string>();

                foreach (FileInfo fi in diTop.EnumerateFiles("*ipranges*"))
                {
                    foreach (string line in File.ReadLines(fi.FullName))
                    {
                        if (line.Contains(":") || line.Contains("/"))
                        {
                            strSubnets.Add(line);
                        }
                    }
                }
                int         numSubnets = strSubnets.Count;
                IPNetwork[] ipSubnets  = new IPNetwork[numSubnets];

                StreamWriter fs = new StreamWriter(args[0] + "/summarizedSubnets.txt");

                for (int i = 0; i < numSubnets; i++)
                {
                    ipSubnets[i] = IPNetwork.Parse(strSubnets[i]);
                }

                IPNetwork[] summIPNetworks = IPNetwork.Supernet(ipSubnets);

                foreach (IPNetwork item in summIPNetworks)
                {
                    fs.WriteLine(item.ToString());
                }
                Console.WriteLine();
                Console.WriteLine($"Aggregate Number of Subnets: {strSubnets.Count}");
                Console.WriteLine($"Summarized Subnets: {summIPNetworks.Length}");
                Console.WriteLine();
                Console.WriteLine($"Output file: {args[0] + "/summarizedSubnets.txt"}");
                Console.WriteLine();


                fs.Close();
            }
            catch (System.ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Please verify file subnets are in proper CIDR notation format with netmasks: '192.168.1.0/24'");
            }
            catch (System.ArgumentException ex) {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Please verify subnet ID within input files utilizes proper CIDR notation format: '192.168.1.0/24'");
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
                Console.WriteLine("There is a disturbance in the force.  Please double check your input files and review usuage guidance by running 'summSub -h'. Proper format per line per file: '192.168.1.0/24'");
            }

            void helpRequested()
            {
                Console.WriteLine();
                Console.WriteLine("Summarize Subnets - summSub usage:");
                Console.WriteLine();
                Console.WriteLine("What does summSub do:  summSub will aggregate all subnets found within any files found within the");
                Console.WriteLine("provided input directory and summarize them.  For example if a file contains:");
                Console.WriteLine();
                Console.WriteLine("192.168.2.0/24");
                Console.WriteLine("192.168.3.0/24");
                Console.WriteLine();
                Console.WriteLine("Then summSub will output a file called 'summarizedSubnets.txt' that contains a single entry:");
                Console.WriteLine();
                Console.WriteLine("192.168.2.0/23");
                Console.WriteLine();
                Console.WriteLine("The new subnet can then be used for both security and routing decisions");
                Console.WriteLine();
                Console.WriteLine("Use of summSub requires the input directory of the files containing the subnets to be summarized");
                Console.WriteLine("supplied as an argument when launching the command as follows:");
                Console.WriteLine();
                Console.WriteLine("-  To parse files in the current working directory, simple use a period:  'summSub .'");
                Console.WriteLine("-  To parse files in a specific directory, simply supply the directory path: 'summSub C:\\subnets\\input'");
                Console.WriteLine();
                Console.WriteLine("Notes:  summSub will parse all files in the input directory with a file name that includes the phrase 'ipranges'");
                Console.WriteLine("and will exclude all lines which do not contain a ':' or a '/'.  summSub should work for both ipv4 and");
                Console.WriteLine("ipv6 summariazation efforts.");
                Console.WriteLine();
                System.Environment.Exit(0);
            }

            bool inputDirectory(string[] inDir)
            {
                if (inDir[0] == ".")
                {
                    args[0] = Environment.CurrentDirectory;
                    return(true);
                }
                else
                {
                    inDir[0] = inDir[0].Replace('\\', '/');
                    inDir[0] = inDir[0].Replace("\"", "");
                    int inDirCheck = inDir[0].IndexOf(".");
                    if (inDirCheck != -1)
                    {
                        string dirPath = Path.Combine(Environment.CurrentDirectory, inDir[0]);
                        inDir[0] = (Path.GetFullPath(dirPath));
                    }

                    if (Directory.Exists(inDir[0]) == true)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            Dictionary <string, string> AreaBlack = new Dictionary <string, string>();
            Dictionary <string, string> AreaWhite = new Dictionary <string, string>();

            AreaBlack.Add("1814991", "CN");
            //AreaWhite.Add("1668284", "TW");
            //AreaWhite.Add("1819730", "HK");
            //AreaWhite.Add("1835841", "KR");
            //AreaWhite.Add("1861060", "JP");
            //AreaWhite.Add("1880251", "SG");
            //AreaWhite.Add("2017370", "RU");
            //AreaWhite.Add("2077456", "AU");
            //AreaWhite.Add("2635167", "GB");
            //AreaWhite.Add("2921044", "DE");
            //AreaWhite.Add("3017382", "FR");
            //AreaWhite.Add("6251999", "CA");
            //AreaWhite.Add("6252001", "US");
            //AreaWhite.Add("6255147", "Asia");
            //AreaWhite.Add("6255148", "Euro");
            List <IPNetwork> Routes = new List <IPNetwork>();
            WebClient        client = new WebClient();

            var            URLAddress     = "https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip";
            Stream         DatabaseFile   = client.OpenRead(URLAddress);
            ZipInputStream DatabaseStream = new ZipInputStream(DatabaseFile);
            ZipEntry       zipEntry       = DatabaseStream.GetNextEntry();

            while (zipEntry != null)
            {
                String entryFileName = zipEntry.Name;
                if (entryFileName.Length == 0)
                {
                    zipEntry = DatabaseStream.GetNextEntry();
                    continue;
                }
                if (!entryFileName.Contains(".csv"))
                {
                    zipEntry = DatabaseStream.GetNextEntry();
                    continue;
                }
                if (!entryFileName.Contains("Blocks"))
                {
                    zipEntry = DatabaseStream.GetNextEntry();
                    continue;
                }
                MemoryStream memStream = new MemoryStream();
                DatabaseStream.CopyTo(memStream);
                var result = System.Text.Encoding.UTF8.GetString(memStream.ToArray()).Split('\n');
                foreach (var line in result)
                {
                    if (line == "")
                    {
                        continue;
                    }
                    var thisline = line.Split(',');
                    if (AreaBlack.ContainsKey(thisline[1]))
                    {
                        continue;
                    }
                    if (AreaBlack.ContainsKey(thisline[2]))
                    {
                        continue;
                    }
                    //if (!AreaWhite.ContainsKey(thisline[1]))
                    //{
                    //    continue;
                    //}
                    //if (!AreaWhite.ContainsKey(thisline[2]))
                    //{
                    //    continue;
                    //}
                    if (!thisline[0].Contains("/"))
                    {
                        continue;
                    }
                    IPNetwork thisNetwork = IPNetwork.Parse(thisline[0]);
                    Routes.Add(thisNetwork);
                }
                zipEntry = DatabaseStream.GetNextEntry();
            }


            String       APNICURLAddress   = "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest";
            Stream       APNICDatabaseFile = client.OpenRead(APNICURLAddress);
            MemoryStream APNICFileStream   = new MemoryStream();

            APNICDatabaseFile.CopyTo(APNICFileStream);
            var APNICData = System.Text.Encoding.UTF8.GetString(APNICFileStream.ToArray()).Split('\n');

            foreach (var line in APNICData)
            {
                if (line == "")
                {
                    continue;
                }
                if (!line.Contains("|"))
                {
                    continue;
                }
                if (line.Contains("asn"))
                {
                    continue;
                }
                var thisline = line.Split('|');
                if (thisline[1].Contains("CN"))
                {
                    continue;
                }
                if (thisline[2].Contains("v4"))
                {
                    IPNetwork thisNetwork = IPNetwork.Parse(thisline[3] + "/" + (32 - Math.Round(Math.Log(2, System.Convert.ToDouble(thisline[4])))).ToString());
                    Routes.Add(thisNetwork);
                }
                else
                {
                    IPNetwork thisNetwork = IPNetwork.Parse(thisline[3] + "/" + thisline[4]);
                    Routes.Add(thisNetwork);
                }
            }

            IPNetwork[] Source         = Routes.ToArray();
            IPNetwork[] SuperNetResult = IPNetwork.Supernet(Source);
            while (true)
            {
                Source         = SuperNetResult;
                SuperNetResult = IPNetwork.Supernet(Source);
                if (Source.Length == SuperNetResult.Length)
                {
                    if (Enumerable.SequenceEqual(Source, SuperNetResult))
                    {
                        break;
                    }
                }
            }
            foreach (var net in SuperNetResult)
            {
                Console.WriteLine(net.ToString());
            }
            Console.WriteLine(Routes.Count);
            Console.WriteLine(SuperNetResult.Length);
            Console.WriteLine();
        }