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(); }
/// /// 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"); }
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> /// 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 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}")); var 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); }
public FwErrorCode AddPort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, string strRegisterName) { if (_mFirewallProfile == null) { return(FwErrorCode.FwErrInitialized); } bool bEnablePort = true; FwErrorCode nError = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort); if (nError != FwErrorCode.FwNoerror) { 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 = _mFirewallProfile.GloballyOpenPorts; if (fwOpenPorts == null) { return(FwErrorCode.FwErrGlobalOpenPorts); } // Create an instance of an open port Type typeFwPort = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); var fwOpenPort = (INetFwOpenPort)Activator.CreateInstance(typeFwPort); // 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(FwErrorCode.FwErrAddToCollection); } } else { return(FwErrorCode.FwErrSamePortExist); } return(FwErrorCode.FwNoerror); }
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
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; } } }
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); } }
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); }
public static void OpenFirewallPort(string port, string name) { Type netFwOpenPortType = Type.GetTypeFromProgID("HNetCfg.FWOpenPort"); INetFwOpenPort openPort = (INetFwOpenPort)Activator.CreateInstance(netFwOpenPortType); openPort.Port = Convert.ToInt32(port); openPort.Name = name; openPort.Enabled = true; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; Type netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); INetFwOpenPorts openPorts = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; openPorts.Add(openPort); }
public static void OpenPort(int port, string applicationName) { EnsureSetup(); if (IsPortOpen(port)) { return; } INetFwOpenPort openPort = GetInstance("INetOpenPort") as INetFwOpenPort; openPort.Port = port; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Name = applicationName; INetFwOpenPorts openPorts = sm_fwProfile.GloballyOpenPorts; openPorts.Add(openPort); }
internal static void OpenPort(int port, string application) { if (IsOpenPort(port)) { return; } INetFwOpenPort openPort = GetInstance <INetFwOpenPort>(); openPort.Port = port; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Name = application; INetFwOpenPorts openPorts = FirewallProfile.GloballyOpenPorts; openPorts.Add(openPort); }
private static void AddFirewallRule(int serverPort) { // From http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx // and from http://stackoverflow.com/questions/8889587/automating-windows-firewall-with Type NetOpenPortType = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); INetFwOpenPort port = (INetFwOpenPort)Activator.CreateInstance(NetOpenPortType); port.Name = "BlinkingEye"; port.Port = serverPort; Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); INetFwOpenPorts ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; ports.Add(port); }
public static void AllowAddressPort(string remoteAddress, int port) { EnsureSetup(); if (IsPortOpen(remoteAddress, port)) { return; } INetFwOpenPort openPort = GetInstance("INetOpenPort") as INetFwOpenPort; openPort.Port = port; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Name = "allow_" + remoteAddress + "_" + port.ToString(); openPort.RemoteAddresses = remoteAddress; INetFwOpenPorts openPorts = sm_fwProfile.GloballyOpenPorts; openPorts.Add(openPort); }
public void OpenFirewall(string applictionName) { INetFwAuthorizedApplications authApps = null; INetFwAuthorizedApplication authApp = null; INetFwOpenPorts openPorts = null; INetFwOpenPort openPort = null; try { if (isAppFound(applictionName) == false) { SetProfile(); authApps = fwProfile.AuthorizedApplications; authApp = GetInstance("INetAuthApp") as INetFwAuthorizedApplication; authApp.Name = applictionName; authApp.ProcessImageFileName = applictionName; 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) { throw ex; } finally { if (authApps != null) { authApps = null; } if (authApp != null) { authApp = null; } if (openPorts != null) { openPorts = null; } if (openPort != null) { openPort = null; } } }
public string OpenFirewall() { INetFwAuthorizedApplications authApps = null; INetFwAuthorizedApplication authApp = null; INetFwOpenPorts openPorts = null; INetFwOpenPort openPort = null; string retMsg = null; try { //string productName = Application.Current.MainWindow.GetType().Assembly.GetName().Name; string productName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + "Server"; string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; if (isAppFound(productName) == false) { SetProfile(); authApps = fwProfile.AuthorizedApplications; authApp = GetInstance("INetAuthApp") as INetFwAuthorizedApplication; authApp.Name = productName; authApp.ProcessImageFileName = exePath; 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 (portsSocket.Length == 2 && 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); } retMsg = "Open firewall successfully, app name = " + productName + ", port1 name = " + portsName[0] + ", port1 = " + portsSocket[0]; if (portsSocket.Length == 2) { retMsg += "port2 name = " + portsName[1] + ", port2 = " + portsSocket[1]; } retMsg += ".\n"; } catch (Exception ex) { //MessageBox.Show(ex.Message); retMsg = "Fw Error with the message = " + ex.Message; } finally { if (authApps != null) { authApps = null; } if (authApp != null) { authApp = null; } if (openPorts != null) { openPorts = null; } if (openPort != null) { openPort = null; } } return(retMsg); }