一、防火墙- @echo off
- rem 设置防火墙服务为自动
- sc config sharedaccess start= auto
- rem 开启防火墙服务
- rem net start sharedaccess
-
-
- rem blockinbound,blockoutbound,allowinbound,allowoutbound
- netsh advfirewall set allprofiles state on
- netsh advfirewall set allprofiles firewallpolicy allowinbound,allowoutbound
- call:blockport 445
- call:blockport 135
- call:blockport 139
-
- pause >nul &exit
-
- rem 如果不起作用,则重置防火墙
- netsh firewall reset
-
- rem 关闭端口
- :blockport
- set port=%1
- call:clearrule %port%
- netsh advfirewall firewall add rule name="deny tcp %port%" dir=in protocol=tcp localport=%port% action=block
- netsh advfirewall firewall add rule name="deny udp %port%" dir=in protocol=udp localport=%port% action=block
- goto :eof
-
-
- rem 开启端口
- :openport
- set port=%1
- call:clearrule %port%
- netsh advfirewall firewall add rule name="allow tcp %port%" dir=in protocol=tcp localport=%port% action=allow
- netsh advfirewall firewall add rule name="allow udp %port%" dir=in protocol=udp localport=%port% action=allow
- goto :eof
-
- rem 清除多余规则
- :clearrule
- set port=%1
- netsh advfirewall firewall delete rule name="allow tcp %port%" protocol=tcp localport=%port% >nul
- netsh advfirewall firewall delete rule name="deny tcp %port%" protocol=tcp localport=%port% >nul
- netsh advfirewall firewall delete rule name="allow udp %port%" protocol=udp localport=%port% >nul
- netsh advfirewall firewall delete rule name="deny udp %port%" protocol=udp localport=%port% >nul
- goto :eof
复制代码 ------------------------------------------------------------------------------------------------------------------------------------------------
二、组策略- @echo off
- rem -p 指定策略名称,如果名称存在,则将该规则加入此策略,否则创建一个。(policy)
- rem -r 指定规则名称。(rule)
- rem -f 筛选器。*表示任何地址(源);0表示本机地址(目标);+表示镜像(双向)筛选(filter)
- rem -n 指定操作,可以是BLOCK、PASS或者INPASS,必须大写。
- rem -x 激活该策略。
- rem -y 使之无效。
- rem -o 删除-p指定的策略。
- rem -w reg 将配置写入注册表,重启后仍有效。
-
-
- sc config PolicyAgent start= auto
- net start PolicyAgent
-
- call:blockport 445
- call:blockport 135
- call:blockport 139
- rem call:blockping
- rem call:openping
- gpupdate/force
- pause&exit
-
- :blockport
- rem 所有的封端口操作只能写成一个组策略BlockPort,不能135一个,445一个
- set port=%1
- ipseccmd -w REG -p BlockPort -r "Block TCP/%port%" -f *+0:%port%:TCP -n BLOCK
- ipseccmd -w REG -p BlockPort -r "Block UDP/%port%" -f *+0:%port%:UDP -n BLOCK
- ipseccmd -w REG -p BlockPort -x
- goto :eof
-
- :blockping
- ipseccmd -w REG -p "BlockPing" -r "Block ping" -f 0+*::ICMP -n BLOCK
- ipseccmd -w REG -p "BlockPing" -x
- goto :eof
-
- :openping
- ipseccmd -w REG -p "BlockPing" -y
- goto :eof
-
- :deletePing
- ipseccmd -w REG -p "BlockPing" -y
- ipseccmd -w REG -p "BlockPing" -o
- goto :eof
复制代码
|