private void AddPortToFirewall(string name, int port) { try { Type TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr"); INetFwMgr icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr); // add a new port Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort"); INetFwOpenPort portClass = (INetFwOpenPort)Activator.CreateInstance(TportClass); // Get the current profile INetFwProfile profile = icfMgr.LocalPolicy.CurrentProfile; // Set the port properties portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL; portClass.Enabled = true; portClass.Protocol = NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; // WoWs Info - 8605 portClass.Name = name; portClass.Port = port; // Add the port to the ICF Permissions List profile.GloballyOpenPorts.Add(portClass); } catch (Exception e) { MessageBox.Show(e.Message, Properties.strings.error_title, MessageBoxButtons.OK, MessageBoxIcon.Error); Application.ExitThread(); } }
public static void AddPort(string name, Protocol protocol, int port, bool enable) { INetFwOpenPort portClass = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWOpenPort")); portClass.Name = name; portClass.Port = port; portClass.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; portClass.Enabled = enable; switch (protocol) { case Protocol.UDP: portClass.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; break; case Protocol.TCP: portClass.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; break; case Protocol.ANY: portClass.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_ANY; break; default: throw new Exception("Protocol not supported."); } INetFwMgr firewallManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); firewallManager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(portClass); }
/// <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 FwErrorCode IsPortEnabled(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, ref bool bEnable) { if (_mFirewallProfile == null) { return(FwErrorCode.FwErrInitialized); } // Retrieve the open ports collection INetFwOpenPorts fwOpenPorts = _mFirewallProfile.GloballyOpenPorts; if (fwOpenPorts == null) { return(FwErrorCode.FwErrGlobalOpenPorts); } // Get the open port try { INetFwOpenPort fwOpenPort = fwOpenPorts.Item(nPortNumber, ipProtocol); bEnable = fwOpenPort?.Enabled == true; } catch (System.IO.FileNotFoundException) { bEnable = false; } return(FwErrorCode.FwNoerror); }
/// <summary> /// 添加防火墙例外端口 /// </summary> /// <param name="name">名称</param> /// <param name="port">端口</param> /// <param name="protocol">协议(TCP、UDP)</param> public static void NetFwAddPorts(string name, int port, string protocol) { INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort")); objPort.Name = name; objPort.Port = port; if (protocol.ToUpper() == "TCP") { objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; } else { objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; } objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; objPort.Enabled = true; bool exist = false; foreach (INetFwOpenPort mPort in netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts) { if (objPort == mPort) { exist = true; break; } } if (!exist) { netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort); } }
public FW_ERROR_CODE IsPortEnabled(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, ref bool bEnable) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } // Retrieve the open ports collection INetFwOpenPorts FWOpenPorts = m_FirewallProfile.GloballyOpenPorts; if (FWOpenPorts == null) { return(FW_ERROR_CODE.FW_ERR_GLOBAL_OPEN_PORTS); } // Get the open port try { INetFwOpenPort FWOpenPort = FWOpenPorts.Item(nPortNumber, ipProtocol); if (FWOpenPort != null) { bEnable = FWOpenPort.Enabled; } else { bEnable = false; } } catch (FileNotFoundException) { bEnable = false; } return(FW_ERROR_CODE.FW_NOERROR); }
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; } }
/// <summary> /// 添加防火墙例外端口 /// </summary> /// <param name="name">名称</param> /// <param name="port">端口</param> /// <param name="protocol">协议(TCP、UDP)</param> public static void NetFwAddPorts(string name, int port, NET_FW_IP_PROTOCOL_ protocol) { var serviceControllers = ServiceController.GetServices(); string fireWallServiceName; Version currentVersion = Environment.OSVersion.Version; if (currentVersion.Major == 5) { fireWallServiceName = "sharedaccess"; } else { fireWallServiceName = "mpssvc"; } var server = serviceControllers.FirstOrDefault(service => service.ServiceName.ToLower() == fireWallServiceName); if (server != null && server.Status == ServiceControllerStatus.Running) { //创建firewall管理类的实例 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FwOpenPort")); objPort.Name = name; objPort.Port = port; objPort.Protocol = protocol; objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; objPort.Enabled = true; //加入到防火墙的管理策略,若已存在会启用该规则 netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort); } }
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); }
/// <summary> /// Returns a friendly string format of the IP version. /// </summary> /// <param name="port">INetFwOpenPort port object</param> /// <returns>string</returns> private string GetIPVersion(INetFwOpenPort port) { string ipVersion = string.Empty; switch (port.IpVersion) { case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY: ipVersion = "Any"; break; case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_MAX: ipVersion = "Max"; break; case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4: ipVersion = "IPV4"; break; case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V6: ipVersion = "IPV6"; break; } return(ipVersion); }
public static void AddPortException(string name, int port, ProtocolType protocol) { //创建firewall管理类的实例 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance( Type.GetTypeFromProgID("HNetCfg.FwOpenPort")); objPort.Name = name; objPort.Port = port; switch (protocol) { case ProtocolType.Tcp: objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; break; case ProtocolType.Udp: objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; break; } objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; objPort.Enabled = true; bool exist = netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Cast <INetFwOpenPort>() .Any(p => p.Equals(objPort)); if (!exist) { netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort); } }
/// /// 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"); }
/// <summary> /// Add port to windows firewall /// Reference: https://social.msdn.microsoft.com/Forums/vstudio/en-US/a3e390d1-4383-4f23-bad9-b725bef33499/add-firewall-rule-programatically?forum=wcf /// </summary> static void AddPortToFirewall(string name, int port) { try { Type TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr"); INetFwMgr icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr); // add a new port Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort"); INetFwOpenPort portClass = (INetFwOpenPort)Activator.CreateInstance(TportClass); // Get the current profile INetFwProfile profile = icfMgr.LocalPolicy.CurrentProfile; // Set the port properties portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL; portClass.Enabled = true; portClass.Protocol = NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; // WoWs Info - 8605 portClass.Name = name; portClass.Port = port; // Add the port to the ICF Permissions List profile.GloballyOpenPorts.Add(portClass); } catch (Exception e) { Console.WriteLine("Failed to add port to firewall. This is the error message.\n"); Console.WriteLine(e.Message); Console.WriteLine("\nPlease feel free to open an issue to discuss this it with me."); Process.Start("https://github.com/HenryQuan/winserver"); } }
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; } } }
public MainWindow() { InitializeComponent(); // Open the port in the firewall Type type = Type.GetTypeFromProgID("HNetCfg.FWOpenPort"); INetFwOpenPort port = Activator.CreateInstance(type) as INetFwOpenPort; port.Port = 19283; port.Name = "Mayhem"; port.Enabled = true; Type netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); INetFwOpenPorts ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; ports.Add(port); // Add the ACL string name = WindowsIdentity.GetCurrent().Name; SecurityIdentity sid = SecurityIdentity.SecurityIdentityFromName(name); string acl = "D:(A;;GA;;;" + sid + ")"; Debug.WriteLine(acl); SetHttpNamespaceAcl("http://+:19283/", acl); Close(); }
public bool IsPortEnabled(int port, NET_FW_IP_PROTOCOL_ ipProtocol) { DoDisposeCheck(); DoInitializationCheck(); bool result = false; // Retrieve the open ports collection INetFwOpenPorts firewallOpenPorts = mFirewallProfile.GloballyOpenPorts; if (firewallOpenPorts == null) { throw new FirewallException("Failed to get globally open ports."); } // Get the open port try { INetFwOpenPort firewallOpenPort = firewallOpenPorts.Item(port, ipProtocol); if (firewallOpenPort != null) { result = firewallOpenPort.Enabled; } } catch (Exception) { } return(result); }
public FW_ERROR_CODE AddPort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, string strRegisterName) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } bool bEnablePort = true; FW_ERROR_CODE nError = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort); if (nError != FW_ERROR_CODE.FW_NOERROR) { return(nError); } // Only add the port, if it isn't added to the collection if (bEnablePort == false) { // Retrieve the collection of globally open ports INetFwOpenPorts FWOpenPorts = m_FirewallProfile.GloballyOpenPorts; if (FWOpenPorts == null) { return(FW_ERROR_CODE.FW_ERR_GLOBAL_OPEN_PORTS); } // Create an instance of an open port Type typeFwPort = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); INetFwOpenPort FWOpenPort = (INetFwOpenPort)Activator.CreateInstance(typeFwPort); if (FWOpenPort == null) { return(FW_ERROR_CODE.FW_ERR_CREATE_PORT_INSTANCE); } // Set the port number FWOpenPort.Port = nPortNumber; // Set the IP Protocol FWOpenPort.Protocol = ipProtocol; // Set the registered name FWOpenPort.Name = strRegisterName; try { FWOpenPorts.Add(FWOpenPort); } catch { return(FW_ERROR_CODE.FW_ERR_ADD_TO_COLLECTION); } } else { return(FW_ERROR_CODE.FW_ERR_SAME_PORT_EXIST); } return(FW_ERROR_CODE.FW_NOERROR); }
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(); } }
private void AddRuleForPortWinXp(int port) { try { _logger.Info("Win xp sp > 1"); var fwMgrType = Type.GetTypeFromProgID(("HNetCfg.FwMgr")); var newRule = false; if (fwMgrType == null) { _logger.Error("Can not add rule for port: HNetCfg.FwMgr is null"); return; } var fwMgr = (INetFwMgr)Activator.CreateInstance(fwMgrType); var profile = fwMgr.LocalPolicy.CurrentProfile; INetFwOpenPort openPort = null; try { foreach (var globallyOpenPort in profile.GloballyOpenPorts) { if (globallyOpenPort is INetFwOpenPort && ((INetFwOpenPort)globallyOpenPort).Name == RuleName) { openPort = globallyOpenPort as INetFwOpenPort; } } } catch (Exception) { openPort = null; } if (openPort == null) { openPort = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWOpenPort")); newRule = true; } openPort.Name = RuleName; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Port = port; openPort.Enabled = true; openPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; if (newRule) { profile.GloballyOpenPorts.Add(openPort); } } catch (Exception e) { _logger.Error("Can not add rule for port: " + e); } }
/// <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; } } }
private INetFwOpenPort GetPortObj(string portNumber, NET_FW_IP_PROTOCOL_ ipProtocol) { Type tpResult = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); INetFwOpenPort port = (INetFwOpenPort)Activator.CreateInstance(tpResult); port.Port = Int32.Parse(portNumber); // port number port.Name = appName; // name of the application using the port port.Enabled = true; // enable the port port.Protocol = ipProtocol; return(port); }
private void _AddPortToFirewall(INetFwProfile profile) { INetFwOpenPort openPort = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(CLSID_NetOpenPort))); openPort.Enabled = true; openPort.Port = _Port; openPort.Protocol = _GetProtocol(); openPort.Name = _RuleName + "(" + _Port + ")"; openPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; profile.GloballyOpenPorts.Add(openPort); }
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; } } }
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
public static OpenPort Convert(INetFwOpenPort item) { return(new OpenPort { Enabled = item.Enabled, IpVersion = Convert(item.IpVersion), Name = item.Name, RemoteAddresses = item.RemoteAddresses, Scope = Convert(item.Scope), Port = item.Port, ProtocolPort = Convert(item.Protocol) }); }
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); } }
protected internal bool isPortFound(int portNumber) { bool boolResult = false; INetFwOpenPorts ports = null; Type progID = null; INetFwMgr firewall = null; INetFwOpenPort currentPort = null; try { // TODO: aprire in pubblico e privato // ref: http://stackoverflow.com/questions/15409790/adding-an-application-firewall-rule-to-both-private-and-public-networks-via-win7 progID = Type.GetTypeFromProgID("HNetCfg.FwMgr"); firewall = Activator.CreateInstance(progID) as INetFwMgr; ports = firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts; IEnumerator portEnumerate = ports.GetEnumerator(); while ((portEnumerate.MoveNext())) { currentPort = portEnumerate.Current as INetFwOpenPort; if (currentPort.Port == portNumber) { boolResult = true; break; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (ports != null) { ports = null; } if (progID != null) { progID = null; } if (firewall != null) { firewall = null; } if (currentPort != null) { currentPort = null; } } return(boolResult); }
protected internal bool isPortFound(int portNumber) { bool boolResult = false; INetFwOpenPorts ports = null; Type progID = null; INetFwMgr firewall = null; INetFwOpenPort currentPort = null; try { progID = Type.GetTypeFromProgID("HNetCfg.FwMgr"); firewall = Activator.CreateInstance(progID) as INetFwMgr; ports = firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts; IEnumerator portEnumerate = ports.GetEnumerator(); while ((portEnumerate.MoveNext())) { currentPort = portEnumerate.Current as INetFwOpenPort; if (currentPort.Port == portNumber) { boolResult = true; break; } } } catch (Exception ex) { //MessageBox.Show(ex.Message); Console.Write(ex.Message); } finally { if (ports != null) { ports = null; } if (progID != null) { progID = null; } if (firewall != null) { firewall = null; } if (currentPort != null) { currentPort = null; } } return(boolResult); }
public bool AddPort(int port, NET_FW_IP_PROTOCOL_ ipProtocol, string registeredName) { DoDisposeCheck(); DoInitializationCheck(); if (string.IsNullOrEmpty(registeredName)) { ThrowHelper.ThrowArgumentNullException("registeredName"); } bool result = false; if (!IsPortEnabled(port, ipProtocol)) { // Retrieve the collection of globally open ports INetFwOpenPorts firewallOpenPorts = mFirewallProfile.GloballyOpenPorts; if (firewallOpenPorts == null) { throw new FirewallException("Failed to get globally open ports."); } // Create an instance of an open port Type firewallPortType = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); INetFwOpenPort firewallOpenPort = (INetFwOpenPort)Activator.CreateInstance(firewallPortType); if (firewallOpenPort == null) { throw new FirewallException("Failed to create port instance."); } // Set the port number firewallOpenPort.Port = port; // Set the IP Protocol firewallOpenPort.Protocol = ipProtocol; // Set the registered name firewallOpenPort.Name = registeredName; try { firewallOpenPorts.Add(firewallOpenPort); result = true; } catch (Exception ex) { throw new FirewallException("Failed to add port.", ex); } } return(result); }
private INetFwOpenPort _GetPort(string title, int portNo, bool isUDP) { Type type = Type.GetTypeFromProgID(PROGID_OPEN_PORT); INetFwOpenPort port = Activator.CreateInstance(type) as INetFwOpenPort; port.Name = title; port.Port = portNo; port.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; port.Protocol = isUDP ? NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP : NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; port.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY; return(port); }
/// <summary> /// Returns a friendly string format of the type of protocol. /// </summary> /// <param name="port">INetFwOpenPort port object</param> /// <returns>string</returns> private string GetPortType(INetFwOpenPort port) { string protocolType = string.Empty; switch (port.Protocol) { case NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP: protocolType = "TCP"; break; case NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP: protocolType = "UDP"; break; } return protocolType; }
/// <summary> /// Returns a friendly string format of the IP version. /// </summary> /// <param name="port">INetFwOpenPort port object</param> /// <returns>string</returns> private string GetIPVersion(INetFwOpenPort port) { string ipVersion = string.Empty; switch (port.IpVersion) { case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY: ipVersion = "Any"; break; case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_MAX: ipVersion = "Max"; break; case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4: ipVersion = "IPV4"; break; case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V6: ipVersion = "IPV6"; break; } return ipVersion; }