简体   繁体   中英

C# TCPClient IP HTTP Banner Scanner (trying to get more ips of the same header, using threading, but pretty slow

So I've developed a decent script to do what I want, however it's slow and boring.

Could somebody please tell me how can I speed this up, or hell, even do it/tweak it for me?

  namespace scan
{
    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;

    internal class Program
    {
        private static long count = 0L;
        private static long banner = 0L;
        private static long valid = 0L;

        private static void KeepAlive()
        {
            while (true)
            {
                Console.Write("\rTotal IPs Scanned: {0}, Valid IPs: {2}, Total Banners Found: {1}...", count, banner, valid);
                Thread.Sleep(1);
            }
        }

        private static void Main(string[] args)
        {
            args = new string[] { "200" };
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: scan <threads (max 254)>");
            }
            else
            {
                int result = 0;
                if (!int.TryParse(args[0], out result))
                {
                    Console.WriteLine("Invalid Input!");
                }
                else if (result > 200)
                {
                    Console.WriteLine("Only 200 Threads Max are supported.");
                }
                else
                {
                    StartWThreads(result);
                    new Thread(new ThreadStart(Program.KeepAlive)) { IsBackground = true }.Start();
                    while (true)
                    {
                        if (Console.ReadLine() == "exit")
                        {
                            Environment.Exit(0);
                        }
                    }
                }
            }
        }

        private static void Scan(object args)
        {
            int[] numArray = (int[]) args;
            int num = numArray[1];
            int num2 = numArray[0];
            int num3 = num;
            int num4 = 0;
            int num5 = 0;
            int num6 = 0;
            while (true)
            {
                TcpClient client = new TcpClient {
                    SendTimeout = 2
                };
                byte[] buffer = new byte[0x3e8];
                try
                {
                    IPAddress address;
                    if (IPAddress.TryParse(string.Format("{0}.{1}.{2}.{3}", new object[] { num3, num4, num5, num6 }), out address))
                    {
                        client.Connect(address, 80);
                    }
                }
                catch (Exception exception)
                {
                    if (!exception.Message.Contains("The requested address is not valid in its context"))
                    {
                    }
                }
                if (client.Connected)
                {
                    try
                    {
                        client.Client.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\n\r\n"));
                        client.Client.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                        if (Encoding.ASCII.GetString(buffer).Trim(new char[1]).IndexOf("(Unix) DAV/2") != -1)
                        {
                            System.IO.File.AppendAllText("results.txt", string.Format("{0}.{1}.{2}.{3}\n", new object[] { num3, num4, num5, num6 }));
                            banner += 1L;
                        }
                        try
                        {
                            client.Close();
                        }
                        catch
                        {
                        }
                        valid += 1L;
                    }
                    catch
                    {
                    }
                }
                count += 1L;
                num6++;
                if (num6 == 0x100)
                {
                    num5++;
                    num6 = 0;
                }
                if (num5 == 0x100)
                {
                    num4++;
                    num5 = 0;
                }
                if (num4 == 0x100)
                {
                    num3++;
                    num4 = 0;
                }
                if (num3 == num2)
                {
                    return;
                }
            }
        }

        private static void StartWThreads(int Threads)
        {
            int num = 0xef / Threads;
            int num2 = 0xef % Threads;
            int num3 = 0xef;
            for (int i = 0; i < Threads; i++)
            {
                if (i == (Threads - 1))
                {
                    num3 -= num;
                    int[] parameter = new int[] { num3 + num, (num3 - num2) + 1 };
                    new Thread(new ParameterizedThreadStart(Program.Scan)) { IsBackground = true }.Start(parameter);
                    Console.WriteLine("Starting Last Thread {0}", i + 1);
                }
                else
                {
                    Console.WriteLine("Starting Thread {0}", i + 1);
                    num3 -= num;
                    int[] numArray2 = new int[] { num3 + num, num3 };
                    new Thread(new ParameterizedThreadStart(Program.Scan)) { IsBackground = true }.Start(numArray2);
                }
            }
        }
    }
}

Hundreds of threads running in parallel is to much. But you should also change maxconnection parameter in the app.config file:

<system.net>
    <connectionManagement>
        <add address = "*" maxconnection = "12" />
    </connectionManagement>
</system.net>

It defaults to 2, which can create a bottleneck to only two threads being able to create connection. Microsoft suggests:

Set the value of the maxconnection parameter to 12*N (where N is the number of CPUs that you have).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM