Exemple #1
0
        public void EvaluateRules(ProgramSet progSet, bool StrictTest = false)
        {
            String InetRanges = FirewallRule.AddrKeywordIntErnet;

            if (UwpFunc.IsWindows7OrLower)
            {
                InetRanges = GetSpecialNet(InetRanges);
            }

            progSet.config.CurAccess = ProgramConfig.AccessLevels.Unconfigured;

            SortedDictionary <ProgramID, RuleStat> RuleStats = new SortedDictionary <ProgramID, RuleStat>();
            int enabledCound = 0;

            foreach (Program prog in progSet.Programs.Values)
            {
                RuleStat Stat = new RuleStat();

                foreach (FirewallRule rule in prog.Rules.Values)
                {
                    if (!rule.Enabled)
                    {
                        continue;
                    }

                    enabledCound++;

                    if (!FirewallRule.IsEmptyOrStar(rule.LocalAddresses))
                    {
                        continue;
                    }
                    if (!FirewallRule.IsEmptyOrStar(rule.LocalPorts) || !FirewallRule.IsEmptyOrStar(rule.RemotePorts))
                    {
                        continue;
                    }
                    if (rule.IcmpTypesAndCodes != null && rule.IcmpTypesAndCodes.Length > 0)
                    {
                        continue;
                    }

                    bool AllProts  = (rule.Protocol == (int)NetFunc.KnownProtocols.Any);
                    bool InetProts = AllProts || (rule.Protocol == (int)FirewallRule.KnownProtocols.TCP) || (rule.Protocol == (int)FirewallRule.KnownProtocols.UDP);

                    if (!InetProts)
                    {
                        continue;
                    }

                    //if (rule.Profile != (int)FirewallRule.Profiles.All && (rule.Profile != ((int)FirewallRule.Profiles.Public | (int)FirewallRule.Profiles.Private | (int)FirewallRule.Profiles.Domain)))
                    //    continue;
                    if (rule.Interface != (int)FirewallRule.Interfaces.All)
                    {
                        continue;
                    }

                    if (FirewallRule.IsEmptyOrStar(rule.RemoteAddresses))
                    {
                        if (rule.Action == FirewallRule.Actions.Allow && InetProts)
                        {
                            Stat.AllowAll.Add(rule.Profile, rule.Direction);
                        }
                        else if (rule.Action == FirewallRule.Actions.Block && AllProts)
                        {
                            Stat.BlockAll.Add(rule.Profile, rule.Direction);
                        }
                    }
                    else if (rule.RemoteAddresses == InetRanges)
                    {
                        if (rule.Action == FirewallRule.Actions.Block && AllProts)
                        {
                            Stat.BlockInet.Add(rule.Profile, rule.Direction);
                        }
                    }
                    else if (rule.RemoteAddresses == FirewallRule.AddrKeywordLocalSubnet)
                    {
                        if (rule.Action == FirewallRule.Actions.Allow && InetProts)
                        {
                            Stat.AllowLan.Add(rule.Profile, rule.Direction);
                        }
                    }
                }

                RuleStats.Add(prog.ID, Stat);
            }

            if (RuleStats.Count == 0 || enabledCound == 0)
            {
                return;
            }

            RuleStat MergedStat = RuleStats.Values.First();

            for (int i = 1; i < RuleStats.Count; i++)
            {
                RuleStat Stat = RuleStats.Values.ElementAt(i);

                MergedStat.AllowAll.Merge(Stat.AllowAll);
                MergedStat.BlockAll.Merge(Stat.BlockAll);
                MergedStat.AllowLan.Merge(Stat.AllowLan);
                MergedStat.BlockInet.Merge(Stat.BlockInet);
            }

            if (MergedStat.BlockAll.IsOutbound(IgnoreDomain) && (!StrictTest || MergedStat.BlockAll.IsInbound(IgnoreDomain)))
            {
                progSet.config.CurAccess = ProgramConfig.AccessLevels.BlockAccess;
            }
            //else if (MergedStat.AllowAll.IsOutbound(SkipDomain) && (!StrictTest || MergedStat.AllowAll.IsInbound(SkipDomain)))
            else if (MergedStat.AllowAll.IsOutbound(IgnoreDomain) && MergedStat.AllowAll.IsInbound(IgnoreDomain))
            {
                progSet.config.CurAccess = ProgramConfig.AccessLevels.FullAccess;
            }
            else if (MergedStat.AllowLan.IsOutbound(IgnoreDomain) && (!StrictTest || (MergedStat.AllowLan.IsInbound(IgnoreDomain) && MergedStat.AllowLan.IsInbound(IgnoreDomain))))
            {
                progSet.config.CurAccess = ProgramConfig.AccessLevels.LocalOnly;
            }
            else if (MergedStat.AllowAll.IsOutbound(IgnoreDomain))
            {
                progSet.config.CurAccess = ProgramConfig.AccessLevels.OutBoundAccess;
            }
            else if (MergedStat.AllowAll.IsInbound(IgnoreDomain))
            {
                progSet.config.CurAccess = ProgramConfig.AccessLevels.InBoundAccess;
            }
            else if (enabledCound > 0)
            {
                progSet.config.CurAccess = ProgramConfig.AccessLevels.CustomConfig;
            }
        }
