Esempio n. 1
0
        public Console()
        {
            InitializeComponent();
            config = new Configuration(this);
            oVPNWin = new OVPN(this);
            consoleTextBox.ReadOnly = true;
            consoleTB = consoleTextBox;
            connectedLabel = label2;
            ipLabel = label8;
            ipLabel.Text = "";

            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(Console_KeyDown);
            this.connectedLabel.TextChanged += new EventHandler(ConnectedChanged);

            guardRules = new List<GuardRule>();

            ReadConfig();

            listBox2.DataSource = guardRules;
            defaultFWRules = GetFWRules();

            if (defaultPort != 0)
                portBox.Text = defaultPort.ToString();

            FindAndListService();
            notifyIcon1.Visible = false;
        }
Esempio n. 2
0
        public bool IsApplicationEnabled(string applicationFileNameWithPath)
        {
            DoDisposeCheck();
            DoInitializationCheck();
            if (string.IsNullOrEmpty(applicationFileNameWithPath))
            {
                ThrowHelper.ThrowArgumentNullException("applicationFileNameWithPath");
            }

            INetFwAuthorizedApplications firewallApps = mFirewallProfile.AuthorizedApplications;

            if (firewallApps == null)
            {
                throw new FirewallException("Failed to get authorized applications.");
            }

            bool result = false;

            try
            {
                INetFwAuthorizedApplication firewallApp = firewallApps.Item(applicationFileNameWithPath);
                // If FAILED, the application is not in the collection list
                if (firewallApp != null)
                {
                    result = firewallApp.Enabled;
                }
            }
            catch (Exception)
            {
            }

            return(result);
        }
        public Console()
        {
            InitializeComponent();
            config  = new Configuration(this);
            oVPNWin = new OVPN(this);
            consoleTextBox.ReadOnly = true;
            consoleTB      = consoleTextBox;
            connectedLabel = label2;
            ipLabel        = label8;
            ipLabel.Text   = "";


            this.KeyPreview = true;
            this.KeyDown   += new KeyEventHandler(Console_KeyDown);
            this.connectedLabel.TextChanged += new EventHandler(ConnectedChanged);

            guardRules = new List <GuardRule>();

            ReadConfig();

            listBox2.DataSource = guardRules;
            defaultFWRules      = GetFWRules();

            if (defaultPort != 0)
            {
                portBox.Text = defaultPort.ToString();
            }

            FindAndListService();
            notifyIcon1.Visible = false;
        }
