Quantcast
Channel: Blog Virtualizacion
Viewing all articles
Browse latest Browse all 679

Firewall Windows vía Powershell

$
0
0

Firewall Windows vía Powershell

¿Cuántas veces te ha tocado configurar un Firewall de Windows por Powershell? Creo que ya llevo varios años en esto, y así como con Iptables ha sido muy común pelearme, creo que nunca me he visto en la situación de un proyecto que haga que revise la configuración de unas reglas de estado en Windows por Powershell (no así el estado del servicio u otras configuraciones más comunes). Son de esas cosas, que raramente realizas porque sueles tener la alternativa gráfica para ayudarte.

Bueno, pues “como de esta vida no te irás sin aprender algo nuevo”, llegó el momento. Así que os comparto mi experiencia.

Son Windows Server Core, con Hyper-V, y tengo un problema con los backups de Veeam. Y mi sospecha es que mi amigo Firewall de Windows de los hosts físicos me esté bloqueando ese tráfico. Ya que mi firewall perimetral no da señal de que sea el problema.

firewall-windows-via-powershell-1

Lo primero es comprobar su estado:

PS C:\Users\Administrador\Documents> netsh advfirewall show allprofiles state

Domain Profile Settings:
----------------------------------------------------------------------
State ON

Private Profile Settings:
----------------------------------------------------------------------
State OFF

Public Profile Settings:
----------------------------------------------------------------------
State OFF
Ok.

Si queremos ampliar la información:

PS C:\Users\Administrador\Documents> netsh.exe advfirewall show allprofiles

Domain Profile Settings:
----------------------------------------------------------------------
State ON
Firewall Policy BlockInbound,AllowOutbound
LocalFirewallRules N/A (GPO-store only)
LocalConSecRules N/A (GPO-store only)
InboundUserNotification Disable
RemoteManagement Disable
UnicastResponseToMulticast Enable

Logging:
LogAllowedConnections Disable
LogDroppedConnections Disable
FileName %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize 4096


Private Profile Settings:
----------------------------------------------------------------------
State ON
Firewall Policy BlockInbound,AllowOutbound
LocalFirewallRules N/A (GPO-store only)
LocalConSecRules N/A (GPO-store only)
InboundUserNotification Disable
RemoteManagement Disable
UnicastResponseToMulticast Enable

Logging:
LogAllowedConnections Disable
LogDroppedConnections Disable
FileName %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize 4096


Public Profile Settings:
----------------------------------------------------------------------
State ON
Firewall Policy BlockInbound,AllowOutbound
LocalFirewallRules N/A (GPO-store only)
LocalConSecRules N/A (GPO-store only)
InboundUserNotification Disable
RemoteManagement Disable
UnicastResponseToMulticast Enable

Logging:
LogAllowedConnections Disable
LogDroppedConnections Disable
FileName %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize 4096

Ok.

Otro comando interesante:

PS C:\Users\Administrador\Documents> Get-NetFirewallProfile

Name : Domain
Enabled : True
DefaultInboundAction : NotConfigured
DefaultOutboundAction : NotConfigured
AllowInboundRules : NotConfigured
AllowLocalFirewallRules : NotConfigured
AllowLocalIPsecRules : NotConfigured
AllowUserApps : NotConfigured
AllowUserPorts : NotConfigured
AllowUnicastResponseToMulticast : NotConfigured
NotifyOnListen : False
EnableStealthModeForIPsec : NotConfigured
LogFileName : %systemroot%\system32\LogFiles\Firewall\pfirewall.log
LogMaxSizeKilobytes : 4096
LogAllowed : False
LogBlocked : False
LogIgnored : NotConfigured
DisabledInterfaceAliases : {NotConfigured}

