windows防火墙的设置与管理 (windows防火墙的使用和设置)

概述

众所周知,系统防火墙是系统安全防护的有效屏障,而实际工作中,系统防火墙规则如何高效地进行维护呢?本期文章将结合实际的场景,向各位小伙伴总结分享命令集的方式玩转Windows系统防火墙。

备注:命令集的方式操作系统防火墙,须以管理员权限打开系统命令提示符(CMD);

不同应用场景下的系统防火墙配置

场景1:系统防火墙开启及关闭操作;

#系统防火墙开启

netsh advfirewall set allprofiles state on

#系统防火墙关闭

netsh advfirewall set allprofiles state off

场景2:系统防火墙恢复默认配置

netsh advfirewall reset

场景3:系统防火墙规则导出备份及导入还原;

#系统防火墙规则导出备份

netsh advfirewall export "c:\advfirewallpolicy2022.pol"

#系统防火墙规则导入还原

netsh advfirewall import "c:\advfirewallpolicy2022.pol"

场景4:允许来自“来宾或公用网络”上的任意IP以TCP方式访问系统的程序Test*ex.e**;

netsh advfirewall firewall add rule name="Test" dir= in action=allow program="C:\Program Files\Test\Testzip\Test*ex.e**" protocol=TCP profile=public enable=yes

释义:

name="Test" :规则名称Test;

dir= in :系统入站规则,系统出站规则即dir= out;

action=allow :允许,拒绝即action=block;

program="C:\Program Files\Test\Testzip\Test*ex.e**" :程序的目录;

protocol=TCP :TCP协议访问;

profile=public :来宾或公用网络,而profile= private指专用网络;

enable=yes :启用规则(系统默认),而enable=no则是禁用规则;

场景5:仅允许来自“来宾或公用网络”上的IP“192.168.1.2”访问系统的TCP 80-81,3389端口;

netsh advfirewall firewall add rule name="Only_192.168.1.2_Access_Local-TCP-80-81,3389" dir=in action=allow remoteip=192.168.1.2 protocol=TCP localport=80-81,3389 profile=public

释义:

remoteip=192.168.1.2 :远程的IP,即访问者的IP;

localport=80-81,3389 :系统本地端口80,81,3389;

场景6:禁止任意IP访问系统的UDP 445端口和TCP 445端口;

netsh advfirewall firewall add rule name="Deny_Any_Access_Local_TCP-445" dir=in action=block localport=445 protocol=tcp

netsh advfirewall firewall add rule name= "Deny_Any_Access_Local_UDP-445" dir=in action=block localport=445 protocol=udp

场景7:配置系统ICMPv4访问规则

#允许任意IP的发送ICMPv4请求类型的报文到系统;

netsh advfirewall firewall add rule name="Allow_Any_Send_ICMPv4_echo_request" protocol=icmpv4:8,any dir=in action=allow

#允许任意IP的发送ICMPv4任意类型的报文到系统;

netsh advfirewall firewall add rule name="Allow_Any_Send_ICMPv4" protocol=icmpv4:any,any dir=in action=allow

#拒绝任意IP的发送ICMPv4时间戳请求类型的报文到系统;

netsh advfirewall firewall add rule name="Deny_Any_Send_ICMPv4_ Timestamp" protocol=icmpv4:13,any dir=in action=block

场景8:配置系统防火墙日志记录功能

#配置防火墙日志的名称和位置

netsh advfirewall set allprofiles logging filename E:\firewall_log\firewall.log

#设置最大日志文件大小(KB),取值范围1-32767

netsh advfirewall set allprofiles logging maxfilesize 4096

#记录允许连接的日志

netsh advfirewall set allprofiles logging allowedconnections enable

#记录拒绝连接的日志

netsh advfirewall set allprofiles logging droppedconnections enable

场景9:删除特定的规则;

#删除Test规则

netsh advfirewall firewall delete rule name="Test"

总结

通过命令集可高效地完成Windows系统防火墙的开启、关闭、重置、规则导出备份、规则导入恢复和日志记录等,另外,结合实际地应用场景介绍了系统防火墙规则的创建和删除,希望各位小伙伴有所收获。