Esempio n. 4
0
        protected internal void OpenFirewall()
        {
            INetFwAuthorizedApplications authApps = null;
            INetFwAuthorizedApplication  authApp  = null;
            INetFwOpenPorts openPorts             = null;
            INetFwOpenPort  openPort = null;

            try {
                if (isAppFound(Application.ProductName + " Server") == false)
                {
                    SetProfile();
                    authApps     = fwProfile.AuthorizedApplications;
                    authApp      = GetInstance("INetAuthApp") as INetFwAuthorizedApplication;
                    authApp.Name = Application.ProductName + " Server";
                    authApp.ProcessImageFileName = Application.ExecutablePath;
                    authApps.Add(authApp);
                }
                if (isPortFound(portsSocket[0]) == false)
                {
                    SetProfile();
                    openPorts         = fwProfile.GloballyOpenPorts;
                    openPort          = GetInstance("INetOpenPort") as INetFwOpenPort;
                    openPort.Port     = portsSocket[0];
                    openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                    openPort.Name     = portsName[0];
                    openPorts.Add(openPort);
                }
                if (isPortFound(portsSocket[1]) == false)
                {
                    SetProfile();
                    openPorts         = fwProfile.GloballyOpenPorts;
                    openPort          = GetInstance("INetOpenPort") as INetFwOpenPort;
                    openPort.Port     = portsSocket[1];
                    openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                    openPort.Name     = portsName[1];
                    openPorts.Add(openPort);
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            finally {
                if (authApps != null)
                {
                    authApps = null;
                }
                if (authApp != null)
                {
                    authApp = null;
                }
                if (openPorts != null)
                {
                    openPorts = null;
                }
                if (openPort != null)
                {
                    openPort = null;
                }
            }
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        ///
        /// Opens the given ports on windows firewall. Also opens entirely the given application on the firewall.
        /// Please remember that you need administrative rights to use this function.
        ///
        /// The path of the application. Please include the ending \
        /// The name of the executable to open
        /// The ports that you wish to open individually
        public void openFirewall(string executableFilePath, string applicationName, int[] portsToOpen)
        {
            ///////////// Firewall Authorize Application ////////////
            try
            {
                setProfile();
                INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;
                INetFwAuthorizedApplication  app  = (INetFwAuthorizedApplication)getInstance("INetAuthApp");
                app.Name = applicationName;
                app.ProcessImageFileName = executableFilePath;
                apps.Add(app);
                apps = null;

                //////////////// Open Needed Ports /////////////////
                INetFwOpenPorts openports = fwProfile.GloballyOpenPorts;
                foreach (int port in portsToOpen)
                {
                    INetFwOpenPort openport = (INetFwOpenPort)getInstance("INetOpenPort");
                    openport.Port     = port;
                    openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
                    openport.Name     = applicationName + " Operation Port (" + port.ToString() + ")";
                    openports.Add(openport);
                }
                openports = null;
            }
            catch (Exception)
            {
                // throw;
            }
            Console.WriteLine("Firewall : Open Ports");
        }
Esempio n. 7
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();
            }
        }
Esempio n. 8
0
        public FW_ERROR_CODE AddApplication(string strProcessImageFileName, string strRegisterName)
        {
            if (m_FirewallProfile == null)
            {
                return(FW_ERROR_CODE.FW_ERR_INITIALIZED);
            }

            if (strProcessImageFileName.Length == 0 || strRegisterName.Length == 0)
            {
                return(FW_ERROR_CODE.FW_ERR_INVALID_ARG);
            }

            // First of all, check the application is already authorized;
            bool          bAppEnable = true;
            FW_ERROR_CODE nError     = IsAppEnabled(strProcessImageFileName, ref bAppEnable);

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

            // Only add the application if it isn't authorized
            if (bAppEnable == false)
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications FWApps = m_FirewallProfile.AuthorizedApplications;

                if (FWApps == null)
                {
                    return(FW_ERROR_CODE.FW_ERR_AUTH_APPLICATIONS);
                }

                // Create an instance of an authorized application
                Type typeFwApp = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));

                var FWApp = (INetFwAuthorizedApplication)Activator.CreateInstance(typeFwApp);
                if (FWApp == null)
                {
                    return(FW_ERROR_CODE.FW_ERR_CREATE_APP_INSTANCE);
                }

                // Set the process image file name
                FWApp.ProcessImageFileName = strProcessImageFileName;
                FWApp.Name = strRegisterName;

                try
                {
                    FWApps.Add(FWApp);
                }
                catch
                {
                    return(FW_ERROR_CODE.FW_ERR_ADD_TO_COLLECTION);
                }
            }

            return(FW_ERROR_CODE.FW_NOERROR);
        }
        private void PrintCurrentFWRules()
        {
            INetFwAuthorizedApplications apps = GetFWRules();

            foreach (INetFwAuthorizedApplication app in apps)
            {
                consoleTB.Text += app.Name + "\r\n";
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Apre tutte le porte sia TCP che UDP per un determinato Exe
        /// </summary>
        /// <param name="appName">Nome che comparira' nella regola del Firewall</param>
        /// <param name="exePath">Percorso alleseguibile da sbloccare sul Firewall</param>
        protected internal void OpenFirewallExe(string appName = "", string exePath = "")
        {
            INetFwAuthorizedApplications authApps = null;
            INetFwAuthorizedApplication  authApp  = null;
            INetFwOpenPorts openPorts             = null;
            INetFwOpenPort  openPort = null;

            if (appName == "")
            {
                appName = Application.ProductName;
            }
            if (exePath == "")
            {
                exePath = Application.ExecutablePath;
            }
            try
            {
                if (isAppFound(Application.ProductName) == false)
                {
                    SetProfile();
                    authApps                     = fwProfile.AuthorizedApplications;
                    authApp                      = GetInstance("INetAuthApp") as INetFwAuthorizedApplication;
                    authApp.Name                 = appName;
                    authApp.Scope                = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
                    authApp.IpVersion            = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;
                    authApp.ProcessImageFileName = exePath;
                    authApps.Add(authApp);

                    logger.Info("Aggiunta regola generale per " + appName + ": " + exePath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (authApps != null)
                {
                    authApps = null;
                }
                if (authApp != null)
                {
                    authApp = null;
                }
                if (openPorts != null)
                {
                    openPorts = null;
                }
                if (openPort != null)
                {
                    openPort = null;
                }
            }
        }
Esempio n. 11
0
        public void FirewallOpenPort(int port, string protocol, string appName)
        {
            ///////////// Firewall Authorize Application ////////////
            String imageFilename = Utils.EmuleAdunanzAExeGetPath();

            setProfile();
            INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;
            INetFwAuthorizedApplication  app  = (INetFwAuthorizedApplication)GetInstance("INetAuthApp");

            app.Name = appName;
            app.ProcessImageFileName = imageFilename;
            apps.Add(app);

            //////////////// Open Needed Ports /////////////////
            INetFwOpenPorts openports = fwProfile.GloballyOpenPorts;
            INetFwOpenPort  openport  = (INetFwOpenPort)GetInstance("INetOpenPort");

            openport.Port = port;
            try
            {
                if (protocol.ToLower() == "udp")
                {
                    openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
                }
                else if (protocol.ToLower() == "tcp")
                {
                    openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                }
                else
                {
                    openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_ANY;
                }

                openport.Name = appName + " " + protocol;
                openports.Add(openport);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            finally
            {
                if (apps != null)
                {
                    apps = null;
                }
                if (openport != null)
                {
                    openport = null;
                }
                logger.Info("Firewall: aggiunta regola per {0} su porta {1}: {2}", appName, port, protocol);
            }
        } // openFirewall
Esempio n. 12
0
        public FwErrorCode AddApplication(string strProcessImageFileName, string strRegisterName)
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }

            if (strProcessImageFileName.Length == 0 || strRegisterName.Length == 0)
            {
                return(FwErrorCode.FwErrInvalidArg);
            }

            // First of all, check the application is already authorized;
            bool        bAppEnable = true;
            FwErrorCode nError     = IsAppEnabled(strProcessImageFileName, ref bAppEnable);

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

            // Only add the application if it isn't authorized
            if (bAppEnable == false)
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications fwApps = _mFirewallProfile.AuthorizedApplications;

                if (fwApps == null)
                {
                    return(FwErrorCode.FwErrAuthApplications);
                }

                // Create an instance of an authorized application
                Type typeFwApp = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));

                var fwApp = (INetFwAuthorizedApplication)Activator.CreateInstance(typeFwApp);

                // Set the process image file name
                fwApp.ProcessImageFileName = strProcessImageFileName;
                fwApp.Name = strRegisterName;

                try
                {
                    fwApps.Add(fwApp);
                }
                catch
                {
                    return(FwErrorCode.FwErrAddToCollection);
                }
            }

            return(FwErrorCode.FwNoerror);
        }
