Exemplo n.º 1
0
        private void scanBtn_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show(this, "Continuing will flood your subnet with ARP packets to look for any responses" +
                                               "\n\nThis wont do any harm, but might get you into trouble or at least look suspicious on networks YOU DO NOT OWN" +
                                               "\n\nAre you sure you want to do this?\n\n", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            if ("OK".Equals(res.ToString()))
            {
                scanBtn.Text = "Scanning...";

                string              ipWithSubnet = _netCardInfo.OurIpAddress.ToString() + "/24";
                IPNetwork           net          = IPNetwork.Parse(ipWithSubnet);
                IPNetworkCollection ips          = IPNetwork.Subnet(net, 32);
                _ipsInSubnet = ips.GetIpRange();

                outputBox.printIpRangeInfo(ips, ipWithSubnet);

                if (_netCardInfo != null)
                {
                    new Thread(() =>
                    {
                        ScanResult scanResult = PerformScan(_netCardInfo);

                        _onlineClients = scanResult.OnlineClients;

                        outputBox.PrintArpScanDetails(scanResult.Duration, _onlineClients, _netCardInfo.GatewayIpAddess, _netCardInfo.OurIpAddress);

                        scanBtn.Invoke((MethodInvoker) delegate { scanBtn.Text = "Scan"; });
                    }).Start();
                }
            }
        }
 public static void printIpRangeInfo(this RichTextBox richTxtBox, IPNetworkCollection ips, string ipWithSubnet)
 {
     richTxtBox.Text += "\nScan coming from " + ipWithSubnet + "\n";
     richTxtBox.Text += ips.Count - 2 + " IP addresses available\n";
     richTxtBox.Text += "First: " + ips[1].ToString().Substring(0, ips[1].ToString().IndexOf("/")) + "\n";
     richTxtBox.Text += "Last: " + ips[ips.Count - 2].ToString().Substring(0, ips[ips.Count - 2].ToString().IndexOf("/")) + "\n";
     richTxtBox.Text += "\nScanning all addresses available...\n";
 }
Exemplo n.º 3
0
        /// <summary>
        /// Parses a comma delimited list of IPv4 addresses in CIDR notation and stores them
        /// in the IpBlocks db table. Each IpBlock is then enumerated and all of the possible
        /// IP addresses in the range are stored in the IpAddresses db table.
        /// </summary>
        /// <param name="ipBlocks">A comma delimited list of IPv4 addresses in CIDR notation.</param>
        /// <param name="ipConfigId">The Id of the IpConfig that these IpBlocks belong to.</param>
        /// <returns></returns>
        private async Task ProcessIpBlocks(List <string> ipBlocks, long ipConfigId)
        {
            HashSet <string> blocks = new HashSet <string>();

            for (int i = 0; i < ipBlocks.Count; i++)
            {
                // Remove whitespace from string
                ipBlocks.ElementAt(i).Replace(ipBlocks.ElementAt(i), Regex.Replace(ipBlocks.ElementAt(i), @"\s+", ""));

                // Check for duplicate IpBlocks
                if (!blocks.Add(ipBlocks.ElementAt(i)))
                {
                    // TODO - One of the IpBlocks is a duplicate
                    throw new NotSupportedException();
                }
            }

            // For each Ipv4 address in CIDR notation, enumerate all the IPs in the range then add them to ipAddresses
            foreach (string cidr in ipBlocks)
            {
                IpBlock ipBlock = new IpBlock
                {
                    CIDR       = cidr,
                    IpConfigId = ipConfigId
                };

                // Store each CIDR IPv4 address in the IpBlocks db table
                _context.IpBlocks.Add(ipBlock);
                await _context._SaveChangesAsync();

                IPNetwork block = IPNetwork.Parse(cidr);

                // Adjust the prefix relative to the IPsPerSub
                byte prefix = (byte)(32 - Math.Log((await _context.IpConfigs.FindAsync(ipConfigId)).IPsPerSub, 2));

                // Break the IpBlock into smaller subnets in CIDR IPv4 notation
                IPNetworkCollection subnets = block.Subnet(prefix);

                // This list will contain all of the enumerated IP address entities for the ipBlock
                List <IpAddress> ipAddressEntities = new List <IpAddress>();

                foreach (IPNetwork network in subnets)
                {
                    IpAddress ip = new IpAddress
                    {
                        Value       = network.Value,
                        IsAvailable = true,
                        IpBlockId   = ipBlock.Id
                    };

                    ipAddressEntities.Add(ip);
                }

                // Store all of the enumerated IP address entities in the IpAddresses db table
                _context.IpAddresses.AddRange(ipAddressEntities);
                await _context._SaveChangesAsync();
            }
        }
        public void MoveNext1()
        {
            IPNetwork ipn = IPNetwork.Parse("192.168.1.0/30");

            using (IPNetworkCollection ipns = ipn.Subnet(32))
            {
                bool next = ipns.MoveNext();
                Assert.AreEqual(true, next, "next");
            }
        }
