• 端口扫描初版


    代码如下:

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



  • 相关阅读:
    oracle11g windows客户端安装
    leetcode做题总结
    后端工程师都应该知道的最佳实践
    阿里云ubuntu下nginx部署上线后报错问题
    漫谈逆向工程
    RPC远程过程调用(Remote Procedure Call)
    stub和proxy使用初衷
    vue如何绑定多个class style
    简单又好看的背景 , 代码实现方格背景
    使用C#.NET调用ICU进行编码检测和编码转换
  • 原文地址:https://www.cnblogs.com/beyond1983/p/3067120.html
Copyright © 2020-2023  润新知