private static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) { string PROGID_AUTHORIZED_APPLICATION = System.Configuration.ConfigurationManager.AppSettings["PROGID_AUTHORIZED_APPLICATION"]; // Create the type from prog id Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); INetFwAuthorizedApplication auth = Activator.CreateInstance(type) as INetFwAuthorizedApplication; auth.Name = title; auth.ProcessImageFileName = applicationPath; auth.Scope = scope; auth.IpVersion = ipVersion; // Unauthorize notepad to connect to internet // Unauthorize wordpad / winword to connect to internet // Unauthorize each tempfile to connect to internet. auth.Enabled = false; INetFwMgr manager = GetFirewallManager(); try { manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); } catch (Exception ex) { return(false); } return(true); }
public bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) { // Create the type from prog id Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); INetFwAuthorizedApplication auth = Activator.CreateInstance(type) as INetFwAuthorizedApplication; auth.Name = title; auth.ProcessImageFileName = applicationPath; auth.Scope = scope; auth.IpVersion = ipVersion; auth.Enabled = true; INetFwMgr manager = GetFirewallManager(); try { manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); } catch (Exception ex) { return(false); } return(true); }
private static void AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) { Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); INetFwAuthorizedApplication auth = Activator.CreateInstance(type) as INetFwAuthorizedApplication; if (auth != null) { auth.Name = title; } if (!File.Exists(applicationPath)) { return; } if (auth != null) { auth.ProcessImageFileName = applicationPath; auth.Scope = scope; auth.IpVersion = ipVersion; auth.Enabled = true; } INetFwMgr manager = GetFirewallManager(); try { manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); } catch { return; } }
private static void GloballyOpenPort(string title, int portNo, NET_FW_SCOPE_ scope, NET_FW_IP_PROTOCOL_ protocol, NET_FW_IP_VERSION_ ipVersion) { Type type = Type.GetTypeFromProgID(PROGID_OPEN_PORT); INetFwOpenPort port = Activator.CreateInstance(type) as INetFwOpenPort; if (port != null) { port.Name = title; port.Port = portNo; port.Scope = scope; port.Protocol = protocol; port.IpVersion = ipVersion; } INetFwMgr manager = GetFirewallManager(); try { manager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port); } catch { return; } }
private static void AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) { Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); INetFwAuthorizedApplication auth = Activator.CreateInstance(type) as INetFwAuthorizedApplication; if (auth != null) { auth.Name = title; } if (!File.Exists(applicationPath)) return; if (auth != null) { auth.ProcessImageFileName = applicationPath; auth.Scope = scope; auth.IpVersion = ipVersion; auth.Enabled = true; } INetFwMgr manager = GetFirewallManager(); try { manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); } catch { return; } }
public PortInfo(int portNumber) { imageFileName = null; ipVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4; this.portNumber = portNumber; localAddress = null; ipProtocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; }
public NetFwAuthorizedApplication(string name, bool enabled, string remoteAddresses, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion, string processImageFileName) { this.Name = name; this.Scope = scope; this.Enabled = enabled; this.IpVersion = ipVersion; this.RemoteAddresses = remoteAddresses; this.ProcessImageFileName = processImageFileName; }
public NetFwAuthorizedApplication() { this.Name = ""; this.Enabled = false; this.RemoteAddresses = ""; this.ProcessImageFileName = ""; this.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; this.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY; }
public static IpVersion Convert(NET_FW_IP_VERSION_ item) { switch (item) { case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_MAX: return(IpVersion.Max); case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4: return(IpVersion.V4); case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V6: return(IpVersion.V6); default: return(IpVersion.Any); } }
public bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) { // Create the type from prog id Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); INetFwAuthorizedApplication auth = Activator.CreateInstance(type) as INetFwAuthorizedApplication; auth.Name = title; auth.ProcessImageFileName = applicationPath; auth.Scope = scope; auth.IpVersion = ipVersion; auth.Enabled = true; INetFwMgr manager = GetFirewallManager(); try { manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); } catch (Exception ex) { return false; } return true; }
public static bool AuthorizeProgram(string title, string path, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver) { Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"); INetFwAuthorizedApplication authapp = Activator.CreateInstance(type) as INetFwAuthorizedApplication; authapp.Name = title; authapp.ProcessImageFileName = path; authapp.Scope = scope; authapp.IpVersion = ipver; authapp.Enabled = true; INetFwMgr mgr = WinFirewallManager(); try { mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp); } catch (Exception ex) { System.Diagnostics.Trace.Write(ex.Message); return false; } return true; }
/// Adds an application to the list of authorized applications. /// If the application is already authorized, does nothing. /// /// /// The full path to the application executable. This cannot /// be blank, and cannot be a relative path. /// /// /// This is the name of the application, purely for display /// puposes in the Microsoft Security Center. /// /// /// When applicationFullPath is null OR /// When appName is null. /// /// /// When applicationFullPath is blank OR /// When appName is blank OR /// applicationFullPath contains invalid path characters OR /// applicationFullPath is not an absolute path /// /// /// If the firewall is not installed OR /// If the firewall does not allow specific application 'exceptions' OR /// Due to an exception in COM this method could not create the /// necessary COM types /// /// /// If no file exists at the given applicationFullPath public void GrantAuthorization(string applicationFullPath, string appName, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) { #region Parameter checking if (applicationFullPath == null) { throw new ArgumentNullException("applicationFullPath"); } if (appName == null) { throw new ArgumentNullException("appName"); } if (applicationFullPath.Trim().Length == 0) { throw new ArgumentException( "applicationFullPath must not be blank"); } if (applicationFullPath.Trim().Length == 0) { throw new ArgumentException("appName must not be blank"); } if (applicationFullPath.IndexOfAny(Path.InvalidPathChars) >= 0) { throw new ArgumentException( "applicationFullPath must not contain invalid path characters"); } if (!Path.IsPathRooted(applicationFullPath)) { throw new ArgumentException( "applicationFullPath is not an absolute path"); } if (!File.Exists(applicationFullPath)) { throw new FileNotFoundException("File does not exist", applicationFullPath); } // State checking if (!IsFirewallInstalled) { throw new FirewallHelperException( "Cannot grant authorization: Firewall is not installed."); } if (!AppAuthorizationsAllowed) { throw new FirewallHelperException( "Application exemptions are not allowed."); } #endregion if (!HasAuthorization(applicationFullPath)) { // Get the type of HNetCfg.FwMgr, or null if an error occurred Type authAppType = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication", false); // Assume failed. INetFwAuthorizedApplication appInfo = null; if (authAppType != null) { try { appInfo = (INetFwAuthorizedApplication) Activator.CreateInstance(authAppType); } // In all other circumnstances, appInfo is null. catch (ArgumentException) { } catch (NotSupportedException) { } catch (TargetInvocationException) { } catch (MissingMethodException) { } catch (MethodAccessException) { } catch (MemberAccessException) { } catch (InvalidComObjectException) { } catch (COMException) { } catch (TypeLoadException) { } } if (appInfo == null) { throw new FirewallHelperException( "Could not grant authorization: can't create INetFwAuthorizedApplication instance."); } appInfo.Name = appName; appInfo.ProcessImageFileName = applicationFullPath; appInfo.Scope = scope; appInfo.IpVersion = ipVersion; appInfo.Enabled = true; // ... // Use defaults for other properties of the AuthorizedApplication COM object // Authorize this application fireWallManager.LocalPolicy.CurrentProfile. AuthorizedApplications.Add(appInfo); } // otherwise it already has authorization so do nothing }
/// <summary> /// Open port in network windows firewall /// </summary> /// <param name="name"></param> /// <param name="portNo"></param> /// <param name="scope"></param> /// <param name="protocol"></param> /// <param name="ipVersion"></param> /// <returns></returns> public static bool GloballyOpenPort(string name, int portNo, NET_FW_SCOPE_ scope, NET_FW_IP_PROTOCOL_ protocol, NET_FW_IP_VERSION_ ipVersion) { INetFwMgr manager = GetFirewallManager(); try { // Check if port does not exists. bool exists = false; foreach (INetFwOpenPort openPort in manager.LocalPolicy.CurrentProfile.GloballyOpenPorts) { if (openPort.Name == name && openPort.Port == portNo) { exists = true; break; } } if (!exists) { // Create the type from prog id Type type = Type.GetTypeFromProgID(PROGID_OPEN_PORT); // Create instance that provides access to the properties of a port that has been opened in the firewall. INetFwOpenPort port = Activator.CreateInstance(type) as INetFwOpenPort; // Set properties for port port.Name = name; port.Port = portNo; port.Scope = scope; port.Protocol = protocol; port.IpVersion = ipVersion; // Add open port to windows firewall manager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port); } } catch (Exception ex) { return(false); } return(true); }
public static bool AuthorizeProgram(string title, string path, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver) { try { log.Debug(string.Format("Adding firewall exception for: {0} [{1}]", title, path)); Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"); INetFwAuthorizedApplication authapp = Activator.CreateInstance(type) as INetFwAuthorizedApplication; authapp.Name = title; authapp.ProcessImageFileName = path; authapp.Scope = scope; authapp.IpVersion = ipver; authapp.Enabled = true; INetFwMgr mgr = WinFirewallManager(); mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp); log.Debug("Firewall exception added properly!"); return(true); } catch (Exception ex) { log.Error("Unhandled exception when trying to add entry to Window's Firewall", ex); return(false); } }
/* Com refrences * using NATUPNPLib; * using NETCONLib; * using NetFwTypeLib; */ public static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) { Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"); INetFwAuthorizedApplication auth = Activator.CreateInstance(type) as INetFwAuthorizedApplication; auth.Name = title; auth.ProcessImageFileName = applicationPath; auth.Scope = scope; auth.IpVersion = ipVersion; auth.Enabled = true; INetFwMgr manager = GetFirewallManager(); if (manager == null) { return(false); } if (!manager.LocalPolicy.CurrentProfile.FirewallEnabled) { manager.LocalPolicy.CurrentProfile.FirewallEnabled = true; } try { manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); } catch// (Exception ex) { // MessageBox.Show(ex.ToString()); return(false); } return(true); }
public static bool AuthorizeProgram(string title, string path, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver) { Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"); INetFwAuthorizedApplication authapp = Activator.CreateInstance(type) as INetFwAuthorizedApplication; authapp.Name = title; authapp.ProcessImageFileName = path; authapp.Scope = scope; authapp.IpVersion = ipver; authapp.Enabled = true; INetFwMgr mgr = WinFirewallManager(); try { mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp); } catch (Exception ex) { System.Diagnostics.Trace.Write(ex.Message); return(false); } return(true); }
public bool GloballyOpenPort(string title, int portNo, NET_FW_SCOPE_ scope, NET_FW_IP_PROTOCOL_ protocol, NET_FW_IP_VERSION_ ipVersion) { Type type = Type.GetTypeFromProgID(PROGID_OPEN_PORT); INetFwOpenPort port = Activator.CreateInstance(type) as INetFwOpenPort; port.Name = title; port.Port = portNo; port.Scope = scope; port.Protocol = protocol; port.IpVersion = ipVersion; //INetFwMgr manager = GetFirewallManagerCached(); try { m_NetFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port); } catch (Exception ex) { throw ex; } return true; }