Giter Club home page Giter Club logo

windowsfirewallhelper's Introduction

Windows Firewall Helper Class Library

A class library to manage the Windows Firewall as well as adding your program to the Windows Firewall Exception list.

WHERE TO FIND

This library is available as a NuGet package at nuget.org.

HOW TO USE

Starting point of this library is the FirewallManager static class which can be used to get the instance of the class managing the firewall currently on the system.

WindowsFirewallHelper.FirewallManager Class

This static class contains properties about the current active Windows Firewall management class instance or the registered active third party firewall. This class also provides methods to register a third party firewall management object.

[Static Read Only Properties]
  • FirewallManager.Instance: Gets the first active third party firewall object or the instance of the Windows Firewall management class.
  • FirewallManager.Version: Gets the type of firewall object that FirewallManager.Instance property returns.
  • FirewallManager.ThirdPartyFirewalls: Gets an array containing all registered third party firewall management objects.
[Static Methods]
  • FirewallManager.RegisterFirewall(): Registers an instance of a third party firewall management class implementing the IFirewall interface.

WindowsFirewallHelper Namespace

This namespace contains shared and general classes as well as main starting point of this library, FirewallManager class.

[Classes]
  • WindowsFirewallHelper.FirewallManager: A static class to manage the current active firewall
  • WindowsFirewallHelper.FirewallProtocol: A class representing a Firewall Protocol
  • WindowsFirewallHelper.ActiveCollection: Represents a dynamic data collection that provides notifications when items get added or removed.
  • WindowsFirewallHelper.ActiveCollectionChangedEventArgs: class containing event data about the ActiveCollection.ItemsModified event.
[Interfaces]
  • WindowsFirewallHelper.IFirewall: Defines expected methods and properties of a firewall program or API
  • WindowsFirewallHelper.IProfile: Defines expected properties of a firewall profile
  • WindowsFirewallHelper.IRule: Defines expected properties of a firewall rule
  • WindowsFirewallHelper.IAddress: Defines expected methods of a network address

WindowsFirewallHelper.FirewallAPIv1 Namespace

This namespace contains the classes to manage and manipulate the Windows Firewall using the Windows Firewall API v1. Supporting Windows XP and above.

[Classes]
  • FirewallAPIv1.Firewall: Contains properties and methods of Windows Firewall v1 - Implementing IFirewall interface
  • FirewallAPIv1.FirewallProfile: Contains properties of a Windows Firewall v1 profile - Implementing IProfile interface
  • FirewallAPIv1.Rules.ApplicationRule: Contains properties of a Windows Firewall v1 application rule - Implementing IRule interface
  • FirewallAPIv1.Rules.PortRule: Contains properties of a Windows Firewall v1 port rule - Implementing IRule interface
  • FirewallAPIv1.FirewallAPIv1NotSupportedException: The exception that is thrown when an invoked method is not supported with the 'Windows Firewall API v1' - Extending NotSupportedException

WindowsFirewallHelper.FirewallAPIv2 Namespace

This namespace contains the classes to manage and manipulate the Windows Firewall using the Windows Firewall API v2 (aka. Windows Firewall with Advanced Security). Supporting Windows Vista and above.

[Classes]
  • FirewallAPIv2.Firewall: Contains properties and methods of Windows Firewall with Advanced Security - Implementing IFirewall interface
  • FirewallAPIv2.FirewallProfile: Contains properties of a Windows Firewall with Advanced Security profile - Implementing IProfile interface
  • FirewallAPIv2.Rules.StandardRule: Contains properties of a Windows Firewall with Advanced Security rule - Implementing IRule interface
  • FirewallAPIv2.Rules.StandardRuleWin7: Contains properties of a Windows Firewall with Advanced Security rule for Windows 7+ - Extending FirewallAPIv2.Rules.StandardRule
  • FirewallAPIv2.Rules.StandardRuleWin8: Contains properties of a Windows Firewall with Advanced Security rule for Windows 8+ - Extending FirewallAPIv2.Rules.StandardRuleWin7
  • FirewallAPIv2.InternetControlMessage: A class representing an Internet Control Message (ICM) type
  • FirewallAPIv2.FirewallAPIv2NotSupportedException: The exception that is thrown when an invoked method is not supported with the 'Windows Firewall with Advanced Security' - Extending NotSupportedException