Esempio n. 13
0
        protected internal void OpenFirewallPort(int port, string protocol, string appName)
        {
            INetFwAuthorizedApplications authApps = null;
            INetFwAuthorizedApplication  authApp  = null;
            INetFwOpenPorts openPorts             = null;
            INetFwOpenPort  openPort = null;

            try
            {
                if (isPortFound(port) == false)
                {
                    SetProfile();
                    openPorts     = fwProfile.GloballyOpenPorts;
                    openPort      = GetInstance("INetOpenPort") as INetFwOpenPort;
                    openPort.Port = port;
                    if (protocol.ToLower() == "udp")
                    {
                        openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
                    }
                    else if (protocol.ToLower() == "tcp")
                    {
                        openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                    }

                    openPort.Name = appName + " " + protocol.ToUpper();
                    openPorts.Add(openPort);
                    logger.Info("Firewall: Aggiunta regola per " + appName + ": " + protocol.ToUpper());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (authApps != null)
                {
                    authApps = null;
                }
                if (authApp != null)
                {
                    authApp = null;
                }
                if (openPorts != null)
                {
                    openPorts = null;
                }
                if (openPort != null)
                {
                    openPort = null;
                }
            }
        }
Esempio n. 14
0
        protected internal bool isAppFound(string appPath)
        {
            bool      boolResult = false;
            Type      progID     = null;
            INetFwMgr firewall   = null;
            INetFwAuthorizedApplications apps = null;
            INetFwAuthorizedApplication  app  = null;

            try
            {
                progID   = Type.GetTypeFromProgID("HNetCfg.FwMgr");
                firewall = Activator.CreateInstance(progID) as INetFwMgr;
                if (firewall.LocalPolicy.CurrentProfile.FirewallEnabled)
                {
                    apps = firewall.LocalPolicy.CurrentProfile.AuthorizedApplications;
                    IEnumerator appEnumerate = apps.GetEnumerator();
                    while ((appEnumerate.MoveNext()))
                    {
                        app = appEnumerate.Current as INetFwAuthorizedApplication;
                        if (app.ProcessImageFileName == appPath)
                        {
                            boolResult = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (progID != null)
                {
                    progID = null;
                }
                if (firewall != null)
                {
                    firewall = null;
                }
                if (apps != null)
                {
                    apps = null;
                }
                if (app != null)
                {
                    app = null;
                }
            }
            return(boolResult);
        }
Esempio n. 15
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;
        }
Esempio n. 16
0
        public bool AddApplication(string applicationFileNameWithPath, string registeredName)
        {
            DoDisposeCheck();
            DoInitializationCheck();
            if (string.IsNullOrEmpty(applicationFileNameWithPath))
            {
                ThrowHelper.ThrowArgumentNullException("applicationFileNameWithPath");
            }
            if (string.IsNullOrEmpty(registeredName))
            {
                ThrowHelper.ThrowArgumentNullException("registeredName");
            }

            bool changed = false;

            // First of all, check the application is already authorized;
            if (!IsApplicationEnabled(applicationFileNameWithPath))
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications firewallApplications = mFirewallProfile.AuthorizedApplications;
                if (firewallApplications == null)
                {
                    throw new FirewallException("Failed to get authorized applications.");
                }

                // Create an instance of an authorized application
                Type firewallApplicationType = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));

                INetFwAuthorizedApplication firewallApplication = (INetFwAuthorizedApplication)Activator.CreateInstance(firewallApplicationType);
                if (firewallApplication == null)
                {
                    throw new FirewallException("Failed to authorize application.");
                }

                // Set the process image file name
                firewallApplication.ProcessImageFileName = applicationFileNameWithPath;
                firewallApplication.Name = registeredName;

                try
                {
                    firewallApplications.Add(firewallApplication);
                    changed = true;
                }
                catch (Exception)
                {
                    throw new FirewallException("Failed to authorize application.");
                }
            }

            return(changed);
        }
