示例#1
0
        public bool RemoveApplication(string applicationFileNameWithPath)
        {
            DoDisposeCheck();
            DoInitializationCheck();
            if (string.IsNullOrEmpty(applicationFileNameWithPath))
            {
                ThrowHelper.ThrowArgumentNullException("applicationFileNameWithPath");
            }

            bool changed = true;

            if (IsApplicationEnabled(applicationFileNameWithPath))
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications firewallApplications = mFirewallProfile.AuthorizedApplications;
                if (firewallApplications == null)
                {
                    throw new FirewallException("Failed to get authorized applications.");
                }

                try
                {
                    firewallApplications.Remove(applicationFileNameWithPath);
                    changed = true;
                }
                catch (Exception)
                {
                    throw new FirewallException("Failed to delete authorization for the application.");
                }
            }

            return(changed);
        }
示例#2
0
        internal static void OpenPortInFirewall(int port, string description, string processExePath)
        {
            if (!FirewallEnabled)
            {
                return;
            }

            INetFwMgr fwManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));

            //On Longhorn if there is this application is blocked, opening the port
            //won't do any good. Because this happens when you dismiss the firewall
            //dialog we make sure the app is not explicitly blocked in case someone
            //accidentily ran the app under LUA and dismissed the dialog.
            INetFwAuthorizedApplications apps = fwManager.LocalPolicy.CurrentProfile.AuthorizedApplications;
            string currentAppPath             = processExePath;

            foreach (INetFwAuthorizedApplication app in apps)
            {
                if (app.ProcessImageFileName.ToLowerInvariant() == currentAppPath.ToLowerInvariant() && !app.Enabled)
                {
                    apps.Remove(currentAppPath);
                    break;
                }
            }

            //Make sure that the port is Logging Port is open in the Windows Firewall so that we dont get dialogs on this
            //NOTE: this will not work if the user is not an Admin
            INetFwOpenPort portExclusion = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort"));

            portExclusion.Port      = port;
            portExclusion.Enabled   = true;
            portExclusion.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;
            portExclusion.Protocol  = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
            portExclusion.Name      = description;
            portExclusion.Scope     = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;

            fwManager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(portExclusion);

            //HACK: Verify because having the firewall dialog enabled
            //does not result in a UnauthorizedAccess exception, even if the operation fails
            bool            added     = false;
            INetFwOpenPorts openPorts = fwManager.LocalPolicy.CurrentProfile.GloballyOpenPorts;

            foreach (INetFwOpenPort openPort in openPorts)
            {
                if (openPort.Port == port)
                {
                    added = true;
                    break;
                }
            }

            if (!added)
            {
                throw new UnauthorizedAccessException();
            }
        }
示例#3
0
        /// <summary>
        /// Removes the authorized application entry from the firewall collection.
        /// </summary>
        /// <param name="applicationPath">The authorized application path.</param>
        public void CloseFirewallAuthorizedApplication(string applicationPath)
        {
            // Set the current access profile.
            SetProfile();

            // Get the collection of applications
            // with the firewall control.
            INetFwAuthorizedApplications openApplications = fwProfile.AuthorizedApplications;

            // Remove the specified application from the collection.
            openApplications.Remove(applicationPath);
            openApplications = null;
        }
示例#4
0
        protected internal void CloseFirewall(int port, string protocol, string appName)
        {
            INetFwAuthorizedApplications apps = null;
            INetFwOpenPorts ports             = null;

            try
            {
                if (isAppFound(Application.ProductName + " Server") == true)
                {
                    SetProfile();
                    apps = fwProfile.AuthorizedApplications;
                    apps.Remove(Application.ExecutablePath);
                }

                if (isPortFound(port) == true)
                {
                    SetProfile();
                    ports = fwProfile.GloballyOpenPorts;
                    if (protocol.ToLower() == "udp")
                    {
                        ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);
                    }
                    else if (protocol.ToLower() == "tcp")
                    {
                        ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                    }
                    else
                    {
                        ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_ANY);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (apps != null)
                {
                    apps = null;
                }
                if (ports != null)
                {
                    ports = null;
                }
            }
        }
示例#5
0
        protected internal void CloseFirewall()
        {
            INetFwAuthorizedApplications apps = null;
            INetFwOpenPorts ports             = null;

            try
            {
                if (isAppFound(Application.ExecutablePath) == true)
                {
                    SetProfile();
                    apps = fwProfile.AuthorizedApplications;
                    apps.Remove(Application.ExecutablePath);
                }

                /*
                 * if (isPortFound(portsSocket[0]) == true)
                 * {
                 *  SetProfile();
                 *  ports = fwProfile.GloballyOpenPorts;
                 *  ports.Remove(portsSocket[0], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                 * }
                 *
                 * if (isPortFound(portsSocket[1]) == true)
                 * {
                 *  SetProfile();
                 *  ports = fwProfile.GloballyOpenPorts;
                 *  ports.Remove(portsSocket[1], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                 * }
                 */
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (apps != null)
                {
                    apps = null;
                }
                if (ports != null)
                {
                    ports = null;
                }
            }
        }
