Esempio n. 1
1
        private void AddPortToFirewall(string name, int port)
        {
            try
            {
                Type      TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");
                INetFwMgr icfMgr  = (INetFwMgr)Activator.CreateInstance(TicfMgr);

                // add a new port
                Type           TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");
                INetFwOpenPort portClass  = (INetFwOpenPort)Activator.CreateInstance(TportClass);

                // Get the current profile
                INetFwProfile profile = icfMgr.LocalPolicy.CurrentProfile;

                // Set the port properties
                portClass.Scope    = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
                portClass.Enabled  = true;
                portClass.Protocol = NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                // WoWs Info - 8605
                portClass.Name = name;
                portClass.Port = port;

                // Add the port to the ICF Permissions List
                profile.GloballyOpenPorts.Add(portClass);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, Properties.strings.error_title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
            }
        }
Esempio n. 2
0
        protected void setProfile()
        {
            INetFwMgr    fwMgr    = null;
            INetFwPolicy fwPolicy = null;

            try
            {
                fwMgr     = GetInstance("INetFwMgr") as INetFwMgr;
                fwPolicy  = fwMgr.LocalPolicy;
                fwProfile = fwPolicy.CurrentProfile;
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            finally
            {
                logger.Info("Firewall: aggiunto profilo ");
                if (fwMgr != null)
                {
                    fwMgr = null;
                }
                if (fwPolicy != null)
                {
                    fwPolicy = null;
                }
            }
        }
Esempio n. 3
0
        public static void AddGlobalOpenPort(string portClassName, long port)
        {
            INetFwMgr icfMgr  = null;
            Type      TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");

            icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr);

            Console.WriteLine("CurrentProfileType: " + icfMgr.CurrentProfileType);

            INetFwProfile profile = icfMgr.LocalPolicy.CurrentProfile;

            INetFwOpenPort portClass;
            Type           TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");

            portClass = (INetFwOpenPort)Activator.CreateInstance(TportClass);

            // Set the port properties
            portClass.Scope    = Scope.All;
            portClass.Enabled  = true;
            portClass.Name     = portClassName;
            portClass.Port     = port;
            portClass.Protocol = IPProtocol.Tcp;

            // Add the port to the ICF Permissions List
            profile.GloballyOpenPorts.Add(portClass);
        }
Esempio n. 4
0
        public void SetProfile()
        {
            INetFwMgr    fwMgr    = null;
            INetFwPolicy fwPolicy = null;

            try
            {
                fwMgr     = GetInstance("INetFwMgr") as INetFwMgr;
                fwPolicy  = fwMgr.LocalPolicy;
                fwProfile = fwPolicy.CurrentProfile;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fwMgr != null)
                {
                    fwMgr = null;
                }
                if (fwPolicy != null)
                {
                    fwPolicy = null;
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Returns a friendly string format of the policy type.
        /// </summary>
        /// <param name="profile">INetFwProfile object</param>
        /// <returns>string</returns>
        private string GetPolicyType(INetFwProfile profile)
        {
            string policyType = string.Empty;

            // Displays what type of policy the Windows Firewall is controlled by.
            switch (profile.Type)
            {
            case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN:
                policyType = "Domain";
                break;

            case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD:
                policyType = "Standard";
                break;

            case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT:
                policyType = "Current";
                break;

            case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_TYPE_MAX:
                policyType = "Max";
                break;
            }

            return(policyType);
        }
Esempio n. 6
0
        public FwErrorCode Initialize()
        {
            if (_mFirewallProfile != null)
            {
                return(FwErrorCode.FwErrInitialized);
            }

            Type typFwMgr = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
            var  fwMgr    = (INetFwMgr)Activator.CreateInstance(typFwMgr);

            INetFwPolicy fwPolicy = fwMgr.LocalPolicy;

            if (fwPolicy == null)
            {
                return(FwErrorCode.FwErrLocalPolicy);
            }

            try
            {
                _mFirewallProfile = fwPolicy.GetProfileByType(fwMgr.CurrentProfileType);
            }
            catch
            {
                return(FwErrorCode.FwErrProfile);
            }

            return(FwErrorCode.FwNoerror);
        }
Esempio n. 7
0
        ///
        /// Create authorization rule for a specific  port
        ///
        /// To view app permissions from command line use:
        /// "netsh advfirewall firewall show rule name=udptool.vshost.exe"
        ///
        public void GrantPortAuthorization(string applicationFullPath, string usedPort, NET_FW_IP_PROTOCOL_ protocol)
        {
            ValidateFields(applicationFullPath);

            if (usedPort == null)
            {
                throw new ArgumentNullException("usedPort");
            }
            if (!IsFirewallInstalled)
            {
                throw new FirewallHelperException("Cannot grant authorization, firewall is not enabled.");
            }
            if (!AppAuthorizationsAllowed)
            {
                throw new FirewallHelperException("Application exceptions are not allowed.");
            }
            // Other properties like Protocol, IP Version can also be set accordingly
            // Now add this to the GloballyOpenPorts collection
            INetFwProfile profile = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT);

            profile.GloballyOpenPorts.Add(GetPortObj(usedPort, protocol));
            profile = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD);
            profile.GloballyOpenPorts.Add(GetPortObj(usedPort, protocol));
            profile = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN);
            profile.GloballyOpenPorts.Add(GetPortObj(usedPort, protocol));
        }
