Esempio n. 1
0
        // 【新版本】创建攻击和关系放在一起的结点
        public void CreateAttackWithRelationVM()
        {
            AttackWithRelation_VM attackWithRelation_VM = new AttackWithRelation_VM(mousePos.X, mousePos.Y);

            AttackTreePVM.UserControlVMs.Add(attackWithRelation_VM);
            ResourceManager.mainWindowVM.Tips = "创建了新的攻击树结点:" + attackWithRelation_VM.AttackWithRelation.Description;
        }
Esempio n. 2
0
        // 【作废】
        // 应用叶子分析的选中项(实际上全部都是翻转规则)

        /*
         * public void ReverseLeafAttackVM()
         * {
         *  ListBox leafAttackVM_ListBox = ControlExtensions.FindControl<ListBox>(this, "leafAttackVM_ListBox");
         *  if (leafAttackVM_ListBox.SelectedItem == null)
         *  {
         *      ResourceManager.mainWindowVM.Tips = "需要选定要应用的叶子处理规则!";
         *      return;
         *  }
         *
         *  // 翻转
         *  Attack_VM attack_VM = (Attack_VM)leafAttackVM_ListBox.SelectedItem;
         *  attack_VM.BeAttacked = !attack_VM.BeAttacked;
         *
         *  // 重新计算
         *  AttackTreePVM.HandleAttackVM.CalculateBeAttacked();
         * }
         */

        /// <summary>
        /// 应用叶子分析的选中项(实际上全部都是翻转规则)
        /// </summary>
        private void ReverseLeafAttackWithRelationVM()
        {
            ListBox leafAttackWithRelationVM_ListBox = ControlExtensions.FindControl <ListBox>(this, nameof(leafAttackWithRelationVM_ListBox));

            if (leafAttackWithRelationVM_ListBox.SelectedItem == null)
            {
                ResourceManager.mainWindowVM.Tips = "需要选定要应用的叶子处理规则!";
                return;
            }

            // 翻转
            AttackWithRelation_VM attackWithRelation_VM = (AttackWithRelation_VM)leafAttackWithRelationVM_ListBox.SelectedItem;

            attackWithRelation_VM.BeAttacked = !attackWithRelation_VM.BeAttacked;

            // 重新计算
            AttackTreePVM.HandleAttackWithRelationVM.CalculateBeAttacked();
        }
Esempio n. 3
0
        // 【作废】
        // "叶子攻击分析"ListBox的选中项变化的处理

        /*
         * private void leafAttackVM_ListBox_Changed(object sender, SelectionChangedEventArgs e)
         * {
         *  // 找出选中项(是一个Attack_VM)
         *  ListBox leafAttackVM_ListBox = ControlExtensions.FindControl<ListBox>(this, "leafAttackVM_ListBox");
         *  if (leafAttackVM_ListBox.SelectedItem == null)
         *  {
         *      return; // 如果选中了某一项,但是换到其它图的Panel,这里就会返回null
         *  }
         *  // 并获得其中Attack的文字
         *  Attack_VM attack_VM = (Attack_VM)leafAttackVM_ListBox.SelectedItem;
         *  string attackContent = attack_VM.Attack.Content;
         *
         *  // 清空[安全策略数据库]的绑定列表
         *  AttackTreePVM.SecurityPolicies.Clear();
         *
         *  // 读取策略数据库文件,解析为策略List
         *  string jsonStr = File.ReadAllText("Assets/SecurityPolicy.json");
         *  List<LabelsContentsPair> labelsContentsPairs = JsonSerializer.Deserialize<List<LabelsContentsPair>>(jsonStr);
         *
         *  // 这里记录那些无法匹配的标签,用于快速判断一个标签是否能和选中项的Attack的文字匹配
         *  HashSet<string> failLabelHashSet = new HashSet<string>();
         *
         *  // 这里记录那些已经存在的安全策略,用于去重(不同标签可能对应相同的安全策略)
         *  HashSet<string> succContentHashSet = new HashSet<string>();
         *
         *  // 遍历策略数据库,检查是否匹配
         *  foreach (LabelsContentsPair labelsContentsPair in labelsContentsPairs)
         *  {
         *      foreach (string label in labelsContentsPair.Labels) // 对其中的每个标签
         *      {
         *          if (failLabelHashSet.Contains(label)) // 先检查是否已经检查过为"不匹配"
         *          {
         *              continue;
         *          }
         *          if (!attackContent.Contains(label)) // 再进行字符串匹配的检查
         *          {
         *              failLabelHashSet.Add(label); // 不匹配还要加入fail集合
         *              continue;
         *          }
         *          // 至此,匹配成功,要将其下的所有安全策略都加入,并break退出这个LabelsContentsPair
         *          // 因为即便这个LabelsContentsPair的其它Label也能匹配成功,也没有新的安全策略要加入了
         *          foreach (string content in labelsContentsPair.Contents)
         *          {
         *              // 注意要检查之前是否放过这个安全策略,因为其它LabelsContentsPair里也可能有
         *              if (!succContentHashSet.Contains(content))
         *              {
         *                  AttackTreePVM.SecurityPolicies.Add(content);
         *              }
         *          }
         *          break;
         *      }
         *  }
         *  // 提示
         *  if (AttackTreePVM.SecurityPolicies.Count == 0)
         *  {
         *      ResourceManager.mainWindowVM.Tips = "没有找到合适的安全策略,请尝试带有关键字地描述攻击结点";
         *  }
         *  else
         *  {
         *      ResourceManager.mainWindowVM.Tips = "在[安全策略数据库]中找到并列出了一些可能可行的策略";
         *  }
         * }
         */

        /// <summary>
        /// "叶子攻击分析"ListBox的选中项变化的处理
        /// </summary>
        private void leafAttackWithRelationVM_ListBox_Changed(object sender, SelectionChangedEventArgs e)
        {
            // 找出选中项(是一个Attack_VM)
            ListBox leafAttackWithRelationVM_ListBox = ControlExtensions.FindControl <ListBox>(this, nameof(leafAttackWithRelationVM_ListBox));

            // 如果选中了某一项,但是换到其它图的Panel,这里就会出现null
            if (leafAttackWithRelationVM_ListBox.SelectedItem == null)
            {
                return;
            }
            // 并获得其中Attack的文字
            AttackWithRelation_VM attackWithRelation_VM = (AttackWithRelation_VM)leafAttackWithRelationVM_ListBox.SelectedItem;
            string attackContent = attackWithRelation_VM.AttackWithRelation.Description;

            // 清空[安全策略数据库]的绑定列表
            AttackTreePVM.SecurityPolicies.Clear();

            // 读取策略数据库文件,解析为策略List
            string fpath   = ResourceManager.security_policy_json;
            string jsonStr = null;

            try
            {
                jsonStr = File.ReadAllText(fpath);
            }
            catch (System.Exception excp)
            {
                // 安全策略数据库文件不存在
                ResourceManager.mainWindowVM.Tips = excp.Message;
                return; // 直接结束
            }

            List <LabelsContentsPair> labelsContentsPairs = JsonSerializer.Deserialize <List <LabelsContentsPair> >(jsonStr);

            // 这里记录那些无法匹配的标签,用于快速判断一个标签是否能和选中项的Attack的文字匹配
            HashSet <string> failLabelHashSet = new HashSet <string>();

            // 这里记录那些已经存在的安全策略,用于去重(不同标签可能对应相同的安全策略)
            HashSet <string> succContentHashSet = new HashSet <string>();

            // 遍历策略数据库,检查是否匹配
            foreach (LabelsContentsPair labelsContentsPair in labelsContentsPairs)
            {
                foreach (string label in labelsContentsPair.Labels) // 对其中的每个标签
                {
                    if (failLabelHashSet.Contains(label))           // 先检查是否已经检查过为"不匹配"
                    {
                        continue;
                    }
                    if (!attackContent.Contains(label)) // 再进行字符串匹配的检查
                    {
                        failLabelHashSet.Add(label);    // 不匹配还要加入fail集合
                        continue;
                    }
                    // 至此,匹配成功,要将其下的所有安全策略都加入,并break退出这个LabelsContentsPair
                    // 因为即便这个LabelsContentsPair的其它Label也能匹配成功,也没有新的安全策略要加入了
                    foreach (string content in labelsContentsPair.Contents)
                    {
                        // 注意要检查之前是否放过这个安全策略,因为其它LabelsContentsPair里也可能有
                        if (!succContentHashSet.Contains(content))
                        {
                            AttackTreePVM.SecurityPolicies.Add(content);
                        }
                    }
                    break;
                }
            }
            // 提示
            if (AttackTreePVM.SecurityPolicies.Count == 0)
            {
                ResourceManager.mainWindowVM.Tips = "没有找到合适的安全策略,请尝试带有关键字地描述攻击结点";
            }
            else
            {
                ResourceManager.mainWindowVM.Tips = "在[安全策略数据库]中找到并列出了一些可能可行的策略";
            }
        }