This is the second part of the Remote Administration Tool series.
In the last part we’ve discussed about communicating between two systems on a network using winsock component. In this section we will be seeing about how to do some miscellaneous operations on the remote system.
This section deals a bit with Windows API functions and the same winsock component. This section is also intended for beginners and from the next part we will be dealing with opening FTP/TELNET ports on the remote system. Let’s now get back to the coding part.
Now for building a remote administration tool we need two programs, one server and other client. The server runs on the remote system on which we’ve to execute commands. The client runs on the system from which we’ve to execute commands.
First let’s look at the client code, Start Visual Basic 6 and open a new project.
To the form add four text boxes to the form and name them to txtip, txtmsg, txtping, text1.
Add six command boxes to the form and name them as cmdsend, command1, command2, command3, command4, command5.
Add a winsock control to the form(Click project menu -> Components and then check "Microsoft Winsock Control 6.0 (SP4)".
A control will now be displayed in the toolbox. Drop that on the form.).
Open the code window and copy the following code to that.
The code is commented wherever needed.
Option Explicit Private Sub Form_Load() ‘connects to port 3539 on remote system
Winsock1.RemotePort = 3539
End Sub
Private Sub txtip_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 13 Then ‘on pressing return(Enter) key
Winsock1.RemoteHost = txtip.Text Winsock1.Connect ‘Connect to the remote host
End If
End Sub
Private Sub cmdsend_Click() ‘Send the typed message to server
Winsock1.SendData ("msg " + txtmsg.Text) txtmsg.Text = ""
End Sub
Private Sub Command1_Click() ‘Send command to open the CD-DRIVE
Winsock1.SendData "cmd opencd"
End Sub
Private Sub Command2_Click() ‘Send command to close CD-DRIVE
Winsock1.SendData "cmd closecd"
End Sub
Private Sub Command3_Click() ‘Send command to shutdown the remote system
Winsock1.SendData "cmd shutdown"
End Sub
Private Sub Command4_Click() ‘Close the connection
Winsock1.Close
End Sub
Private Sub Command5_Click() ‘Disable double clicking on the remote system
Winsock1.SendData "cmd blockdbl"
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) ‘on pressing return(Enter) execute the path specified on the remote system
If KeyCode = 13 Then Winsock1.SendData "exe " & Text1.Text
End If
End Sub
Private Sub txtmsg_KeyDown(KeyCode As Integer, Shift As Integer) ‘On pressing return key send the message to server
If KeyCode = 13 Then Winsock1.SendData ("msg " + txtmsg.Text) txtmsg.Text = ""
End If
End Sub
Private Sub txtping_KeyDown(KeyCode As Integer, Shift As Integer) Dim b As String Dim a As Double If KeyCode = 13 Then ‘Ping the specified address
b = "ping " & txtping.Text a = Shell(b, vbNormalFocus)
End If
End Sub
Now let’s look at the server component. The server hides itself and receives messages from the client and then executes the specified command for that message. The code is pretty simple.
Open a new project and add a winsock component of the form and switch to code window. Copy the following code to the code window.
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) ‘If connection is requested by a remote system ‘Close the current connection and accept the new connection
If Winsock1.State <> sckClosed Then Winsock1.Close Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) ‘when data arrives from the remote system
Dim data As String Winsock1.GetData data ‘Get the arrived data to code variable ‘Get the first 3 characters of the data to cmd variable cmd = Mid(data, 1, 3) ‘Get all the data from the fifth character to cmdtxt variable cmdtxt = Mid(data, 5) On Error Resume Next If cmd = "cmd" Then ‘if the data arrived is a command
Select Case cmdtxt Case "opencd" Call mciExecute("Set CDaudio door open") ‘opens CD-DRIVE Case "closecd" Call mciExecute("Set CDaudio door closed") ‘close CD-DRIVE Case "shutdown" rVal = ExitWindowsEx(EWX_SHUTDOWN, 0&) ‘Shutdown Case "blockdbl" a = SetDoubleClickTime(50) ‘disable double click End Select End If If cmd = "exe" Then ‘To execute the path sent a = Shell(cmdtxt, vbNormalFocus) ‘Open the file specified End If If cmd = "msg" Then ‘If arrived is a message MsgBox cmdtxt, vbCritical, "Message" ‘Display that message in message box
End If
End Sub
Private Sub Form_Load() a = RegisterServiceProcess(GetCurrentProcessId, 1) Me.Visible = False ‘Hides the program from the notice of user
Winsock1.LocalPort = 3539 ‘sets local port to 3539 Winsock1.Listen ‘Listens at the port specified, 3539
End Sub
Now a module to the server program and copy the following lines of code to the module. All these are API functions which help us to interact with Input/Otput devices of the system, multimedia system etc…
Declare Function SetDoubleClickTime Lib "user32" (ByVal wCount As Long) As Long Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long Public Declare Function RegisterServiceProcess Lib "kernel32.dll" (ByVal dwProcessId As Long, ByVal dwType As Long) As Long Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long Now make Executable files of the server and client programs.
To test this, run server component and also run client component. Now connect the client to local IP i.e.,127.0.0.1 and test the operations. The project file can be downloaded from
http://www.geocities.com/neworder_0072002/HRVG2.zip
Sincerely yours,
assassin007
http://hrvg.tk
assassin_007@rediffmail.com