参考

使用 netsh advfirewall 防火墙而不是 netsh 防火墙来控制Windows防火墙行为
Netsh AdvFirewall Firewall Commands?redirectedfrom=MSDN)

简介

netsh advfirewall firewall 命令行在Windows Vista 和 Windows Server 2008 中可用。
它提供了用于控制 Windows 防火墙行为的功能。
在早期版本的 Windows <包含Win7> 操作系统中应该使用 netsh firewall 命令。
新版本的命令行提供了更精确地控制的防火墙规则的功能,这些规则包括以下的每个配置文件设置︰

  • 域(Domain)
  • 私有(Private)
  • 公共(Public)

    基本使用

    使用help ```powershell C:\WINDOWS\system32>netsh advfirewall help

下列指令有效:

此上下文中的命令: ? - 显示命令列表。 consec - 更改到 netsh advfirewall consec' 上下文。 dump - 显示一个配置脚本。 export - 将当前策略导出到文件。 firewall - 更改到netsh advfirewall firewall’ 上下文。 help - 显示命令列表。 import - 将策略文件导入当前策略存储。 mainmode - 更改到 netsh advfirewall mainmode' 上下文。 monitor - 更改到netsh advfirewall monitor’ 上下文。 reset - 将策略重置为默认全新策略。 set - 设置每个配置文件或全局设置。 show - 显示配置文件或全局属性。

下列的子上下文可用: consec firewall mainmode monitor

若需要命令的更多帮助信息,请键入命令,接着是空格, 后面跟 ?。

C:\WINDOWS\system32>

  1. ```powershell
  2. C:\WINDOWS\system32>netsh advfirewall firewall help
  3. 下列指令有效:
  4. 此上下文中的命令:
  5. ? - 显示命令列表。
  6. add - 添加新入站或出站防火墙规则。
  7. delete - 删除所有匹配的防火墙规则。
  8. dump - 显示一个配置脚本。
  9. help - 显示命令列表。
  10. set - 为现有规则的属性设置新值。
  11. show - 显示指定的防火墙规则。
  12. 若需要命令的更多帮助信息,请键入命令,接着是空格,
  13. 后面跟 ?。

netsh advfirewall firewall add rule

将新的入站或出站规则添加到防火墙策略。

  1. netsh advfirewall firewall add rule ?