示例#6
0
        protected internal void CloseFirewall()
        {
            INetFwAuthorizedApplications apps = null;
            INetFwOpenPorts ports             = null;

            try
            {
                string exePath     = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string productName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                if (isAppFound(productName + " Server") == true)
                {
                    SetProfile();
                    apps = fwProfile.AuthorizedApplications;
                    apps.Remove(exePath);
                }

                if (isPortFound(portsSocket[0]) == true)
                {
                    SetProfile();
                    ports = fwProfile.GloballyOpenPorts;
                    ports.Remove(portsSocket[0], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                }

                if (isPortFound(portsSocket[1]) == true)
                {
                    SetProfile();
                    ports = fwProfile.GloballyOpenPorts;
                    ports.Remove(portsSocket[1], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                }
            }
            catch (Exception ex)
            {
                string byteMsg = ex.Message;
            }
            finally
            {
                if (apps != null)
                {
                    apps = null;
                }
                if (ports != null)
                {
                    ports = null;
                }
            }
        }
示例#7
0
        public void CloseFirewall(string applicationName)
        {
            INetFwAuthorizedApplications apps = null;
            INetFwOpenPorts ports             = null;

            try
            {
                if (isAppFound(applicationName) == true)
                {
                    SetProfile();
                    apps = fwProfile.AuthorizedApplications;
                    apps.Remove(applicationName);
                }

                if (isPortFound(portsSocket[0]) == true)
                {
                    SetProfile();
                    ports = fwProfile.GloballyOpenPorts;
                    ports.Remove(portsSocket[0], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                }

                if (isPortFound(portsSocket[1]) == true)
                {
                    SetProfile();
                    ports = fwProfile.GloballyOpenPorts;
                    ports.Remove(portsSocket[1], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (apps != null)
                {
                    apps = null;
                }
                if (ports != null)
                {
                    ports = null;
                }
            }
        }
示例#8
0
 ///
 /// Deletes the firewall ports specified from the windows firewall exclusions list. Also deletes
 /// the given application from the excluded apps.
 ///
 /// The path of the application. Please include the ending \
 /// The name of the executable to close
 /// The ports that you wish to close individually
 public void closeFirewall(string executableFilePath, string applicationName, int[] portsToClose)
 {
     try
     {
         setProfile();
         INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;
         apps.Remove(executableFilePath);
         apps = null;
         INetFwOpenPorts ports = fwProfile.GloballyOpenPorts;
         foreach (int port in portsToClose)
         {
             ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);
         }
         ports = null;
     }
     catch (Exception)
     {
     }
     Console.WriteLine("Firewall : Close Ports");
 }
示例#9
0
        } // openFirewall

        public void FirewallClosePort(int port, string proto)
        {
            INetFwOpenPorts ports             = fwProfile.GloballyOpenPorts;
            INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;

            try
            {
                String imageFilename = Utils.EmuleAdunanzAExeGetPath();
                setProfile();
                apps.Remove(imageFilename);

                if (proto.ToLower() == "udp")
                {
                    ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);
                }
                else if (proto.ToLower() == "tcp")
                {
                    ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                }
                else
                {
                    ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_ANY);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            finally
            {
                logger.Info("Firewall: eliminata regola per porta {0} {1}", port, proto);
                if (apps != null)
                {
                    apps = null;
                }
                if (ports != null)
                {
                    ports = null;
                }
            }
        }
示例#10
0
        public FW_ERROR_CODE RemoveApplication(string strProcessImageFileName)
        {
            if (m_FirewallProfile == null)
            {
                return(FW_ERROR_CODE.FW_ERR_INITIALIZED);
            }
            if (strProcessImageFileName.Length == 0)
            {
                return(FW_ERROR_CODE.FW_ERR_INVALID_ARG);
            }

            bool          bAppEnable = true;
            FW_ERROR_CODE nError     = IsAppEnabled(strProcessImageFileName, ref bAppEnable);

            if (nError != FW_ERROR_CODE.FW_NOERROR)
            {
                return(nError);
            }

            // Only remove the application if it is authorized
            if (bAppEnable)
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications FWApps = m_FirewallProfile.AuthorizedApplications;
                if (FWApps == null)
                {
                    return(FW_ERROR_CODE.FW_ERR_AUTH_APPLICATIONS);
                }

                try
                {
                    FWApps.Remove(strProcessImageFileName);
                }
                catch
                {
                    return(FW_ERROR_CODE.FW_ERR_REMOVE_FROM_COLLECTION);
                }
            }

            return(FW_ERROR_CODE.FW_NOERROR);
        }
示例#11
0
        public FwErrorCode RemoveApplication(string strProcessImageFileName)
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }
            if (strProcessImageFileName.Length == 0)
            {
                return(FwErrorCode.FwErrInvalidArg);
            }

            bool        bAppEnable = true;
            FwErrorCode nError     = IsAppEnabled(strProcessImageFileName, ref bAppEnable);

            if (nError != FwErrorCode.FwNoerror)
            {
                return(nError);
            }

            // Only remove the application if it is authorized
            if (bAppEnable)
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications fwApps = _mFirewallProfile.AuthorizedApplications;
                if (fwApps == null)
                {
                    return(FwErrorCode.FwErrAuthApplications);
                }

                try
                {
                    fwApps.Remove(strProcessImageFileName);
                }
                catch
                {
                    return(FwErrorCode.FwErrRemoveFromCollection);
                }
            }

            return(FwErrorCode.FwNoerror);
        }