简介
netsh advfirewall firewall 命令行在Windows Vista 和 Windows Server 2008 中可用。
它提供了用于控制 Windows 防火墙行为的功能。
在早期版本的 Windows <包含Win7> 操作系统中应该使用 netsh firewall 命令。
新版本的命令行提供了更精确地控制的防火墙规则的功能,这些规则包括以下的每个配置文件设置︰
- 域(Domain)
- 私有(Private)
- 公共(Public)
基本使用
进入防火墙
C:\Users\0310011772>netshnetsh>
使用help
```java C:\Users\0310011772>netsh netsh>help
下列指令有效:
此上下文中的命令:
.. - 移到上一层上下文级。
? - 显示命令列表。
abort - 丢弃在脱机模式下所做的更改。
add - 在项目列表上添加一个配置项目。
advfirewall - 更改到 netsh advfirewall' 上下文。
alias - 添加一个别名
branchcache - 更改到netsh branchcache’ 上下文。
bridge - 更改到 netsh bridge' 上下文。
bye - 退出程序。
commit - 提交在脱机模式中所做的更改。
delete - 在项目列表上删除一个配置项目。
dhcpclient - 更改到netsh dhcpclient’ 上下文。
dnsclient - 更改到 netsh dnsclient' 上下文。
dump - 显示一个配置脚本。
exec - 运行一个脚本文件。
exit - 退出程序。
firewall - 更改到netsh firewall’ 上下文。
help - 显示命令列表。
http - 更改到 netsh http' 上下文。
interface - 更改到netsh interface’ 上下文。
ipsec - 更改到 netsh ipsec' 上下文。
lan - 更改到netsh lan’ 上下文。
mbn - 更改到 netsh mbn' 上下文。
namespace - 更改到netsh namespace’ 上下文。
netio - 更改到 netsh netio' 上下文。
offline - 将当前模式设置成脱机。
online - 将当前模式设置成联机。
p2p - 更改到netsh p2p’ 上下文。
popd - 从堆栈上打开一个上下文。
pushd - 将当前上下文放入堆栈。
quit - 退出程序。
ras - 更改到 netsh ras' 上下文。
rpc - 更改到netsh rpc’ 上下文。
set - 更新配置设置。
show - 显示信息。
trace - 更改到 netsh trace' 上下文。
unalias - 删除一个别名。
wcn - 更改到netsh wcn’ 上下文。
wfp - 更改到 netsh wfp' 上下文。
winhttp - 更改到netsh winhttp’ 上下文。
winsock - 更改到 netsh winsock' 上下文。
wlan - 更改到netsh wlan’ 上下文。
下列的子上下文可用: advfirewall branchcache bridge dhcpclient dnsclient firewall http interface ipsec lan mbn namespace netio p2p ras rpc trace wcn wfp winhttp winsock wlan
若需要命令的更多帮助信息,请键入命令,接着是空格, 后面跟 ?。
<a name="txYAa"></a># netsh firewall><a name="LKVMZ"></a>## help```javanetsh firewall>help下列指令有效:命令从 netsh 上下文继承:.. - 移到上一层上下文级。abort - 丢弃在脱机模式下所做的更改。add - 在项目列表上添加一个配置项目。advfirewall - 更改到 `netsh advfirewall' 上下文。alias - 添加一个别名branchcache - 更改到 `netsh branchcache' 上下文。bridge - 更改到 `netsh bridge' 上下文。bye - 退出程序。commit - 提交在脱机模式中所做的更改。delete - 在项目列表上删除一个配置项目。dhcpclient - 更改到 `netsh dhcpclient' 上下文。dnsclient - 更改到 `netsh dnsclient' 上下文。exit - 退出程序。firewall - 更改到 `netsh firewall' 上下文。http - 更改到 `netsh http' 上下文。interface - 更改到 `netsh interface' 上下文。ipsec - 更改到 `netsh ipsec' 上下文。lan - 更改到 `netsh lan' 上下文。mbn - 更改到 `netsh mbn' 上下文。namespace - 更改到 `netsh namespace' 上下文。netio - 更改到 `netsh netio' 上下文。offline - 将当前模式设置成脱机。online - 将当前模式设置成联机。p2p - 更改到 `netsh p2p' 上下文。popd - 从堆栈上打开一个上下文。pushd - 将当前上下文放入堆栈。quit - 退出程序。ras - 更改到 `netsh ras' 上下文。rpc - 更改到 `netsh rpc' 上下文。set - 更新配置设置。show - 显示信息。trace - 更改到 `netsh trace' 上下文。unalias - 删除一个别名。wcn - 更改到 `netsh wcn' 上下文。wfp - 更改到 `netsh wfp' 上下文。winhttp - 更改到 `netsh winhttp' 上下文。winsock - 更改到 `netsh winsock' 上下文。wlan - 更改到 `netsh wlan' 上下文。此上下文中的命令:? - 显示命令列表。add - 添加防火墙配置。delete - 删除防火墙配置。dump - 显示一个配置脚本。help - 显示命令列表。set - 设置防火墙配置。show - 显示防火墙配置。若需要命令的更多帮助信息,请键入命令,接着是空格,后面跟 ?。netsh firewall>
实例
// 查看是否启动windows防火墙netsh advfirewall show allprofiles state// 查看默认防火墙规则netsh advfirewall show allprofiles firewallpolicy//查看windows防火墙所有入站规则netsh advfirewall firewall show rule name=all dir=in type=static verbose//查看windows防火墙指定端口规则netsh advfirewall firewall show rule name=all localport=8089
//查看防火墙放行的程序netsh firewall>show allowedprogram // (当已经进入到netsh firewall,则不用再输入相关内容)netsh firewall show allowedprogram//打开445端口netsh firewall set portopening TCP 445 ENABLE//关闭3389端口netsh firewall set portopening TCP 3389 disablenetsh firewall set portopening TCP 8088 disablenetsh firewall set portopening TCP 3389 disable//删除放行程序A.exenetsh firewall delete allowedprogram C:\A.exe//添加程序C盘下的A.exe并放行netsh firewall set allowedprogram C:\A.exe A ENABLEnetsh firewall add allowedprogram C:\A.exe A ENABLE//开启ICMP协议netsh firewall set icmpsettting type=ALL mode=enablenetsh firewall set icmpsettting type=ALL mode=disable//允许出站数据包太大netsh firewall set icmpsettting type=2 mode=enable//删除针对本地21端口的所有入站规则:netsh advfirewall firewall delete name rule name=all protocol=tcp localport=8089// 重新载入firewall --reload// 开启&关闭防火墙netsh advfirewall set allprofiles state onnetsh advfirewall set allprofiles state off// 关闭端口// 测试oknetsh advfirewall firewall add rule name=test1 dir=in action=block protocol=TCP localport=8089netsh firewall add portopening protocol = TCP port = 8088 name = "关闭8088" mode = DISABLEnetsh firewall set portopening protocol=tcp port=8088 mode=disable name=deny8088firewall --reloadnetsh firewall set icmpsettting type=ALL mode=enable //开启ICMP协议开启端口netsh firewall add portopening protocol = TCP port = 139 name = "开启139" mode = ENABLEnetsh firewall add portopening protocol = TCP port = 445 name = "开启445" mode = ENABLEnetsh firewall add portopening protocol = UDP port = 137 name = "开启137" mode = ENABLEnetsh firewall add portopening protocol = UDP port = 138 name = "开启138" mode = ENABLE检测端口是否关闭成功netstat -na -p tcp | findstr "8088"没有任何记录返回,则说明该端口关闭成功了。
C:\WINDOWS\system32>netsh advfirewall firewall delete rule提供的许多参数无效。请查看帮助获取正确语法。用法: delete rule name=<string>[dir=in|out][profile=public|private|domain|any[,...]][program=<program path>][service=<service short name>|any][localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>][remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>][localport=0-65535|<port range>[,...]|RPC|RPC-EPMap|any][remoteport=0-65535|<port range>[,...]|any][protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|tcp|udp|any]注释:- 删除按名称识别的规则,也可按终结点、端口、协议和类型识别规则- 如果找到多个匹配项,则删除所有匹配规则。- 如果指定 name=all,则从指定的类型和配置文件中删除所有规则。示例:删除本地端口 80 的所有入则:netsh advfirewall firewall delete rule name=all protocol=tcp localport=80删除名为 "allow80" 的规则:netsh advfirewall firewall delete rule name="allow80"
(1)恢复初始防火墙设置netsh advfirewall reset(2)关闭防火墙netsh advfirewall set allprofiles state off(3)启用桌面防火墙netsh advfirewall set allprofiles state on(4)设置默认输入和输出策略netsh advfirewall set allprofiles firewallpolicy allowinbound,allowoutbound以上是设置为允许,如果设置为拒绝使用blockinbound,blockoutbound(5)关闭tcp协议的139端口netsh advfirewall firewall add rule name=”deny tcp 139″ dir=in protocol=tcp localport=139 action=block(6)关闭udp协议的139端口netsh advfirewall firewall add rule name=”deny udp 139″ dir=in protocol=udp localport=139 action=block(7)关闭tcp协议的445端口netsh advfirewall firewall add rule name=”deny tcp 445″ dir=in protocol=tcp localport=445 action=block(8)关闭udp协议的445端口netsh advfirewall firewall add rule name=”deny udp 445″ dir=in protocol=udp localport=445 action=block(9)使用相同的方法,依次关闭TCP协议的21、22、23、137、138、3389、5800、5900端口。netsh advfirewall firewall add rule name= “deny tcp 21″ dir=in protocol=tcp localport=21 action=blocknetsh advfirewall firewall add rule name= “deny tcp 22″ dir=in protocol=tcp localport=22 action=blocknetsh advfirewall firewall add rule name= “deny tcp 23″ dir=in protocol=tcp localport=23 action=blocknetsh advfirewall firewall add rule name= “deny tcp 3389″ dir=in protocol=tcp localport=3389 action=block 学习电脑netsh advfirewall firewall add rule name= “deny tcp 5800″ dir=in protocol=tcp localport=5800 action=blocknetsh advfirewall firewall add rule name= “deny tcp 5900″ dir=in protocol=tcp localport=5900 action=blocknetsh advfirewall firewall add rule name= “deny tcp 137″ dir=in protocol=tcp localport=137 action=blocknetsh advfirewall firewall add rule name= “deny tcp 138″ dir=in protocol=tcp localport=138 action=block以上内容搬运自百度知道https://zhidao.baidu.com/question/2075121309871751268.html,稍作整理
