惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Security Latest
Security Latest
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
博客园 - 叶小钗
H
Help Net Security
大猫的无限游戏
大猫的无限游戏
U
Unit 42
G
Google Developers Blog
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
PCI Perspectives
PCI Perspectives
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
SecWiki News
SecWiki News
Google Online Security Blog
Google Online Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
The Hacker News
The Hacker News
月光博客
月光博客
T
Threatpost
B
Blog
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
L
LangChain Blog
Scott Helme
Scott Helme
Last Week in AI
Last Week in AI
C
Check Point Blog
P
Privacy International News Feed

博客园 - 嘻哈呵嘿

编程实现QQ表情文件CFC格式 使用反射为指定的文件类型创建关联 一个小小的实用控件。 在asp.net中使用异步同步rss 为FireFox的XMLDocument 增加 LoadXML,SelectNodes,SelectSingleNode方法。 - 嘻哈呵嘿 AJAX实现的购物车,使用Cookie保存。 - 嘻哈呵嘿 - 博客园 AJAX查询域名。:) - 嘻哈呵嘿 - 博客园 FreeTextBox的Toolbars? ZeroForums论坛正式开始测试运行 看看你的PageRank? 不知不觉在网上就拥有了两G的邮局..... 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#) 根据指定Value选定winForm中的ComboBox中的Item 影视频道完工中。 Windows 的 calc 的弱智bug~~~~ 原来老外也喜欢盗版和盗连。呵呵。 Flash的中文输入以有及乱码问题。(已解决) 使用自定义的WebControl来构建简单的WebForm 用xhtml和css构建快速的Web页。
C#版的端口扫描器(PortScanner)
嘻哈呵嘿 · 2006-09-24 · via 博客园 - 嘻哈呵嘿

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

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

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

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

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

 65    }

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

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

 95            }

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

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

112        }

113    }

114}

115
116
117

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

有问题来论坛,大家一起交流!记住我们的网址:

无垠IT教育网_bbs.5inet.net