欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 设置防火墙的创建规则

程序员文章站 2022-06-09 16:41:09
对于某些程序,我们只允许它使用某些特定端口、网络类型或者特定ip类型等信息。这时候,需要使用到防火墙里面的“高级设置”,创建某些特定的入站或者出栈规则,以规避其程序使用允许端口等意外的信息。  下面以...

  对于某些程序,我们只允许它使用某些特定端口、网络类型或者特定ip类型等信息。这时候,需要使用到防火墙里面的“高级设置”,创建某些特定的入站或者出栈规则,以规避其程序使用允许端口等意外的信息。

  下面以创建出站规则为例,编写一条出站规则,规避除允许规则以外的通过防火墙。创建规则时,会使用到接口inetfwrule,其有关介绍参照msdn文档

  创建规则的方法:

/// <summary>
/// 为windowsdefender防火墙添加一条通信端口出站规则
/// </summary>
/// <param name="type">规则类型</param>
/// <param name="rulename">规则名称</param>
/// <param name="apppath">应用程序完整路径</param>
/// <param name="localaddresses">本地地址</param>
/// <param name="localports">本地端口</param>
/// <param name="remoteaddresses">远端地址</param>
/// <param name="remoteports">远端端口</param>
public static bool createoutrule(net_fw_ip_protocol_ type, string rulename, string apppath, string localaddresses = null, string localports = null, string remoteaddresses = null, string remoteports = null)
{
  //创建防火墙策略类的实例
  inetfwpolicy2 policy2 = (inetfwpolicy2)activator.createinstance(type.gettypefromprogid("hnetcfg.fwpolicy2"));
  //检查是否有同名规则
  foreach (inetfwrule item in policy2.rules)
  {
    if (item.name == rulename)
    {
      return true;
    }
  }
  //创建防火墙规则类的实例: 有关该接口的详细介绍:https://docs.microsoft.com/zh-cn/windows/win32/api/netfw/nn-netfw-inetfwrule
  inetfwrule rule = (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 (exception e)
  {
    string error = $"防火墙添加规则出错:{rulename} {e.message}";
    applog.error(error);
    throw new exception(error);
  }
  return true;
}

  创建tcp的出站规则

  使用上述代码,为创建一条tcp类型的出站规则:

/// <summary>
 /// 为windowsdefender防火墙添加一条u3d通信tcp端口出站规则
 /// </summary>
 /// <param name="apppath">应用程序完整路径</param>
 /// <param name="localaddresses">本地地址</param>
 /// <param name="localports">本地端口</param>
 /// <param name="remoteaddresses">远端地址</param>
 /// <param name="remoteports">远端端口</param>
 public static bool createtcpoutrule(string apppath, string localaddresses = null, string localports = null, string remoteaddresses = null, string remoteports = null)
 {
   try
   {
     string rulename = $"{system.io.path.getfilenamewithoutextension(apppath)}tcp";
     createoutrule(net_fw_ip_protocol_.net_fw_ip_protocol_tcp, rulename, apppath, localaddresses, localports, remoteaddresses, remoteports);

   }
   catch (exception e)
   {
     applog.error(e.message);
     throw new exception(e.message);
   }
   return true;
 }

  创建udp的出站规则

  和tcp的出站规则类似,只是传入的类型不一样。使用前面的代码,创建一条udp的出站规则:

/// <summary>
/// 为windowsdefender防火墙添加一条通信udp端口出站规则
/// </summary>
/// <param name="apppath">应用程序完整路径</param>
/// <param name="localaddresses">本地地址</param>
/// <param name="localports">本地端口</param>
/// <param name="remoteaddresses">远端地址</param>
/// <param name="remoteports">远端端口</param>
public static bool createudpoutrule(string apppath, string localaddresses = null, string localports = null, string remoteaddresses = null, string remoteports = null)
{
  try
  {
    string rulename = $"{system.io.path.getfilenamewithoutextension(apppath)}udp";
    createoutrule(net_fw_ip_protocol_.net_fw_ip_protocol_udp, rulename, apppath, localaddresses, localports, remoteaddresses, remoteports);

  }
  catch (exception e)
  {
    applog.error(e.message);
    throw new exception(e.message);
  }
  return true;
}

  删除出入站规则

  注意出入站规则的名称,前面我创建出站规则的时候,使用的“应用程序名+网络类型”创建的,所以删除时,传入的名称也应一样,并且还可以判断网络类型是否一致,一致才删除。

/// <summary>
/// 删除windowsdefender防火墙规则
/// <summary>
/// <param name="apppath">应用程序完整路径</param>
public static bool deleterule(string apppath)
{
  //创建防火墙策略类的实例
  inetfwpolicy2 policy2 = (inetfwpolicy2)activator.createinstance(type.gettypefromprogid("hnetcfg.fwpolicy2"));
  string rulename = system.io.path.getfilenamewithoutextension(apppath);
  try
  {
    //根据规则名称移除规则
    policy2.rules.remove(rulename);
  }
  catch (exception e)
  {
    string error = $"防火墙删除规则出错:{rulename} {e.message}";
    applog.error(error);
    throw new exception(error);
  }
  return true;
}

以上就是c# 设置防火墙的创建规则的详细内容,更多关于c# 防火墙的资料请关注其它相关文章!

相关标签: c# 防火墙