Hack 73. Run Commands Remotely Using Chat
Have you ever wanted to run a shell command on a remote machine? This hack shows a simple way you can do this using Skype chat as the communications mechanism. Works with: Windows version of Skype. Have you ever wanted to run a command on a remote machine? If so, this hack is for you. By leaving Skype and this script, run_command.vbs, running on a remote machine, you can use Skype chat to send commandsin fact, any command you could run from the command line were you sitting at that computerto be executed on the remote machine. Moreover, as commands will be executed only for a named Skype user, and because Skype chat is encrypted end to end, you can run the commands in a relatively secure fashion. Figure 8-3. Record and play back your voicemail greeting' File: run_command.vbs ' To use this script yourself, you will need to change: ' 1) The name of the Skype user, held in strSkypeHostName, ' on the host machine that will run commands. ' 2) The name of the remote Skype user, held in ' strSkypeRemoteName, who is allowed to run commands ' on the host machine. Dim objShell, objSkype, objExec, strChatID Dim strSkypeHostName, strSkypeRemoteName Set objShell = WScript.CreateObject ("WSCript.shell") Set objSkype = WScript.CreateObject("SkypeAPI.Access", _ "SkypeAPI_") ' Only process commands on this host for this remote user strSkypeHostName = "ajtsheppard" strSkypeRemoteName = "skypehackswindows" ' Initialize strChatID = "" Set objExec = Nothing ' Skype event handler Sub SkypeAPI_ChatMessageReceived(ChatMessage) On Error Resume Next Dim objMsgFrom, objChat Set objChat = ChatMessage.Chat strChatID = objChat.Name Set objMsgFrom = ChatMessage.MessageFrom If objMsgFrom.Handle = strSkypeRemoteName Then Set objExec = objShell.Exec(ChatMessage.Body) If Err <> 0 Then objSkype.SendBlockingCommand("CHATMESSAGE " & _ strChatID & _ " Unable to run: " & _ ChatMessage.Body) strChatID = "" Set objExec = Nothing End If Else objSkype.SendBlockingCommand("CHATMESSAGE " & _ strChatID & _ " You are not authorized" & _ " to run: " & ChatMessage.Body) strChatID = "" Set objExec = Nothing End If End Sub ' Main script begins here objSkype.ConnectAndWait 15000 Do While True WScript.Sleep 500 ' Send a chat message back when the command has completed If Not (objExec Is Nothing) Then If objExec.status <> 0 Then objSkype.SendBlockingCommand("CHATMESSAGE " & _ strChatID & _ " StdOut: " & _ objExec.StdOut.ReadAll) objSkype.SendBlockingCommand("CHATMESSAGE " & _ strChatID & _ " StdErr: " & _ objExec.StdErr.ReadAll) strChatID = "" Set objExec = Nothing End If End If Loop The script run_command.vbs will continue running until it is either killed explicitly, you log off, or you shut down your machine. Running commands remotely has never been so easy. For example, by running Skype and run_command.vbs on the target (or host) machine with "ajtsheppard" logged onto Skype, "skypehackswindows" on a different machine can send a command as a chat message to "ajtsheppard"say, calc (which is what you would enter at the command line on the remote target machine to run Calculator, were you sitting there)and it will run and pop up "Calculator" on the remote target machine (see Figure 8-4). Running Calculator on a remote machine is just an example, because any command you could enter and run at the command line while sitting at the remote target machine, you can now run remotely using this hack. Figure 8-4. Running Calculator on a remote machine using Skype chatIt's also important to realize that while the target machine must be running Windows to take advantage of this hack, any other version of Skype running anywhere can be used to send commands to that target machine using chat. |