Exemple #2
0
        public void EvaluateRules(Program prog, bool apply)
        {
            String InetRanges = NetFunc.GetNonLocalNet();

            prog.config.CurAccess = Program.Config.AccessLevels.Unconfigured;

            bool StrictTest = false;

            if (prog.Rules.Count > 0)
            {
                SortedDictionary <ProgramList.ID, RuleStat> RuleStats = new SortedDictionary <ProgramList.ID, RuleStat>();
                int enabledCound = 0;

                foreach (FirewallRule rule in prog.Rules.Values.ToList())
                {
                    RuleStat Stat;
                    if (!RuleStats.TryGetValue(rule.mID, out Stat))
                    {
                        Stat = new RuleStat();
                        RuleStats.Add(rule.mID, Stat);
                    }

                    if (!rule.Enabled)
                    {
                        continue;
                    }

                    enabledCound++;

                    if (!IsEmptyOrStar(rule.LocalAddresses))
                    {
                        continue;
                    }
                    if (!IsEmptyOrStar(rule.LocalPorts) || !IsEmptyOrStar(rule.RemotePorts))
                    {
                        continue;
                    }
                    if (!IsEmptyOrStar(rule.IcmpTypesAndCodes))
                    {
                        continue;
                    }

                    bool AllProts  = (rule.Protocol == (int)NetFunc.KnownProtocols.Any);
                    bool InetProts = AllProts || (rule.Protocol == (int)FirewallRule.KnownProtocols.TCP) || (rule.Protocol == (int)FirewallRule.KnownProtocols.UDP);

                    if (!InetProts)
                    {
                        continue;
                    }

                    if (rule.Profile != (int)Profiles.All && (rule.Profile != ((int)Profiles.Public | (int)Profiles.Private | (int)Profiles.Domain)))
                    {
                        continue;
                    }
                    if (rule.Interface != (int)Interfaces.All)
                    {
                        continue;
                    }

                    if (IsEmptyOrStar(rule.RemoteAddresses))
                    {
                        if (rule.Action == Actions.Allow && InetProts)
                        {
                            Stat.AllowAll |= ((int)rule.Direction);
                        }
                        else if (rule.Action == Actions.Block && AllProts)
                        {
                            Stat.BlockAll |= ((int)rule.Direction);
                        }
                    }
                    else if (rule.RemoteAddresses == InetRanges)
                    {
                        if (rule.Action == Actions.Block && AllProts)
                        {
                            Stat.BlockInet |= ((int)rule.Direction);
                        }
                    }
                    else if (rule.RemoteAddresses == "LocalSubnet")
                    {
                        if (rule.Action == Actions.Allow && InetProts)
                        {
                            Stat.AllowLan |= ((int)rule.Direction);
                        }
                    }
                    RuleStats[rule.mID] = Stat;
                }

                RuleStat MergedStat = RuleStats.Values.ElementAt(0);

                for (int i = 1; i < RuleStats.Count; i++)
                {
                    RuleStat Stat = RuleStats.Values.ElementAt(i);

                    MergedStat.AllowAll  &= Stat.AllowAll;
                    MergedStat.BlockAll  &= Stat.BlockAll;
                    MergedStat.AllowLan  &= Stat.AllowLan;
                    MergedStat.BlockInet &= Stat.BlockInet;
                }

                if ((MergedStat.BlockAll & (int)Directions.Outboun) != 0 && (!StrictTest || (MergedStat.BlockAll & (int)Directions.Inbound) != 0))
                {
                    prog.config.CurAccess = Program.Config.AccessLevels.BlockAccess;
                }
                else if ((MergedStat.AllowAll & (int)Directions.Outboun) != 0 && (!StrictTest || (MergedStat.AllowAll & (int)Directions.Inbound) != 0))
                {
                    prog.config.CurAccess = Program.Config.AccessLevels.FullAccess;
                }
                else if ((MergedStat.AllowLan & (int)Directions.Outboun) != 0 && (!StrictTest || ((MergedStat.AllowLan & (int)Directions.Inbound) != 0 && (MergedStat.AllowLan & (int)Directions.Inbound) != 0)))
                {
                    prog.config.CurAccess = Program.Config.AccessLevels.LocalOnly;
                }
                else if (enabledCound > 0)
                {
                    prog.config.CurAccess = Program.Config.AccessLevels.CustomConfig;
                }
            }


            if (!apply || prog.config.NetAccess == Program.Config.AccessLevels.Unconfigured || prog.config.NetAccess == Program.Config.AccessLevels.CustomConfig)
            {
                return;
            }

            if (prog.config.NetAccess == prog.config.CurAccess)
            {
                return;
            }


            if (prog.config.NetAccess != Program.Config.AccessLevels.CustomConfig)
            {
                DisableUserRules(prog);
            }

            ClearPrivRules(prog);

            foreach (ProgramList.ID id in prog.IDs)
            {
                for (int i = 1; i <= 2; i++)
                {
                    Directions direction = (Directions)i;

                    switch (prog.config.NetAccess)
                    {
                    case Program.Config.AccessLevels.FullAccess:

                        // add and enable allow all rule
                        UpdateRule(FirewallRule.MakeAllowRule(id, direction), true);
                        break;

                    case Program.Config.AccessLevels.LocalOnly:

                        // create block rule only of we operate in blacklist mode
                        //if (GetFilteringMode() == FilteringModes.BlackList)
                        //{
                        //add and enable block rules for the internet
                        UpdateRule(FirewallRule.MakeBlockInetRule(id, direction), true);
                        //}

                        //add and enable allow rules for the lan
                        UpdateRule(FirewallRule.MakeAllowLanRule(id, direction), true);
                        break;

                    case Program.Config.AccessLevels.BlockAccess:

                        // add and enable broad block rules
                        UpdateRule(FirewallRule.MakeBlockRule(id, direction), true);
                        break;
                    }
                }
            }

            prog.config.CurAccess = prog.config.NetAccess;

            App.engine.NotifyChange(prog);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            //var dataTable = FileOperation.ReadDataFile(@"C:\Users\kumar\OneDrive\Documents\Projects\CharMax\CharMax\Datasets\breast-35.d");

            var files = Directory.GetFiles(ConfigurationManager.AppSettings["DataSetFolder"], "*.d", SearchOption.TopDirectoryOnly);
            ConcurrentBag <Rules> AllRules = new ConcurrentBag <Rules>();


            List <List <int> > temp = new List <List <int> >();

            temp.Add(new List <int> {
                1, 2, 3, 4
            });
            temp.Add(new List <int> {
                3, 4
            });
            temp.Add(new List <int> {
                4, 3, 3, 4
            });

            var t = temp.IntersectAll().ToList();

            //Parallel.ForEach(files, file =>
            //{
            //    var dataTable = FileOperation.ReadDataFile(file);
            //    Data data = new Data(dataTable);
            //    var alphaValue = 0.0f;
            //    Parallel.For(0, 12, alpha =>
            //    {
            //        alphaValue = (float)Math.Round(alphaValue + 0.10f, 1);
            //        RuleInduction ruleInductionCharacteristic = new RuleInduction();

            //        RuleInduction ruleInductionMCB = new RuleInduction();
            //        var charTask = Task.Factory.StartNew(() =>
            //        {
            //            var charApprox = ProbApprox.GetConceptApprox(data.Characteristic, data.ConditionalProbability.CharacteristicCondProb, alphaValue);
            //            return ruleInductionCharacteristic.ComputeRules(data, data.AttributeValuePairs, charApprox);
            //        });

            //        var mcBTask = Task.Factory.StartNew(() =>
            //        {
            //            var mcbApprox = ProbApprox.GetConceptApprox(data.MaximalConsistent, data.ConditionalProbability.MaximalCondProb, alphaValue);
            //            return ruleInductionMCB.ComputeRules(data, data.AttributeValuePairs, mcbApprox);
            //        });

            //        Task.WaitAll(charTask, mcBTask);

            //        var charRule = charTask.Result;
            //        var mcbRule = mcBTask.Result;

            //        Rules rules = new Rules(Path.GetFileNameWithoutExtension(file), alphaValue, charRule, mcbRule);

            //        AllRules.Add(rules);
            //    });

            //for (float alphaValue = 0.0f; alphaValue <= 1.0f; alphaValue = (float)Math.Round(alphaValue + 0.10f, 1))
            //{
            //    //RuleInduction ruleInductionCharacteristic = new RuleInduction();

            //    //RuleInduction ruleInductionMCB = new RuleInduction();
            //    //var charTask = Task.Factory.StartNew(() =>
            //    //{
            //    //    var charApprox = ProbApprox.GetConceptApprox(data.Characteristic, data.ConditionalProbability.CharacteristicCondProb, alphaValue);
            //    //    return ruleInductionCharacteristic.ComputeRules(data, data.AttributeValuePairs, charApprox);
            //    //});

            //    //var mcBTask = Task.Factory.StartNew(() =>
            //    //{
            //    //    var mcbApprox = ProbApprox.GetConceptApprox(data.MaximalConsistent, data.ConditionalProbability.MaximalCondProb, alphaValue);
            //    //    return ruleInductionMCB.ComputeRules(data, data.AttributeValuePairs, mcbApprox);
            //    //});

            //    //Task.WaitAll(charTask, mcBTask);

            //    //var charRule = charTask.Result;
            //    //var mcbRule = mcBTask.Result;

            //    //Rules rules = new Rules(Path.GetFileNameWithoutExtension(file), alphaValue, charRule, mcbRule);

            //    //AllRules.Add(rules);

            //}
            //});

            foreach (var file in files)
            {
                var  dataTable = FileOperation.ReadDataFile(file);
                Data data      = new Data(dataTable);
                //var alphaValue = 0.0f;
                for (float alphaValue = 0.01f; alphaValue <= 1.0f; alphaValue = (float)Math.Round(alphaValue + 0.10f, 1))
                {
                    RuleInduction ruleInductionCharacteristic = new RuleInduction();

                    RuleInduction ruleInductionMCB = new RuleInduction();
                    var           charTask         = Task.Factory.StartNew(() =>
                    {
                        var charApprox = ProbApprox.GetConceptApprox(data.Characteristic, data.ConditionalProbability.CharacteristicCondProb, alphaValue);
                        return(ruleInductionCharacteristic.ComputeRules(data, data.AttributeValuePairs, charApprox));
                    });

                    var mcBTask = Task.Factory.StartNew(() =>
                    {
                        var mcbApprox = ProbApprox.GetConceptApprox(data.MaximalConsistent, data.ConditionalProbability.MaximalCondProb, alphaValue);
                        return(ruleInductionMCB.ComputeRules(data, data.AttributeValuePairs, mcbApprox));
                    });

                    //Task.WaitAll(charTask, mcBTask);

                    var charRule = charTask.Result;
                    var mcbRule  = mcBTask.Result;

                    //var charApprox = ProbApprox.GetConceptApprox(data.Characteristic, data.ConditionalProbability.CharacteristicCondProb, alphaValue);
                    //var r = ruleInductionCharacteristic.ComputeRules(data, data.AttributeValuePairs, charApprox);

                    //var mcbApprox = ProbApprox.GetConceptApprox(data.MaximalConsistent, data.ConditionalProbability.MaximalCondProb, alphaValue);
                    //var r1 = ruleInductionMCB.ComputeRules(data, data.AttributeValuePairs, mcbApprox);

                    Rules rules = new Rules(Path.GetFileNameWithoutExtension(file), alphaValue, charRule, mcbRule);
                    //Rules rules = new Rules(Path.GetFileNameWithoutExtension(file), alphaValue, r, r1);

                    AllRules.Add(rules);
                }
            }


            RuleStat ruleStat = new RuleStat();

            ruleStat.SaveRuleStat(AllRules.ToList());

            //Console.WriteLine("Rules - Characteristic Set");
            //PrintRules(charRule);

            //Console.WriteLine("\n\n\n End of Characteristic Rules \n \n");
            //Console.WriteLine("Rules - MCB");
            //PrintRules(mcbRule);
        }