文章 | 入侵攻击 | 安全防御 | 操作系统 | 建站技术 | 脚本编程 | 路由交换 | 灾难恢复 | 新闻资讯 | 安全公告   
下载 | 漏洞扫描 | 加密破解 | 入侵攻击 | 后门木马 | 溢出程序 | 综合工具 | 安全防护 | 原创发布 | 动画教程 
论坛 | 配服务器 | 黑客情感 | 免费资源 | 爆笑贴图 | 灌水无罪 | 会员照片 | 在线服务 | 站长博客 | 网站首页 
 您现在的位置: 华夏黑客联盟 >> 攻防技术 >> 脚本编程 >> 文章正文  

黑客教程:利用C#设计制作端口扫描器

www.hxhack.com 阅读: 时间:2007-9-3 6:13:41 整理:华夏黑盟
------------------------------------------------------------------

 
黑客教程:利用C#设计制作端口扫描器
今天主要使用到的是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 openedPorts = new List();
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端口哦。

 

   -------------------------------------------------------------------------------------------
  • 上一篇文章:

  • 下一篇文章:
  •    -------------------------------------------------------------------------------------------
    用户名:
    Email:
    评论内容:
     
      精品推荐

     十招教你学会破解(学黑客
     C语言与C++有什么区别吗
     VB病毒编写——初学编程
     世界编程大赛第一名写的
     木马是如何编写的
     绝版破解软件教程就不信
     如何编写木马病毒
     隐藏cmd命令行运行
     学ASP只需一小时!
     如何提高自己的编程能力
     初学者天地--用C语言写的
     四个经典的vbs脚本整理
     Visual C++编程窃取QQ密
     怎样学好编程C语言
     跟我学做记事本
     编程语言初步知识(供菜
     一个程序员写的求爱程序
     阿拉QQ大盗盗号原理分析
     什么是vb,vb是什么意思
     成为编程高手的二十二条
     看黑客编写强力蓝屏炸弹
     C语言基础教程合集(整理
     学C++时要注意的
     C语言之精华总结
     [VB]猎取当前QQ聊天内容


    设为首页 | 软件发布 | 联系方式 | 友情链接 | 关于我们 | 本站声明 | 免责条款 | 网站留言
    Copyright © 2004-2007 Www.Hxhack.Com
    版本:华夏黑客联盟 Email:hxhack.com@163.com
    中国·广东 请使用IE6.0版本, 分辩率1024×768进行浏览
    版权所有 任意抄袭 注意完整
    粤ICP备06123842号