Esempio n. 17
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;
                }
            }
        }
Esempio n. 18
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;
                }
            }
        }
Esempio n. 19
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;
                }
            }
        }
Esempio n. 20
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;
                }
            }
        }
Esempio n. 21
0
        internal static void DisableFirewallForExecutingApplication(string processExePath)
        {
            if (!FirewallEnabled)
            {
                return;
            }

            //Create an exception in the Windows Firewall for this application so that we dont get dialogs
            //NOTE: this will not work if the user is not an Admin

            //Create the Application Exception
            INetFwAuthorizedApplication appExclusion = (INetFwAuthorizedApplication)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));

            appExclusion.ProcessImageFileName = processExePath;
            appExclusion.Name      = Path.GetFileNameWithoutExtension(processExePath);
            appExclusion.Scope     = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
            appExclusion.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;
            appExclusion.Enabled   = true;

            //Add the application exception
            INetFwMgr fwManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));

            fwManager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(appExclusion);

            //HACK: Verify the application was added successfully because having the firewall dialog enabled
            //does not result in a UnauthorizedAccess exception, even if the operation fails
            bool added = false;
            INetFwAuthorizedApplications authorizedApplications = fwManager.LocalPolicy.CurrentProfile.AuthorizedApplications;

            foreach (INetFwAuthorizedApplication authorizedApplication in authorizedApplications)
            {
                if (authorizedApplication.ProcessImageFileName.ToLowerInvariant().Equals(processExePath.ToLowerInvariant()))
                {
                    added = true;
                    break;
                }
            }

            if (!added)
            {
                throw new UnauthorizedAccessException();
            }
        }
Esempio n. 22
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");
 }
Esempio n. 23
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);
        }
Esempio n. 24
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);
        }
Esempio n. 25
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;
                }
            }
        }
