elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


+  Foro de elhacker.net
|-+  Seguridad Informática
| |-+  Análisis y Diseño de Malware (Moderador: fary)
| | |-+  [VBS] Mutex o Similar
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [VBS] Mutex o Similar  (Leído 2,932 veces)
avenkanet

Desconectado Desconectado

Mensajes: 2


Ver Perfil
[VBS] Mutex o Similar
« en: 23 Junio 2015, 18:39 pm »

Estaba buscando info para hacer algo similar a la API CreateMutex desde VBS. La idea es evitar que nuestro script este corriendo dos o mas veces al mismo tiempo, y encontré este código:

Código
  1. Function MutexOpt()
  2. Dim oProcesses
  3. Dim oProcess
  4. Dim iProcCount
  5. Dim bQuit
  6. Set oProcesses = GetObject("winmgmts:\\.\root\cimv2").ExecQuery( "Select * from Win32_Process where Name='cscript.exe' or Name='wscript.exe'",,48)
  7.  
  8. For Each oProcess in oProcesses
  9. If Instr(1, oProcess.CommandLine, WScript.ScriptName, 1) > 0 Then
  10. iProcCount = iProcCount + 1
  11. End If
  12. Next
  13.  
  14. MutexOpt = (iProcCount > 1)
  15.  
  16. End Sub
  17.  

Pero no creo que sea la mejor opción porque si corremos nuestro script dos o mas veces pero con diferentes nombres este función no serviría.

Alguien tiene alguna mejor idea ?

Gracias.
« Última modificación: 23 Junio 2015, 18:47 pm por avenkanet » En línea

XcryptOR

Desconectado Desconectado

Mensajes: 228



Ver Perfil
Re: [VBS] Mutex o Similar
« Respuesta #1 en: 25 Junio 2015, 21:10 pm »

encontré esto por ahí, no se si sea una mejor opción

Código
  1. ''Objects
  2. set shell = createObject("wscript.shell")
  3. Set objFSO = CreateObject("Scripting.FileSystemObject")
  4.  
  5.  
  6. ''Check the args passed into the script. Specifically looking for the last argument which is the script mutex
  7. Set args = Wscript.Arguments
  8. argsFound = false
  9. For Each arg In args
  10. argsFound = arg
  11. Next
  12.  
  13. ''If we didn't get any arguments re-run self with a random arg string, this will be the mutex
  14. if argsFound = false then
  15. runSelf(genRndStr(8))
  16. Else
  17.  
  18. ''The last argument is the mutex string
  19. mutex = argsFound
  20. end if
  21.  
  22. ''remove any other instances of this script
  23. killPastInstances(mutex)
  24.  
  25.  
  26.  
  27. ''This sub will kill all instnances of the currently running vbscript that are running under the same interpreter
  28. ''but it will not kill it's self
  29. ''note, this requires that this script has a uniquite mutex
  30. sub killPastInstances(mutex)
  31.  
  32. ''Get self name
  33. scriptName = WScript.ScriptFullName
  34.  
  35. Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
  36. Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process",,48)
  37. For Each objItem in colItems
  38.  
  39. if instr(objItem.CommandLine, scriptName) > 0 Then
  40.  ''If the instance of the script is NOT this instance
  41.  if not instr(objItem.CommandLine, mutex) > 0 then
  42.  
  43.   ''Kill it!
  44.   objItem.Terminate()
  45.  end if
  46. end if
  47. Next
  48. end sub
  49.  
  50.  
  51. ''generates a random string of length "count"
  52. Function genRndStr(Count)
  53.    Randomize
  54.    For i = 1 To Count
  55.        If (Int((1 - 0 + 1) * Rnd + 0)) Then
  56.            genRndStr = genRndStr & Chr(Int((90 - 65 + 1) * Rnd + 65))
  57.        Else
  58.            genRndStr = genRndStr & Chr(Int((57 - 48 + 1) * Rnd + 48))
  59.        End If
  60.    Next
  61. End Function
  62.  
  63.  
  64. ''re-runs the curernt script with args in cscript if it is running in wscript. current script exits
  65. sub forceCscript
  66.  If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
  67.  
  68. Set args = Wscript.Arguments
  69. argStr = ""
  70. For Each arg In args
  71. argStr = argStr & " " & arg
  72. Next
  73.  
  74.    Shell.Run "cscript """ & WScript.ScriptFullName & """" & argStr, 1, False
  75.    WScript.Quit
  76.  End If
  77. end sub
  78.  
  79.  
  80.  
  81. ''Runs a new instance of the current script with additional arguments. Current script exits
  82. sub runSelf(extraArgStr)
  83.  
  84. ''Are we runing in C or W script?
  85. interpreter = "wscript.exe"
  86. If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
  87. interpreter = "wscript.exe"
  88. else
  89. interpreter = "cscript.exe"
  90. end if
  91.  
  92. ''Get current args
  93. Set args = Wscript.Arguments
  94. argStr = ""
  95. For Each arg In args
  96. argStr = argStr & " " & arg
  97. Next
  98.  
  99. ''append new args if required
  100. if extraArgStr <> "" then argStr = argStr & " " & extraArgStr
  101.  
  102.    Shell.Run interpreter & " """ & WScript.ScriptFullName & """" & argStr, 1, False
  103.    WScript.Quit
  104.  
  105. end sub
« Última modificación: 25 Junio 2015, 21:13 pm por XcryptOR » En línea



Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Threads Mutex perl
Scripting
dxr 0 3,411 Último mensaje 19 Diciembre 2005, 12:04 pm
por dxr
BSOD con Kernel mutex
Programación C/C++
Distorsion 7 4,216 Último mensaje 25 Febrero 2011, 18:25 pm
por Eternal Idol
[Ayuda] Juego similar a Dofus y Similar a WoW
Juegos y Consolas
SyStemPRG 2 5,537 Último mensaje 13 Febrero 2012, 22:16 pm
por SyStemPRG
Mutex en win api
Programación C/C++
framled 4 2,383 Último mensaje 10 Diciembre 2012, 23:57 pm
por framled
Sincronizacion de hilos en C (MUTEX)
Programación C/C++
angelicavf 2 2,593 Último mensaje 22 Enero 2016, 23:51 pm
por sodark
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines