Example #1
0
        /// <summary>
        /// Pings specified ComputerNames to identify live systems.
        /// </summary>
        /// <param name="ComputerNames">ComputerNames to ping.</param>
        /// <param name="Timeout">Timeout (in milliseconds) before a ComputerName is considered down.</param>
        /// <param name="Threads">Number of threads with which to ping simultaneously</param>
        /// <returns></returns>
        public static SharpSploitResultList <PingResult> Ping(IList <string> ComputerNames, int Timeout = 250, int Threads = 100)
        {
            IList <string> pingAddresses = Utilities.ConvertCidrToIPs(ComputerNames).Distinct().ToList();
            SharpSploitResultList <PingResult> pingResults = new SharpSploitResultList <PingResult>();

            using (CountdownEvent waiter = new CountdownEvent(pingAddresses.Count))
            {
                object pingResultsLock = new object();
                int    runningThreads  = 0;
                foreach (string ComputerName in pingAddresses)
                {
                    Ping       ping       = new Ping();
                    PingResult pingResult = new PingResult(ComputerName, true);
                    ping.PingCompleted += new PingCompletedEventHandler((sender, e) =>
                    {
                        if (e.Reply != null && e.Reply.Status == IPStatus.Success)
                        {
                            lock (pingResultsLock)
                            {
                                pingResults.Add(pingResult);
                            }
                        }
                        ((CountdownEvent)e.UserState).Signal();
                    });
                    while (runningThreads >= Threads)
                    {
                        waiter.WaitOne();
                        runningThreads--;
                    }
                    try
                    {
                        ping.SendAsync(ComputerName, Timeout, waiter);
                        runningThreads++;
                    }
                    catch { }
                }
                waiter.Wait(Timeout * pingAddresses.Count);
            }
            return(pingResults);
        }
Example #2
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);
        }