Esempio n. 26
0
 // WINDOWS防火墙添加例外程序,name程序名称,filename程序的绝对路径(包含文件名)
 public static bool addFirewallExceptions(string name, string filename)
 {
     try
     {
         StringUtility.leftToRight(ref filename);
         // 创建firewall管理接口的实例
         INetFwMgr netFwMgr = createInstance <INetFwMgr>(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
         INetFwAuthorizedApplications profileApplication = netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
         // 创建一个认证程序接口的实例
         INetFwAuthorizedApplication app = createInstance <INetFwAuthorizedApplication>
                                               (Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
         app.Name = name;                                // 在例外列表里,程序显示的名称
         app.ProcessImageFileName = filename;            // 程序的绝对路径
         app.Enabled = true;                             // 是否启用该规则
         profileApplication.Add(app);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 27
0
        internal static void EnableFirewallForExecutingApplication(string processExePath, bool removeRule)
        {
            if (!FirewallEnabled)
            {
                return;
            }

            //Remove the exception in the Windows Firewall for this application is made so that we dont get dialogs
            //NOTE: this will not work if the user is not an Admin
            INetFwMgr fwManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));

            if (removeRule)
            {
                fwManager.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(processExePath);
            }

            //HACK: Verify because having the firewall dialog enabled
            //does not result in a UnauthorizedAccess exception, even if the operation fails
            bool foundRule = false;
            INetFwAuthorizedApplications authorizedApplications = fwManager.LocalPolicy.CurrentProfile.AuthorizedApplications;

            foreach (INetFwAuthorizedApplication authorizedApplication in authorizedApplications)
            {
                if (authorizedApplication.ProcessImageFileName.ToLowerInvariant().Equals(processExePath.ToLowerInvariant()))
                {
                    if (!removeRule)    //Disable the rule instead of removing it
                    {
                        authorizedApplication.Enabled = false;
                    }
                    foundRule = true;
                    break;
                }
            }

            if (removeRule && foundRule)
            {
                throw new UnauthorizedAccessException();
            }
        }
Esempio n. 28
0
        internal static FirewallRule GetFirewallRuleForApplication(string processExePath)
        {
            FirewallRule firewallRule = FirewallRule.None;

            if (!FirewallEnabled)
            {
                return(firewallRule);
            }

            INetFwMgr fwManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
            INetFwAuthorizedApplications firewallApplications = fwManager.LocalPolicy.CurrentProfile.AuthorizedApplications;

            foreach (INetFwAuthorizedApplication firewallApplication in firewallApplications)
            {
                if (firewallApplication.ProcessImageFileName.ToLowerInvariant() == processExePath.ToLowerInvariant())
                {
                    firewallRule = firewallApplication.Enabled ? FirewallRule.Enabled : FirewallRule.Exist;
                    break;
                }
            }

            return(firewallRule);
        }
Esempio n. 29
0
        public FW_ERROR_CODE IsAppEnabled(string strProcessImageFileName, ref bool bEnable)
        {
            if (m_FirewallProfile == null)
            {
                return(FW_ERROR_CODE.FW_ERR_INITIALIZED);
            }

            if (strProcessImageFileName.Length == 0)
            {
                return(FW_ERROR_CODE.FW_ERR_INVALID_ARG);
            }

            INetFwAuthorizedApplications FWApps = m_FirewallProfile.AuthorizedApplications;

            if (FWApps == null)
            {
                return(FW_ERROR_CODE.FW_ERR_AUTH_APPLICATIONS);
            }

            try
            {
                INetFwAuthorizedApplication FWApp = FWApps.Item(strProcessImageFileName);
                // If FAILED, the appliacation is not in the collection list
                if (FWApp == null)
                {
                    return(FW_ERROR_CODE.FW_ERR_APPLICATION_ITEM);
                }

                bEnable = FWApp.Enabled;
            }
            catch (FileNotFoundException)
            {
                bEnable = false;
            }

            return(FW_ERROR_CODE.FW_NOERROR);
        }
Esempio n. 30
0
        public FwErrorCode IsAppEnabled(string strProcessImageFileName, ref bool bEnable)
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }

            if (strProcessImageFileName.Length == 0)
            {
                return(FwErrorCode.FwErrInvalidArg);
            }

            INetFwAuthorizedApplications fwApps = _mFirewallProfile.AuthorizedApplications;

            if (fwApps == null)
            {
                return(FwErrorCode.FwErrAuthApplications);
            }

            try
            {
                INetFwAuthorizedApplication fwApp = fwApps.Item(strProcessImageFileName);
                // If FAILED, the appliacation is not in the collection list
                if (fwApp == null)
                {
                    return(FwErrorCode.FwErrApplicationItem);
                }

                bEnable = fwApp.Enabled;
            }
            catch (System.IO.FileNotFoundException)
            {
                bEnable = false;
            }

            return(FwErrorCode.FwNoerror);
        }
Esempio n. 31
0
        /// <summary>
        /// Creates a new authorized application entry in the firewall collection.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="applicationPath">The authorized application path.</param>
        /// <param name="scope">The scope of the control.</param>
        public void OpenFirewallAuthorizedApplication(string name,
                                                      string applicationPath, NET_FW_SCOPE_ scope)
        {
            // Set the current access profile.
            SetProfile();

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

            // Create a new instance of the
            // open new authorized application type.
            INetFwAuthorizedApplication openAuthApp = (INetFwAuthorizedApplication)GetInstance("INetAuthApp");

            // Assign the authorized application specifications.
            openAuthApp.Name  = name;
            openAuthApp.Scope = scope;
            openAuthApp.ProcessImageFileName = applicationPath;

            // Add the new application to the
            // collection of authorized applications.
            openApplications.Add(openAuthApp);
            openApplications = null;
        }