程序的代码以及说明如下:
‘本程序在VB6.0+Windows2000下测试通过! Dim tmpstr As String Dim NOW_OUT As Integer ''总共出去连接的有几个Winsock Private Sub Command1_Click() ListPorts.AddItem txtADDPORT.Text End Sub
Private Sub Command2_Click() On Error Resume Next If Command2.Caption = "监听" Then For i = 0 To ListPorts.ListCount - 1 Load Winsock1(i + 1) ''加载监听端口的winsock1数组控件 Winsock1(i + 1).LocalPort = ListPorts.List(i) ''设定端口 Winsock1(i + 1).Listen ''监听 Next i Command2.Caption = "停止" Else For i = 1 To Winsock1.Count - 1 Unload Winsock1(i) Next i For i2 = 1 To Winsock2.Count - 1 Unload Winsock2(i2) Next i2 Command2.Caption = "监听" End If End Sub
Private Sub Command3_Click() Unload Me End Sub
Private Sub Form_Load() txtLOG.Text = "日志:" & vbCrLf NOW_OUT = 1 End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long) Load Winsock2(NOW_OUT) ''加载建立连接的Winsock2数组控件 Winsock2(NOW_OUT).Accept requestID ''建立连接 Winsock1(Index).Close Winsock1(Index).Listen ''Winsock1继续监听 NOW_OUT = NOW_OUT + 1 ''连接的控件累加 myAddLog "来自" & Winsock1(Index).RemoteHostIP & "连接到本地端口:" & Winsock1(Index).LocalPort ''显示捕获的连接 End Sub
Private Sub Winsock2_DataArrival(Index As Integer, ByVal bytesTotal As Long) Winsock2(Index).GetData tmpstr ''通过Getdata捕获数据 myAddLog "来自" & Winsock2(Index).RemoteHostIP & "的数据:" & tmpstr ''显示捕获的数据 End Sub
Sub myAddLog(tmptext As String) ''加入日志 tmptext = tmptext & vbCrLf txtLOG.SelStart = Len(txtLOG.Text) txtLOG.SelText = tmptext End Sub
总结:其实一些程序的原理很简单,我们只要善于实践,并且能够把所学的只是运用到你的程序中去,我想编出类似的程序是很简单的。如木马、端口扫描器、客户端工具、CGI漏洞扫描器……。VB的Winsock控件虽说不是底层操作。及不上C语言的Socket编程的功能。但实现普通的操作还是很有用的。简单的例子就是Cookie欺骗,用VB来编写这种程序十分简单。
|