示例#1
0
        public void TestPortScanCidrThreaded()
        {
            List <string> hosts = new List <string> {
                "127.0.0.1", "8.8.8.8/24"
            };
            List <int> ports = new List <int> {
                80, 443, 445
            };

            SharpSploitResultList <Network.PortScanResult> results1 = Network.PortScan(hosts, ports, true, 8000, 120);
            SharpSploitResultList <Network.PortScanResult> results2 = Network.PortScan(hosts, ports, true, 10000, 1);

            Assert.IsNotNull(results1);
            Assert.IsNotNull(results2);
            Assert.AreEqual(results1.Count, results2.Count);
            Assert.AreEqual(results1.Where(R => R.IsOpen).Count(), results2.Where(R => R.IsOpen).Count());
            Assert.AreEqual(String.Join(",", results1.Select(R => R.ComputerName).OrderBy(C => C).ToArray()), String.Join(",", results2.Select(R => R.ComputerName).OrderBy(C => C).ToArray()));
            results1.AddRange(results2);
            foreach (Network.PortScanResult result in results1)
            {
                Assert.IsNotNull(result);
                Assert.AreNotEqual(result.ComputerName, "");
                Assert.IsInstanceOfType(result.ComputerName, typeof(string));
                Assert.IsInstanceOfType(result.IsOpen, typeof(bool));
            }
        }
示例#2
0
        public void TestPingList()
        {
            List <string> hosts = new List <string> {
                "127.0.0.1", "8.8.8.8", "1.1.1.1", "google.com", "192.168.200.1"
            };

            SharpSploitResultList <Network.PingResult> results = Network.Ping(hosts, 10000);

            Assert.IsNotNull(results);
            Assert.AreEqual(4, results.Count);
            Assert.AreEqual(4, results.Where(R => R.IsUp).ToList().Count);
            foreach (Network.PingResult result in results)
            {
                Assert.IsNotNull(result);
                Assert.AreNotEqual(result.ComputerName, "");
                Assert.IsInstanceOfType(result.ComputerName, typeof(string));
                Assert.IsInstanceOfType(result.IsUp, typeof(bool));
            }
        }
示例#3
0
        public void TestPortScanList()
        {
            List <string> hosts = new List <string> {
                "127.0.0.1", "8.8.8.8", "1.1.1.1", "google.com", "192.168.200.1"
            };
            List <int> ports = new List <int> {
                80, 443, 445
            };

            SharpSploitResultList <Network.PortScanResult> results = Network.PortScan(hosts, ports, true, 1000, 300);

            Assert.IsNotNull(results);
            Assert.AreEqual(4, results.Count);
            Assert.AreEqual(4, results.Where(R => R.IsOpen).Count());
            foreach (Network.PortScanResult result in results)
            {
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result.ComputerName, typeof(string));
                Assert.AreNotEqual("", result.ComputerName);
                Assert.IsTrue(ports.Contains(result.Port));
            }
        }
示例#4
0
        /// <summary>
        /// Conducts a port scan of specified ComputerNames on specified ports and reports open ports.
        /// </summary>
        /// <param name="ComputerNames">ComputerNames to port scan.</param>
        /// <param name="Ports">Ports to scan.</param>
        /// <param name="Ping">Optional switch. If true, pings the ComputerNames to ensure each is up before port scanning.</param>
        /// <param name="Timeout">Timeout (in milliseconds) before a port is considered down.</param>
        /// <param name="Threads">Number of threads with which to portscan simultaneously</param>
        /// <returns>List of PortScanResults</returns>
        public static SharpSploitResultList <PortScanResult> PortScan(IList <string> ComputerNames, IList <int> Ports, bool Ping = true, int Timeout = 250, int Threads = 100)
        {
            IList <string> scanAddresses = Utilities.ConvertCidrToIPs(ComputerNames).Distinct().ToList();
            IList <int>    scanPorts     = Ports.Where(P => P > 1 && P < 65536).Distinct().ToList();

            if (Ping)
            {
                SharpSploitResultList <PingResult> pingResults = Network.Ping(scanAddresses, Timeout, Threads);
                scanAddresses = pingResults.Where(PR => PR.IsUp).Select(PR => PR.ComputerName).ToList();
            }
            IList <PortScanResult> portScanResults = new List <PortScanResult>();

            using (CountdownEvent waiter = new CountdownEvent(scanAddresses.Count * Ports.Count))
            {
                object portScanResultsLock = new object();
                int    runningThreads      = 0;
                foreach (string ComputerName in scanAddresses)
                {
                    foreach (int Port in scanPorts)
                    {
                        TcpClient client = null;
                        if (!Utilities.IsIP(ComputerName))
                        {
                            client = new TcpClient();
                        }
                        else
                        {
                            IPAddress.TryParse(ComputerName, out IPAddress address);
                            client = new TcpClient(address.AddressFamily);
                        }
                        PortScanResult portScanResult = new PortScanResult(ComputerName, Port, true);
                        while (runningThreads >= Threads)
                        {
                            waiter.WaitOne(Timeout);
                            runningThreads--;
                        }
                        IAsyncResult asyncResult = client.BeginConnect(ComputerName, Port, new AsyncCallback((state) => {
                            try
                            {
                                client.EndConnect(state);
                                client.Close();
                            }
                            catch
                            {
                                portScanResult.IsOpen = false;
                            }
                            if (portScanResult.IsOpen)
                            {
                                lock (portScanResultsLock)
                                {
                                    portScanResults.Add(portScanResult);
                                }
                            }
                            ((CountdownEvent)state.AsyncState).Signal();
                        }), waiter);
                        runningThreads++;
                    }
                }
                waiter.Wait(Timeout * scanAddresses.Count * Ports.Count);
            }
            SharpSploitResultList <PortScanResult> results = new SharpSploitResultList <PortScanResult>();

            results.AddRange(portScanResults);

            return(results);
        }