Exemplo n.º 5
0
        public static List <string> GetIpRange(this IPNetworkCollection ips)
        {
            List <string> cleanIps = new List <string>();

            for (int i = 1; i < ips.Count - 1; i++)
            {
                cleanIps.Add(ips[i].ToString().Substring(0, ips[i].ToString().IndexOf("/")));
            }

            return(cleanIps);
        }
        public void TestEnumerator()
        {
            IPNetwork ipn = IPNetwork.Parse("192.168.0.0/32");

            using (IPNetworkCollection ipns = ipn.Subnet(32))
            {
                var ipnse = (Collections.IEnumerable)ipns;
                var ee    = ipnse.GetEnumerator();
                ee.MoveNext();
                var ipn0 = ee.Current;
                Assert.AreEqual("192.168.0.0/32", ipn0.ToString(), "ipn0");
            }
        }
        public void TestReset1()
        {
            IPNetwork ipn = IPNetwork.Parse("192.168.1.0/29");

            using (IPNetworkCollection ipns = ipn.Subnet(32))
            {
                var ipn0 = ipns.Current;
                ipns.MoveNext();
                ipns.Reset();
                var ipn1 = ipns.Current;

                Assert.AreEqual(ipn0, ipn1, "reset");
            }
        }
Exemplo n.º 8
0
        private static void SubnetNetworks(ProgramContext ac)
        {
            BigInteger i = 0;

            foreach (IPNetwork ipnetwork in ac.Networks)
            {
                i++;
                int networkLength = ac.Networks.Length;
                IPNetworkCollection ipnetworks = null;
                if (!ipnetwork.TrySubnet(ac.SubnetCidr, out ipnetworks))
                {
                    Console.WriteLine("Unable to subnet ipnetwork {0} into cidr {1}", ipnetwork, ac.SubnetCidr);
                    Program.PrintSeparator(networkLength, i);
                    continue;
                }

                Program.PrintNetworks(ac, ipnetworks, ipnetworks.Count);
                Program.PrintSeparator(networkLength, i);
            }
        }
Exemplo n.º 9
0
        public Subnet()
        {
            string    rndIP     = rnd.Next(0, 256) + "." + rnd.Next(0, 256) + "." + rnd.Next(0, 256) + "." + rnd.Next(0, 256) + "/" + rnd.Next(4, 29);
            IPNetwork ipnetwork = IPNetwork.Parse(rndIP);

            IpAdress = rndIP.Substring(0, rndIP.IndexOf('/'));

            CidrOld = ipnetwork.Cidr;


            int limit = (CidrOld + 20 < 30) ? CidrOld + 20 : 30;

            CidrNew = rnd.Next(CidrOld + 1, limit);

            IPNetworkCollection subneted = ipnetwork.Subnet((byte)CidrNew);

            subnetCount = (int)subneted.Count;
            subnetArr   = new string[subnetCount];
            for (int i = 0; i < subnetCount; i++)
            {
                subnetArr[i] = Convert.ToString(subneted[i]);
            }
        }
 public void TestCtor2()
 {
     IPNetworkCollection ipn = new IPNetworkCollection(IPNetwork.IANA_ABLK_RESERVED1, 2);
 }
