private bool createAllowRule(CurrentConn activeConn, string[] services, bool isTemp) { int Profiles = _optionsView.IsCurrentProfileChecked ? FirewallHelper.GetCurrentProfile() : FirewallHelper.GetGlobalProfile(); FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(activeConn.RuleName, activeConn.CurrentPath, _optionsView.IsAppChecked? activeConn.CurrentAppPkgId : null, 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 bool createBlockRule(CurrentConn activeConn, string[] services, bool isTemp) { bool success = false; if (!isTemp) { if (Settings.Default.UseBlockRules) { int Profiles = _optionsView.IsCurrentProfileChecked ? FirewallHelper.GetCurrentProfile() : FirewallHelper.GetGlobalProfile(); FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(activeConn.RuleName, activeConn.CurrentPath, _optionsView.IsAppChecked ? activeConn.CurrentAppPkgId : null, 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); } } else { string entry = (!_optionsView.IsServiceRuleChecked || String.IsNullOrEmpty(activeConn.CurrentService) ? activeConn.CurrentPath : activeConn.CurrentService) + (_optionsView.IsLocalPortChecked ? ";" + activeConn.LocalPort : ";") + (_optionsView.IsTargetIPChecked ? ";" + activeConn.Target : ";") + (_optionsView.IsTargetPortChecked ? ";" + activeConn.TargetPort : ";"); //FIXME: Need to add more? using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exclusions.set"), true)) { sw.WriteLine(entry); } success = true; } } return(success); }
private static bool CreateDefaultRules() { bool ret = true; var rules = FirewallHelper.GetRules(); using (ServiceController sc = new ServiceController()) { string rname; // Windows 8 or higher if (Environment.OSVersion.Version >= new System.Version(6, 2)) { 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, 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, 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, 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, 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, FirewallHelper.GetGlobalProfile(), "A"); // ret = ret && newRule.Apply(false); //} } return(ret); }
static void Main(string[] args) { LogHelper.Debug("Starting RuleManager: " + Environment.CommandLine); try { if (args.Count() == 0) { MessageBox.Show(Resources.MSG_RULEMANAGER_ARGUMENTS_ERR, Resources.MSG_DLG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(1); } if (args.Count() != 1) { throw new ArgumentException("Wrong number of arguments!"); } string[] param = Encoding.Unicode.GetString(Convert.FromBase64String(args[0])).Split(new string[] { "#$#" }, StringSplitOptions.None); if (param.Count() != 11) { throw new ArgumentException("Invalid arguments!"); } string rname = param[0]; string path = param[1]; string appPkgId = param[2]; string localUserOwner = param[3]; string sv = param[4]; string[] services = (sv != null ? sv.Split(',') : new string[] { }); int protocol = int.Parse(param[5]); string target = param[6]; string targetPort = param[7]; string localPort = param[8]; int profile = int.Parse(param[9]); string action = param[10]; bool keepOpen = false; bool ret = true; switch (action) { case "A": 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, profile, action); ret = ret && newRule.Apply(false); } break; case "T": tmpnames = new List <string>(); foreach (var service in services) { string tmpRuleName = "[WFN Temp Rule] " + Guid.NewGuid().ToString(); tmpnames.Add(tmpRuleName); FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(tmpRuleName, path, appPkgId, localUserOwner, service, protocol, target, targetPort, localPort, profile, "A"); //FIXME: Hardcoded action! ret = ret && newRule.Apply(true); } keepOpen = true; break; default: throw new Exception("Unknown action type: " + action.ToString()); } if (!ret) { throw new Exception("Unable to create the rule"); } else if (keepOpen) { NotifyIcon ni = new NotifyIcon(); ni.Click += new EventHandler(ni_Click); ni.BalloonTipIcon = ToolTipIcon.Info; ni.BalloonTipTitle = Resources.RULE_TEMP; ni.BalloonTipText = String.Format(Resources.RULE_TEMP_DESCRIPTION, path); ni.Icon = new Icon(SystemIcons.Shield, new Size(16, 16)); ni.Visible = true; ni.ShowBalloonTip(2000); Application.Run(); } } catch (Exception e) { LogHelper.Error("WFNRuleManager failure", e); Environment.Exit(1); } Environment.Exit(0); }
static void Main(string[] args) { LogHelper.Debug("Starting RuleManager: " + Environment.CommandLine); try { if (args.Count() == 0) { MessageBox.Show(Resources.MSG_RULEMANAGER_ARGUMENTS_ERR, Resources.MSG_DLG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(1); } if (args.Count() != 1) { throw new ArgumentException("Wrong number of arguments!"); } string[] param = Encoding.Unicode.GetString(Convert.FromBase64String(args[0])).Split(new string[] { "#$#" }, StringSplitOptions.None); if (param.Count() != 11) { throw new ArgumentException("Invalid arguments!"); } string rname = param[0]; string path = param[1]; string appPkgId = param[2]; string localUserOwner = param[3]; string sv = param[4]; string[] services = (sv != null ? sv.Split(',') : new string[] { }); int protocol = int.Parse(param[5]); string target = param[6]; string targetPort = param[7]; string localPort = param[8]; int profile = int.Parse(param[9]); CustomRuleAction action = (CustomRuleAction)Enum.Parse(typeof(CustomRuleAction), param[10]); bool isTemp = Boolean.Parse(param[11]); bool ret = true; switch (action) { case CustomRuleAction.A: case CustomRuleAction.B: // FIXME: Only action A is handled below! if (!isTemp) { foreach (var service in services) { 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); } } else { tmpnames = new List <string>(); foreach (var service in services) { string tmpRuleName = Common.Properties.Resources.RULE_TEMP_PREFIX + " " + Guid.NewGuid().ToString(); tmpnames.Add(tmpRuleName); FirewallHelper.CustomRule newRule = new FirewallHelper.CustomRule(tmpRuleName, path, appPkgId, localUserOwner, service, protocol, target, targetPort, localPort, profile, CustomRuleAction.A); //FIXME: Only A is handled! ret = ret && newRule.Apply(true); } } break; default: throw new Exception("Unknown action type: " + action.ToString()); } if (!ret) { throw new Exception("Unable to create the rule"); } else if (isTemp) { // tray icon for temporary rule NotifyIcon ni = new NotifyIcon(); ni.Click += new EventHandler(ni_Click); // shown in message center on win10 ni.BalloonTipIcon = ToolTipIcon.Info; ni.BalloonTipTitle = Resources.RULE_TEMP_TITLE; ni.BalloonTipText = String.Format(Resources.RULE_TEMP_DESCRIPTION, path); // tooltip shown on tray icon ni.Text = String.Format(Resources.RULE_TEMP_DESCRIPTION, System.IO.Path.GetFileName(path)).Substring(0, 63); // limit 64 chars on win10 ni.Icon = new Icon(SystemIcons.Shield, new Size(16, 16)); ni.Visible = true; ni.ShowBalloonTip(2000); Application.Run(); } } catch (Exception e) { LogHelper.Error("WFNRuleManager failure", e); Environment.Exit(1); } Environment.Exit(0); }