Esempio n. 8
0
        public FW_ERROR_CODE Initialize()
        {
            if (m_FirewallProfile != null)
            {
                return(FW_ERROR_CODE.FW_ERR_INITIALIZED);
            }

            Type      typFwMgr = null;
            INetFwMgr fwMgr    = null;

            typFwMgr = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
            fwMgr    = (INetFwMgr)Activator.CreateInstance(typFwMgr);
            if (fwMgr == null)
            {
                return(FW_ERROR_CODE.FW_ERR_CREATE_SETTING_MANAGER);
            }
            INetFwPolicy fwPolicy = fwMgr.LocalPolicy;

            if (fwPolicy == null)
            {
                return(FW_ERROR_CODE.FW_ERR_LOCAL_POLICY);
            }

            try
            {
                m_FirewallProfile = fwPolicy.GetProfileByType(fwMgr.CurrentProfileType);
            }
            catch
            {
                return(FW_ERROR_CODE.FW_ERR_PROFILE);
            }

            return(FW_ERROR_CODE.FW_NOERROR);
        }
Esempio n. 9
0
        public bool AddPort(ushort portNumber, String appName)
        {
            bool result = false;

            try
            {
                INetFwMgr       fwMgr     = (INetFwMgr)getInstance("INetFwMgr");
                INetFwPolicy    fwPolicy  = fwMgr.LocalPolicy;
                INetFwProfile   fwProfile = fwPolicy.CurrentProfile;
                INetFwOpenPorts ports     = fwProfile.GloballyOpenPorts;
                INetFwOpenPort  port      = (INetFwOpenPort)getInstance("INetOpenPort");
                port.Port    = portNumber; /* port no */
                port.Name    = appName;    /*name of the application using the port */
                port.Enabled = true;       /* enable the port */

                /*other properties like Protocol, IP Version can also be set accordingly
                 * now add this to the GloballyOpenPorts collection */

                Type      NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
                INetFwMgr mgr          = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
                ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;

                ports.Add(port);
                result = true;
            }
            catch (UnauthorizedAccessException ex) { result = false; }
            return(result);
        }