Exemplo n.º 11
0
        private void Send_pings(object data)
        {
            List <string> datapassed  = (List <string>)data;
            string        IP_label    = datapassed[0];
            string        subnet      = datapassed[1];
            bool          validsubnet = false;
            List <string> Addresses   = new List <string>();

            //Addresses.Add("aalsa007");
            try
            {
                IPNetwork           net = IPNetwork.Parse(subnet);
                IPNetworkCollection ips = IPNetwork.Subnet(net, 32);
                validsubnet = true;
                foreach (IPNetwork ipnetwork in ips)
                {
                    Addresses.Add(ipnetwork.ToString().Substring(0, ipnetwork.ToString().Length - 3));
                }
            }
            catch
            {
                MessageBox.Show("Invalid Subnet");
            }
            if (validsubnet)
            {
                Set_IP_of_label("/ " + Addresses.Count.ToString());
                List <Task <PingReply> > pingTasks = new List <Task <PingReply> >();
                for (int i = 0; i < Addresses.Count; i++)
                {
                    pingTasks.Add(PingAsync(Addresses[i]));
                }
                List <string> finishedIDs = new List <string>();
                List <string> toadd       = new List <string>();
                finishedIDs.Add("Not an ip");
                bool alldone = false;
                //while (finishedIDs.Count != Addresses.Count)
                while (!alldone)
                {
                    alldone = true;
                    foreach (var t in pingTasks.ToArray())
                    {
                        update_progressbar((float)finishedIDs.Count / (float)Addresses.Count);
                        if (t.IsCompleted)
                        {
                            if (t.Result != null)
                            {
                                bool isShown = false;
                                foreach (var ID in finishedIDs)
                                {
                                    if (t.Result.Address.ToString() == ID)
                                    {
                                        isShown = true;
                                    }
                                }
                                if (!isShown)
                                {
                                    if (t.Result.Address.ToString() != "0.0.0.0")
                                    {
                                        //Thread updateUEthread = new Thread(new ParameterizedThreadStart(add_IP));
                                        List <string> results = new List <string>();
                                        //add_IP(t.Result.RoundtripTime.ToString() + " ms", t.Result.Address.ToString(), GetHostName(t.Result.Address.ToString()), RDP);
                                        results.Add(t.Result.Address.ToString());
                                        results.Add("-");
                                        results.Add(t.Result.RoundtripTime.ToString() + " ms");
                                        results.Add("-");
                                        results.Add("-");
                                        results.Add("-");
                                        add_IP(results);
                                        toadd.Add(t.Result.Address.ToString());

                                        Thread hostname_thread = new Thread(new ParameterizedThreadStart(update_hostname));
                                        hostname_thread.Start(t.Result.Address.ToString());
                                        Thread RDP_thread = new Thread(new ParameterizedThreadStart(update_RDP));
                                        RDP_thread.Start(t.Result.Address.ToString());
                                        Thread SSH_thread = new Thread(new ParameterizedThreadStart(update_SSH));
                                        SSH_thread.Start(t.Result.Address.ToString());
                                        Thread HTTP_thread = new Thread(new ParameterizedThreadStart(update_HTTP));
                                        HTTP_thread.Start(t.Result.Address.ToString());
                                    }
                                }
                            }
                            foreach (var toaddID in toadd)
                            {
                                finishedIDs.Add(toaddID);
                            }
                            toadd.Clear();
                            //MessageBox.Show(t.Result.RoundtripTime.ToString() + t.Result.Address);
                        }
                        else
                        {
                            alldone = false;
                        }
                    }
                    if (alldone)
                    {
                        update_progressbar(1);
                    }
                    Thread.Sleep(10);
                }
            }
        }