通过win7 FirewallAPI向私有和公共网络添加应用程序防火墙规则

本文关键字:网络 添加 应用程序 规则 防火墙 FirewallAPI win7 通过 | 更新日期: 2023-09-27 18:03:57

简单介绍一下:我想为私有网络和公共网络添加一个程序防火墙访问规则。

我过去经常用这个-"netsh firewall add allowedprogram program= "Path.." name=AppName启用范围=所有配置文件=当前"

但是现在我想使用COM对象稍微自动化一下这个过程。找到这段闪亮的代码- http://web.archive.org/web/20070707110141/http://www.dot.net.nz/Default.aspx?tabid=42&mid=404&ctl=Details&ItemID=8

实现类后,我一直试图使用-FirewallHelper.Instance.GrantAuthorization(@"路径…"、"浏览器名称",NET_FW_SCOPE_.NET_FW_SCOPE_ALL NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);

我面临的问题是,GrantAuthorization方法只会为公共或私有网络添加一条规则,而我的旧netsh命令会为每个网络添加2条规则。

这些命令实际上看起来非常相似,所以对我来说有点模糊。

所以…如何添加两个网络规则?

肖恩

通过win7 FirewallAPI向私有和公共网络添加应用程序防火墙规则

我的答案来自David的答案,但更详细。并修复关于设置Localports的问题。在配置"Localports"之前,需要先配置"Protocol"。详情如下:

首先,您需要导入引用FirewallAPI.dll。在"C:'Windows'System32'FirewallAPI.dll"中然后:

using NetFwTypeLib;

和插入代码到:

        Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
        INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
        var currentProfiles = fwPolicy2.CurrentProfileTypes;
        // Let's create a new rule
        INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
        inboundRule.Enabled = true;
        //Allow through firewall
        inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
        //Using protocol TCP
        inboundRule.Protocol = 6; // TCP
        //Port 81
        inboundRule.LocalPorts = "81";
        //Name of rule
        inboundRule.Name = "MyRule";
        // ...//
        inboundRule.Profiles = currentProfiles;
        // Now add the rule
        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
        firewallPolicy.Rules.Add(inboundRule);

我认为你最好的办法是与带有高级安全API的Windows防火墙对话。

快速搜索"c# INetFwRule2"将向您展示如何注册或更新防火墙规则的许多示例。

为了添加公共和私有策略,我使用了类似

的内容
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
var currentProfiles = fwPolicy2.CurrentProfileTypes;
// Let's create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
inboundRule.Enabled = true;
inboundRule.LocalPorts = "1234";
inboundRule.Protocol = 6; // TCP
// ...
inboundRule.Profiles = currentProfiles;
// Now add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);

这个页面没有说这个问题已经被回答过了,所以为了以防万一,为了将来的使用,我会回答这个问题。

首先,导入参考FirewallAPI.dll,位于"C:'Windows'System32'FirewallAPI.dll",然后添加using指令

using NetFwTypeLib;

inboundRule.Profiles属性似乎被归类为一组具有以下值的标志(该属性的类型是int,因此我创建了一个enum):

public enum FirewallProfiles
{
    Domain = 1,
    Private = 2,
    Public = 4
}
因此,使用这些代码,我们可以将profile更改为以下内容:
// Create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
// Enable the rule
inboundRule.Enabled = true;
// Allow through firewall
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
// Using protocol TCP
inboundRule.Protocol = 6; // TCP
// Set port number
inboundRule.LocalPorts = "1234";
// Name of rule
inboundRule.Name = "Name Of Firewall Rule";
// Set profiles
inboundRule.Profiles = (int)(FirewallProfiles.Private | FirewallProfiles.Public);
// Add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);

或者您可以将inboundRule.Profiles更改为int值

两个音符:

1:如果您没有在管理权限下运行此代码,

firewallPolicty.Rules.Add(inboundRule);

将抛出异常。

2: inboundRule.Profiles取值必须在1 ~ 7之间。否则,它将抛出一个异常

以防你们想要出站规则:

inboundRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;