C#版的端口扫描器(PortScanner)

本文首发于 无垠论坛
作者:嘻哈呵嘿

上网很久的朋友一定对端口扫描器不会陌生吧。XScanner,SuperScanner大家一定都使用过。
今天我们就用最新的.Net技术来制作一个自己的端口扫描器,无垠出品!

今天主要使用到的是System.Net和System.Threading名称空间.

  1 None.gif
  2 None.gif using  System;
  3 None.gif using  System.Collections.Generic;
  4 None.gif using  System.Text;
  5 None.gif
  6 None.gif using  System.Net;
  7 None.gif using  System.Net.Sockets;
  8 None.gif
  9 None.gif using  System.Threading;
 10 None.gif
 11 None.gif namespace  PortScanner
 12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 13InBlock.gif    class Program
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        //已扫描端口数目
 16InBlock.gif        internal static int scannedCount = 0;
 17InBlock.gif        //正在运行的线程数目
 18InBlock.gif        internal static int runningThreadCount = 0;
 19InBlock.gif        //打开的端口数目
 20InBlock.gif        internal static List<int> openedPorts = new List<int>();
 21InBlock.gif        //起始扫描端口
 22InBlock.gif        static int startPort = 1;
 23InBlock.gif        //结束端口号
 24InBlock.gif        static int endPort = 500;
 25InBlock.gif        //最大工作线程数
 26InBlock.gif        static int maxThread = 100;
 27InBlock.gif        static void Main(string[] args)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29InBlock.gif            //接收传入参数一作为要扫描的主机
 30InBlock.gif            string host = args[0];
 31InBlock.gif            //接收传入参数二作为端口扫描范围,如1-4000
 32InBlock.gif            string portRange = args[1];
 33InBlock.gif            startPort = int.Parse(portRange.Split('-')[0].Trim());
 34InBlock.gif            endPort = int.Parse(portRange.Split('-')[1].Trim());
 35InBlock.gif
 36InBlock.gif            for (int port = startPort; port < endPort; port++)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                //创建扫描类
 39InBlock.gif                Scanner scanner = new Scanner(host, port);
 40InBlock.gif                Thread thread = new Thread(new ThreadStart(scanner.Scan));
 41InBlock.gif                thread.Name = port.ToString();
 42InBlock.gif                thread.IsBackground = true;
 43InBlock.gif                //启动扫描线程
 44InBlock.gif                thread.Start();
 45InBlock.gif
 46InBlock.gif                runningThreadCount++;
 47InBlock.gif
 48InBlock.gif                Thread.Sleep(10);
 49InBlock.gif                //循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
 50InBlock.gif                while (runningThreadCount >= maxThread) ;
 51ExpandedSubBlockEnd.gif            }

 52InBlock.gif
 53InBlock.gif            //空循环,直到所有端口扫描完毕
 54InBlock.gif            while (scannedCount + 1 < (endPort - startPort)) ;
 55InBlock.gif
 56InBlock.gif            Console.WriteLine();
 57InBlock.gif            Console.WriteLine();
 58InBlock.gif            //输出结果
 59InBlock.gif            Console.WriteLine("Scan for host: {0} has been completed , \n total {1} ports scanned, \nopened ports :{2}",
 60InBlock.gif                host, (endPort - startPort), openedPorts.Count);
 61InBlock.gif
 62InBlock.gif            foreach (int port in openedPorts)
 63InBlock.gif                Console.WriteLine("\tPort: {0} is open", port.ToString().PadLeft(6));
 64ExpandedSubBlockEnd.gif        }

 65ExpandedSubBlockEnd.gif    }

 66InBlock.gif
 67InBlock.gif    //扫描类
 68InBlock.gif    class Scanner
 69ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 70InBlock.gif        string m_host;
 71InBlock.gif        int m_port;
 72InBlock.gif
 73InBlock.gif        public Scanner(string host, int port)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            m_host = host; m_port = port;
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78InBlock.gif        public void Scan()
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            //我们直接使用比较高级的TcpClient类
 81InBlock.gif            TcpClient tc = new TcpClient();
 82InBlock.gif            //设置超时时间
 83InBlock.gif            tc.SendTimeout = tc.ReceiveTimeout = 2000;
 84InBlock.gif            try
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 86InBlock.gif                //Console.Write("Checking port: {0}", m_port);
 87InBlock.gif                //尝试连接
 88InBlock.gif                tc.Connect(m_host, m_port);
 89InBlock.gif                if (tc.Connected)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 91InBlock.gif                    //如果连接上,证明此商品为开放状态
 92InBlock.gif                    Console.WriteLine("Port {0} is Open", m_port.ToString().PadRight(6));
 93InBlock.gif                    Program.openedPorts.Add(m_port);
 94ExpandedSubBlockEnd.gif                }

 95ExpandedSubBlockEnd.gif            }

 96InBlock.gif            catch (System.Net.Sockets.SocketException e)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 98InBlock.gif                //容错处理
 99InBlock.gif                Console.WriteLine("Port {0} is closed", m_port.ToString().PadRight(6));
100InBlock.gif                //Console.WriteLine(e.Message);
101ExpandedSubBlockEnd.gif            }

102InBlock.gif            finally
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                tc.Close();
105InBlock.gif                tc = null;
106InBlock.gif                Program.scannedCount++;
107InBlock.gif                Program.runningThreadCount--;
108InBlock.gif
109InBlock.gif                //Console.WriteLine(Program.scannedCount);
110InBlock.gif
111ExpandedSubBlockEnd.gif            }

112ExpandedSubBlockEnd.gif        }

113ExpandedSubBlockEnd.gif    }

114ExpandedBlockEnd.gif}

115 None.gif
116 None.gif
117 None.gif

好了,代码很简单吧!只能扫描TCP端口哦。

有问题来论坛,大家一起交流!记住我们的网址: 无垠IT教育网_bbs.5inet.net
posted on 2006-09-24 22:11 嘻哈呵嘿 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/skyover/archive/2006/09/24/513563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值