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 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 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 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; } }
/// <summary> /// Creates a new port entry in the firewall collection. /// </summary> /// <param name="port">The port number.</param> /// <param name="name">The name of the port.</param> /// <param name="protocol">The protocol used.</param> /// <param name="scope">The scope of the control.</param> public void OpenFirewallPort(int port, string name, NET_FW_IP_PROTOCOL_ protocol, NET_FW_SCOPE_ scope) { // Set the current access profile. SetProfile(); // Get the current globall // open port profile control. INetFwOpenPorts openPorts = fwProfile.GloballyOpenPorts; // Create a new instance of the // open new port type. INetFwOpenPort openPort = (INetFwOpenPort)GetInstance("INetOpenPort"); // Assign the port specifications. openPort.Port = port; openPort.Name = name; openPort.Scope = scope; openPort.Protocol = protocol; // Add the new port to the // collection of ports. openPorts.Add(openPort); openPorts = null; }
public static Scope Convert(NET_FW_SCOPE_ item) { switch (item) { case NET_FW_SCOPE_.NET_FW_SCOPE_CUSTOM: return(Scope.Custom); case NET_FW_SCOPE_.NET_FW_SCOPE_LOCAL_SUBNET: return(Scope.LocalSubnet); case NET_FW_SCOPE_.NET_FW_SCOPE_MAX: return(Scope.Max); default: return(Scope.All); } }
private void OpenPort(string name, int port, NET_FW_IP_PROTOCOL_ protocol, NET_FW_SCOPE_ scope) { if (openPorts.OfType <INetFwOpenPort>().Where(x => x.Name == name).Count() == 0) { INetFwOpenPort openPort = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort")); openPort.Port = port; openPort.Protocol = protocol; openPort.Scope = scope; openPort.Name = name; openPorts.Add(openPort); } }
/// <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 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 bool GloballyOpenPort(string title, int portNo, NET_FW_SCOPE_ scope, NET_FW_IP_PROTOCOL_ protocol/*, NET_FW_IP_VERSION_ ipVersion*/, INetFwMgr manager) { 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; try { manager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port); } catch (Exception) { 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 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); } }
internal static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope/*, NET_FW_IP_VERSION_ ipVersion*/, int port, NET_FW_IP_PROTOCOL_ protocol) { // 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; // Not implemented auth.Enabled = true; INetFwMgr manager = GetFirewallManager(); try { manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); } catch (Exception) { return false; } return GloballyOpenPort(title, port, scope, protocol, manager); }
/// <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; }
/// 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 }
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; } }
/* 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 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 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; }