0/5 (5 avis)
Vue 10 288 fois - Téléchargée 640 fois
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); } } }
12 mars 2007 à 09:37
Warny, j'ai jugé utile de ne pas devoir instancié la classe pour utiliser ces méthodes voila tout. Mais il est vrai pour améliorer la chose, on pourrait faire de cette classe une classe singleton, les inferfaces INetFwMgr et INetFwProfile auraient pu etre instancier et détruite avec un Dispose() en implémenant IDisposable.
Jet_D_Ail, tout comptes peut lire les règles, pour modifier la config du Firewall droits admin oblige (en même temps sous xp j'ai rarement vu des comptes non admin du poste)
++
12 mars 2007 à 08:04
Mais ça voudrait dire que n'importe quelle application peut ajouter des règles de firewall ??
11 mars 2007 à 08:48
10 mars 2007 à 11:59
oui elles sont toujours d'un bon niveau les sources de Willi
en tout cas ca va encore m'apprendre des trucs
merci
9 mars 2007 à 08:24
Et très utile en plus...
Bravo!
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.