-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathFirewall.cs
96 lines (85 loc) · 7.61 KB
/
Firewall.cs
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
using MsmhToolsClass;
using static MsmhToolsClass.WindowsFirewall;
namespace SecureDNSClient;
public partial class FormMain
{
public async Task AddOrUpdateFirewallRules()
{
await Task.Run(async () =>
{
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(NL));
bool isFwEnabled = await IsWindowsFirewallEnabledAsync();
if (!isFwEnabled)
{
this.InvokeIt(() => CustomRichTextBoxLog.AppendText($"Windows Firewall Is Not Enabled.{NL}", Color.OrangeRed));
return;
}
List<RuleSet> rules = new()
{
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcIn, ExePath = SecureDNS.CurrentExecutablePath, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcOut, ExePath = SecureDNS.CurrentExecutablePath, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcDnsLookupIn, ExePath = SecureDNS.DnsLookup, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcDnsLookupOut, ExePath = SecureDNS.DnsLookup, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcSDCLookupIn, ExePath = SecureDNS.SDCLookupPath, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcSDCLookupOut, ExePath = SecureDNS.SDCLookupPath, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcAgnosticServerIn, ExePath = SecureDNS.AgnosticServerPath, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcAgnosticServerOut, ExePath = SecureDNS.AgnosticServerPath, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcGoodbyeDpiIn, ExePath = SecureDNS.GoodbyeDpi, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcGoodbyeDpiOut, ExePath = SecureDNS.GoodbyeDpi, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivertIn, ExePath = SecureDNS.WinDivert, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivertOut, ExePath = SecureDNS.WinDivert, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert32In, ExePath = SecureDNS.WinDivert32, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert32Out, ExePath = SecureDNS.WinDivert32, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert64In, ExePath = SecureDNS.WinDivert64, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert64Out, ExePath = SecureDNS.WinDivert64, Direction = RuleDirection.OUT, Action = RuleAction.Allow}
};
for (int n = 0; n < rules.Count; n++)
{
if (IsExiting) break;
RuleSet rule = rules[n];
string ruleName = rule.RuleName;
string exePath = rule.ExePath;
RuleDirection dir = rule.Direction;
RuleAction action = rule.Action;
bool re = await IsRuleExistAsync(ruleName);
string msg = re ? $"Updating Firewall" : $"Creating Firewall";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msg, Color.LightGray));
msg = dir == RuleDirection.IN ? " Inbound " : " Outbound ";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msg, Color.Orange));
msg = "Rule for ";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msg, Color.LightGray));
msg = $"{Path.GetFileName(exePath)}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msg, Color.DodgerBlue));
msg = $"... ";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msg, Color.LightGray));
bool success = await AddOrUpdateRuleAsync(ruleName, exePath, dir, action);
msg = success ? $"Success{NL}" : $"Failed{NL}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msg, success ? Color.MediumSeaGreen : Color.IndianRed));
}
});
}
public static async void AddOrUpdateFirewallRulesNoLog()
{
if (!await IsWindowsFirewallEnabledAsync()) return;
List<RuleSet> rules = new()
{
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcIn, ExePath = SecureDNS.CurrentExecutablePath, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcOut, ExePath = SecureDNS.CurrentExecutablePath, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcDnsLookupIn, ExePath = SecureDNS.DnsLookup, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcDnsLookupOut, ExePath = SecureDNS.DnsLookup, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcSDCLookupIn, ExePath = SecureDNS.SDCLookupPath, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcSDCLookupOut, ExePath = SecureDNS.SDCLookupPath, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcAgnosticServerIn, ExePath = SecureDNS.AgnosticServerPath, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcAgnosticServerOut, ExePath = SecureDNS.AgnosticServerPath, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcGoodbyeDpiIn, ExePath = SecureDNS.GoodbyeDpi, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcGoodbyeDpiOut, ExePath = SecureDNS.GoodbyeDpi, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivertIn, ExePath = SecureDNS.WinDivert, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivertOut, ExePath = SecureDNS.WinDivert, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert32In, ExePath = SecureDNS.WinDivert32, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert32Out, ExePath = SecureDNS.WinDivert32, Direction = RuleDirection.OUT, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert64In, ExePath = SecureDNS.WinDivert64, Direction = RuleDirection.IN, Action = RuleAction.Allow},
new RuleSet{ RuleName = SecureDNS.FirewallRule_SdcWinDivert64Out, ExePath = SecureDNS.WinDivert64, Direction = RuleDirection.OUT, Action = RuleAction.Allow}
};
AddOrUpdateRule(rules);
}
}