【问题标题】:Programatically tell windows firewall to use the Private Network以编程方式告诉 Windows 防火墙使用专用网络
【发布时间】:2014-09-10 16:40:12
【问题描述】:

当我运行我的 c# 控制台应用程序时,Windows 防火墙会弹出请求访问 vshost32(我的应用程序使用 TCP 和 UDP 侦听端口 1234 上的传入消息)。我接受提供的建议(专用网络)。然后控制台应用程序就可以正常工作了。

我不想让用户处理这个,所以我添加了下面的代码。但是,当我在“控制面板”>“防火墙”中调查此操作时,似乎已为“公共网络”而不是专用网络启用了它。就允许我的应用程序运行而言,这是没有用的。

下面的代码有没有调整强制到私网?

INetFwOpenPorts ports3;
INetFwOpenPort port3 = (INetFwOpenPort)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FWOpenPort"));
port3.Port = 1234; 
port3.Name = "vshost32.exe"; 
port3.Enabled = true;

//**UPDATE** added for suggestion in answer below - still doesnt change anything though
port3.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_LOCAL_SUBNET;

Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr3 = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
ports3 = (INetFwOpenPorts)mgr3.LocalPolicy.CurrentProfile.GloballyOpenPorts;
ports3.Add(port3);

【问题讨论】:

    标签: c# windows-firewall


    【解决方案1】:

    将我的answer 转至您之前的@​​987654322@。

    请看以下几行:

    private static int Main (string [] args)
    {
        var application = new NetFwAuthorizedApplication()
        {
            Name = "MyService",
            Enabled = true,
            RemoteAddresses = "*",
            Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
            IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY,
            ProcessImageFileName = "ServiceAssemblyName.dll",
        };
    
        return (FirewallUtilities.AddApplication(application, out exception) ? 0 : -1);
    }
    

    NET_FW_SCOPE_ 枚举具有以下值:

    • NET_FW_SCOPE_ALL = 0,
    • NET_FW_SCOPE_LOCAL_SUBNET = 1,
    • NET_FW_SCOPE_CUSTOM = 2,
    • NET_FW_SCOPE_MAX = 3,

    您可以进一步限制规则的端口、协议以及远程地址。

    更新:

    这是缺少的ReleaseComObject 函数。将其放置在任何命名空间中并删除对 ComUtilities 的引用。

        public static void ReleaseComObject (object o)
        {
            try
            {
                if (o != null)
                {
                    if (Marshal.IsComObject(o))
                    {
                        Marshal.ReleaseComObject(o);
                    }
                }
            }
            finally
            {
                o = null;
            }
        }
    

    这是NetFwAuthorizedApplication 类:

    命名空间 MySolution.Configurator.Firewall { 使用系统; 使用 System.Linq; 使用 NetFwTypeLib;

    public sealed class NetFwAuthorizedApplication:
        INetFwAuthorizedApplication
    {
        public string Name { get; set; }
        public bool Enabled { get; set; }
        public NET_FW_SCOPE_ Scope { get; set; }
        public string RemoteAddresses { get; set; }
        public string ProcessImageFileName { get; set; }
        public NET_FW_IP_VERSION_ IpVersion { get; set; }
    
        public NetFwAuthorizedApplication ()
        {
            this.Name = "";
            this.Enabled = false;
            this.RemoteAddresses = "";
            this.ProcessImageFileName = "";
            this.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
            this.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;
        }
    
        public NetFwAuthorizedApplication (string name, bool enabled, string remoteAddresses, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion, string processImageFileName)
        {
            this.Name = name;
            this.Scope = scope;
            this.Enabled = enabled;
            this.IpVersion = ipVersion;
            this.RemoteAddresses = remoteAddresses;
            this.ProcessImageFileName = processImageFileName;
        }
    
        public static NetFwAuthorizedApplication FromINetFwAuthorizedApplication (INetFwAuthorizedApplication application)
        {
            return (new NetFwAuthorizedApplication(application.Name, application.Enabled, application.RemoteAddresses, application.Scope, application.IpVersion, application.ProcessImageFileName));
        }
    }
    

    }

    【讨论】:

    • 谢谢,我猜 NET_FW_SCOPE_LOCAL_SUBNET 相当于“私有”。
    • 这很奇怪,我在port3.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_LOCAL_SUBNET; 中添加了它(请参阅添加的行)但似乎没有做任何事情。还尝试了NET_FW_SCOPE_ALL
    • 似乎您正在操纵全局开放端口集合。如果您的 Windows 防火墙设置配置为通用默认值,我不确定这是否允许访问特定应用程序。看看那个答案中的 AddApplication 方法。它添加到授权的应用程序集合中。顺便说一句,您可以“按原样”使用这两个类。只需将 Main 方法内容粘贴到您的控制台应用程序中,您就可以开始了。
    • 我确实尝试了您的解决方案,但我无法弄清楚如何解决 ComUtilitiesNetFwAuthorizedApplication。我试图在nuget上找到一个包。老实说,我对那里的代码量有点不知所措,并且对调整它没有信心。任何关于如何跑步的提示都非常感谢。
    • 我的错。 NetFwAuthorizedApplication 只是一个实现INetFwAuthorizedApplication 的自定义类。它存在于该答案中,但我必须替换命名空间。在上面的答案中还包括ComUtilities。不用担心代码量。这两个类是完全独立的。请过几分钟再回来查看答案的更新。
    【解决方案2】:
        INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
            Type.GetTypeFromProgID("HNetCfg.FWRule"));
    
        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
            Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
    
        firewallRule.ApplicationName = "<path to your app>";
    
        firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
        firewallRule.Description = " My Windows Firewall Rule";
        firewallRule.Enabled = true;
        firewallRule.InterfaceTypes = "All";
        firewallRule.Name = "<your rule name>";
    
        // Should really check that rule is not already present before add in
        firewallPolicy.Rules.Add(firewallRule);        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-17
      • 2021-11-06
      • 1970-01-01
      • 2023-03-31
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多