C#TCPClient IP HTTP Banner Scanner(尝试使用线程获取相同标头的更多ips,但速度很慢

时间:2013-11-16 22:19:48

标签: c# multithreading tcpclient

所以我开发了一个不错的脚本来做我想做的事情,但它很慢而且很无聊。

有人可以告诉我,我怎样才能加快速度,或者地狱,甚至为我调整/调整它?

  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);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

数百个并行运行的线程非常多。但您还应该更改app.config文件中的maxconnection参数:

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

默认为2,这可能会导致只有两个线程能够创建连接的瓶颈。 Microsoft建议:

  

将maxconnection参数的值设置为12 * N(其中N是您拥有的CPU数量)。