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

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  [APORTE] [VBS] Snippets para manipular reglas de bloqueo del firewall de Windows
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [APORTE] [VBS] Snippets para manipular reglas de bloqueo del firewall de Windows  (Leído 4,081 veces)
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.874



Ver Perfil
[APORTE] [VBS] Snippets para manipular reglas de bloqueo del firewall de Windows
« en: 3 Febrero 2014, 11:56 am »

Bueno, he codeado este mini Script para determinar si un proceso (existente) tiene las conexiones entrantes o salientes bloqueadas por el firewall de Windows.

Mi intención es usar este script desde unas opciones que crearé en el menú contextual de Windows para bloquear conexiones, desbloquear, y comprobar si un proceso ya está bloqueado.

EDITO: Mejorado.
Código
  1. ' *********************
  2. ' FirewallRuleCheck.vbs
  3. ' *********************
  4. ' By Elektro
  5.  
  6.  
  7. ' ------------
  8. ' Description:
  9. ' ------------
  10. '
  11. ' This script determines whether a program has the Inbound or Outbound connections blocked by the Windows Firewall rules.
  12. '
  13. ' NOTE: Possibly this script will not work under Windows XP where;
  14. '       the Netsh syntax is different and maybe the Firewall registry values could be diferent too, I've don't tested it.
  15. '       Tested on Windows 8.
  16.  
  17.  
  18. ' -------
  19. ' Syntax:
  20. ' -------
  21. '
  22. ' FirewallRuleCheck.vbs "[File]" "[ConnectionType]" "[ReturnResult]"
  23.  
  24.  
  25. ' -----------
  26. ' Parameters:
  27. ' -----------
  28. '
  29. ' [File]
  30. ' This parameter indicates the file to check their connection status.
  31. ' The value should be the relative or absolute filepath of an existing file.
  32. '
  33. ' [ConnectionType]
  34. ' This parameter indicates whether to check inbound or outbound connection status.
  35. ' The value should be "In" or "Out".
  36. '
  37. ' [ReturnResult]
  38. ' This parameter indicates whether the result should be returned without displaying any info;
  39. ' for example, when calling this script from other script to expect a Boolean result.
  40. ' The value is Optional, and should be "True" or "False". Default value is "False".
  41.  
  42.  
  43. ' ---------------
  44. ' Usage examples:
  45. ' ---------------
  46. '
  47. ' FirewallRuleCheck.vbs "C:\Program.exe"  IN
  48. ' FirewallRuleCheck.vbs "C:\Program.exe" OUT
  49. ' BooleanExitCode = FirewallRuleCheck.vbs "C:\Program.exe"  IN True
  50. ' BooleanExitCode = FirewallRuleCheck.vbs "C:\Program.exe" OUT True
  51.  
  52.  
  53. ' -----------
  54. ' Exit codes:
  55. ' -----------
  56. '
  57. ' When 'ReturnResult' parameter is set to 'False':
  58. '      0: Successful exit.
  59. '      1: Missing arguments or too many arguments.
  60. '      2: File not found.
  61. '      3: Wrong value specified for parameter '[ConnectionType]'
  62. '      4: Wrong value specified for parameter '[ReturnResult]'
  63. '      5: Specific Error.
  64. '
  65. ' When 'ReturnResult' parameter is set to 'True':
  66. '     -1: 'True'  (Rule is not added).
  67. '      0: 'False' (Rule is already added).
  68. '      (All the other ExitCodes: '1', '2', '3', '4' and '5' can happen in this mode, except '0')
  69.  
  70.  
  71. ' *************
  72. Option Explicit
  73.  
  74. Const MsgBoxSyntax   = "FirewallRuleCheck.vbs ""[File]"" ""[ConnectionType]"" ""[ReturnResult]"""
  75. Const MsgBoxCaption  = "Firewall Rule Check"
  76. Const MsgBoxErrorIco = 16
  77. Const MsgBoxInfoIco  = 64
  78. Const MsgBoxDebugIco = 48
  79.  
  80. Dim objFile        ' Indicates the file object.
  81. Dim objReg         ' Indicates the registry object.
  82. Dim Root           ' Indicates the root registry key.
  83. Dim Key            ' Indicates the registry key.
  84. Dim MatchData      ' Indicates the data to match.
  85. Dim Values         ' Indicates the registry value collection.
  86. Dim Value          ' Indicates the registry value.
  87. Dim Data           ' Indicates the registry data.
  88. Dim DataIsMatched  ' Indicates whether the data is matched.
  89. Dim ConnectionType ' Indicates whether to check inbound or outbound connection status.
  90. Dim ReturnResult   ' Indicates whether the result should be returned without displaying any info;
  91.                   ' for example, when calling this script from other script to expect a Boolean result.
  92. Dim DebugMode      ' Indicates whether the debug mode is activated.
  93.  
  94.  
  95. ' Set the debug mode to 'True' if need to test the values.
  96. DebugMode = False
  97.  
  98. ' Set the 'HKEY_LOCAL_MACHINE' as Root registry key.
  99. Root = &H80000002
  100.  
  101. ' Set the Firewall rules registry location as key.
  102. Key = "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules"
  103.  
  104. ' Sets the Registry object.
  105. Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  106.  
  107. ' Argument error handling.
  108. If Wscript.Arguments.Count = 0 Then
  109.  
  110. ' Notify the error to the user.
  111. MsgBox "Syntax:" & VBNewLine & _
  112.       MsgBoxSyntax          , _
  113.       MsgBoxErrorIco, MsgBoxCaption
  114.  
  115. ' Exit with reason: 'Missing arguments'.
  116. Wscript.Quit(1)
  117.  
  118. ElseIf Wscript.Arguments.Count < 2 Then
  119.  
  120. ' Notify the error to the user.
  121. MsgBox "Missing arguments."  & _
  122.       VBNewLine & VBNewLine & _
  123.       "Syntax:" & VBNewLine & _
  124.       MsgBoxSyntax          , _
  125.       MsgBoxErrorIco, MsgBoxCaption
  126.  
  127. ' Exit with reason: 'Missing arguments'.
  128. Wscript.Quit(1)
  129.  
  130. ElseIf Wscript.Arguments.Count = 3 Then
  131.  
  132. If LCase(Wscript.Arguments(2)) = LCase("True") Then
  133. ReturnResult = True
  134.  
  135. Elseif LCase(Wscript.Arguments(2)) = LCase("False") Then
  136. ReturnResult = False
  137.  
  138. Else
  139.  
  140. ' Notify the error to the user.
  141. MsgBox "Wrong value specified for parameter 'Return Result'", _
  142.       MsgBoxErrorIco, MsgBoxCaption
  143.  
  144. ' Exit with reason: 'Wrong value specified for parameter '[Return Result]''.
  145. Wscript.Quit(4)
  146.  
  147. End If
  148.  
  149. ElseIf Wscript.Arguments.Count > 3 Then
  150.  
  151. ' Notify the error to the user.
  152. MsgBox "Too many arguments." & _
  153.       VBNewLine & VBNewLine & _
  154.       "Syntax:" & VBNewLine & _
  155.       MsgBoxSyntax          , _
  156.       MsgBoxErrorIco, MsgBoxCaption
  157.  
  158. ' Exit with reason: 'Too many arguments'.
  159. Wscript.Quit(1)
  160.  
  161. End If
  162.  
  163. On Error Resume Next
  164.  
  165. ' Set the FileObject with the file passed through the first argument.
  166. Set objFile = Createobject("Scripting.FileSystemObject"). _
  167.              GetFile(Wscript.Arguments(0))
  168.  
  169. ' File-Error handling.
  170. If Err.Number = 53 Then
  171.  
  172. ' Notify the error to the user.
  173. MsgBox "File not found:"   & _
  174.       vbnewline           & _
  175.       Wscript.Arguments(0), _
  176.       MsgBoxErrorIco, MsgBoxCaption
  177.  
  178. ' Exit with reason: 'File not found'.
  179. Wscript.Quit(2)
  180.  
  181. End If
  182.  
  183. ' Set the partial data to match on each value-data.
  184. If LCase(Wscript.Arguments(1)) = LCase("IN") Then
  185.  
  186. ' Set the ConnectionType to 'Inbound'
  187. ConnectionType = "Inbound"
  188.  
  189. Elseif LCase(Wscript.Arguments(1)) = LCase("OUT") Then
  190.  
  191. ' Set the ConnectionType to 'Outbound'
  192. ConnectionType = "Outbound"
  193.  
  194. Else ' Wrong argument.
  195.  
  196. ' Notify the error to the user.
  197. MsgBox "Wrong value specified for parameter '[ConnectionType]'", _
  198.       MsgBoxErrorIco, MsgBoxCaption
  199.  
  200. ' Exit with reason: 'Wrong value specified for parameter '[ConnectionType]''.
  201. Wscript.Quit(3)
  202.  
  203. End If
  204.  
  205. ' Set the data to match (It's a portion of the firewall rule).
  206. MatchData = "Action=Block|Active=TRUE|Dir=" & Wscript.Arguments(1) & "|App=" & objFile.Path
  207.  
  208. ' Enumerate the registry values.
  209. objReg.EnumValues Root, Key, Values
  210.  
  211. If DebugMode Then
  212.  
  213. ' Notify the debug information.
  214. MsgBox "File: "            & objFile.Path   & vbnewline & vbnewline & _
  215.       "ConnectionType: "  & ConnectionType & vbnewline & vbnewline & _
  216.       "Key: "             & Key            & vbnewline & vbnewline & _
  217.       "Value count: "     & UBound(Values) & vbnewline & vbnewline & _
  218.       "MatchData: "       & MatchData      & vbnewline             , _
  219.       MsgBoxDebugIco,     "Debug Info | "  & MsgBoxCaption
  220.  
  221. End If
  222.  
  223. ' Loop through the enumerated registry values.
  224. For Each Value In Values
  225.  
  226. ' Get the registry data.
  227. objReg.GetStringValue Root, Key, Value, Data
  228.  
  229. ' If registry data is not empty then...
  230. If Not IsNull(Data) Then
  231. ' Match the partial data onto the registry data.
  232.  
  233. ' If partial data matched in into the data then...
  234. If InStr(1, Data, MatchData, 1) Then
  235.  
  236. ' Set the DataIsMatched flag to 'True'.
  237. DataIsMatched = True
  238.  
  239. ' ...and stop the iteration.
  240. Exit For
  241.  
  242. End If ' // InStr()
  243.  
  244. End If ' // IsNull()
  245.  
  246. Next ' // Value
  247.  
  248. ' Error handling.
  249. If Err.Number <> 0 Then
  250.  
  251. ' Notify the error to the user.
  252. MsgBox "Error Code: "   & Err.Number & vbnewline & _
  253.       "Error Source: " & Err.Source & vbnewline & _
  254.       "Description: "  & Err.Description        , _
  255.       MsgBoxErrorIco, MsgBoxCaption
  256.  
  257. ' Exit with reason: 'Specific error'.
  258. Wscript.Quit(5)
  259.  
  260. End If
  261.  
  262. If ReturnResult Then
  263.  
  264. If DataIsMatched = True Then
  265. ' Exit with boolean result 'True' (Rule already exist).
  266. Wscript.Quit(-1)
  267. Else
  268. ' Exit with boolean result 'False' (Rule doesn't exist).
  269. Wscript.Quit(0)
  270. End If
  271.  
  272. End If
  273.  
  274. ' This (ridiculous) conversion is needed;
  275. ' because the VBS engine prints the boolean value into a MsgBox;
  276. ' according to the OS language ( Spanish: Verdadero|Falso )
  277. If DataIsMatched = True Then
  278. DataIsMatched = "True"
  279. Else
  280. DataIsMatched = "False"
  281. End If
  282.  
  283. ' Notify the information to the user.
  284. MsgBox "File: "       & objFile.Name    & vbnewline & _
  285.       "Connection: " & ConnectionType  & vbnewline & _
  286.       "Blocked?: "   & DataIsMatched               , _
  287.       MsgBoxInfoIco, MsgBoxCaption
  288.  
  289. ' Exit successfully.
  290. Wscript.Quit(0)

