程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> 在Vista中編程控制防火牆設定

在Vista中編程控制防火牆設定

編輯:C#基礎知識
  在編程控制防火牆前先要有個前提,就是你必須是管理員權限, 這樣本例的程序才能用"Run as administrator"的方式運行,並成功修改。 如果你本身就是用Administrator這個用戶登錄的話,直接運行就行了。 建議最好在這個用戶下來調試程序。

  本程序只是個初始的例子,裡面的功能只開發了一部分,各位有興趣的話可以繼續深入運用。 像Vista的防火牆就比較Bt,除了基本設定外,在"Control Panel\Administrative Tools\Windows Firewall with Advanced Security" 還有高級設定,好像用程序都可控制。

  FireWallManager 程序主要功能有

  1. public void FireWallTrigger( bool enable ) //開關防火牆。 貌似在Vista裡面有問題,XP sp2好像可以。 但是用INetFwPolicy2.set_FirewallEnabled的方法的話,Vista也能搞定。

  2. public void FireWallService( string name, bool enable ) //開關防火牆服務程序,一般裡面的 File and Printer Sharing 服務比較有用。

  3. public bool AddPort( string portName, int portNumber, string protocol ) // 開啟一個端口。

  4. public bool RemovePort( int portNumber, string protocol ) //刪除開啟的端口

  5. public bool AddAplication( string discriptionName, string fileName ) //開啟放行應用程序

  6. public bool RemoveApplication( string fileName ) // 關閉放行的應用程序。

  裡面還有個 protected Object getInstance( String typeName ) 本來是用CLSID來實例化那些接口的,後來發現ProgID其實更簡單,不需要查,裡面有個規律,只需把接口的INet刪掉就是ProgID了。 如 INetFwOpenPort port = ( INetFwOpenPort )Activator.CreateInstance( Type.GetTypeFromProgID( "HNetCfg.FwOpenPort" ) ); 中 INetFwOpenPort 與 FwOpenPort.

  首先,創建一個Console程序,在程序中添加引用,在COM對象中找到"NetFwTypeLib" ,添加即可。 防火牆主要是靠這個對象操作的。 貌似不止Vista, Xp也是一樣的。核心程序如下:

 FireWallManager.cs
