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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 云的恬淡,风的自由

现在我空仓!随时出击,不是现在,是在将来,要知什么时候,常来这里看看! 沪市,深市创近年来最大走跌幅 理解ASP.NET中的三层中的DAL、BLL和USL(转自51aspx) 隐藏的文件夹 基本应用程序的创建和部署 创建部署项目 汇编代码 照片尺寸 ASCII 基本字符对照表 一个函数控制多个按纽的两种方法 - 云的恬淡,风的自由 - 博客园 button的command和click事件的区别 c#编写QQ群发器 datagridview分页时直接到指定页 世界各地域名后缀对照表 建立隐藏帐号 端口服务对照表 GridView学习 csdn中的一些asp.net2.0好文章 Asp.net学习资源收藏
网络扫描器
云的恬淡,风的自由 · 2007-09-17 · via 博客园 - 云的恬淡,风的自由

网络扫描器

Posted on 2007-09-17 08:50  云的恬淡,风的自由  阅读(152)  评论()    收藏  举报

using 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            //接收传入参数二作为端口扫描范围,pmT|=-Zh6]Ym!08EO:lL育R8xl管F络软13ZFv*Y如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                //循环,]中g垠i'?d6x
t网教教+件业4L管X+
直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
 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                    //如果连接上,
I(RO专ALvTUy\kmt
的^8\0RrO==m~I理2+y证明此商品为开放状态
 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