Esempio n. 10
0
        protected internal void SetProfile()
        {
            INetFwMgr    fwMgr    = null;
            INetFwPolicy fwPolicy = null;

            try
            {
                fwMgr     = GetInstance("INetFwMgr") as INetFwMgr;
                fwPolicy  = fwMgr.LocalPolicy;
                fwProfile = fwPolicy.CurrentProfile;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (fwMgr != null)
                {
                    fwMgr = null;
                }
                if (fwPolicy != null)
                {
                    fwPolicy = null;
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Adds an application with specified parameters to a XP SP2-compatible firewall exception list.
        /// </summary>
        /// <param name="name">Title of the rule</param>
        /// <param name="imageName">Full path of the image</param>
        /// <param name="strLocalSubnet">Space seperated network addresses permitted to access the application
        /// (e.g. "LocalSubnet", "*", "192.168.10.0/255.255.255.0")</param>
        /// <param name="enabled">If the exception rule should be enabled</param>
        /// <remarks>
        /// WARNING: This method does not inform the user that the firewall punchthrough is being added.  Applications
        /// should always inform the user when adding punchthroughs to the firewall, for security reasons.
        /// </remarks>
        public static void AddAppToSP2Firewall(String name, String imageName, String strLocalSubnet, bool enabled)
        {
            // Instantiating the HNetCfg.NetFwMgr object to get "LocalPolicy" and then "CurrentProfile"
            INetFwMgr     fwMgr     = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(INetFwMgrGuid)), true);
            INetFwPolicy  fwPolicy  = fwMgr.LocalPolicy;
            INetFwProfile fwProfile = fwPolicy.CurrentProfile;

            // Checking got skipped since the entry gets update if exist and inserted if not
            // (No check necessary); Check if the entry already exists. "System.IO.FileNotFoundException"
            // will be thrown if entry doesn't exist.
            // fwAA = fwProfile.AuthorizedApplications.Item(imageName);

            // Instantiating the HNetCfg.NetFwAuthorizedApplication object
            INetFwAuthorizedApplication fwAA = (INetFwAuthorizedApplication)Activator.CreateInstance(
                Type.GetTypeFromCLSID(new Guid(INetFwAuthorizedApplicationGuid)), true);

            // Assigning values to the AuthorizedApplication to be added to the firewall permission list.
            // Make this entry Enabled/Disabled
            fwAA.Enabled = enabled;

            // The friendly name for this "Exception" rule
            fwAA.Name = name;

            // Whether only the local subnet can access this application or not
            fwAA.RemoteAddresses = strLocalSubnet;

            // The image name full path
            fwAA.ProcessImageFileName = imageName;

            // Adding AuthorizedApplication to the Exception List
            fwProfile.AuthorizedApplications.Add(fwAA);
        }
Esempio n. 12
0
        ///
        /// create authorization rule for application
        ///
        /// To verify app permissions with cmd prompt use command:
        /// "netsh advfirewall firewall show rule name=udptool.vshost.exe"
        ///
        public void GrantAuthorization(string applicationFullPath)
        {
            ValidateFields(applicationFullPath);

            if (!IsFirewallInstalled)
            {
                throw new FirewallHelperException("Cannot grant authorization: Firewall is not installed.");
            }
            if (!AppAuthorizationsAllowed)
            {
                throw new FirewallHelperException("Application exemptions are not allowed.");
            }

            if (!HasAuthorization(applicationFullPath))
            {
                INetFwProfile profileDomain = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD);
                profileDomain.AuthorizedApplications.Add(GetAuthAppObj(applicationFullPath, appName));

                profileDomain = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN);
                profileDomain.AuthorizedApplications.Add(GetAuthAppObj(applicationFullPath, appName));

                profileDomain = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT);
                profileDomain.AuthorizedApplications.Add(GetAuthAppObj(applicationFullPath, appName));
            }
        }
Esempio n. 13
0
 public Firewall()
 {
     policyManager = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
     manager       = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
     profile       = manager.LocalPolicy.CurrentProfile;
     openPorts     = profile.GloballyOpenPorts;
 }
Esempio n. 14
0
        ///
        /// Remove app authorization rules.
        ///
        public void RemoveAuthorization(string applicationFullPath)
        {
            ValidateFields(applicationFullPath);

            if (!IsFirewallInstalled)
            {
                throw new FirewallHelperException("Cannot remove authorization: Firewall is not installed.");
            }

            if (HasAuthorization(applicationFullPath))
            {
                foreach (string appName in GetAuthorizedAppPaths())
                {
                    if (appName.ToLower() == applicationFullPath.ToLower())
                    {
                        // Remove Authorizations for this application
                        INetFwProfile profileDomain = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD);
                        profileDomain.AuthorizedApplications.Remove(applicationFullPath);

                        profileDomain = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN);
                        profileDomain.AuthorizedApplications.Remove(applicationFullPath);

                        profileDomain = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT);
                        profileDomain.AuthorizedApplications.Remove(applicationFullPath);
                    }
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Add port to windows firewall
        /// Reference: https://social.msdn.microsoft.com/Forums/vstudio/en-US/a3e390d1-4383-4f23-bad9-b725bef33499/add-firewall-rule-programatically?forum=wcf
        /// </summary>
        static void AddPortToFirewall(string name, int port)
        {
            try
            {
                Type      TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");
                INetFwMgr icfMgr  = (INetFwMgr)Activator.CreateInstance(TicfMgr);

                // add a new port
                Type           TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");
                INetFwOpenPort portClass  = (INetFwOpenPort)Activator.CreateInstance(TportClass);

                // Get the current profile
                INetFwProfile profile = icfMgr.LocalPolicy.CurrentProfile;

                // Set the port properties
                portClass.Scope    = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
                portClass.Enabled  = true;
                portClass.Protocol = NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                // WoWs Info - 8605
                portClass.Name = name;
                portClass.Port = port;

                // Add the port to the ICF Permissions List
                profile.GloballyOpenPorts.Add(portClass);
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to add port to firewall. This is the error message.\n");
                Console.WriteLine(e.Message);
                Console.WriteLine("\nPlease feel free to open an issue to discuss this it with me.");
                Process.Start("https://github.com/HenryQuan/winserver");
            }
        }