using System;
using System.Collections.Generic;
using System.Text;
using NetFwTypeLib;
namespace FirewallManager
{
class FwManager
{
private INetFwMgr NetFwMgr;
private INetFwProfile NetFwProfile;
private INetFwPolicy2 NetFwPolicy2; //this interface contains lots of usefull functions.
public FwManager()
{
//Create Com Object
//Type NetFwMgrType = Type.GetTypeFromCLSID( new Guid( "{304CE942-6E39-40D8-943A-B913C40C9CD4}" ) );
Type NetFwMgrType = Type.GetTypeFromProgID( "HNetCfg.FwMgr" );
object NetFwMgrObject = Activator.CreateIn
stance( NetFwMgrType );
NetFwMgr = ( INetFwMgr )NetFwMgrObject;
NetFwProfile = NetFwMgr.LocalPolicy.CurrentProfile;
Type NetFwPolicy2Type = Type.GetTypeFromProgID( "HNetCfg.FwPolicy2" );
object NetFwPolicy2Object = System.Activator.CreateInstance( NetFwPolicy2Type );
NetFwPolicy2 = ( INetFwPolicy2 )NetFwPolicy2Object;
}
public void ShowInfo()
{
switch( NetFwProfile.Type )
{
case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN:
Console.WriteLine( "Network Profile Type1: " + "Domain" );
break;
case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD:
Console.WriteLine( "Network Profile Type1: " + "Standard" );
break;
case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT:
Console.WriteLine( "Network Profile Type1: " + "Current" );
break;
case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_TYPE_MAX:
Console.WriteLine( "Network Profile Type1: " + "Max" );
break;
}
switch( ( NET_FW_PROFILE_TYPE2_ )NetFwPolicy2.CurrentProfileTypes )
{
case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN:
Console.WriteLine( "Network Profile Type2: " + "Domain" );
break;
case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE:
Console.WriteLine( "Network Profile Type2: " + "Private" );
break;
case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC:
Console.WriteLine( "Network Profile Type2: " + "Public" );
break;
case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL:
Console.WriteLine( "Network Profile Type2: " + "All" );
break;
}
Console.WriteLine( "Firewall Enabled: " + NetFwProfile.FirewallEnabled );
Console.WriteLine( "Exceptions Not Allowed: " + NetFwProfile.ExceptionsNotAllowed );
Console.WriteLine( "Notifications Disabled: " + NetFwProfile.NotificationsDisabled );
//Console.WriteLine("UnicastResponsestoMulticastBroadcastDisabled: " + NetFwProfile.UnicastResponsestoMulticastBroadcastDisabled);
//Remote Admin
INetFwRemoteAdminSettings RASettings = NetFwP
rofile.RemoteAdminSettings;
Console.WriteLine( "Remote Administration Enabled: " + RASettings.Enabled );
switch( RASettings.IpVersion )
{
case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4:
Console.WriteLine( "Remote Administration IP Version: V4" );
break;
case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V6:
Console.WriteLine( "Remote Administration IP Version: V6" );
break;
case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_MAX:
Console.WriteLine( "Remote Administration IP Version: MAX" );
break;
case NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY:
Console.WriteLine( "Remote Administration IP Version: ANY" );
break;
}
switch( RASettings.Scope )
{
case NET_FW_SCOPE_.NET_FW_SCOPE_ALL:
Console.WriteLine( "Remote Administration Scope: ALL" );
break;
case NET_FW_SCOPE_.NET_FW_SCOPE_CUSTOM:
Console.WriteLine( "Remote Administration Scope: Custom" );
break;
case NET_FW_SCOPE_.NET_FW_SCOPE_LOCAL_SUBNET:
Console.WriteLine( "Remote Administration Scope: Local Subnet" );
break;
case NET_FW_SCOPE_.NET_FW_SCOPE_MAX:
Console.WriteLine( "Remote Administration Scope: MAX" );
break;
}
// ICMP
INetFwIcmpSettings icmpSettings = NetFwProfile.IcmpSettings;
Console.WriteLine( "ICMP Settings:" );
Console.WriteLine( " AllowOutboundDestinationUnreachable: " + icmpSettings.AllowOutboundDestinationUnreachable );
Console.WriteLine( " AllowOutboundSourceQuench: " + icmpSettings.AllowOutboundSourceQuench );
Console.WriteLine( " AllowRedirect: " + icmpSettings.AllowRedirect );
Console.WriteLine( " AllowInboundEchoRequest: " + icmpSettings.AllowInboundEchoRequest );
Console.WriteLine( " AllowInboundRouterRequest: " + icmpSettings.AllowInboundRouterRequest );
Console.WriteLine( " AllowOutboundTimeExceeded: " + icmpSettings.AllowOutboundTimeExceeded );
Console.WriteLine( " AllowOutboundParameterProblem: " + icmpSettings.AllowOutboundParameterProblem );
Console
.WriteLine( " AllowInboundTimestampRequest: " + icmpSettings.AllowInboundTimestampRequest );
Console.WriteLine( " AllowInboundMaskRequest: " + icmpSettings.AllowInboundMaskRequest );
// Gloabal Open ports
foreach( INetFwOpenPort port in NetFwProfile.GloballyOpenPorts )
{
Console.WriteLine( "Open port: " + port.Name + ":" + port.Port + ", " + port.Protocol + " " + port.Enabled );
}
// Services
foreach( INetFwService serv in NetFwProfile.Services )
{
Console.WriteLine( "Service: " + serv.Name + ": " + serv.Enabled );
}
// Autorised Applications
foreach( INetFwAuthorizedApplication app in NetFwProfile.AuthorizedApplications )
{
Console.WriteLine( "AuthorizedApplication: " + app.Name + ": " + app.Enabled );
}
Console.WriteLine();
}
public void FireWallTrigger( bool enable )
{
try
{
NetFwProfile.FirewallEnabled = enable;
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
//try
//{
// NetFwPolicy2.set_FirewallEnabled( NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, enable );
/
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved