Oyi*_*wai 5 c# firewall windows-firewall windows-firewall-api firewall-access
一点背景:Basicaly我想为私人和公共网络添加程序防火墙访问规则.
我曾经使用过这个 - "netsh firewall add allowedprogram program ="Path .."name = AppName ENABLE scope = ALL profile = CURRENT"
但是现在我想使用COM对象自动化一个进程.找到了这段闪亮的代码 - http://web.archive.org/web/20070707110141/http://www.dot.net.nz/Default.aspx?tabid=42&mid=404&ctl=Details&ItemID=8
在实现课程后,我一直在尝试使用FirewallHelper.Instance.GrantAuthorization(@"Path ...","AppName",NET_FW_SCOPE_.NET_FW_SCOPE_ALL,NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);
我面临的问题是GrantAuthorization方法只会为公共OR专用网络添加规则,而我的旧netsh命令会为每个网络添加1个规则.
命令实际上看起来非常相似,所以它对我来说有点温和.
那么......如何添加两个网络规则?
肖恩
Heo*_*des 14
我的答案来自大卫的答案,但更详细.并修复有关设置Localports的问题.您需要在设置Localports之前设置Protocol.更多细节如下:
首先,您需要导入参考FirewallAPI.dll.它位于"C:\ Windows\System32\FirewallAPI.dll"中,然后:
using NetFwTypeLib;
Run Code Online (Sandbox Code Playgroud)
并将代码插入到您的:
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
var currentProfiles = fwPolicy2.CurrentProfileTypes;
// Let's create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
inboundRule.Enabled = true;
//Allow through firewall
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
//Using protocol TCP
inboundRule.Protocol = 6; // TCP
//Port 81
inboundRule.LocalPorts = "81";
//Name of rule
inboundRule.Name = "MyRule";
// ...//
inboundRule.Profiles = currentProfiles;
// Now add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
Run Code Online (Sandbox Code Playgroud)
我认为您最好的选择是使用高级安全API与Windows防火墙对话.
快速谷歌"C#INetFwRule2"将向您展示如何注册或更新防火墙规则的大量示例.
为了增加公共和私人政策,我使用了一些东西
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
var currentProfiles = fwPolicy2.CurrentProfileTypes;
// Let's create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
inboundRule.Enabled = true;
inboundRule.LocalPorts = "1234";
inboundRule.Protocol = 6; // TCP
// ...
inboundRule.Profiles = currentProfiles;
// Now add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
Run Code Online (Sandbox Code Playgroud)
小智 7
此页面并没有说这个问题已经得到回答并且已经过时了,所以为了以防万一,为了将来的使用,我会回答这个问题。
首先,导入引用 FirewallAPI.dll,位于“C:\Windows\System32\FirewallAPI.dll”,然后添加 using 指令
using NetFwTypeLib;
Run Code Online (Sandbox Code Playgroud)
该inboundRule.Profiles属性似乎被分类为一组具有以下值的标志(该属性的类型是 int,所以我创建了一个枚举):
public enum FirewallProfiles
{
Domain = 1,
Private = 2,
Public = 4
}
Run Code Online (Sandbox Code Playgroud)
因此,使用该代码,我们可以将配置文件更改为以下内容:
// Create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
// Enable the rule
inboundRule.Enabled = true;
// Allow through firewall
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
// Using protocol TCP
inboundRule.Protocol = 6; // TCP
// Set port number
inboundRule.LocalPorts = "1234";
// Name of rule
inboundRule.Name = "Name Of Firewall Rule";
// Set profiles
inboundRule.Profiles = (int)(FirewallProfiles.Private | FirewallProfiles.Public);
// Add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
Run Code Online (Sandbox Code Playgroud)
或者您可以更改inboundRule.Profiles为 int 值。
两个注意事项:
1:如果您不在管理权限下运行此代码,
firewallPolicty.Rules.Add(inboundRule);
Run Code Online (Sandbox Code Playgroud)
会抛出异常。
2:inboundRule.Profiles必须在值1和7之间。否则会抛出异常
| 归档时间: |
|
| 查看次数: |
14845 次 |
| 最近记录: |