Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.

Join the forum, it's quick and easy

Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.
Evo. G Tech Team Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

【c#】【分享】c#扫描器

Go down

【c#】【分享】c#扫描器 Empty 【c#】【分享】c#扫描器

Post by cyjian December 13th 2014, 09:54

XScannerSuperScanner,小榕的流光。。。大家都听过并且玩过吧?在这里我发一个实现这种端口扫描器的demoC#编写。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
     
  7. namespace PortScanner
  8. {
  9. class Program
  10. {
  11.  //已扫描端口数目
  12.  internal static int scannedCount = 0;
  13.  //正在运行的线程数目
  14.  internal static int runningThreadCount = 0;
  15.  //打开的端口数目
  16.  internal static List openedPorts = new List();
  17.  //起始扫描端口
  18.  static int startPort = 1;
  19.  //结束端口号
  20.  static int endPort = 500;
  21.  //最大工作线程数
  22.  static int maxThread = 100;
  23.  static void Main(string[] args)
  24.  {
  25.   //接收传入参数一作为要扫描的主机
  26.   string host = args[0];
  27.   //接收传入参数二作为端口扫描范围,如1-4000
  28.   string portRange = args[1];
  29.   startPort = int.Parse(portRange.Split('-')[0].Trim());
  30.   endPort = int.Parse(portRange.Split('-')[1].Trim());
     
  31.   for (int port = startPort; port < endPort; port++)
  32.   {
  33.    //创建扫描类
  34.    Scanner scanner = new Scanner(host, port);
  35.    Thread thread = new Thread(new ThreadStart(scanner.Scan));
  36.    thread.Name = port.ToString();
  37.    thread.IsBackground = true;
  38.    //启动扫描线程
  39.    thread.Start();
     
  40.    runningThreadCount++;
     
  41.    Thread.Sleep(10);
  42.    //循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
  43.    while (runningThreadCount >= maxThread) ;
  44.   }
     
  45.   //空循环,直到所有端口扫描完毕
  46.   while (scannedCount + 1 < (endPort - startPort)) ;
     
  47.    Console.WriteLine();
  48.    Console.WriteLine();
  49.    //输出结果
  50.    Console.WriteLine("Scan for host: {0} has been completed , \n total {1} ports scanned, \nopened ports :{2}",
  51. host, (endPort - startPort), openedPorts.Count);
     
  52.   foreach (int port in openedPorts)
  53.    Console.WriteLine("\tPort: {0} is open", port.ToString().PadLeft(6));
  54.  }
  55. }
     
  56. //扫描类
  57. class Scanner
  58. {
  59.  string m_host;
  60.  int m_port;
     
  61.  public Scanner(string host, int port)
  62.  {
  63.   m_host = host; m_port = port;
  64.  }
     
  65.  public void Scan()
  66.  {
  67.   //我们直接使用比较高级的TcpClient
  68.   TcpClient tc = new TcpClient();
  69.   //设置超时时间
  70.   tc.SendTimeout = tc.ReceiveTimeout = 2000;
  71.   try
  72.   {
  73.    //Console.Write("Checking port: {0}", m_port);
  74.    //尝试连接
  75.    tc.Connect(m_host, m_port);
  76.    if (tc.Connected)
  77.    {
  78.     //如果连接上,证明此商品为开放状态
  79.     Console.WriteLine("Port {0} is Open", m_port.ToString().PadRight(6));
  80.     Program.openedPorts.Add(m_port);
  81.    }
  82.   }
  83.   catch (System.Net.Sockets.SocketException e)
  84.   {
  85.    //容错处理
  86.    Console.WriteLine("Port {0} is closed", m_port.ToString().PadRight(6));
  87.     [You must be registered and logged in to see this link.]
  88.    }
  89.    finally
  90.    {
  91.     tc.Close();
  92.     tc = null;
  93.     Program.scannedCount++;
  94.     Program.runningThreadCount--;
     
  95.     [You must be registered and logged in to see this link.]
      
  96.    }
  97.   }
  98. }
  99. }

cyjian
Spammer
Spammer

Posts : 211
Points : 72995
Reputation : 0
Join date : 2014-06-18

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum