aboutsummaryrefslogtreecommitdiff
path: root/src/test/burn/WixTestTools/Firewall/Verifier.cs
blob: 2c273e7a6f99336d7edc988fe5742cf6692de58e (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixTestTools.Firewall
{
    using System;
    using System.Collections.Generic;
    using NetFwTypeLib;
    using Xunit;

    public static class Verifier
    {
        static INetFwRules GetINetFwRules()
        {
            var policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", true);
            var policyInstance = Activator.CreateInstance(policyType);
            var policy2 = policyInstance as INetFwPolicy2;
            return policy2.Rules;
        }

        static INetFwRule3 GetINetFwRule3(string name, UniqueCheck unique)
        {
            var rules = GetINetFwRules();
            INetFwRule3 rule3;

            if (unique != null)
            {
                var enumerator = rules.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    rule3 = enumerator.Current as INetFwRule3;
                    if (!unique.FirewallRuleIsUnique(rule3))
                    {
                        continue;
                    }

                    return rule3;
                }
            }

            var rule1 = rules.Item(name);
            rule3 = rule1 as INetFwRule3;
            return rule3;
        }

        public static RuleDetails GetFirewallRule(string name, UniqueCheck unique)
        {
            var rule = GetINetFwRule3(name, unique);
            var details = new RuleDetails(rule);
            return details;
        }

        public static bool FirewallRuleExists(string name, UniqueCheck unique = null)
        {
            try
            {
                GetINetFwRule3(name, unique);
                return true;
            }
            catch (System.IO.FileNotFoundException)
            {
                return false;
            }
        }

        public static IEnumerable<RuleDetails> GetFirewallRules()
        {
            var rules = GetINetFwRules();
            var enumerator = rules.GetEnumerator();
            while (enumerator.MoveNext())
            {
                var rule3 = enumerator.Current as INetFwRule3;
                yield return new RuleDetails(rule3);
            }
        }

        public static void AddFirewallRule(RuleDetails information)
        {
            var rules = GetINetFwRules();
            var rule1 = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
            var rule3 = rule1 as INetFwRule3;

            rule3.Name = information.Name;

            if (!String.IsNullOrEmpty(information.Description))
            {
                rule3.Description = information.Description;
            }

            if (!String.IsNullOrEmpty(information.ApplicationName))
            {
                rule3.ApplicationName = information.ApplicationName;
            }

            if (!String.IsNullOrEmpty(information.ServiceName))
            {
                rule3.serviceName = information.ServiceName;
            }

            if (information.Protocol.HasValue)
            {
                rule3.Protocol = information.Protocol.Value;
            }

            if (!String.IsNullOrEmpty(information.LocalPorts))
            {
                rule3.LocalPorts = information.LocalPorts;
            }

            if (!String.IsNullOrEmpty(information.RemotePorts))
            {
                rule3.RemotePorts = information.RemotePorts;
            }

            if (!String.IsNullOrEmpty(information.LocalAddresses))
            {
                rule3.LocalAddresses = information.LocalAddresses;
            }

            if (!String.IsNullOrEmpty(information.RemoteAddresses))
            {
                rule3.RemoteAddresses = information.RemoteAddresses;
            }

            if (!String.IsNullOrEmpty(information.IcmpTypesAndCodes))
            {
                rule3.IcmpTypesAndCodes = information.IcmpTypesAndCodes;
            }

            if (information.Direction.HasValue)
            {
                rule3.Direction = information.Direction.Value;
            }

            if (information.Interfaces != null)
            {
                rule3.Interfaces = information.Interfaces;
            }

            if (!String.IsNullOrEmpty(information.InterfaceTypes))
            {
                rule3.InterfaceTypes = information.InterfaceTypes;
            }

            if (information.Enabled.HasValue)
            {
                rule3.Enabled = information.Enabled.Value;
            }

            if (!String.IsNullOrEmpty(information.Grouping))
            {
                rule3.Grouping = information.Grouping;
            }

            if (information.Profiles.HasValue)
            {
                rule3.Profiles = information.Profiles.Value;
            }

            if (information.EdgeTraversal.HasValue)
            {
                rule3.EdgeTraversal = information.EdgeTraversal.Value;
            }

            if (information.Action.HasValue)
            {
                rule3.Action = information.Action.Value;
            }

            if (information.EdgeTraversalOptions.HasValue)
            {
                rule3.EdgeTraversalOptions = information.EdgeTraversalOptions.Value;
            }

            if (!String.IsNullOrEmpty(information.LocalAppPackageId))
            {
                rule3.LocalAppPackageId = information.LocalAppPackageId;
            }

            if (!String.IsNullOrEmpty(information.LocalUserOwner))
            {
                rule3.LocalUserOwner = information.LocalUserOwner;
            }

            if (!String.IsNullOrEmpty(information.LocalUserAuthorizedList))
            {
                rule3.LocalUserAuthorizedList = information.LocalUserAuthorizedList;
            }

            if (!String.IsNullOrEmpty(information.RemoteUserAuthorizedList))
            {
                rule3.RemoteUserAuthorizedList = information.RemoteUserAuthorizedList;
            }

            if (!String.IsNullOrEmpty(information.RemoteMachineAuthorizedList))
            {
                rule3.RemoteMachineAuthorizedList = information.RemoteMachineAuthorizedList;
            }

            if (information.SecureFlags.HasValue)
            {
                rule3.SecureFlags = information.SecureFlags.Value;
            }

            rules.Add(rule3);
        }

        public static void UpdateFirewallRule(string name, RuleDetails information, UniqueCheck unique = null)
        {
            var rule = GetINetFwRule3(name, unique);

            // remove ports so the Protocol can be changed, if required
            if (information.Protocol.HasValue && rule.Protocol != information.Protocol.Value)
            {
                rule.LocalPorts = null;
                rule.RemotePorts = null;
            }

            rule.Name = information.Name;
            rule.Description = information.Description;
            rule.Direction = information.Direction ?? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
            rule.ApplicationName = information.ApplicationName;
            rule.serviceName = information.ServiceName;
            rule.Protocol = information.Protocol ?? 256;
            rule.LocalPorts = information.LocalPorts;
            rule.RemotePorts = information.RemotePorts;
            rule.LocalAddresses = information.LocalAddresses;
            rule.RemoteAddresses = information.RemoteAddresses;
            rule.IcmpTypesAndCodes = information.IcmpTypesAndCodes;
            rule.Interfaces = information.Interfaces;
            rule.InterfaceTypes = information.InterfaceTypes;
            rule.Enabled = information.Enabled ?? false;
            rule.Grouping = information.Grouping;
            rule.Profiles = information.Profiles ?? 0x7fffffff;
            rule.EdgeTraversal = information.EdgeTraversal ?? false;
            rule.Action = information.Action ?? NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
            rule.EdgeTraversalOptions = information.EdgeTraversalOptions ?? 0x0;
            rule.LocalAppPackageId = information.LocalAppPackageId;
            rule.LocalUserOwner = information.LocalUserOwner;
            rule.LocalUserAuthorizedList = information.LocalUserAuthorizedList;
            rule.RemoteUserAuthorizedList = information.RemoteUserAuthorizedList;
            rule.RemoteMachineAuthorizedList = information.RemoteMachineAuthorizedList;
            rule.SecureFlags = information.SecureFlags ?? 0;
        }

        public static void EnableFirewallRule(string name, UniqueCheck unique = null)
        {
            var rule = GetINetFwRule3(name, unique);
            rule.Enabled = true;
        }

        public static void DisableFirewallRule(string name, UniqueCheck unique = null)
        {
            var rule = GetINetFwRule3(name, unique);
            rule.Enabled = false;
        }

        /// <summary>
        /// Removes a firewall rule by name. If multiple rules with the same name exist, only one of them is removed.<br/>
        /// This behavior is different from <b>netsh advfirewall firewall delete rule</b> where all matching rules are deleted if multiple matches are found.<br/>
        /// The firewall rule name cannot be null or an empty string.
        /// </summary>
        /// <param name="name">Name of the firewall rule to be removed.</param>
        public static void RemoveFirewallRuleByName(string name)
        {
            var rules = GetINetFwRules();
            rules.Remove(name);
        }

        static string FormatErrorMessage(string name, string property, object expected, object actual, UniqueCheck unique)
        {
            return $"Assert Failure: {property} differ on rule: {name}" +
                "\nExpected: " + expected +
                "\nActual: " + actual +
                "\n\nDirection: " + unique?.Direction +
                "\nProfile: " + unique?.Profile +
                "\nProtocol: " + unique?.Protocol +
                "\nApplicationName: " + unique?.ApplicationName +
                "\nLocalUserOwner: " + unique?.LocalUserOwner;
        }

        public static void VerifyFirewallRule(string name, RuleDetails expected, UniqueCheck unique = null)
        {
            var actual = GetFirewallRule(name, unique);
            Assert.True(expected.Name == actual.Name, String.Format("Assert Failure: Names differ on rule: \nExpected: {0}\nActual: {1}", expected.Name, actual.Name));
            Assert.True(expected.Description == actual.Description, FormatErrorMessage(name, "Descriptions", expected.Description, actual.Description, unique));
            Assert.True(expected.ApplicationName == actual.ApplicationName, FormatErrorMessage(name, "ApplicationNames", expected.ApplicationName, actual.ApplicationName, unique));
            Assert.True(expected.ServiceName == actual.ServiceName, FormatErrorMessage(name, "ServiceNames", expected.ServiceName, actual.ServiceName, unique));
            Assert.True(expected.Protocol == actual.Protocol, FormatErrorMessage(name, "Protocols", expected.Protocol, actual.Protocol, unique));
            Assert.True(expected.LocalPorts == actual.LocalPorts, FormatErrorMessage(name, "LocalPorts", expected.LocalPorts, actual.LocalPorts, unique));
            Assert.True(expected.LocalAddresses == actual.LocalAddresses, FormatErrorMessage(name, "LocalAddresses", expected.LocalAddresses, actual.LocalAddresses, unique));
            Assert.True(expected.RemotePorts == actual.RemotePorts, FormatErrorMessage(name, "RemotePorts", expected.RemotePorts, actual.RemotePorts, unique));
            Assert.True(expected.RemoteAddresses == actual.RemoteAddresses, FormatErrorMessage(name, "RemoteAddresses", expected.RemoteAddresses, actual.RemoteAddresses, unique));
            Assert.True(expected.IcmpTypesAndCodes == actual.IcmpTypesAndCodes, FormatErrorMessage(name, "IcmpTypesAndCodes", expected.IcmpTypesAndCodes, actual.Description, unique));
            Assert.True(expected.Direction == actual.Direction, FormatErrorMessage(name, "Directions", expected.Direction, actual.Direction, unique));
            Assert.Equal<object>(expected.Interfaces, actual.Interfaces);
            Assert.True(expected.InterfaceTypes == actual.InterfaceTypes, FormatErrorMessage(name, "InterfaceTypes", expected.InterfaceTypes, actual.InterfaceTypes, unique));
            Assert.True(expected.Enabled == actual.Enabled, FormatErrorMessage(name, "Enabled flags", expected.Enabled, actual.Enabled, unique));
            Assert.True(expected.Grouping == actual.Grouping, FormatErrorMessage(name, "Groupings", expected.Grouping, actual.Grouping, unique));
            Assert.True(expected.Profiles == actual.Profiles, FormatErrorMessage(name, "Profiles", expected.Profiles, actual.Profiles, unique));
            Assert.True(expected.EdgeTraversal == actual.EdgeTraversal, FormatErrorMessage(name, "EdgeTraversals", expected.EdgeTraversal, actual.EdgeTraversal, unique));
            Assert.True(expected.Action == actual.Action, FormatErrorMessage(name, "Actions", expected.Action, actual.Action, unique));
            Assert.True(expected.EdgeTraversalOptions == actual.EdgeTraversalOptions, FormatErrorMessage(name, "EdgeTraversalOptions", expected.EdgeTraversalOptions, actual.EdgeTraversalOptions, unique));
            Assert.True(expected.LocalAppPackageId == actual.LocalAppPackageId, FormatErrorMessage(name, "LocalAppPackageIds", expected.LocalAppPackageId, actual.LocalAppPackageId, unique));
            Assert.True(expected.LocalUserOwner == actual.LocalUserOwner, FormatErrorMessage(name, "LocalUserOwners", expected.LocalUserOwner, actual.LocalUserOwner, unique));
            Assert.True(expected.LocalUserAuthorizedList == actual.LocalUserAuthorizedList, FormatErrorMessage(name, "LocalUserAuthorizedLists", expected.LocalUserAuthorizedList, actual.LocalUserAuthorizedList, unique));
            Assert.True(expected.RemoteUserAuthorizedList == actual.RemoteUserAuthorizedList, FormatErrorMessage(name, "RemoteUserAuthorizedLists", expected.RemoteUserAuthorizedList, actual.RemoteUserAuthorizedList, unique));
            Assert.True(expected.RemoteMachineAuthorizedList == actual.RemoteMachineAuthorizedList, FormatErrorMessage(name, "RemoteMachineAuthorizedLists", expected.RemoteMachineAuthorizedList, actual.RemoteMachineAuthorizedList, unique));
            Assert.True(expected.SecureFlags == actual.SecureFlags, FormatErrorMessage(name, "SecureFlags", expected.SecureFlags, actual.SecureFlags, unique));
        }
    }
}