语法规则

  1. add rule name=<string>
  2. dir=in|out
  3. action=allow|block|bypass
  4. [program=<program path>]
  5. [service=<service short name>|any]
  6. [description=<string>]
  7. [enable=yes|no (default=yes)]
  8. [profile=public|private|domain|any[,...]]
  9. [localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  10. [remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|
  11. <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  12. [localport=0-65535|<port range>[,...]|RPC|RPC-EPMap|IPHTTPS|any (default=any)]
  13. [remoteport=0-65535|<port range>[,...]|any (default=any)]
  14. [protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|
  15. tcp|udp|any (default=any)]
  16. [interfacetype=wireless|lan|ras|any]
  17. [rmtcomputergrp=<SDDL string>]
  18. [rmtusrgrp=<SDDL string>]
  19. [edge=yes|deferapp|deferuser|no (default=no)]
  20. [security=authenticate|authenc|authdynenc|authnoencap|notrequired
  21. (default=notrequired)]

参数含义

name 规则名称,规则名称应该是唯一的,且不能为 “all”
dir 方向,in表示进站,out表示出站
action 对规则的操作,allow表示允许连接,block表示阻止连接,bypass表示只允许安全连接
program 指定程序的路径
service
description
enable 是否启用规则,默认yes
profile 指定这个规则所用于的配置文件,取值可以是public,private,domain,any,分别表示共用网络,专用网络,域和所有;
localip
remoteip 远程地址
localport 指定本地端口,可以指定端口号,默认为any,表示任何端口
remoteport 远程端口
protocol 协议类型,默认为any,可以指定tcp,udp,icmp等等。
interfacetype
rmtcomputergrp
rmtusrgrp
edge
security

禁用端口

  1. // 关闭端口,测试ok
  2. netsh advfirewall firewall add rule name=test1 dir=in action=block protocol=TCP localport=8089
  3. netsh advfirewall firewall add rule name=test1 dir=in action=block protocol=TCP localport=8089 remoteip="223.76.234.68"
  4. netsh advfirewall firewall add rule name=test1 dir=out action=block protocol=TCP localport=8089
  5. netsh advfirewall firewall add rule name=禁用TCP端口 dir=in action=block protocol=TCP localport="135-139,445,3389"
  6. netsh advfirewall firewall add rule name=禁用UDP端口 dir=in action=block protocol=UDP localport="135-139,445"
  7. netsh advfirewall firewall add rule name=允许指定IP访问3389 dir=in action=allow protocol=TCP localport="3389" remoteip="aip,bip,cip"
  1. netsh advfirewall firewall add rule name= deny tcp 21 dir=in protocol=tcp localport=21 action=block
  2. netsh advfirewall firewall add rule name= deny tcp 22 dir=in protocol=tcp localport=22 action=block
  3. netsh advfirewall firewall add rule name= deny tcp 23 dir=in protocol=tcp localport=23 action=block
  4. netsh advfirewall firewall add rule name= deny tcp 3389 dir=in protocol=tcp localport=3389 action=block
  5. netsh advfirewall firewall add rule name= deny tcp 5800 dir=in protocol=tcp localport=5800 action=block
  6. netsh advfirewall firewall add rule name= deny tcp 5900 dir=in protocol=tcp localport=5900 action=block
  7. netsh advfirewall firewall add rule name= deny tcp 137 dir=in protocol=tcp localport=137 action=block
  8. netsh advfirewall firewall add rule name= deny tcp 138 dir=in protocol=tcp localport=138 action=block
  9. //禁用8089
  10. netsh advfirewall firewall add rule name= “禁用8089 dir=in protocol=tcp localport=8089 action=block
  1. //删除针对本地8089端口的所有入站规则: 测试OK
  2. netsh advfirewall firewall delete rule name=all protocol=tcp localport=8089
  3. netsh advfirewall firewall delete rule name=all protocol=udp localport=8089

配置 ICMP 设置

  1. netsh advfirewall firewall add rule ?
  1. netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol=icmpv4:8,any dir=in action=allow
  2. netsh advfirewall firewall add rule name= "All ICMP V4" protocol=icmpv4:any,any dir=in action=allow
  3. netsh advfirewall firewall add rule name="Block Type 13 ICMP V4" protocol=icmpv4:13,any dir=in action=block

netsh advfirewall firewall delete rule

  1. C:\WINDOWS\system32>netsh advfirewall firewall delete rule
  2. 提供的许多参数无效。请查看帮助获取正确语法。
  3. 用法: delete rule name=<string>
  4. [dir=in|out]
  5. [profile=public|private|domain|any[,...]]
  6. [program=<program path>]
  7. [service=<service short name>|any]
  8. [localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  9. [remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|
  10. <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  11. [localport=0-65535|<port range>[,...]|RPC|RPC-EPMap|any]
  12. [remoteport=0-65535|<port range>[,...]|any]
  13. [protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|
  14. tcp|udp|any]
  15. 注释:
  16. - 删除按名称识别的规则,也可按终结点、端口、
  17. 协议和类型识别规则
  18. - 如果找到多个匹配项,则删除所有匹配规则。
  19. - 如果指定 name=all,则从指定的
  20. 类型和配置文件中删除所有规则。
  21. 示例:
  22. 删除本地端口 80 的所有入则:
  23. netsh advfirewall firewall delete rule name=all protocol=tcp localport=80
  24. 删除名为 "allow80" 的规则:
  25. netsh advfirewall firewall delete rule name="allow80"
  26. netsh advfirewall firewall delete rule name="test1"

实例

查看规则

查看规则:

  1. // 使用帮助
  2. netsh advfirewall firewall show rule
  3. netsh advfirewall firewall show rule name="test1"

实例

  1. // 查看是否启动windows防火墙
  2. netsh advfirewall show allprofiles state
  3. // 查看默认防火墙规则
  4. netsh advfirewall show allprofiles firewallpolicy
  5. //查看windows防火墙所有入站规则
  6. netsh advfirewall firewall show rule name=all dir=in type=static verbose
  7. //查看windows防火墙指定端口规则
  8. netsh advfirewall firewall show rule name=all localport=8089
  1. // 重新载入
  2. firewall --reload
  3. // 开启&关闭防火墙
  4. netsh advfirewall set allprofiles state on
  5. netsh advfirewall set allprofiles state off
  6. 检测端口是否关闭成功
  7. netstat -na -p tcp | findstr "8088"
  8. 没有任何记录返回,则说明该端口关闭成功了。
  1. 1)恢复初始防火墙设置
  2. netsh advfirewall reset
  3. 2)关闭防火墙
  4. netsh advfirewall set allprofiles state off
  5. 3)启用桌面防火墙
  6. netsh advfirewall set allprofiles state on
  7. 4)设置默认输入和输出策略
  8. netsh advfirewall set allprofiles firewallpolicy allowinbound,allowoutbound
  9. 以上是设置为允许,如果设置为拒绝使用blockinbound,blockoutbound
  10. 5)关闭tcp协议的139端口
  11. netsh advfirewall firewall add rule name=”deny tcp 139 dir=in protocol=tcp localport=139 action=block
  12. 6)关闭udp协议的139端口
  13. netsh advfirewall firewall add rule name=”deny udp 139 dir=in protocol=udp localport=139 action=block
  14. 7)关闭tcp协议的445端口
  15. netsh advfirewall firewall add rule name=”deny tcp 445 dir=in protocol=tcp localport=445 action=block
  16. 8)关闭udp协议的445端口
  17. netsh advfirewall firewall add rule name=”deny udp 445 dir=in protocol=udp localport=445 action=block
  18. 9)使用相同的方法,依次关闭TCP协议的212223137138338958005900端口。
  19. netsh advfirewall firewall add rule name= deny tcp 21 dir=in protocol=tcp localport=21 action=block
  20. netsh advfirewall firewall add rule name= deny tcp 22 dir=in protocol=tcp localport=22 action=block
  21. netsh advfirewall firewall add rule name= deny tcp 23 dir=in protocol=tcp localport=23 action=block
  22. netsh advfirewall firewall add rule name= deny tcp 3389 dir=in protocol=tcp localport=3389 action=block 学习电脑
  23. netsh advfirewall firewall add rule name= deny tcp 5800 dir=in protocol=tcp localport=5800 action=block
  24. netsh advfirewall firewall add rule name= deny tcp 5900 dir=in protocol=tcp localport=5900 action=block
  25. netsh advfirewall firewall add rule name= deny tcp 137 dir=in protocol=tcp localport=137 action=block
  26. netsh advfirewall firewall add rule name= deny tcp 138 dir=in protocol=tcp localport=138 action=block
  27. 以上内容搬运自百度知道https://zhidao.baidu.com/question/2075121309871751268.html,稍作整理

Set命令