Esempio n. 16
0
 private static void EnsureSetup()
 {
     if (_profile is null)
     {
         _profile = (GetInstance("INetFwMgr") as INetFwMgr)?.LocalPolicy?.CurrentProfile;
     }
 }
Esempio n. 17
0
        private static INetFwOpenPorts GetOpenPorts()
        {
            INetFwMgr       manager   = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
            INetFwProfile   profile   = manager.LocalPolicy.CurrentProfile;
            INetFwOpenPorts openPorts = profile.GloballyOpenPorts;

            return(openPorts);
        }
Esempio n. 18
0
 private bool _IsAppAuthorized(INetFwProfile profile)
 {
     if (!profile.FirewallEnabled)
     {
         return(true);
     }
     return(profile.AuthorizedApplications.Cast <INetFwAuthorizedApplication>().Any(a => a.ProcessImageFileName == _ExePath));
 }
Esempio n. 19
0
        /// <summary>
        /// Removes an application from an XP SP2-compatible firewall exception list.
        /// </summary>
        /// <param name="imageName">Full name of image to be removed from FW exception list</param>
        /// <remarks>
        /// WARNING: This method does not inform the user that the firewall punchthrough is being added.  Applications
        /// should always inform the user when adding punchthroughs to the firewall, for security reasons.
        /// </remarks>
        public static void RemoveAppFromSP2Firewall(String imageName)
        {
            // Instantiating the HNetCfg.NetFwMgr object to get "LocalPolicy" and then "CurrentProfile"
            INetFwMgr     fwMgr     = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(INetFwMgrGuid)), true);
            INetFwPolicy  fwPolicy  = fwMgr.LocalPolicy;
            INetFwProfile fwProfile = fwPolicy.CurrentProfile;

            // Remove application from exception rule list
            fwProfile.AuthorizedApplications.Remove(imageName);
        }
Esempio n. 20
0
        protected void setProfile()
        {
            // Access INetFwMgr
            INetFwMgr    fwMgr    = (INetFwMgr)getInstance("INetFwMgr");
            INetFwPolicy fwPolicy = fwMgr.LocalPolicy;

            fwProfile = fwPolicy.CurrentProfile;
            fwMgr     = null;
            fwPolicy  = null;
        }
Esempio n. 21
0
        public static void RemovePortExceptionFromSP2Firewall(int port, NET_FW_IP_PROTOCOL_ protocol)
        {
            // Instantiating the HNetCfg.NetFwMgr object to get "LocalPolicy" and then "CurrentProfile"
            INetFwMgr     fwMgr     = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(INetFwMgrGuid)), true);
            INetFwPolicy  fwPolicy  = fwMgr.LocalPolicy;
            INetFwProfile fwProfile = fwPolicy.CurrentProfile;

            // Remove application from exception rule list
            fwProfile.GloballyOpenPorts.Remove(port, protocol);
        }
Esempio n. 22
0
        private bool _IsPortOpen(INetFwProfile profile)
        {
            NET_FW_IP_PROTOCOL_ protocol = _GetProtocol();

            if (!profile.FirewallEnabled)
            {
                return(true);
            }
            return(profile.GloballyOpenPorts.Cast <INetFwOpenPort>().Any(p => p.Protocol == protocol && p.Port == _Port));
        }
Esempio n. 23
0
        /// <summary>
        /// Set the current managment profile.
        /// </summary>
        private void SetProfile()
        {
            // Access INetFwMgr.
            INetFwMgr    fwMgr    = (INetFwMgr)GetInstance("INetFwMgr");
            INetFwPolicy fwPolicy = fwMgr.LocalPolicy;

            // Get the current application profile.
            fwProfile = fwPolicy.CurrentProfile;
            fwMgr     = null;
            fwPolicy  = null;
        }
Esempio n. 24
0
        private void _AddAppToFirewall(INetFwProfile profile)
        {
            INetFwAuthorizedApplication application = (INetFwAuthorizedApplication)Activator.CreateInstance(
                Type.GetTypeFromCLSID(new Guid(CLSID_NetAuthApp)));

            application.Name = _RuleName;
            application.ProcessImageFileName = _ExePath;
            application.Enabled = true;
            application.Scope   = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
            profile.AuthorizedApplications.Add(application);
        }
        private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            INetFwMgr manager = GetFirewallManager();

            bool isFirewallEnabled =
                manager.LocalPolicy.CurrentProfile.FirewallEnabled;

            INetFwProfile profileForDeskTop = manager.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT);

            // Identify what profile group trying to connect
            profileForDeskTop.FirewallEnabled = true;
            // the above will enable firewall for the connected domain / current profile.

            if (isFirewallEnabled == false)
            {
                manager.LocalPolicy.CurrentProfile.FirewallEnabled = true;
            }
            // the above will enable the firewall if its turned off by hacker
            // Disable to access internet make auth disabled if its turned on by hacker


            AuthorizeApplication("Notepad", @"C:\Windows\Notepad.exe",
                                 NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
                                 NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);

            AuthorizeApplication("WinWord", @"C:\Program Files\Microsoft Office 15\root\office15\winword.exe",
                                 NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
                                 NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);


            // Stop any file to make
            try
            {
                FileInfo info = null;
                foreach (string path in Directory.EnumerateFiles(@"C:\Users\rqadri\AppData\Local\Temp\"))
                {
                    info = new FileInfo(path);
                    if (info.Extension == "tmp" || info.Extension == "tr0" || info.Extension == "4g3")
                    {
                        AuthorizeApplication("ANY", path, NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
                                             NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);
                        File.Delete(path);
                    }
                }
            }
            catch (Exception excp)
            {
            }



            BlockAllOutgoingConnections();
        }
Esempio n. 26
0
        private void _AddPortToFirewall(INetFwProfile profile)
        {
            INetFwOpenPort openPort = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(CLSID_NetOpenPort)));

            openPort.Enabled  = true;
            openPort.Port     = _Port;
            openPort.Protocol = _GetProtocol();
            openPort.Name     = _RuleName + "(" + _Port + ")";
            openPort.Scope    = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;

            profile.GloballyOpenPorts.Add(openPort);
        }
Esempio n. 27
0
        public static void AddPortExceptionToSP2Firewall(string name, int port, NET_FW_IP_PROTOCOL_ protocol)
        {
            // Instantiating the HNetCfg.NetFwMgr object to get "LocalPolicy" and then "CurrentProfile"
            INetFwMgr     fwMgr     = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(INetFwMgrGuid)), true);
            INetFwPolicy  fwPolicy  = fwMgr.LocalPolicy;
            INetFwProfile fwProfile = fwPolicy.CurrentProfile;

            INetFwOpenPort fwOpenPort = (INetFwOpenPort)Activator.CreateInstance(
                Type.GetTypeFromCLSID(new Guid(INetFwOpenPortGuid)), true);

            fwOpenPort.Name     = name;
            fwOpenPort.Port     = port;
            fwOpenPort.Protocol = protocol;
            fwOpenPort.Enabled  = true;
            fwProfile.GloballyOpenPorts.Add(fwOpenPort);
        }
Esempio n. 28
0
 private void Dispose(bool disposing)
 {
     if (!mDisposed)
     {
         if (mFirewallProfile != null)
         {
             try
             {
                 Marshal.ReleaseComObject(mFirewallProfile);
             }
             catch (Exception) { }
             mFirewallProfile = null;
         }
         mDisposed = true;
     }
 }
