很久以前写的,于是就修改了一下发上来。本来想改成SYN半开式扫描器的,后来想起XP SP2不支持原始套接字,也就将就改成活跃式的扫描器了。如果有对SYN感兴趣的朋友,可以用Winpcap改成SYN的。

  

    其实主要的代码就两块:

 

 

代码
1 Private JustScanPing As Boolean = True
2
3 Private ClientTimeOut As Integer = 1
4 Private NumThread As Integer = 0
5
6 Private NowScanState As ScanState = ScanState.Stoped
7
8 Private ActiveThreadNum As Integer = 0
9
10 Enum ScanState As Integer
11
12 Scanning
13 Stopping
14 Stoped
15
16 End Enum
17
18  Private Sub MainScanSub() '主控扫描线程,为扫描线程分配IP和端口
19  
20 Dim EndIPIndex As Integer = 0
21 Dim EndIPConnt As Integer = IPList.Items.Count - 1
22
23 Dim StartIPString() As String = IPList.Items(EndIPIndex).Text.Split(".")
24 Dim StartIP() As Byte = {CByte(StartIPString(0)), CByte(StartIPString(1)), CByte(StartIPString(2)), CByte(StartIPString(3))}
25
26
27 Dim EndIPString() As String = IPList.Items(EndIPIndex).SubItems(1).Text.Split(".")
28 Dim EndIP() As Byte = {CByte(EndIPString(0)), CByte(EndIPString(1)), CByte(EndIPString(2)), CByte(EndIPString(3))}
29
30 Dim Ports(PortList.Items.Count - 1) As Integer
31
32 For p As Integer = 0 To PortList.Items.Count - 1
33
34 Ports(p) = CInt(PortList.Items(p).Text)
35
36 Next
37
38
39 Dim Port_UPLimit As Integer = Ports.Length - 1
40
41
42 '////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43  
44 txtScanSituation.Text = "开始扫描......"
45
46 Do
47
48 If (NowScanState = ScanState.Scanning) Then
49
50 Dim i As Integer = -1
51
52 Do Until (i = Port_UPLimit)
53
54 If ActiveThreadNum < NumThread Then
55
56 i += 1
57
58 Dim Addr As New IPandPort
59 Addr.ScanIP = StartIP
60 Addr.ScanPort = Ports(i)
61
62 Dim DoThread As New Thread(AddressOf DoScanThread)
63 DoThread.IsBackground = True
64 DoThread.Start(Addr)
65
66 Addr = Nothing
67
68 txtThreadNum.Text = ActiveThreadNum
69 Thread.Sleep(20)
70
71 End If
72
73 Loop
74
75
76 If IsIPequal(StartIP, EndIP) Then
77
78 If EndIPIndex < EndIPConnt Then
79
80 EndIPIndex += 1
81
82 EndIPString = IPList.Items(EndIPIndex).SubItems(1).Text.Split(".")
83 EndIP = New Byte() {CByte(EndIPString(0)), CByte(EndIPString(1)), CByte(EndIPString(2)), CByte(EndIPString(3))}
84
85 StartIPString = IPList.Items(EndIPIndex).Text.Split(".")
86 StartIP = New Byte() {CByte(StartIPString(0)), CByte(StartIPString(1)), CByte(StartIPString(2)), CByte(StartIPString(3))}
87
88 Continue Do
89
90 Else
91
92 Exit Do
93
94 End If
95
96 End If
97
98
99 If StartIP(3) < 255 Then
100 StartIP(3) += 1
101 ElseIf StartIP(2) < 255 Then
102 StartIP(3) = 0
103 StartIP(2) += 1
104 ElseIf StartIP(1) < 255 Then
105 StartIP(3) = 0
106 StartIP(2) = 0
107 StartIP(1) += 1
108 ElseIf StartIP(0) < 255 Then
109 StartIP(3) = 0
110 StartIP(2) = 0
111 StartIP(1) = 0
112 StartIP(0) += 1
113 End If
114
115 Else
116
117 Exit Do
118
119 End If
120
121 Loop
122
123 txtScanSituation.Text = "已停止扫描,等待线程返回......"
124
125 Do Until ActiveThreadNum = 0
126
127 txtThreadNum.Text = ActiveThreadNum
128 Thread.Sleep(100)
129
130 Loop
131
132 StopedSet()
133
134 End Sub
135
136 Private Sub DoScanThread(ByVal Addr As IPandPort) '执行实际扫描的线程,每个线程扫描一个端口
137
138 Interlocked.Increment(ActiveThreadNum)
139
140 Dim portstring As String = CStr(Addr.ScanPort)
141 Dim ipstring As String = ""
142 For i As Integer = 0 To 3
143 ipstring = ipstring & CStr(Addr.ScanIP(i)) & "."
144 Next
145 ThreadMessageList.Items.Insert(0, ipstring & " " & portstring)
146
147 Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
148 sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, ClientTimeOut)
149
150 Dim IPPoint As New IPEndPoint(New IPAddress(Addr.ScanIP), Addr.ScanPort)
151
152 Try
153
154 sock.Connect(IPPoint)
155 ResultList.Items.Add(ipstring).SubItems.Add(portstring)
156 sock.Shutdown(SocketShutdown.Both)
157
158 Catch ex As Exception
159
160 Finally
161
162 sock.Close()
163
164 End Try
165
166 Interlocked.Decrement(ActiveThreadNum)
167
168 End Sub
169
170

 

  改下来后,虽然是活跃式的,但速度比我想象的好,虽然没有C/C++写的快(毕竟.net不擅长这方面),但我感觉比网上其它一些人的好。过去我用过一个别人写的多线程扫描器,还是控制台形式的,但我扫描一个IP的13个端口,居然用了三十多秒!!而我的这个只用一秒多点(不算等待超时的)。

posted on 2010-10-20 14:22  AniX  阅读(1827)  评论(0编辑  收藏  举报