Name : Private
Enabled : True
DefaultInboundAction : NotConfigured
DefaultOutboundAction : NotConfigured
AllowInboundRules : NotConfigured
AllowLocalFirewallRules : NotConfigured
AllowLocalIPsecRules : NotConfigured
AllowUserApps : NotConfigured
AllowUserPorts : NotConfigured
AllowUnicastResponseToMulticast : NotConfigured
NotifyOnListen : False
EnableStealthModeForIPsec : NotConfigured
LogFileName : %systemroot%\system32\LogFiles\Firewall\pfirewall.log
LogMaxSizeKilobytes : 4096
LogAllowed : False
LogBlocked : False
LogIgnored : NotConfigured
DisabledInterfaceAliases : {NotConfigured}

Name : Public
Enabled : True
DefaultInboundAction : NotConfigured
DefaultOutboundAction : NotConfigured
AllowInboundRules : NotConfigured
AllowLocalFirewallRules : NotConfigured
AllowLocalIPsecRules : NotConfigured
AllowUserApps : NotConfigured
AllowUserPorts : NotConfigured
AllowUnicastResponseToMulticast : NotConfigured
NotifyOnListen : False
EnableStealthModeForIPsec : NotConfigured
LogFileName : %systemroot%\system32\LogFiles\Firewall\pfirewall.log
LogMaxSizeKilobytes : 4096
LogAllowed : False
LogBlocked : False
LogIgnored : NotConfigured
DisabledInterfaceAliases : {NotConfigured}

Si queréis sacar directamente donde está el log alojado del firewall por defecto (Domain, Private y Public):

PS C:\Users\Administrador\Documents> netsh advfirewall show allprofiles | Select-String Filename

FileName %systemroot%\system32\LogFiles\Firewall\pfirewall.log
FileName %systemroot%\system32\LogFiles\Firewall\pfirewall.log
FileName %systemroot%\system32\LogFiles\Firewall\pfirewall.log

Habilitar el log (false para deshabilitar):

Set-NetFirewallProfile -name domain -LogMaxSizeKilobytes 10240 -LogAllowed true -LogBlocked true
Set-NetFirewallProfile -name public -LogMaxSizeKilobytes 10240 -LogAllowed true -LogBlocked true
Set-NetFirewallProfile -name private -LogMaxSizeKilobytes 10240 -LogAllowed true -LogBlocked true

Cambiarán estas líneas:

Domain Profile Settings:
----------------------------------------------------------------------
State ON
Firewall Policy BlockInbound,AllowOutbound
LocalFirewallRules N/A (GPO-store only)
LocalConSecRules N/A (GPO-store only)
InboundUserNotification Disable
RemoteManagement Disable
UnicastResponseToMulticast Enable

Logging:
<strong>LogAllowedConnections Enable</strong>
<strong>LogDroppedConnections Enable</strong>
FileName %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize 4096

Revisar lo rechazado, por ejemplo. Colocamos el more por si es muy grande:

Select-String -Path "c:\Windows\system32\LogFiles\Firewall\pfirewall.log" -Pattern "drop" | more

O si queréis encontrar una IP en concreto:

PS C:\Windows\System32\LogFiles\Firewall&gt; Select-String -Path .\pfirewall.log -Pattern "192.168.91.52"

pfirewall.log:2913:2018-10-29 16:00:26 ALLOW UDP 192.168.91.52 192.168.91.255 137 137 0 - - - - - - - RECEIVE
pfirewall.log:2914:2018-10-29 16:00:26 ALLOW UDP 192.168.91.52 224.0.0.252 58070 5355 0 - - - - - - - RECEIVE

Yo no lo he tenido que hacer, pero si queréis generar reglas o borrarlas os dejo ejemplos de comandos:

New-NetFirewallRule -DisplayName "Block Outbound Port 80" -Direction Outbound -LocalPort 80 -Protocol TCP -Action Block

New-NetFirewallRule -DisplayName "Allow Messenger" -Direction Inbound -Program "C:\Program Files (x86)\Messenger\msmsgs.exe" -RemoteAddress LocalSubnet -Action Allow

Remove-NetFirewallRule

Espero que os sirva de ayuda…

La entrada Firewall Windows vía Powershell se publicó primero en VMware Citrix Blog.


Viewing all articles
Browse latest Browse all 679