Skip to content

Commit

Permalink
Made the way profiles are handled more in line with the rest of the r…
Browse files Browse the repository at this point in the history
…ule properties.
  • Loading branch information
AtlasHackert committed May 30, 2017
1 parent acd4e8c commit 8f677ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
18 changes: 9 additions & 9 deletions Common/Helpers/FirewallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal static string getProfile(int profile_type)
return FirewallHelper.GetProfileAsText(profile_type);
}

public bool ApplyIndirect(bool isTemp, bool currentProfile)
public bool ApplyIndirect(bool isTemp)
{
string actionString;
switch (Action)
Expand All @@ -159,11 +159,11 @@ public bool ApplyIndirect(bool isTemp, bool currentProfile)
{
actionString = "T";
}
string param = Convert.ToBase64String(Encoding.Unicode.GetBytes(String.Format(indParamFormat, Name, ApplicationName, AppPkgId, LUOwn, ServiceName, Protocol, RemoteAddresses, RemotePorts, LocalPorts, currentProfile, actionString)));
string param = Convert.ToBase64String(Encoding.Unicode.GetBytes(String.Format(indParamFormat, Name, ApplicationName, AppPkgId, LUOwn, ServiceName, Protocol, RemoteAddresses, RemotePorts, LocalPorts, Profiles, actionString)));
return ProcessHelper.getProcessFeedback(WFNRuleManagerEXE, param, true, false);
}

public abstract bool Apply(bool isTemp, bool currentProfile);
public abstract bool Apply(bool isTemp);
}

public class WSHRule : Rule
Expand Down Expand Up @@ -429,7 +429,7 @@ public override string ServiceName
// }
//}

public override bool Apply(bool isTemp, bool currentProfile)
public override bool Apply(bool isTemp)
{
try
{
Expand All @@ -447,7 +447,7 @@ public override bool Apply(bool isTemp, bool currentProfile)
firewallRule.Action = Action;
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.Enabled = true;
firewallRule.Profiles = currentProfile ? FirewallHelper.GetCurrentProfile() : FirewallHelper.GetGlobalProfile();
firewallRule.Profiles = Profiles;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = Name;
firewallRule.ApplicationName = ApplicationName;
Expand Down Expand Up @@ -630,7 +630,7 @@ public override string Name
public override string RemotePorts { get { return InnerRule.RemotePorts; } }
public override string ServiceName { get { return InnerRule.serviceName; } }

public override bool Apply(bool isTemp, bool currentProfile)
public override bool Apply(bool isTemp)
{
try
{
Expand Down Expand Up @@ -675,7 +675,7 @@ public class CustomRule : Rule
public override string RemotePorts { get; }
public override string ServiceName { get; }

public override bool Apply(bool isTemp, bool currentProfile)
public override bool Apply(bool isTemp)
{
try
{
Expand All @@ -693,7 +693,7 @@ public override bool Apply(bool isTemp, bool currentProfile)
firewallRule.Action = Action;
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
firewallRule.Enabled = true;
firewallRule.Profiles = currentProfile ? FirewallHelper.GetCurrentProfile() : FirewallHelper.GetGlobalProfile();
firewallRule.Profiles = Profiles;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = Name;
firewallRule.ApplicationName = ApplicationName;
Expand Down Expand Up @@ -815,7 +815,7 @@ public static bool RemoveRule(string ruleName)
return false;
}

private static int GetGlobalProfile()
public static int GetGlobalProfile()
{
return (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;
}
Expand Down
20 changes: 10 additions & 10 deletions Console/Helpers/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,41 @@ private static bool CreateDefaultRules()
rname = String.Format(Resources.RULE_NAME_FORMAT, "Windows Applications (auto)");
if (rules.All(r => r.Name != rname))
{
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\wwahost.exe", null, null, (string)null, (int)FirewallHelper.Protocols.ANY, null, null, null, 0, "A");
ret = ret && newRule.Apply(false, false);
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\wwahost.exe", null, null, (string)null, (int)FirewallHelper.Protocols.ANY, null, null, null, FirewallHelper.GetGlobalProfile(), "A");
ret = ret && newRule.Apply(false);
}
}

sc.ServiceName = "wuauserv";
rname = String.Format(Resources.RULE_NAME_FORMAT, sc.DisplayName + " (auto)");
if (rules.All(r => r.Name != rname + " [R:80,443]"))
{
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null, "wuauserv", (int)FirewallHelper.Protocols.TCP, null, "80,443", null, 0, "A");
ret = ret && newRule.Apply(false, false);
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null, "wuauserv", (int)FirewallHelper.Protocols.TCP, null, "80,443", null, FirewallHelper.GetGlobalProfile(), "A");
ret = ret && newRule.Apply(false);
}

sc.ServiceName = "bits";
rname = String.Format(Resources.RULE_NAME_FORMAT, sc.DisplayName + "(auto)");
if (rules.All(r => r.Name != rname + " [R:80,443]"))
{
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null, "bits", (int)FirewallHelper.Protocols.TCP, null, "80,443", null, 0, "A");
ret = ret && newRule.Apply(false, false);
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null, "bits", (int)FirewallHelper.Protocols.TCP, null, "80,443", null, FirewallHelper.GetGlobalProfile(), "A");
ret = ret && newRule.Apply(false);
}

sc.ServiceName = "cryptsvc";
rname = String.Format(Resources.RULE_NAME_FORMAT, sc.DisplayName + "(auto)");
if (rules.All(r => r.Name != rname + " [R:80]"))
{
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null, "cryptsvc", (int)FirewallHelper.Protocols.TCP, null, "80", null, 0, "A");
ret = ret && newRule.Apply(false, false);
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null, "cryptsvc", (int)FirewallHelper.Protocols.TCP, null, "80", null, FirewallHelper.GetGlobalProfile(), "A");
ret = ret && newRule.Apply(false);
}

//sc.ServiceName = "aelookupsvc";
//rname = String.Format(Resources.RULE_NAME_FORMAT, sc.DisplayName + "(auto)");
//if (rules.All(r => r.Name != rname + " [R:80]"))
//{
// FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null,"aelookupsvc", (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, null, "80", null, 0, "A");
// ret = ret && newRule.Apply(false, false);
// FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname, Environment.SystemDirectory + "\\svchost.exe", null, null,"aelookupsvc", (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, null, "80", null, FirewallHelper.GetGlobalProfile(), "A");
// ret = ret && newRule.Apply(false);
//}
}

Expand Down
10 changes: 6 additions & 4 deletions Notifier/UI/Windows/NotificationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ private bool createBlockRule(CurrentConn activeConn, string[] services, bool isT
{
if (Settings.Default.UseBlockRules)
{
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(activeConn.RuleName, activeConn.CurrentPath, activeConn.CurrentAppPkgId, activeConn.CurrentLocalUserOwner, services, _optionsView.IsProtocolChecked ? activeConn.Protocol : -1, _optionsView.IsTargetIPChecked ? activeConn.Target : null, _optionsView.IsTargetPortChecked ? activeConn.TargetPort : null, _optionsView.IsLocalPortChecked ? activeConn.LocalPort : null, 0, "B");
success = newRule.ApplyIndirect(isTemp, _optionsView.IsCurrentProfileChecked);
int Profiles = _optionsView.IsCurrentProfileChecked ? FirewallHelper.GetCurrentProfile() : FirewallHelper.GetGlobalProfile();
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(activeConn.RuleName, activeConn.CurrentPath, activeConn.CurrentAppPkgId, activeConn.CurrentLocalUserOwner, services, _optionsView.IsProtocolChecked ? activeConn.Protocol : -1, _optionsView.IsTargetIPChecked ? activeConn.Target : null, _optionsView.IsTargetPortChecked ? activeConn.TargetPort : null, _optionsView.IsLocalPortChecked ? activeConn.LocalPort : null, Profiles, "B");
success = newRule.ApplyIndirect(isTemp);
if (!success)
{
MessageBox.Show(Common.Properties.Resources.MSG_RULE_FAILED, Common.Properties.Resources.MSG_DLG_ERR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error);
Expand All @@ -431,8 +432,9 @@ private bool createBlockRule(CurrentConn activeConn, string[] services, bool isT

private bool createAllowRule(CurrentConn activeConn, string[] services, bool isTemp)
{
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(activeConn.RuleName, activeConn.CurrentPath, activeConn.CurrentAppPkgId, activeConn.CurrentLocalUserOwner, services, _optionsView.IsProtocolChecked? activeConn.Protocol : -1, _optionsView.IsTargetIPChecked? activeConn.Target: null, _optionsView.IsTargetPortChecked? activeConn.TargetPort: null, _optionsView.IsLocalPortChecked? activeConn.LocalPort: null, 0, "A");
return newRule.ApplyIndirect(isTemp, _optionsView.IsCurrentProfileChecked);
int Profiles = _optionsView.IsCurrentProfileChecked ? FirewallHelper.GetCurrentProfile() : FirewallHelper.GetGlobalProfile();
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(activeConn.RuleName, activeConn.CurrentPath, activeConn.CurrentAppPkgId, activeConn.CurrentLocalUserOwner, services, _optionsView.IsProtocolChecked? activeConn.Protocol : -1, _optionsView.IsTargetIPChecked? activeConn.Target: null, _optionsView.IsTargetPortChecked? activeConn.TargetPort: null, _optionsView.IsLocalPortChecked? activeConn.LocalPort: null, Profiles, "A");
return newRule.ApplyIndirect(isTemp);
}

private void btnSkip_Click(object sender, RoutedEventArgs e)
Expand Down
10 changes: 5 additions & 5 deletions RuleManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static void Main(string[] args)
string target = param[6];
string targetPort = param[7];
string localPort = param[8];
bool useCurrentProfile = bool.Parse(param[9]);
int profile = int.Parse(param[9]);
string action = param[10];
bool keepOpen = false;
bool ret = true;
Expand All @@ -56,8 +56,8 @@ static void Main(string[] args)
case "B":
foreach (var service in services)
{
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname + (service != null ? "[" + service + "]" : ""), path, appPkgId, localUserOwner, service, protocol, target, targetPort, localPort, 0, action);
ret = ret && newRule.Apply(false, useCurrentProfile);
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname + (service != null ? "[" + service + "]" : ""), path, appPkgId, localUserOwner, service, protocol, target, targetPort, localPort, profile, action);
ret = ret && newRule.Apply(false);
}
break;

Expand All @@ -66,8 +66,8 @@ static void Main(string[] args)
foreach (var service in services)
{
tmpnames.Add(service, "[WFN Temp Rule] " + Guid.NewGuid().ToString());
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname + (service != null ? "[" + service + "]" : ""), path, appPkgId, localUserOwner, service, protocol, target, targetPort, localPort, 0, "A"); //FIXME: Hardcoded action!
ret = ret && newRule.Apply(true, useCurrentProfile);
FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(rname + (service != null ? "[" + service + "]" : ""), path, appPkgId, localUserOwner, service, protocol, target, targetPort, localPort, profile, "A"); //FIXME: Hardcoded action!
ret = ret && newRule.Apply(true);
}
keepOpen = true;
break;
Expand Down

0 comments on commit 8f677ab

Please sign in to comment.