EXAMPLES

Check the 'WindowsFirewallHelper.Sample' project as a brief example of what can be done using this class library. Screenshot

BASIC EXAMPLES

  • Creating and registering a new application exception rule for out-bound traffic on the currently enable profile:
  var rule = FirewallManager.Instance.CreateApplicationRule(
        FirewallManager.Instance.GetProfile().Type, @"MyApp Rule", 
        FirewallAction.Allow, @"C:\MyApp.exe");
  rule.Direction = FirewallDirection.Outbound;
  FirewallManager.Instance.Rules.Add(rule);
  • Creating and registering a new port rule for in-bound traffic on the currently enable profile:
  var rule = FirewallManager.Instance.CreatePortRule(
      FirewallManager.Instance.GetProfile().Type,
      @"Port 80 - Any Protocol", FirewallAction.Allow, 80, 
      FirewallProtocol.Any);
  FirewallManager.Instance.Rules.Add(rule);
  • Getting the list of all registered rules:
  var allRules = FirewallManager.Instance.Rules.ToArray();
  • Removing a rule by name:
  var myRule = FirewallManager.Instance.Rules.SingleOrDefault(r => r.Name == "My Rule");
  if (myRule != null)
  {
    FirewallManager.Instance.Rules.Remove(myRule);
  }
  • Disabling notifications for all firewall profiles:
  foreach (var profile in FirewallManager.Instance.Profiles)
  {
    profile.ShowNotifications = false;
  }

ADVANCED EXAMPLES

  • Creating a heavily customized application rule (Some parts of the following code are only applicable to Windows Vista, Windows 7 and above):
  var rule = FirewallManager.Instance.CreatePortRule(
      FirewallManager.Instance.GetProfile().Type,
      @"Port 80 - Any Protocol", FirewallAction.Allow, 80, 
      FirewallProtocol.Any);
  if (rule is FirewallAPIv2.Rules.StandardRule)
  {
    ((FirewallAPIv2.Rules.StandardRule)rule).Interfaces =
        NetworkInterface.GetAllNetworkInterfaces()
            .Where(@interface => @interface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
            .ToArray();
    ((FirewallAPIv2.Rules.StandardRule)rule).IcmpTypesAndCodes = new[]
    {
        new InternetControlMessage(InternetControlMessageKnownTypes.Echo),
        new InternetControlMessage(InternetControlMessageKnownTypes.EchoReply)
    };
    if (rule is FirewallAPIv2.Rules.StandardRuleWin7)
    {
        ((FirewallAPIv2.Rules.StandardRuleWin7)rule).EdgeTraversalOptions = EdgeTraversalAction.Deny;
    }
  }
  rule.Direction = FirewallDirection.Outbound;
  FirewallManager.Instance.Rules.Add(rule);
  • Working directly with the desired firewall management class without using the FirewallManager to add a new port rule (Following example is limited to Windows 8 and above):
  if (FirewallAPIv2.Firewall.Instance.IsSupported && StandardRuleWin8.IsSupported)
  {
    rule = new StandardRuleWin8("My Port Rule", 1080, FirewallAction.Allow,
        FirewallDirection.Inbound,
        FirewallProfiles.Domain | FirewallProfiles.Private | FirewallProfiles.Public)
    {
        Description =
            "'My Port Rule' Allows Inbound traffic to my local Proxy Server from Wireless Adapters",
        InterfaceTypes = FirewallInterfaceTypes.Wireless,
        Protocol = FirewallProtocol.TCP
    };
    FirewallAPIv2.Firewall.Instance.Rules.Add(rule);
  }

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Soroush

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

windowsfirewallhelper's People

Contributors

falahati avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.