• Stars
    star
    246
  • Rank 159,390 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

This project supports dotNet4.6, NetStandard2 and NetCore5, therefore, is compatible with NetCore2+ and any version of dotNet equal or greater than version 4.6.

Even though it is possible to reference this library under Linux or Mac; it's obviously not going to work.

This readme file is for the version 2 of this library. Please check the V1 branch for older readme file.

How to get

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

Help me fund my own Death Star

--OR--

You can always donate your time by contributing to the project or by introducing it to others.

How to use

The 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.

If you are only targeting WinVista+ consider using the FirewallWAS.Instance static property to access the library's functionality. It allows for more flexibility and is easier to work with.

WindowsFirewallHelper.FirewallManager Class

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

WindowsFirewallHelper.FirewallManager Static Properties

  • FirewallManager.Instance: Gets an instance of the Windows Firewall management class.
  • FirewallManager.Version: Gets the type of firewall that FirewallManager.Instance property returns.
  • FirewallManager.IsServiceRunning: Gets a boolean value indicating if the Windows Firewall Service is currently running.
  • FirewallManager.RegisteredProducts: Gets an array containing all registered third party firewall products.

WindowsFirewallHelper.FirewallManager Static Methods

  • FirewallManager.RegisterProduct(): Registers a third-party firewall product returning a handle that will unregisters the product while getting disposed.

WindowsFirewallHelper Namespace

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

WindowsFirewallHelper Classes

  • FirewallManager: A static class to manage the current active firewall
  • FirewallProtocol: A class representing a Firewall Protocol
  • FirewallLegacy: Contains properties and methods of Windows Firewall v1 - Implementing the IFirewall interface
  • FirewallLegacyProfile: Contains properties of a Windows Firewall v1 profile - Implementing the IFirewallProfile interface
  • FirewallWAS: Contains properties and methods of Windows Firewall with Advanced Security (Vista+) - Implementing the IFirewall interface
  • FirewallWASProfile: Contains properties of a Windows Firewall with Advanced Security profile (Vista+) - Implementing the IFirewallProfile interface
  • FirewallWASRuleGroup: Contains properties and methods for managing a Windows Firewall with Advanced Security rule group (Vista+)
  • FirewallWASInternetControlMessage: Representing an Internet Control Message (ICM) type
  • FirewallProduct: Representing a third-party firewall product
  • FirewallProductRegistrationHandle: Representing a third-party firewall product registration handle that will automatically unregisters the product while getting disposed.

WindowsFirewallHelper Interfaces

  • IFirewall: Defines expected methods and properties of a firewall program or API
  • IFirewallProfile: Defines expected properties of a firewall profile
  • IFirewallRule: Defines expected properties of a firewall rule
  • IAddress: Defines expected methods of a network address

WindowsFirewallHelper.FirewallRules Namespace

This namespace contains classes that can be used for direct manipulation of a firewall rule.

WindowsFirewallHelper.FirewallRules Classes

  • FirewallLegacyApplicationRule: Contains properties of a Windows Firewall v1 application rule - Implementing the IFirewallRule interface
  • FirewallLegacyPortRule: Contains properties of a Windows Firewall v1 port rule - Implementing the IFirewallRule interface
  • FirewallWASRule: Contains properties of a Windows Firewall with Advanced Security rule - Implementing the IFirewallRule interface
  • FirewallWASRuleWin7: Contains properties of a Windows Firewall with Advanced Security rule for Windows 7+ - Extending the FirewallWASRule class
  • FirewallWASRuleWin8: Contains properties of a Windows Firewall with Advanced Security rule for Windows 8+ - Extending the FirewallWASRuleWin7 class

WindowsFirewallHelper.Exceptions Namespace

This namespace contains exception classes that might be thrown when using this library

WindowsFirewallHelper.Exceptions Classes

  • FirewallLegacyNotSupportedException: The exception that is thrown when an invoked method or action is not supported with the 'Windows Firewall API v1' - Extending the NotSupportedException class
  • FirewallWASNotSupportedException: The exception that is thrown when an invoked method or action is not supported with the 'Windows Firewall with Advanced Security' - Extending the NotSupportedException class
  • FirewallWASInvalidProtocolException: The exception that is thrown when a passed FirewallProtocol is invalid for a 'Windows Firewall with Advanced Security' action or method - Extending the `InvalidOperationException`` class

WindowsFirewallHelper.Addresses Namespace

This namespace contains the classes needed for manipulating or understanding a network address or a network service.

WindowsFirewallHelper.Addresses Classes

  • SingleIP: Represents a single network IP address - Implementing the IAddress interface
  • IPRange: Represents a range of network IP addresses - Implementing the IAddress interface
  • NetworkAddress: Represents a range of network IP addresses by subnet - Implementing the IAddress interface
  • SpecialAddress: An abstract class represents a special network address or network service - Implementing the IAddress interface
  • DefaultGateway: Represents the default network gateway - Extending the `SpecialAddress`` class
  • LocalSubnet: Represents thelocal network subnet - Extending the `SpecialAddress`` class
  • DHCPService: Represents the DHCP service - Extending the `SpecialAddress`` class
  • DNSService: Represents the DNS service - Extending the `SpecialAddress`` class
  • WINSService: Represents the WINS service - Extending the `SpecialAddress`` class

WindowsFirewallHelper.COMInterop Namespace

This namespace contains the interfaces and enums that is used to access the underlying COM objects. Some of these types are public and can be used to directly modify a COM object. Usually firewall rules. Rest of types are internal to this library.

Examples

