腾讯云的Windows服务器防火墙,通过设置陷阱端口,屏蔽恶意访问的IP,将IP加入到腾讯云的安全组规则中,同时加入到服务器的防火墙中
unknown
2022-06-29 00ebde85aa3d586be2d2dfb755d01bf1b4b3025b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetFwTypeLib;
namespace FirewallWindowsServer.Methods
{
    public class Demo
    {
        /// <summary>
        /// 为WindowsDefender防火墙添加一条通信端口出站规则
        /// </summary>
        /// <param name="type">规则类型</param>
        /// <param name="ruleName">规则名称</param>
        /// <param name="appPath">应用程序完整路径</param>
        /// <param name="localAddresses">本地地址</param>
        /// <param name="localPorts">本地端口</param>
        /// <param name="remoteAddresses">远端地址</param>
        /// <param name="remotePorts">远端端口</param>
        public static bool CreateOutRule(NET_FW_IP_PROTOCOL_ type, string ruleName, string appPath, string localAddresses = null, string localPorts = null, string remoteAddresses = null, string remotePorts = null)
        {
            //创建防火墙策略类的实例
            INetFwPolicy2 policy2 = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            //检查是否有同名规则
            foreach (INetFwRule item in policy2.Rules)
            {
                if (item.Name == ruleName)
                {
                    return true;
                }
            }
            //创建防火墙规则类的实例: 有关该接口的详细介绍:https://docs.microsoft.com/zh-cn/windows/win32/api/netfw/nn-netfw-inetfwrule
            INetFwRule rule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
            //为规则添加名称
            rule.Name = ruleName;
            //为规则添加描述
            rule.Description = "禁止程序访问非指定端口";
            //选择入站规则还是出站规则,IN为入站,OUT为出站
            rule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
            //为规则添加协议类型
            rule.Protocol = (int)type;
            //为规则添加应用程序(注意这里是应用程序的绝对路径名)
            rule.ApplicationName = appPath;
            //为规则添加本地IP地址
            if (!string.IsNullOrEmpty(localAddresses))
            {
                rule.LocalAddresses = localAddresses;
            }
            //为规则添加本地端口
            if (!string.IsNullOrEmpty(localPorts))
            {
                //需要移除空白字符(不能包含空白字符,下同)
                rule.LocalPorts = localPorts.Replace(" ", "");// "1-29999, 30003-33332, 33334-55554, 55556-60004, 60008-65535";
            }
            //为规则添加远程IP地址
            if (!string.IsNullOrEmpty(remoteAddresses))
            {
                rule.RemoteAddresses = remoteAddresses;
            }
            //为规则添加远程端口
            if (!string.IsNullOrEmpty(remotePorts))
            {
                rule.RemotePorts = remotePorts.Replace(" ", "");
            }
            //设置规则是阻止还是允许(ALLOW=允许,BLOCK=阻止)
            rule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
            //分组名
            rule.Grouping = "GroupsName";
            rule.InterfaceTypes = "All";
            //是否启用规则
            rule.Enabled = true;
            try
            {
                //添加规则到防火墙策略
                policy2.Rules.Add(rule);
            }
            catch (Exception e)
            {
                string error = $"防火墙添加规则出错:{ruleName} {e.Message}";
                //AppLog.Error(error);
                throw new Exception(error);
            }
            return true;
        }
        /// <summary>
        /// 为WindowsDefender防火墙添加一条U3D通信TCP端口出站规则
        /// </summary>
        /// <param name="appPath">应用程序完整路径</param>
        /// <param name="localAddresses">本地地址</param>
        /// <param name="localPorts">本地端口</param>
        /// <param name="remoteAddresses">远端地址</param>
        /// <param name="remotePorts">远端端口</param>
        public static bool CreateTCPOutRule(string appPath, string localAddresses = null, string localPorts = null, string remoteAddresses = null, string remotePorts = null)
        {
            try
            {
                string ruleName = $"{System.IO.Path.GetFileNameWithoutExtension(appPath)}TCP";
                CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, ruleName, appPath, localAddresses, localPorts, remoteAddresses, remotePorts);
            }
            catch (Exception e)
            {
                //AppLog.Error(e.Message);
                throw new Exception(e.Message);
            }
            return true;
        }
        /// <summary>
        /// 为WindowsDefender防火墙添加一条通信UDP端口出站规则
        /// </summary>
        /// <param name="appPath">应用程序完整路径</param>
        /// <param name="localAddresses">本地地址</param>
        /// <param name="localPorts">本地端口</param>
        /// <param name="remoteAddresses">远端地址</param>
        /// <param name="remotePorts">远端端口</param>
        public static bool CreateUDPOutRule(string appPath, string localAddresses = null, string localPorts = null, string remoteAddresses = null, string remotePorts = null)
        {
            try
            {
                string ruleName = $"{System.IO.Path.GetFileNameWithoutExtension(appPath)}UDP";
                CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP, ruleName, appPath, localAddresses, localPorts, remoteAddresses, remotePorts);
            }
            catch (Exception e)
            {
                //AppLog.Error(e.Message);
                throw new Exception(e.Message);
            }
            return true;
        }
        /// <summary>
        /// 删除WindowsDefender防火墙规则
        /// <summary>
        /// <param name="appPath">应用程序完整路径</param>
        public static bool DeleteRule(string appPath)
        {
            //创建防火墙策略类的实例
            INetFwPolicy2 policy2 = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            string ruleName = System.IO.Path.GetFileNameWithoutExtension(appPath);
            try
            {
                //根据规则名称移除规则
                policy2.Rules.Remove(ruleName);
            }
            catch (Exception e)
            {
                string error = $"防火墙删除规则出错:{ruleName} {e.Message}";
                //AppLog.Error(error);
                throw new Exception(error);
            }
            return true;
        }
    }
}