Démonstration de l'utilisation de l'API du Firewall de Windows.
Doc MSDN:
http://msdn2.microsoft.com/en-us/library/aa366453.aspx
La classe principale wrapper fournit les méthodes nécessaires à leur maipulation.
-Etat du firewall
-Activation/Desactivation
-Liste des applications autorisés à ouvrir des ports
-Ajouter/Supprimer une application autorisé
-Liste des services étant autorisés à écouter. (Ports ouverts...)
.....
Je n'ai pas exploiter toutes les propriétés/méthodes proposées par les interfaces INetFwxxx. Cependant cela donne un bon point de départ pour ceux désirant enrichir les fonctions de ma classe wrapper.
PS: La source est commentée ;)
Source / Exemple :
using System;
using System.Collections.Generic;
using System.Text;
using DemoINetFwMgr.NetFw;
using System.Runtime.InteropServices;
using System.Collections;
namespace DemoINetFwMgr
{
public static class FwWrapper
{
/// <summary>
/// Fournit le statut du firewall de Windows XP SP2.
/// </summary>
/// <returns></returns>
public static bool FirewallStatus()
{
//Nouvelle instance
INetFwMgr fwMgr = (INetFwMgr)new NetFwMgr(); //Manager
INetFwProfile fwProfile = fwMgr.LocalPolicy.CurrentProfile; //Du profile
//Fournit l'état de du FireWall
bool bret = fwProfile.FirewallEnabled;
//Libère les réf. COM appelé par le runtime.
Marshal.ReleaseComObject(fwProfile);
Marshal.ReleaseComObject(fwMgr);
return bret;
}
/// <summary>
/// Activation du firewall de Windows XP SP2.
/// </summary>
/// <param name="enable">True pour activer; False pour désactiver.</param>
public static void EnableFirewall(bool enable)
{
EnableFirewall(enable, false);
}
/// <summary>
/// Activation du firewall de Windows XP SP2.
/// </summary>
/// <param name="enable">True pour activer; False pour désactiver.</param>
/// <param name="ExceptionsNotAllowed">Autorise les exeptions.</param>
public static void EnableFirewall(bool enable, bool ExceptionsNotAllowed)
{
INetFwMgr fwMgr = (INetFwMgr)new NetFwMgr();
INetFwProfile fwProfile = fwMgr.LocalPolicy.CurrentProfile;
//Applique le nouvel etat.
fwProfile.FirewallEnabled = enable;
fwProfile.ExceptionsNotAllowed = ExceptionsNotAllowed;
Marshal.ReleaseComObject(fwProfile);
Marshal.ReleaseComObject(fwMgr);
}
/// <summary>
/// Fournit une collection des ports ouverts autorisés par les applications.
/// </summary>
/// <returns></returns>
public static List<FwAuthAppInfo> AuthorizedApplications()
{
List<FwAuthAppInfo> lsAuthApps = new List<FwAuthAppInfo>();
INetFwMgr fwMgr = (INetFwMgr)new NetFwMgr();
INetFwProfile fwProfile = fwMgr.LocalPolicy.CurrentProfile;
INetFwAuthorizedApplications fwAuthApps = fwProfile.AuthorizedApplications;
//Récupère et prise en charge d'une collection de type INetFwAuthorizedApplication.
IEnumerator e = fwAuthApps._NewEnum;
//Itération sur la collection.
while (e.MoveNext())
{
//Obtient l'élément INetFwAuthorizedApplication courant.
INetFwAuthorizedApplication fwAuthApp = (INetFwAuthorizedApplication)e.Current;
//Ajoute à la collection
lsAuthApps.Add(new FwAuthAppInfo(fwAuthApp.Name, fwAuthApp.ProcessImageFileName, fwAuthApp.IpVersion.ToString(),
fwAuthApp.Scope.ToString(), fwAuthApp.RemoteAddresses, fwAuthApp.Enabled));
Marshal.ReleaseComObject(fwAuthApp);
}
Marshal.ReleaseComObject(fwAuthApps);
Marshal.ReleaseComObject(fwProfile);
Marshal.ReleaseComObject(fwMgr);
return lsAuthApps;
}
/// <summary>
/// Fournit une collection des services qui peuvent être autorisés à écouter.
/// </summary>
/// <returns></returns>
public static List<FwAuthServicesInfo> AuthorizedServicesToListen()
{
List<FwAuthServicesInfo> lsAuthSrvs = new List<FwAuthServicesInfo>();
INetFwMgr fwMgr = (INetFwMgr)new NetFwMgr();
INetFwProfile fwProfile = fwMgr.LocalPolicy.CurrentProfile;
INetFwServices fwAuthSrvs = fwProfile.Services; //Instance d'une collection des services autorisé sur le Firewall.
//Récupère et prise en charge d'une collection de type INetFwAuthorizedApplication.
IEnumerator e = fwAuthSrvs._NewEnum;
//Itération sur la collection.
while (e.MoveNext())
{
StringBuilder szPorts = new StringBuilder();
INetFwService fwAuthSrv = (INetFwService)e.Current;
INetFwOpenPorts fwPorts = fwAuthSrv.GloballyOpenPorts;
//Récupère et prise en charge d'une collection (type INetFwOpenPorts) des ports (type INetFwOpenPort)
IEnumerator ep = fwPorts._NewEnum;
//Itération sur la collection des ports autorisés.
while (ep.MoveNext())
{
//Ajoute le port à la liste des ports du service.
INetFwOpenPort fwPort = (INetFwOpenPort)ep.Current;
szPorts.AppendFormat("{0}/", fwPort.Port.ToString());
Marshal.ReleaseComObject(fwPort);
}
//Ajoute à la collection des services.
lsAuthSrvs.Add(new FwAuthServicesInfo(fwAuthSrv.Name, fwAuthSrv.Customized.ToString(), fwAuthSrv.Type.ToString(),
szPorts.ToString(), fwAuthSrv.Scope.ToString(), fwAuthSrv.RemoteAddresses, fwAuthSrv.Enabled));
Marshal.ReleaseComObject(fwPorts);
Marshal.ReleaseComObject(fwAuthSrv);
}
Marshal.ReleaseComObject(fwAuthSrvs);
Marshal.ReleaseComObject(fwProfile);
Marshal.ReleaseComObject(fwMgr);
return lsAuthSrvs;
}
/// <summary>
/// Autorise ou non une application à ouvrir des ports.
/// </summary>
/// <param name="appName">Chemin de l'application.</param>
/// <param name="status">True pour autoriser l'application; False pour interdire.</param>
public static void SetAuthApplication(string appName, bool status)
{
INetFwMgr fwMgr = (INetFwMgr)new NetFwMgr();
INetFwProfile fwProfile = fwMgr.LocalPolicy.CurrentProfile;
INetFwAuthorizedApplications fwAuthApps = fwProfile.AuthorizedApplications;
//Obtient l'élément INetFwAuthorizedApplication correspondant au nom de l'image.
INetFwAuthorizedApplication FwAuthApp = fwAuthApps.Item(appName);
//Définit le nouveau statut
FwAuthApp.Enabled = status;
Marshal.ReleaseComObject(fwAuthApps);
Marshal.ReleaseComObject(fwProfile);
Marshal.ReleaseComObject(fwMgr);
}
/// <summary>
/// Supprime une application de celle autorisées.
/// </summary>
/// <param name="appName">Chemin de l'application.</param>
public static void RemoveAuthApplicationFromCollection(string appName)
{
INetFwMgr fwMgr = (INetFwMgr)new NetFwMgr();
INetFwProfile fwProfile = fwMgr.LocalPolicy.CurrentProfile;
INetFwAuthorizedApplications fwAuthApps = fwProfile.AuthorizedApplications;
//Supprime l'application
fwAuthApps.Remove(appName);
Marshal.ReleaseComObject(fwAuthApps);
Marshal.ReleaseComObject(fwProfile);
Marshal.ReleaseComObject(fwMgr);
}
/// <summary>
/// Ajoute une nouvelle application à la collection des applications autorisées à ouvrir des ports.
/// </summary>
/// <param name="name">Nom.</param>
/// <param name="remoteAddr">Adresses distantes.</param>
/// <param name="AppPath">Chemin de l'appli.</param>
/// <param name="enable">Activer/Désactiver.</param>
/// <param name="scope">Portée.</param>
public static void AddAuthApplication(string name, string remoteAddr, string AppPath, bool enable, NET_FW_SCOPE scope)
{
INetFwMgr fwMgr = (INetFwMgr)new NetFwMgr();
INetFwProfile fwProfile = fwMgr.LocalPolicy.CurrentProfile;
//Nouvelle instance de l'application à rajouter à celles existantes.
INetFwAuthorizedApplication FwNewAuthApp = (INetFwAuthorizedApplication)new NetFwAuthorizedApplication();
//Définit les propriétés
FwNewAuthApp.Enabled = enable;
FwNewAuthApp.Name = name;
FwNewAuthApp.ProcessImageFileName = AppPath;
FwNewAuthApp.RemoteAddresses = remoteAddr;
FwNewAuthApp.Scope = scope;
//Ajoute à la collection la nouvelle app.
INetFwAuthorizedApplications fwAuthApps = fwProfile.AuthorizedApplications;
fwAuthApps.Add(FwNewAuthApp);
Marshal.ReleaseComObject(FwNewAuthApp);
Marshal.ReleaseComObject(fwAuthApps);
Marshal.ReleaseComObject(fwProfile);
Marshal.ReleaseComObject(fwMgr);
}
}
}
Conclusion :
Voila :)
Bonne démo à tous ;)