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

2016-02-19 13:48 1 1 收藏

想不想get新技能酷炫一下,今天图老师小编就跟大家分享个简单的在Vista中编程控制防火墙设定教程,一起来看看吧!超容易上手~

【 tulaoshi.com - 编程语言 】

在编程控制防火墙前先要有个前提,就是你必须是管理员权限, 这样本例的程序才能用"Run as administrator"的方式运行,并成功修改。 如果你本身就是用Administrator这个用户登录的话,直接运行就行了。 建议最好在这个用户下来调试程序。

  本程序只是个初始的例子,里面的功能只开发了一部分,各位有兴趣的话可以继续深入运用。 像Vista的防火墙就比较Bt,除了基本设定外,在"Control PanelAdministrative ToolsWindows 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 ) // 关闭放行的应用程序。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  里面还有个 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也是一样的。核心程序如下:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/) 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 );
/

来源:https://www.tulaoshi.com/n/20160219/1605248.html

延伸阅读
这次测试的目的是为了知道防火墙是否想我们想象中的意图来工作的。在此之前你必须: ·制定一个完整的测试计划,测试的意图主要集中在路由、包过滤、日志记录与警报的性能上 ·测试当防火墙系统处于非正常工作状态时的恢复防御方案 ·设计你的初步测试组件 其中比较重要的的测试包括: ·硬件测试(处理器、内外储存...
1.什么是防火墙?  防火墙是一个或一组系统,它在网络之间执行访问控制策略。实防火墙的实际方式各不相同,但是在原则上,防火墙可以被认为是这样一对机制:一种机制是拦阻传输流通行,另一种机制是允许传输流通过。一些防火墙偏重拦阻传输流的通行,而另一些防火墙则偏重允许传输流通过。了解有关防火墙的最重要的概念可能就是它实...
〖1〗Perl解释器的漏洞 Netscape Communications Server中无法识别cgi-bin下的扩展名 及其应 用关系,如:.pl是PERL的代码程序自动调用 perl.exe文件解 释,即使现在也只 能把perl.exe文件存放在cgi-bin目录之下。执行 如: /cgi-bin/perl.exe?&my_script.pl. 但是这就给任何人都有执行 perl 的可能, 当有些人在其浏览器的URL...
标签: 防火墙
打开控制面板 点击电脑左下角的开始,然后打开控制面板。 选择防火墙 然后,在控制面板中选择防火墙。 选择打开或关闭Windows防护墙 接着,再选择左侧的打开或关闭Windows防护墙。 选择关闭Windows防火墙 在窗口中选择关闭Windows防火墙,最后点击确定就OK了。 微信公众号搜索“ 图老师 ”或者“ tulaoshi_com ”加关注...
控制Windows的桌面壁纸是一些看图工具常有的功能,要实现该功能是非常容易的,以下的程序使壁纸换为我们想要的图片,如果THEPCHAR为空,那么就取消壁纸,变为Win默认色彩;但这种方法只是暂时的,在WINDOWS重新启动后还是恢复原来的位图,所以要永久保留还需要对WIN.INI文件进行改写操作,才能保存住我们改动的图片不被替换。但这种暂时性...