Check the 'WindowsFirewallHelper.Sample' and 'WindowsFirewallHelper.NetCoreSample' projects 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 outbound traffic on the currently active profile:
var rule = FirewallManager.Instance.CreateApplicationRule(
    @"MyApp Rule",
    FirewallAction.Allow,
    @"C:\MyApp.exe"
);
rule.Direction = FirewallDirection.Outbound;
FirewallManager.Instance.Rules.Add(rule);
  • Creating and registering a new port rule for inbound traffic on the currently active profile:
var rule = FirewallManager.Instance.CreatePortRule(
    @"Port 80 - Any Protocol",
    FirewallAction.Allow,
    80,
    FirewallProtocol.TCP
);
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(
    @"Port 80 - Any Protocol",
    FirewallAction.Allow,
    80,
    FirewallProtocol.Any
);
if (rule is FirewallWASRule wasRule)
{
    wasRule.Interfaces = NetworkInterface.GetAllNetworkInterfaces()
        .Where(i => i.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
        .ToArray();
    wasRule.ICMPTypesAndCodes = new[]
    {
        new FirewallWASInternetControlMessage(InternetControlMessageKnownTypes.Echo),
        new FirewallWASInternetControlMessage(InternetControlMessageKnownTypes.EchoReply)
    };
    if (rule is FirewallWASRuleWin7 wasRuleWin7)
    {
        wasRuleWin7.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 (FirewallWAS.IsSupported && FirewallWASRuleWin8.IsSupported)
{
    var rule = new FirewallWASRuleWin8(
        "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",
        NetworkInterfaceTypes = NetworkInterfaceTypes.Wireless,
        Protocol = FirewallProtocol.TCP
    };
    FirewallWAS.Instance.Rules.Add(rule);
}

License

The MIT License (MIT)

Copyright (c) 2016-2020 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.

More Repositories

1

LiteDBViewer

LiteDB Viewer is a simple tool that let you open a LiteDB database file and shows its contents; think of it as a GUI for LiteDBShell
Inno Setup
290
star
2

NvAPIWrapper

NvAPIWrapper is a .Net wrapper for NVIDIA public API, capable of managing all aspects of a display setup using NVIDIA GPUs
C#
245
star
3

CircularProgressBar

Circular ProgressBar is a custom control for WinForm with animation.
C#
244
star
4

HeliosDisplayManagement

An open source display profile management program for Windows with support for NVIDIA Surround
C#
239
star
5

NetworkAdapterSelector

A simple solution to let you force bind a program to a specific network adapter
C#
198
star
6

WinFormAnimation

A simple library for animating controls/values in .Net WinForm (.Net 3.5 and later). Key frame (Path) based and fully customizable.
C#
196
star
7

NiVirtualCam

OpenNI 2 Virtual Webcam - Directshow Filter
C#
124
star
8

WindowsDisplayAPI

WindowsDisplayAPI is a .Net wrapper for Windows Display and Windows CCD APIs
C#
74
star
9

UACHelper

A helper class library to detect, manage and use UAC functionalities in your program.
C#
65
star
10

NiWrapper.Net

OpenNI 2 and NiTE 2 .Net Wrapper
C#
49
star
11

PHP-MP3

PHP-MP3 is a simple library for reading and manipulating MPEG audio (MP3)
PHP
42
star
12

EDIDParser

EDIDParser is a library allowing all .Net developers to parse and to extract information from raw EDID binary data. (Extended Display Identification Data)
C#
29
star
13

NVIDIASurroundToggler

[OBSOLETE] NVidia Surround Toggler is a simple tool/program that try to fill the main gap left by NVIDIA in their surround technology's user experience by letting the user toggle between the two modes (Surround and Extended) as fast and with less pain as possible. Replaced by: https://github.com/falahati/HeliosDisplayManagement
C#
25
star
14

StartupHelper

A .Net library to add or remove your program to the startup list as well as detecting the startup session. Supporting Windows XP+ with and without administrator rights.
C#
24
star
15

veterans

"Veterans Only" (or simply "veterans") is a Plugin for SourceMod and written with SourcePawn to restrict access of players based on their playtime in a specific game
SourcePawn
16
star
16

HybridBridge

This is a PCL library that let you connect JavaScript on the browser side to the C# side by proxying C# code.
C#
12
star
17

SDroid

SDroid is a framework for designing and running custom bots for Steam network capable of trading and managing the Steam Account they are connected to.
C#
11
star
18

3DPhotoCaptureTool

OpenNI 3D Photo Capture Tool
C#
8
star
19

SharpWorker

SharpWorker is a multi-platform execution environment and helper library for scheduled, API controlled tasks. (examples: Bots, Data Miners, Web Crawlers, API Services, etc)
C#
7
star
20

ConsoleUtilities

ConsoleUtilities is a simple set of tools for creating console navigations, tables and terminals.
C#
6
star
21

SharedDLLCleanup

Cleanup any missing or specified SharedDLL entry from registry; useful for cleaning installation processes
C#
6
star
22

PyWhatsAppWeb

WhatsApp web client automation using selenium in Python
Python
5
star
23

PCIIdentificationResolver

PCI Identification Resolver is a library containing the list of known PCI vendors, devices and other related information for .Net
C#
4
star
24

DownloadHelper

C#
2
star
25

Bitfinex.Net

C#
1
star
26

warmupDM

Warmup DeathMatch (warmupDM) is a Plugin for the Valve's FPS game server, Counter Strike: Global Offensive Dedicated Server using SourceMod and written with SourcePawn
SourcePawn
1
star
27

EnkompassDBackupConverter

Simple command line tool to extract the database script from an enkompass backup file
C#
1
star
28

AnagramFinder

This is a NetStandard C# answer to the TrustPilot's Rabbit Hole Challenge
C#
1
star
29

AdvancedServiceWorker

A customizable and dynamic, yet simple service worker for your website
TypeScript
1
star