关于C#操作防火墙,阻止程序联网

    //开启服务、开启防火墙
        public void OpenFileWall()
        {
            // 1. 判断当前系统为XP或Win7
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\\Microsoft\\Windows NT\\CurrentVersion");
            var VersionName = rk.GetValue("ProductName").ToString();
            rk.Close();
            RegistryKey key;
            //获取防火墙服务名称
            var ServicerName = "";
            if (VersionName.Contains("XP"))
            {
                ServicerName = "SharedAccess";
                key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\SharedAccess", true);
            }
            else
            {
                ServicerName = "MpsSvc";
                key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\MpsSvc", true);
            }
            // 2. 判断防火墙启动类型是否为禁止,若为禁止设置其为自动
            var StartIndex = key.GetValue("Start").ToString();
            if (StartIndex == "4")
            {
                ProcessStartInfo objProInfo = new ProcessStartInfo();
                objProInfo.FileName = "cmd.exe";
                objProInfo.CreateNoWindow = false;
                objProInfo.WindowStyle = ProcessWindowStyle.Hidden;
                objProInfo.Arguments = "/c sc config " + ServicerName + " start= " + "auto";
                Process.Start(objProInfo);
                //挂起线程1s后启动服务
                System.Threading.Thread.Sleep(1000);
            }
            key.Close();
            // 3. 判断防火墙服务是否启动
            ServiceController sc = new ServiceController(ServicerName);
            if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))
            {
                sc.Start();
                //挂起线程1s后开启防火墙
                System.Threading.Thread.Sleep(1000);
            }
            // 4.开启防火墙
            if (VersionName.Contains("XP"))
            {
                RegistryKey rekey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile", true);
                var Enablefilewall = rekey.GetValue("EnableFirewall").ToString();
                if (Enablefilewall == "0")
                {
                    rekey.SetValue("EnableFirewall", 1);
                }
                rekey.Close();
            }
            else
            {
                INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
                // 启用<高级安全Windows防火墙> - 专有配置文件的防火墙
                firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, true);
                // 启用<高级安全Windows防火墙> - 公用配置文件的防火墙
                firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, true);
            }
        }

        //关闭防火墙
        public void CloseFileWall()
        {
            // 1. 判断当前系统为XP或Win7
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\\Microsoft\\Windows NT\\CurrentVersion");
            var VersionName = rk.GetValue("ProductName").ToString();
            rk.Close();
            // 2.关闭防火墙
            if (VersionName.Contains("XP"))
            {
                RegistryKey rekey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile", true);
                var Enablefilewall = rekey.GetValue("EnableFirewall").ToString();
                if (Enablefilewall == "1")
                {
                    rekey.SetValue("EnableFirewall", 0);
                }
                rekey.Close();
            }
            else
            {
                INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
                // 禁用<高级安全Windows防火墙> - 专有配置文件的防火墙
                firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, false);
                // 禁用<高级安全Windows防火墙> - 公用配置文件的防火墙
                firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, false);
            }
        }

        //当检测不满足条件,阻止所有访问,只允许本程序运行
        public void UnAllowOpenFW()
        {
            //判断系统属于xp还是win7
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\\Microsoft\\Windows NT\\CurrentVersion"))
            {
                var VersionName = rk.GetValue("ProductName").ToString();
                if (VersionName.Contains("XP"))
                {
                    // 1. 创建一个认证程序类的实例
                    INetFwAuthorizedApplication Fwapp = (INetFwAuthorizedApplication)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
                    Fwapp.Name = "360安全浏览器";
                    //Fwapp.Name = Application.ProductName;
                    Fwapp.ProcessImageFileName = "C:\\Documents and Settings\\Administrator\\Application Data\\360se6\\Application\\360se.exe";
                    //Fwapp.ProcessImageFileName = Application.ExecutablePath;
                    //是否启用
                    Fwapp.Enabled = true;
                    // 2. 创建firewall管理类的实例 ,添加程序到防火墙例外
                    INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
                    netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(Fwapp);
                }
                else
                {
                    // 1. 创建实例,阻止所有的出站连接 
                    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
                    //启用或禁用<高级安全Windows防火墙> - 专有配置文件的出站连接
                    firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, NET_FW_ACTION_.NET_FW_ACTION_BLOCK);
                    //启用或禁用<高级安全Windows防火墙> - 公用配置文件的出站连接
                    firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, NET_FW_ACTION_.NET_FW_ACTION_BLOCK);
                    // 2. 创建本程序出站规则,只运行本程序的连接。
                    INetFwRule2 OutBoundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
                    OutBoundRule.Name = "360安全浏览器";
                    //OutBoundRule.Name = Application.ProductName;
                    OutBoundRule.Description = "360安全浏览器";
                    //程序路径
                    OutBoundRule.ApplicationName = "C:\\Users\\Administrator\\AppData\\Roaming\\360se6\\Application\\360se.exe";
                    //OutBoundRule.ApplicationName = Application.ExecutablePath;
                    OutBoundRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
                    OutBoundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
                    OutBoundRule.Enabled = true;
                    // 3.添加出站规则
                    firewallPolicy.Rules.Add(OutBoundRule);
                }
            }
        }

        //检测满足条件,开启所有访问
        public void AllowOpenFW()
        {
            //判断系统属于xp还是win7
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\\Microsoft\\Windows NT\\CurrentVersion"))
            {
                var ApplicationName = "360安全浏览器";
                // var ApplicationName = Application.ProductName;
                var VersionName = rk.GetValue("ProductName").ToString();
                if (VersionName.Contains("XP"))
                {
                    // 创建firewall管理类的实例 ,删除添加程序到防火墙例外
                    INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
                    netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(ApplicationName);
                }
                else
                {
                    // 1. 创建实例,允许所有程序的连接。
                    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
                    //启用或禁用<高级安全Windows防火墙> - 专有配置文件的出站连接
                    firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, NET_FW_ACTION_.NET_FW_ACTION_ALLOW);
                    //启用或禁用<高级安全Windows防火墙> - 公用配置文件的出站连接
                    firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, NET_FW_ACTION_.NET_FW_ACTION_ALLOW);
                    // 2. 删除本程序的出站规则删除规则
                    firewallPolicy.Rules.Remove(ApplicationName);
                }
            }
        }

 

你可能感兴趣的:(关于C#操作防火墙,阻止程序联网)