WEB开发网
开发学院操作系统Windows Vista 在Vista中编程控制防火墙设定 阅读

在Vista中编程控制防火墙设定

 2007-09-01 16:37:44 来源:WEB开发网   
核心提示: 首先,创建一个Console程序,在Vista中编程控制防火墙设定(2),在程序中添加引用,在COM对象中找到"NetFwTypeLib" ,添加即可, 防火墙主要是靠这个对象操作的

首先,创建一个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 );
//}
//catch( Exception e )
//{
// Console.WriteLine( e.Message );
//}
Console.WriteLine( "FireWall Enabled: " + NetFwProfile.FirewallEnabled );
}
public bool FireWallState()
{
return NetFwProfile.FirewallEnabled;
}
public void FireWallService( string name, bool enable )
{
try
{
foreach( INetFwService serv in NetFwProfile.Services )
{
if( serv.Name.ToUpper() == name.ToUpper() )
{
serv.Enabled = enable;
Console.WriteLine( "Service: " + serv.Name + ": " + serv.Enabled );
return;
}
}
Console.WriteLine( "The service '{0}' does not exist!", name );
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
}
private NET_FW_IP_PROTOCOL_ GetProtocol( string protocol )
{
NET_FW_IP_PROTOCOL_ prot;
if( protocol.ToUpper() == "TCP"
)
prot = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
else if( protocol.ToUpper() == "UDP" )
prot = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
else
prot = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_ANY;
return prot;
}
public bool AddPort( string portName, int portNumber, string protocol )
{
try
{
INetFwOpenPort port = ( INetFwOpenPort )Activator.CreateInstance(
Type.GetTypeFromProgID( "HNetCfg.FwOpenPort" )
);
port.Name = portName;
port.Port = portNumber;
port.Protocol = GetProtocol( protocol );
port.Enabled = true;
NetFwProfile.GloballyOpenPorts.Add( port );
return true;
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
return false;
}
public bool RemovePort( int portNumber, string protocol )
{
try
{
NetFwProfile.GloballyOpenPorts.Remove( portNumber, GetProtocol( protocol ) );
return true;
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
return false;
}
public bool AddAplication( string discriptionName, string fileName )
{
try
{
INetFwAuthorizedApplication app = ( INetFwAuthorizedApplication )Activator.CreateInstance(
Type.GetTypeFromProgID( "HNetCfg.FwAuthorizedApplication" )
);
app.Name = discriptionName;
app.ProcessImageFileName = fileName;
app.Enabled = true;
NetFwProfile.AuthorizedApplications.Add( app );
return true;
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
return false;
}
public bool RemoveApplication( string fileName )
{
try
{
NetFwProfile.AuthorizedApplications.Remove( fileName );
return true;
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
return false;
}
protected Object getInstance( String typeName )
{
if( typeName == "INetFwMgr" )
{
Type type = Type.GetTypeFromCLSID( new Guid( "{304CE942-6E39-40D8-943A-B913C40C9CD4}" ) );
return Activator.CreateInstance( type );
}
else if( typeName == "INetAuthApp" )
{
Type type = Type.GetTypeFromCLSID( new Guid( "{EC9846B3-2762-4A6B-A214-6ACB603462D2}" ) );
return Activator.CreateInstance( type );
}
else if( typeName == "INetOpenPort" )
{
Type type = Type.GetTypeFromCLSID( new Guid( "{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}" ) );
return Activator.CreateInstance( type );
}
else
return null;
}
}
}

上一页  1 2 

Tags:Vista 编程 控制

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接