Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically manage Windows Firewall

I am trying to programmatically create an Outbound Windows firewall rule. In addition, I'd like to programmatically enable and disable this rule. How can I go about doing this in C#? Manually, I can do this by going into control panel, clicking on Windows Firewall, then clicking advanced settings.

like image 484
Icemanind Avatar asked Feb 27 '12 20:02

Icemanind


2 Answers

You can use this nuget package WindowsFirewallHelper

PM> Install-Package WindowsFirewallHelper

Example code add a new outbound rule for an application

var rule = FirewallManager.Instance.CreateApplicationRule(
    @"MyApp Rule",
    FirewallAction.Allow,
    @"C:\MyApp.exe"
);
rule.Direction = FirewallDirection.Outbound;
FirewallManager.Instance.Rules.Add(rule);
like image 111
live2 Avatar answered Sep 19 '22 14:09

live2


You could use "netsh" command. Make a method to call it.
Use this if you don't want to reference FirewallAPI.dll or install the nuget WindowsFirewallHelper.

Example:


        /// <summary>
        /// Creates a Firewall Rule on current computer. Uses 'netsh'
        /// </summary>
        /// <param name="rulename"></param>
        /// <param name="protocol"></param>
        /// <param name="port"></param>
        /// <param name="direction">"in" or "out"</param>
        /// <param name="action"></param>
        /// <returns>netsh command response</returns>
        public static string CreateFirewalPort(string rulename, string protocol, int port, string direction = "in", string action = "allow")
        {
            // https://support.microsoft.com/en-us/help/947709/how-to-use-the-netsh-advfirewall-firewall-context-instead-of-the-netsh

            //Remove any rule with the same name. Otherwise every time you run this code a new rule is added.  
            Process removeproc = new Process
            {
                StartInfo = {
                    FileName = "netsh",
                    Arguments = $@"advfirewall firewall delete rule name=""{rulename}""",
                    UseShellExecute = false,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    RedirectStandardOutput = true
                }
            };
            try
            {
                removeproc.Start();
                var output = removeproc.StandardOutput.ReadToEnd();
                removeproc.WaitForExit();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message);
            }

            Process process = new Process
            {
                StartInfo = {
                    FileName = "netsh",
                    Arguments = $@"advfirewall firewall add rule name=""{rulename}"" protocol={protocol} localport={port} dir={direction} action={action}",
                    UseShellExecute = false,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    RedirectStandardOutput = true
                }
            };

            try
            {
                process.Start();
                var output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                return output;
            }
            catch (Exception ex)
            {
                return ex.ExceptionToString();
            }
        }
like image 20
Rui Caramalho Avatar answered Sep 20 '22 14:09

Rui Caramalho