wakawaka

Monitor for system deletion events (VBS)

Nov 3rd, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Create some Global variables; Change these as you need
  2. Dim sIEPath : sIEPath = "C:\Program Files\Internet Explorer\iexplore.exe"
  3. Dim sIEProcessName : sIEProcessName = "iexplore.exe"
  4.  
  5.  
  6. 'Create the WMI object and connect to the localhost
  7. Dim oWMI : Set oWMI = GetObject("winmgmts://./root/cimv2")
  8.  
  9. 'Create a callback method name that the events will be raised at
  10. Dim myEventSink : Set myEventSink = WScript.CreateObject("WbemScripting.SWbemSink","ProcessDeletion_")
  11.  
  12. 'Start the async operation and send the event results to the "ProcessDeletion_OnObjectReady" event callback method
  13. oWMI.ExecNotificationQueryAsync myEventSink, "SELECT * FROM __InstanceDeletionEvent  WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
  14.  
  15.  
  16. 'Have to keep the script alive
  17. While(true)
  18.     wscript.sleep 1000
  19. WEnd
  20.  
  21.  
  22. '''<summary>A call back for events raised by the ExecNotificationQueryAsync method</summary>
  23. '''<param name="oObject">An SWbemObject object</param>
  24. '''<param name="oAsyncContext">An SWbemNamedValueSet object that is passed to the original asynchronous call</param>
  25. '''<remarks>This method needs to be 'Public' in order to be called from the ExecNotificationQueryAsync method</remarks>
  26. Public Sub ProcessDeletion_OnObjectReady(ByRef oObject, ByRef oAsyncContext)
  27.     Dim bResult
  28.    
  29.     bResult = CheckProcess(".", sIEProcessName)
  30.  
  31.     If Not(bResult) Then
  32.         Call CreateProcess(".", sIEPath)
  33.     End If
  34. End Sub
  35.  
  36.  
  37. '''<summary>Checks to see if a process exists</summary>
  38. '''<param name="sComputer">The local or remote endpoint to connect to</param>
  39. '''<param name="sParam">Name of process that you want to find</param>
  40. '''<returns>A boolean value if the processes exists</returns>
  41. Private Function CheckProcess(ByVal sComputer, ByVal sParam)
  42.     On Error Resume Next
  43.    
  44.     Dim oWMI : Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & sComputer & "/root/cimv2")
  45.     Dim sQuery : sQuery = "SELECT * FROM Win32_Process WHERE Name='" & sParam & "'"
  46.     Dim bResult : bResult = True
  47.     Dim oProcessCollection
  48.    
  49.     Set oProcessCollection = oWMI.ExecQuery(sQuery)
  50.  
  51.     If oProcessCollection.Count = 0 Then
  52.         bResult = False
  53.     End If
  54.    
  55.     CheckProcess = bResult
  56. End Function
  57.  
  58.  
  59. '''<summary>Starts a process</summary>
  60. '''<param name="sComputer">The local or remote endpoint to connect to</param>
  61. '''<param name="sParam">Path to the process you want to start</param>
  62. '''<returns>Process ID of the process created</returns>
  63. Private Function CreateProcess(ByVal sComputer, ByVal sParam)
  64.     Dim oWMI : Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & sComputer & "/root/cimv2")
  65.     Dim oProcess : Set oProcess = oWMI.Get("Win32_Process")
  66.     Dim iProcessID
  67.    
  68.     oProcess.Create sParam, Null, , iProcessID
  69.  
  70.     CreateProcess = iProcessID
  71. End Function
Advertisement
Add Comment
Please, Sign In to add comment