C# 设置防火墙的创建规则
对于某些程序,我们只允许它使用某些特定端口、网络类型或者特定IP类型等信息。这时候,需要使用到防火墙里面的“高级设置”,创建某些特定的入站或者出栈规则,以规避其程序使用允许端口等意外的信息。
下面以创建出站规则为例,编写一条出站规则,规避除允许规则以外的通过防火墙。创建规则时,会使用到接口INetFwRule,其有关介绍参照MSDN文档。
创建规则的方法:
//////为WindowsDefender防火墙添加一条通信端口出站规则 /// ///规则类型 /// 规则名称 /// 应用程序完整路径 /// 本地地址 /// 本地端口 /// 远端地址 /// 远端端口 publicstaticboolCreateOutRule(NET_FW_IP_PROTOCOL_type,stringruleName,stringappPath,stringlocalAddresses=null,stringlocalPorts=null,stringremoteAddresses=null,stringremotePorts=null) { //创建防火墙策略类的实例 INetFwPolicy2policy2=(INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); //检查是否有同名规则 foreach(INetFwRuleiteminpolicy2.Rules) { if(item.Name==ruleName) { returntrue; } } //创建防火墙规则类的实例:有关该接口的详细介绍:https://docs.microsoft.com/zh-cn/windows/win32/api/netfw/nn-netfw-inetfwrule INetFwRulerule=(INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule")); //为规则添加名称 rule.Name=ruleName; //为规则添加描述 rule.Description="禁止程序访问非指定端口"; //选择入站规则还是出站规则,IN为入站,OUT为出站 rule.Direction=NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; //为规则添加协议类型 rule.Protocol=(int)type; //为规则添加应用程序(注意这里是应用程序的绝对路径名) rule.ApplicationName=appPath; //为规则添加本地IP地址 if(!string.IsNullOrEmpty(localAddresses)) { rule.LocalAddresses=localAddresses; } //为规则添加本地端口 if(!string.IsNullOrEmpty(localPorts)) { //需要移除空白字符(不能包含空白字符,下同) rule.LocalPorts=localPorts.Replace("","");//"1-29999,30003-33332,33334-55554,55556-60004,60008-65535"; } //为规则添加远程IP地址 if(!string.IsNullOrEmpty(remoteAddresses)) { rule.RemoteAddresses=remoteAddresses; } //为规则添加远程端口 if(!string.IsNullOrEmpty(remotePorts)) { rule.RemotePorts=remotePorts.Replace("",""); } //设置规则是阻止还是允许(ALLOW=允许,BLOCK=阻止) rule.Action=NET_FW_ACTION_.NET_FW_ACTION_BLOCK; //分组名 rule.Grouping="GroupsName"; rule.InterfaceTypes="All"; //是否启用规则 rule.Enabled=true; try { //添加规则到防火墙策略 policy2.Rules.Add(rule); } catch(Exceptione) { stringerror=$"防火墙添加规则出错:{ruleName}{e.Message}"; AppLog.Error(error); thrownewException(error); } returntrue; }
创建TCP的出站规则
使用上述代码,为创建一条TCP类型的出站规则:
//////为WindowsDefender防火墙添加一条U3D通信TCP端口出站规则 /// ///应用程序完整路径 /// 本地地址 /// 本地端口 /// 远端地址 /// 远端端口 publicstaticboolCreateTCPOutRule(stringappPath,stringlocalAddresses=null,stringlocalPorts=null,stringremoteAddresses=null,stringremotePorts=null) { try { stringruleName=$"{System.IO.Path.GetFileNameWithoutExtension(appPath)}TCP"; CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP,ruleName,appPath,localAddresses,localPorts,remoteAddresses,remotePorts); } catch(Exceptione) { AppLog.Error(e.Message); thrownewException(e.Message); } returntrue; }
创建UDP的出站规则
和TCP的出站规则类似,只是传入的类型不一样。使用前面的代码,创建一条UDP的出站规则:
//////为WindowsDefender防火墙添加一条通信UDP端口出站规则 /// ///应用程序完整路径 /// 本地地址 /// 本地端口 /// 远端地址 /// 远端端口 publicstaticboolCreateUDPOutRule(stringappPath,stringlocalAddresses=null,stringlocalPorts=null,stringremoteAddresses=null,stringremotePorts=null) { try { stringruleName=$"{System.IO.Path.GetFileNameWithoutExtension(appPath)}UDP"; CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP,ruleName,appPath,localAddresses,localPorts,remoteAddresses,remotePorts); } catch(Exceptione) { AppLog.Error(e.Message); thrownewException(e.Message); } returntrue; }
删除出入站规则
注意出入站规则的名称,前面我创建出站规则的时候,使用的“应用程序名+网络类型”创建的,所以删除时,传入的名称也应一样,并且还可以判断网络类型是否一致,一致才删除。
//////删除WindowsDefender防火墙规则 /// /// 应用程序完整路径 publicstaticboolDeleteRule(stringappPath) { //创建防火墙策略类的实例 INetFwPolicy2policy2=(INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); stringruleName=System.IO.Path.GetFileNameWithoutExtension(appPath); try { //根据规则名称移除规则 policy2.Rules.Remove(ruleName); } catch(Exceptione) { stringerror=$"防火墙删除规则出错:{ruleName}{e.Message}"; AppLog.Error(error); thrownewException(error); } returntrue; }
以上就是C#设置防火墙的创建规则的详细内容,更多关于c#防火墙的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。