• 端口扫描初版


    代码如下:

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



  • 相关阅读:
    二进制,八进制,十六进制,十进制之间的换算
    14简化路径(71)
    13字符串解码(394)
    12 反转每对括号间的子串(1190)
    11 使括号有效的最少添加(921)
    10 K 个一组翻转链表(25)
    9 从链表中删去总和值为零的连续节点(1171)
    8 链表中的下一个更大节点(1019)
    7两两交换链表中的节点(24)
    6 奇偶链表(
  • 原文地址:https://www.cnblogs.com/beyond1983/p/3067120.html
Copyright © 2020-2023  润新知