/// <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); }
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; } } }
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); }
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); }
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; } } }
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; } } }
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); }
protected void setProfile() { // Access INetFwMgr INetFwMgr fwMgr = (INetFwMgr)getInstance("INetFwMgr"); INetFwPolicy fwPolicy = fwMgr.LocalPolicy; fwProfile = fwPolicy.CurrentProfile; fwMgr = null; fwPolicy = null; }
/// <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); }
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); }
/// <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; }
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); }
public void Initialize() { DoDisposeCheck(); if (mFirewallProfile == null) { Type firewallManagerType = null; INetFwMgr firewallManager = null; firewallManagerType = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}")); firewallManager = (INetFwMgr)Activator.CreateInstance(firewallManagerType); if (firewallManager == null) { throw new InitializationException("Failed to create Settings Manager instance."); } else { INetFwPolicy fwPolicy = firewallManager.LocalPolicy; if (fwPolicy == null) { throw new InitializationException("Failed to get local policy."); } else { try { mFirewallProfile = fwPolicy.GetProfileByType(firewallManager.CurrentProfileType); mInitialized = true; } catch (Exception ex) { throw new InitializationException("Failed to get firewall profile.", ex); } } } } }