Ejemplos de uso:

Código:
Wscript.exe ThisScript.vbs "C:\Program.exe" IN

Código:
Wscript.exe ThisScript.vbs "C:\Program.exe" OUT

PD: No sé si funcionará en WindowsXP, por que Netsh usa una sintaxis distinta a las versiones posteriores de Windows y supongo que en los valores de las claves de las reglas del Firewall también se verán reflejados estos cambios de sintaxis, no lo sé, no lo he comprobado.

Saludos!





Otro script, para añadir reglas de bloqueo de conexiones entrantes o salientes del firewall de Windows:

( Estos scripts dependen del primer script, 'FirewallRuleCheck.vbs', puesto que llaman a dicho script para verificar si una regla existe o si no existe )

EDITO: Mejorado
Código
  1. ' *******************
  2. ' FirewallRuleAdd.vbs
  3. ' *******************
  4. ' By Elektro
  5.  
  6.  
  7. ' ------------
  8. ' Description:
  9. ' ------------
  10. '
  11. ' This Script adds a Firewall rule to block the Inbound or Outbound connections of a file.
  12. '
  13. ' NOTE: Possibly this script will not work under Windows XP where;
  14. '       the Netsh syntax is different and maybe the Firewall registry values could be diferent too, I've don't tested it.
  15. '       Tested on Windows 8.
  16.  
  17.  
  18. ' -------
  19. ' Syntax:
  20. ' -------
  21. '
  22. ' FirewallRuleAdd.vbs "[File]" "[ConnectionType]"
  23.  
  24.  
  25. ' -----------
  26. ' Parameters:
  27. ' -----------
  28. '
  29. ' [File]
  30. ' This parameter indicates the file to block.
  31. ' The value should be the relative or absolute filepath of an existing file.
  32. '
  33. ' [ConnectionType]
  34. ' This parameter indicates whether to add a rule to block inbound or outbound connections.
  35. ' The value should be "In" or "Out".
  36.  
  37.  
  38. ' ---------------
  39. ' Usage examples:
  40. ' ---------------
  41. '
  42. ' FirewallRuleAdd.vbs "C:\Program.exe" IN
  43. ' FirewallRuleAdd.vbs "C:\Program.exe" OUT
  44.  
  45.  
  46. ' -----------
  47. ' Exit codes:
  48. ' -----------
  49. '
  50. ' -1: Rule already exist.
  51. '  0: Successful exit.
  52. '  1: Missing arguments or too many arguments.
  53. '  2: File not found.
  54. '  3: Wrong value specified for parameter '[ConnectionType]'
  55. '  4: Specific Error.
  56.  
  57.  
  58. ' *************
  59. Option Explicit
  60.  
  61. Const MsgBoxSyntax   = "FirewallRuleAdd.vbs ""[File]"" ""[ConnectionType]"""
  62. Const MsgBoxCaption  = "Firewall Rule Add"
  63. Const MsgBoxErrorIco = 16
  64. Const MsgBoxInfoIco  = 64
  65. Const MsgBoxDebugIco = 48
  66.  
  67. Dim objFile        ' Indicates the File Object.
  68. Dim Process        ' Indicates the process to run.
  69. Dim Arguments      ' Indicates the process arguments.
  70. Dim Result         ' Indicates the result (Exit Code) of the process.
  71. Dim ConnectionType ' Indicates whether to block inbound or outbound connections.
  72. Dim DebugMode      ' Indicates whether the debug mode is activated.
  73.  
  74.  
  75. ' Set the debug mode to 'True' if need to test the values.
  76. DebugMode = False
  77.  
  78. ' Argument error handling.
  79. If Wscript.Arguments.Count = 0 Then
  80.  
  81. ' Notify the error to the user.
  82. MsgBox "Syntax:" & VBNewLine & _
  83.       MsgBoxSyntax          , _
  84.       MsgBoxErrorIco, MsgBoxCaption
  85.  
  86. ' Exit with reason: 'Missing arguments' error-code.
  87. Wscript.Quit(1)
  88.  
  89. ElseIf Wscript.Arguments.Count < 2 Then
  90.  
  91. ' Notify the error to the user.
  92. MsgBox "Missing arguments."  & _
  93.       VBNewLine & VBNewLine & _
  94.       "Syntax:" & VBNewLine & _
  95.       MsgBoxSyntax          , _
  96.       MsgBoxErrorIco, MsgBoxCaption
  97.  
  98. ' Exit with reason: 'Missing arguments'.
  99. Wscript.Quit(1)
  100.  
  101. ElseIf Wscript.Arguments.Count > 2 Then
  102.  
  103. ' Notify the error to the user.
  104. MsgBox "Too many arguments." & _
  105.       VBNewLine & VBNewLine & _
  106.       "Syntax:" & VBNewLine & _
  107.       MsgBoxSyntax          , _
  108.       MsgBoxErrorIco, MsgBoxCaption
  109.  
  110. ' Exit with reason: 'Too many arguments'.
  111. Wscript.Quit(1)
  112.  
  113. ElseIf Wscript.Arguments.Count = 2 Then
  114.  
  115. If LCase(Wscript.Arguments(1)) = LCase("IN") Then
  116.  
  117. ' Set the ConnectionType to 'Inbound'
  118. ConnectionType = "Inbound"
  119.  
  120. Elseif LCase(Wscript.Arguments(1)) = LCase("OUT") Then
  121.  
  122. ' Set the ConnectionType to 'Outbound'
  123. ConnectionType = "Outbound"
  124.  
  125. Else ' Wrong argument.
  126.  
  127. ' Notify the error to the user.
  128. MsgBox "Wrong value specified for parameter '[ConnectionType]'", _
  129.       MsgBoxErrorIco, MsgBoxCaption
  130.  
  131. ' Exit with reason: 'Wrong value specified for parameter '[ConnectionType]''.
  132. Wscript.Quit(3)
  133.  
  134. End If
  135.  
  136. End If
  137.  
  138. On Error Resume Next
  139.  
  140. ' Set the FileObject with the file passed through the first argument.
  141. Set objFile = Createobject("Scripting.FileSystemObject"). _
  142.              GetFile(Wscript.Arguments(0))
  143.  
  144. ' File-Error handling.
  145. If Err.Number = 53 Then
  146.  
  147. ' Notify the error to the user.
  148. MsgBox "File not found:"   & _
  149.       vbnewline           & _
  150.       Wscript.Arguments(0), _
  151.       MsgBoxErrorIco, MsgBoxCaption
  152.  
  153. ' Exit with reason: 'File not found'.
  154. Wscript.Quit(2)
  155.  
  156. End If
  157.  
  158. ' Set the firewall process.
  159. Process   = "netsh.exe"
  160.  
  161. ' Set the firewall rule parameters to add Inbound or Outbound blocking rule.
  162. Arguments = "AdvFirewall Firewall Add Rule"       & _
  163.            " Name=" & """" & objFile.Name & """" & _
  164.            " Dir="  & Wscript.Arguments(1)       & _
  165.            " Action=Block"                       & _
  166.            " Program=" & """" & objFile.Path & """"
  167.  
  168. ' Call the 'FirewallRuleCheck' script to retrieve their exit code;
  169. ' This way I determine whether the bloking rule already exist or not.
  170. Result = WScript.CreateObject("WScript.Shell"). _
  171.         Run("FirewallRuleCheck.vbs"    & " " & _
  172.             """" & objFile.Path & """" & " " & _
  173.             Wscript.Arguments(1)       & " " & _
  174.             "True", 0, True)
  175.  
  176. If DebugMode Then
  177.  
  178. ' Notify the debug information.
  179. MsgBox "File: "            & objFile.Path   & vbnewline & vbnewline & _
  180.       "ConnectionType: "  & ConnectionType & vbnewline & vbnewline & _
  181.       "Process: "         & Process        & vbnewline & vbnewline & _
  182.       "Arguments: "       & Arguments      & vbnewline & vbnewline & _
  183.       "Reult: "           & Result         & vbnewline             , _
  184.       MsgBoxDebugIco,     "Debug Info | "  & MsgBoxCaption
  185.  
  186. End If
  187.  
  188. ' Error handling.
  189. If Err.Number <> 0 Then
  190.  
  191. ' Notify the error to the user.
  192. MsgBox "Error Code: "   & Err.Number & vbnewline & _
  193.       "Error Source: " & Err.Source & vbnewline & _
  194.       "Description: "  & Err.Description        , _
  195.       MsgBoxErrorIco, MsgBoxCaption
  196.  
  197. ' Exit with reason: 'Specific error'.
  198. Wscript.Quit(5)
  199.  
  200. End If
  201.  
  202. If Result = -1 Then ' Rule already exist.
  203.  
  204. ' Notify the error to the user.
  205. MsgBox ConnectionType & " connection blocking rule already exist for file:" & _
  206.       vbnewline                                                            & _
  207.       objFile.Name                                                         , _
  208.       MsgBoxErrorIco, MsgBoxCaption
  209.  
  210. ' Exit with reason: 'Rule already exist'.
  211. Wscript.Quit(-1)
  212.  
  213. Else ' Rule added successfully.
  214.  
  215. WScript.CreateObject("WScript.Shell").Run Process & " " & Arguments, 0, True
  216.  
  217. ' Notify the information to the user.
  218. MsgBox ConnectionType & " connection blocking rule successfully added for file:" & _
  219.       vbnewline                                                                 & _
  220.       objFile.Name                                                              , _
  221.       MsgBoxInfoIco, MsgBoxCaption
  222.  
  223. End If
  224.  
  225. ' Exit successfully.
  226. Wscript.Quit(0)