Esempio n. 29
0
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            try
            {
                INetFwMgr     mgr     = (INetFwMgr) new NetFwMgr();
                INetFwProfile profile = mgr.LocalPolicy.CurrentProfile;
                string        winDir  = System.Environment.GetFolderPath(Environment.SpecialFolder.System);
                winDir = winDir.Substring(0, winDir.LastIndexOf(Path.DirectorySeparatorChar));

                INetFwAuthorizedApplication fwApp = (INetFwAuthorizedApplication) new NetFwAuthorizedApplication();
                fwApp.Name = "Media Center Extensibility Host";
                fwApp.ProcessImageFileName = winDir + @"\ehome\ehexthost.exe";
                fwApp.Enabled         = true;
                fwApp.IpVersion       = IPVersion.IPAny;
                fwApp.Scope           = Scope.Subnet;
                fwApp.RemoteAddresses = "*";

                profile.AuthorizedApplications.Add(fwApp);
            }
            catch (Exception ex)
            {
                throw new InstallException("Error during firewall registration of ehexthost.exe", ex);
            }

            try
            {
                INetFwMgr     mgr     = (INetFwMgr) new NetFwMgr();
                INetFwProfile profile = mgr.LocalPolicy.CurrentProfile;
                string        winDir  = System.Environment.GetFolderPath(Environment.SpecialFolder.System);
                winDir = winDir.Substring(0, winDir.LastIndexOf(Path.DirectorySeparatorChar));

                INetFwAuthorizedApplication fwApp = (INetFwAuthorizedApplication) new NetFwAuthorizedApplication();
                fwApp.Name = "Media Center Media Status Aggregator Service";
                fwApp.ProcessImageFileName = winDir + @"\ehome\ehmsas.exe";
                fwApp.Enabled         = true;
                fwApp.IpVersion       = IPVersion.IPAny;
                fwApp.Scope           = Scope.Subnet;
                fwApp.RemoteAddresses = "*";

                profile.AuthorizedApplications.Add(fwApp);
            }
            catch (Exception ex)
            {
                throw new InstallException("Error during firewall registration of ehmsas.exe", ex);
            }
        }
Esempio n. 30
0
        ///
        /// Remove port authorization rules.
        ///
        public void RemovePortAuthorization(string applicationFullPath, string usedPort, NET_FW_IP_PROTOCOL_ protocol)
        {
            ValidateFields(applicationFullPath);

            if (usedPort == null)
            {
                throw new ArgumentNullException("usedPort");
            }

            int port = Int32.Parse(usedPort);

            INetFwProfile profile = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT);

            profile.GloballyOpenPorts.Remove(port, protocol);
            profile = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD);
            profile.GloballyOpenPorts.Remove(port, protocol);
            profile = mgr.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN);
            profile.GloballyOpenPorts.Remove(port, protocol);
        }
Esempio n. 31
0
        public FwErrorCode Initialize()
        {
            if (_mFirewallProfile != null)
                return FwErrorCode.FwErrInitialized;

            Type typFwMgr = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
            var fwMgr = (INetFwMgr)Activator.CreateInstance(typFwMgr);

            INetFwPolicy fwPolicy = fwMgr.LocalPolicy;
            if (fwPolicy == null)
                return FwErrorCode.FwErrLocalPolicy;

            try
            {
                _mFirewallProfile = fwPolicy.GetProfileByType(fwMgr.CurrentProfileType);
            }
            catch
            {
                return FwErrorCode.FwErrProfile;
            }

            return FwErrorCode.FwNoerror;
        }
 public FW_ERROR_CODE Uninitialize()
 {
     m_FirewallProfile = null;
     return FW_ERROR_CODE.FW_NOERROR;
 }
Esempio n. 33
0
        public FW_ERROR_CODE Initialize()
        {
            if (m_FirewallProfile != null)
                return FW_ERROR_CODE.FW_ERR_INITIALIZED;

            Type typFwMgr = null;
            INetFwMgr fwMgr = null;

            typFwMgr = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
            fwMgr = (INetFwMgr)Activator.CreateInstance(typFwMgr);
            if (fwMgr == null)
                return FW_ERROR_CODE.FW_ERR_CREATE_SETTING_MANAGER;
            INetFwPolicy fwPolicy = fwMgr.LocalPolicy;
            if (fwPolicy == null)
                return FW_ERROR_CODE.FW_ERR_LOCAL_POLICY;

            try
            {
                m_FirewallProfile = fwPolicy.GetProfileByType(fwMgr.CurrentProfileType);
            }
            catch
            {
                return FW_ERROR_CODE.FW_ERR_PROFILE;
            }

            return FW_ERROR_CODE.FW_NOERROR;
        }
Esempio n. 34
0
 protected void setProfile()
 {
     INetFwMgr fwMgr = null;
     INetFwPolicy fwPolicy = null;
     try
     {
         fwMgr = GetInstance("INetFwMgr") as INetFwMgr;
         fwPolicy = fwMgr.LocalPolicy;
         fwProfile = fwPolicy.CurrentProfile;
     }
     catch (Exception ex)
     {
         logger.Error(ex.Message);
     }
     finally
     {
         logger.Info("Firewall: aggiunto profilo ");
         if (fwMgr != null) fwMgr = null;
         if (fwPolicy != null) fwPolicy = null;
     }
 }
