端口扫描初版

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace TcpScanner
{
    class Program
    {

        internal static int scannedCount = 0;//已扫描端口数目
        internal static string host;//主机地址
        internal static int runningThreadCount = 0;//运行的线程总数

        internal static List<int> openedPorts = new List<int>();//打开的端口号集合

        static int startPort = 1;//默认起始端口号
        static int endPort = 500;//默认结束端口号

        static int maxThread = 100;//最大线程数
        static void Main(string[] args)
        {

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Title = "Scanner 0.1";
           Console.WriteLine("//////////////////////////////////////////");
           Console.WriteLine("/////  Port Scanner  /////");
            Console.WriteLine("///  Writer By:Zeor!  ///");
            Console.WriteLine("/////////////////////////////////////////");
            Console.WriteLine("请输入要扫描的主机(如:192.168.1.1):");
            host = Console.ReadLine();
            if (string.IsNullOrEmpty(host))
            {
                host = Dns.GetHostName();//默认为本机IP
            }
            Console.WriteLine("主机地址:{0}",host);
            Console.WriteLine("请输入扫描的端口 例如:1-800");
            string portRange = Console.ReadLine();
            if (!string.IsNullOrEmpty(portRange))
            {
                startPort = int.Parse(portRange.Split('-')[0].Trim());
                endPort = int.Parse(portRange.Split('-')[1].Trim());
            }
            Console.WriteLine("扫描端口范围{0}-{1}",startPort,endPort);
            for (int port = startPort; port < endPort; port++)
            {
                Scanner scanner = new Scanner(host, port);
                Thread thread = new Thread(new ThreadStart(scanner.Scan));
                thread.Name = port.ToString();
                thread.IsBackground = true;
                thread.Start();

                runningThreadCount++;
                Thread.Sleep(10);

                //循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
                while (runningThreadCount >= maxThread) ;
            }
            //空循环,直到所有端口扫描完毕
            while (scannedCount + 1 < (endPort - startPort)) ;
            Console.WriteLine();
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Green;
            //输出结果
            Console.WriteLine("对{0} 的扫描已完成, \n 总共扫描了 {1} 个端口, \n 打开的端口数量:{2}", host, (endPort - startPort), openedPorts.Count);

            foreach (int port in openedPorts)
            {
                Console.WriteLine("\t端口号: {0} 打开", port.ToString().PadLeft(6));
            }

            Console.ReadLine();

           
            
            
        }


        class Scanner
        {
            string m_host;
            int m_port;

            public Scanner(string host, int port)
            {
                m_host = host;
                m_port = port;
            }
            public void Scan()
            {
                TcpClient tc = new TcpClient();
                tc.SendTimeout = tc.ReceiveTimeout = 2000;

                try
                {
                    tc.Connect(m_host, m_port);
                    if (tc.Connected)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("端口号: {0} 打开", m_port.ToString().PadRight(6));
                        Program.openedPorts.Add(m_port);
                    }
                }
                catch
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("端口号: {0}  关闭", m_port.ToString().PadRight(6));
                }
                finally
                {
                    tc.Close();
                    tc = null;
                    Program.scannedCount++;
                    Program.runningThreadCount--;
                }
            }
        }
    }
}

 



posted @ 2013-05-08 16:43  zero@  阅读(245)  评论(0编辑  收藏  举报