Y otro para eliminar reglas:

EDITO: Mejorado
Código
  1. ' *******************
  2. ' FirewallRuleDel.vbs
  3. ' *******************
  4. ' By Elektro
  5.  
  6.  
  7. ' ------------
  8. ' Description:
  9. ' ------------
  10. '
  11. ' This Script deletes an existing firewall rule that is blocking the Inbound or Outbound connections of a file.
  12. '
  13. ' NOTE: Possibly this script will not work under Windows XP where;
  14. '       the Netsh syntax is different and maybe the Firewall registry values could be diferent too, I've doesn't tested it.
  15. '       Tested on Windows 8.
  16.  
  17.  
  18. ' -------
  19. ' Syntax:
  20. ' -------
  21. '
  22. ' FirewallRuleDel.vbs "[File]" "[ConnectionType]"
  23.  
  24.  
  25. ' -----------
  26. ' Parameters:
  27. ' -----------
  28. '
  29. ' [File]
  30. ' This parameter indicates the file to block.
  31. ' The value should be the relative or absolute filepath of an existing file.
  32. '
  33. ' [ConnectionType]
  34. ' This parameter indicates whether to delete the rule that is blocking inbound or outbound connections.
  35. ' The value should be "In" or "Out".
  36.  
  37.  
  38. ' ---------------
  39. ' Usage examples:
  40. ' ---------------
  41. '
  42. ' FirewallRuleDel.vbs "C:\Program.exe" IN
  43. ' FirewallRuleDel.vbs "C:\Program.exe" OUT
  44.  
  45.  
  46. ' -----------
  47. ' Exit codes:
  48. ' -----------
  49. '
  50. ' -1: Rule doesn't exist.
  51. '  0: Successful exit.
  52. '  1: Missing arguments or too many arguments.
  53. '  2: File not found.
  54. '  3: Wrong value specified for parameter '[ConnectionType]'
  55. '  4: Specific Error.
  56.  
  57.  
  58. ' *************
  59. Option Explicit
  60.  
  61. Const MsgBoxSyntax   = "FirewallRuleDel.vbs ""[File]"" ""[ConnectionType]"""
  62. Const MsgBoxCaption  = "Firewall Rule Del"
  63. Const MsgBoxErrorIco = 16
  64. Const MsgBoxInfoIco  = 64
  65. Const MsgBoxDebugIco = 48
  66.  
  67. Dim objFile        ' Indicates the File Object.
  68. Dim Process        ' Indicates the process to run.
  69. Dim Arguments      ' Indicates the process arguments.
  70. Dim Result         ' Indicates the result (Exit Code) of the process.
  71. Dim ConnectionType ' Indicates whether to unblock inbound or outbound connections.
  72. Dim DebugMode      ' Indicates whether the debug mode is activated.
  73.  
  74.  
  75. ' Set the debug mode to 'True' if need to test the values.
  76. DebugMode = False
  77.  
  78. ' Argument error handling.
  79. If Wscript.Arguments.Count = 0 Then
  80.  
  81. ' Notify the error to the user.
  82. MsgBox "Syntax:" & VBNewLine & _
  83.       MsgBoxSyntax          , _
  84.       MsgBoxErrorIco, MsgBoxCaption
  85.  
  86. ' Exit with reason: 'Missing arguments' error-code.
  87. Wscript.Quit(1)
  88.  
  89. ElseIf Wscript.Arguments.Count < 2 Then
  90.  
  91. ' Notify the error to the user.
  92. MsgBox "Missing arguments."  & _
  93.       VBNewLine & VBNewLine & _
  94.       "Syntax:" & VBNewLine & _
  95.       MsgBoxSyntax          , _
  96.       MsgBoxErrorIco, MsgBoxCaption
  97.  
  98. ' Exit with reason: 'Missing arguments'.
  99. Wscript.Quit(1)
  100.  
  101. ElseIf Wscript.Arguments.Count > 2 Then
  102.  
  103. ' Notify the error to the user.
  104. MsgBox "Too many arguments." & _
  105.       VBNewLine & VBNewLine & _
  106.       "Syntax:" & VBNewLine & _
  107.       MsgBoxSyntax          , _
  108.       MsgBoxErrorIco, MsgBoxCaption
  109.  
  110. ' Exit with reason: 'Too many arguments'.
  111. Wscript.Quit(1)
  112.  
  113. ElseIf Wscript.Arguments.Count = 2 Then
  114.  
  115. If LCase(Wscript.Arguments(1)) = LCase("IN") Then
  116.  
  117. ' Set the ConnectionType to 'Inbound'
  118. ConnectionType = "Inbound"
  119.  
  120. Elseif LCase(Wscript.Arguments(1)) = LCase("OUT") Then
  121.  
  122. ' Set the ConnectionType to 'Outbound'
  123. ConnectionType = "Outbound"
  124.  
  125. Else ' Wrong argument.
  126.  
  127. ' Notify the error to the user.
  128. MsgBox "Wrong value specified for parameter '[ConnectionType]'", _
  129.   MsgBoxErrorIco, MsgBoxCaption
  130.  
  131. ' Exit with reason: 'Wrong value specified for parameter '[ConnectionType]''.
  132. Wscript.Quit(3)
  133.  
  134. End If
  135.  
  136. End If
  137.  
  138. On Error Resume Next
  139.  
  140. ' Set the FileObject with the file passed through the first argument.
  141. Set objFile = Createobject("Scripting.FileSystemObject"). _
  142.              GetFile(Wscript.Arguments(0))
  143.  
  144. ' File-Error handling.
  145. If Err.Number = 53 Then
  146.  
  147. ' Notify the error to the user.
  148. MsgBox "File not found:"   & _
  149.       vbnewline           & _
  150.       Wscript.Arguments(0), _
  151.       MsgBoxErrorIco, MsgBoxCaption
  152.  
  153. ' Exit with reason: 'File not found'.
  154. Wscript.Quit(2)
  155.  
  156. End If
  157.  
  158. ' Set the firewall process.
  159. Process   = "netsh.exe"
  160.  
  161. ' Set the firewall rule parameters to delete Inbound or Outbound blocking rule.
  162. Arguments = "AdvFirewall Firewall Delete Rule"    & _
  163.            " Name=" & """" & objFile.Name & """" & _
  164.            " Dir="  & Wscript.Arguments(1)
  165.  
  166. ' Call the 'FirewallRuleCheck' script to retrieve their exit code;
  167. ' This way I determine whether the bloking rule is exist or not.
  168. Result = WScript.CreateObject("WScript.Shell"). _
  169.         Run("FirewallRuleCheck.vbs"    & " " & _
  170.             """" & objFile.Path & """" & " " & _
  171.             Wscript.Arguments(1)       & " " & _
  172.             "True", 0, True)
  173.  
  174. If DebugMode Then
  175.  
  176. ' Notify the debug information.
  177. MsgBox "File: "            & objFile.Path   & vbnewline & vbnewline & _
  178.       "ConnectionType: "  & ConnectionType & vbnewline & vbnewline & _
  179.       "Process: "         & Process        & vbnewline & vbnewline & _
  180.       "Arguments: "       & Arguments      & vbnewline & vbnewline & _
  181.       "Reult: "           & Result         & vbnewline             , _
  182.       MsgBoxDebugIco,     "Debug Info | "  & MsgBoxCaption
  183.  
  184. End If
  185.  
  186. ' Error handling.
  187. If Err.Number <> 0 Then
  188.  
  189. ' Notify the error to the user.
  190. MsgBox "Error Code: "   & Err.Number & vbnewline & _
  191.       "Error Source: " & Err.Source & vbnewline & _
  192.       "Description: "  & Err.Description        , _
  193.       MsgBoxErrorIco, MsgBoxCaption
  194.  
  195. ' Exit with reason: 'Specific error'.
  196. Wscript.Quit(5)
  197.  
  198. End If
  199.  
  200. If Result = 0 Then ' Rule doesn't exist.
  201.  
  202. ' Notify the error to the user.
  203. MsgBox ConnectionType & " connection blocking rule doesn't exist for file:" & _
  204.       vbnewline                                                            & _
  205.       objFile.Name                                                         , _
  206.       MsgBoxErrorIco, MsgBoxCaption
  207.  
  208. ' Exit with reason: 'Rule doesn't exist'.
  209. Wscript.Quit(-1)
  210.  
  211. Else ' Rule deleted successfully.
  212.  
  213. WScript.CreateObject("WScript.Shell").Run Process & " " & Arguments, 0, True
  214.  
  215. ' Notify the information to the user.
  216. MsgBox ConnectionType & " connection block rule successfully deleted for file:" & _
  217.       vbnewline                                                                & _
  218.       objFile.Name                                                             , _
  219.       MsgBoxInfoIco, MsgBoxCaption
  220.  
  221. End If
  222.  
  223. ' Exit successfully.
  224. Wscript.Quit(0)


« Última modificación: 3 Febrero 2014, 20:15 pm por Eleкtro » En línea



Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.874



Ver Perfil
Re: [APORTE] [VBS] Snippets para manipular reglas de bloqueo del firewall de Windows
« Respuesta #1 en: 3 Febrero 2014, 20:19 pm »

Al final, como dije, añadí estas opciones al menú contextual (para archivos executables sólamente)



También hice un instalador, si alguien lo quiere... ~> http://www.mediafire.com/download/e8chmiqq6behvmc/Firewall.exe

Un saludo!


« Última modificación: 3 Febrero 2014, 20:55 pm por Eleкtro » En línea



Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines