Create firewall rule to open port per application programmatically in c#
Asked Answered
K

2

8

I need to open specific port for my application.

I have tried using INetFwAuthorizedApplication rule per application for all ports.

fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app)

Alternatively open one port for all appllications using INetFwOpenPort.

firewallManager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port)

Is there any way to programmatically open only single port per application programmatically? I can do it manually through firewall settings.

Keslie answered 13/12, 2011 at 11:14 Comment(2)
I hope this isn't possible otherwise my investment in a security software was a waste of money. Are you 100% sure you even need to open the port, most applications don't even require this, even though they provide instructions on how to do it to their users. What do you mean by "I won't need to open UDP either if that is possible" the statement makes no sense.Graves
Instead of opening too much I am trying to be more restrictive. By adding my application to AuthorizedApplications application is able to listen/communicate on all local and remote ports. I really need to open my application for one single port only. When you manually create firewall rule you can specify application and specific port only. UDP vs TCP is not so important and it only means I do not have to have two inbound rules on my firewall. I will remove it from my question since it is second question really.Keslie
N
7

There's a question about blocking connections with an answer with instructions for creating firewall rules in C#. You should be able to adapt this for any kind of firewall rule I imagine.

https://mcmap.net/q/456450/-any-way-to-turn-the-quot-internet-off-quot-in-windows-using-c

The following code creates a firewall rule that blocks any outgoing connections on all of your network adapters:

using NetFwTypeLib; // Located in FirewallAPI.dll
...
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Description = "Used to block all internet access.";
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = "Block Internet";

INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
Northwestward answered 14/12, 2011 at 13:12 Comment(0)
I
8

You could also just use PowerShell.

using System.Management.Automation;
...
private void OpenPort(int port)
{
    var powershell = PowerShell.Create();
    var psCommand = $"New-NetFirewallRule -DisplayName \"<rule description>\" -Direction Inbound -LocalPort {port} -Protocol TCP -Action Allow";
    powershell.Commands.AddScript(psCommand);
    powershell.Invoke();
}
Immingle answered 19/8, 2018 at 11:13 Comment(0)
N
7

There's a question about blocking connections with an answer with instructions for creating firewall rules in C#. You should be able to adapt this for any kind of firewall rule I imagine.

https://mcmap.net/q/456450/-any-way-to-turn-the-quot-internet-off-quot-in-windows-using-c

The following code creates a firewall rule that blocks any outgoing connections on all of your network adapters:

using NetFwTypeLib; // Located in FirewallAPI.dll
...
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Description = "Used to block all internet access.";
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = "Block Internet";

INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
Northwestward answered 14/12, 2011 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.