Esempio n. 35
0
 private void SetProfile()
 {
     var fwMgr = (INetFwMgr)GetInstance("INetFwMgr");
     var fwPolicy = fwMgr.LocalPolicy;
     _fwProfile = fwPolicy.CurrentProfile;
 }
Esempio n. 36
0
 protected void setProfile()
 {
     // Access INetFwMgr
     INetFwMgr fwMgr = (INetFwMgr)getInstance("INetFwMgr");
     INetFwPolicy fwPolicy = fwMgr.LocalPolicy;
     fwProfile = fwPolicy.CurrentProfile;
     fwMgr = null;
     fwPolicy = null;
 }
Esempio n. 37
0
        /// <summary>
        /// Returns a friendly string format of the policy type.
        /// </summary>
        /// <param name="profile">INetFwProfile object</param>
        /// <returns>string</returns>
        private string GetPolicyType(INetFwProfile profile)
        {
            string policyType = string.Empty;

            // Displays what type of policy the Windows Firewall is controlled by.
            switch (profile.Type)
            {
                case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN:
                    policyType = "Domain";
                    break;

                case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD:
                    policyType = "Standard";
                    break;

                case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT:
                    policyType = "Current";
                    break;

                case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_TYPE_MAX:
                    policyType = "Max";
                    break;

            }

            return policyType;
        }
Esempio n. 38
0
 protected internal void SetProfile()
 {
     INetFwMgr fwMgr = null;
     INetFwPolicy fwPolicy = null;
     try
     {
         fwMgr = GetInstance("INetFwMgr") as INetFwMgr;
         fwPolicy = fwMgr.LocalPolicy;
         fwProfile = fwPolicy.CurrentProfile;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     finally
     {
         if (fwMgr != null) fwMgr = null;
         if (fwPolicy != null) fwPolicy = null;
     }
 }
Esempio n. 39
0
        private void SetFirewallProfile()
        {
            INetFwMgr firewallManager = null;
            INetFwPolicy firewallPolicy = null;

            try
            {
                firewallManager = GetFirewallInstance("INetFwMgr") as INetFwMgr;
                firewallPolicy = firewallManager.LocalPolicy;
                fireWallProfile = firewallPolicy.CurrentProfile;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                if (firewallManager != null) firewallManager = null;
                if (firewallPolicy != null) firewallPolicy = null;
            }
        }
Esempio n. 40
0
        //Непосредственно добавляет правило для приложения в список общих правил.
        private void AddToPermissionsList(string name, string imageName, 
            bool enabled, INetFwProfile profile)
        {
            // Add the application to the ICF Permissions List

            // Get the type of HNetCfg.FwMgr, or null if an error occurred
            Type authAppType = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication", false);

            // Assume failed.
            INetFwAuthorizedApplication app = null;

            if (authAppType != null)
            {
                try
                {
                    app = (INetFwAuthorizedApplication)Activator.CreateInstance(authAppType);
                }
                // In all other circumnstances, appInfo is null.
                catch (Exception)
                {
                    throw new Exception("Could not grant authorization: " +
                        "can't create INetFwAuthorizedApplication instance.");
                }
            }

            app.Enabled = enabled;
            app.Name = name;
            app.ProcessImageFileName = imageName;
            profile.AuthorizedApplications.Add(app);
        }
Esempio n. 41
0
 //Непосредственно удаляет правило из списка.
 private void RemoveFromPermissionsList(string imageName,
     INetFwProfile profile)
 {
     // Remove the application from the ICF Permissions List
     profile.AuthorizedApplications.Remove(imageName);
 }
Esempio n. 42
0
 public FwErrorCode Uninitialize()
 {
     _mFirewallProfile = null;
     return FwErrorCode.FwNoerror;
 }
Esempio n. 43
0
 protected void setProfile()
 {
     // Access INetFwMgr
     INetFwMgr fwMgr = (INetFwMgr)GetInstance(MANAGER);
     INetFwPolicy fwPolicy = fwMgr.LocalPolicy;
     fwProfile = fwPolicy.CurrentProfile;
 }