Exemplo n.º 1
0
        private bool TryWhitelistAssemblies(IList <string> libraries, bool singleFile)
        {
            int added   = 0;
            int updated = 0;

            int count = libraries.Count;

            for (int i = 0; i < count; i++)
            {
                string libraryPath = libraries[i];
                try
                {
                    AssemblyName       assName            = AssemblyName.GetAssemblyName(libraryPath);
                    WhitelistingResult whitelistingResult = TryWhitelistAssemblyName(assName, singleFile);
                    switch (whitelistingResult)
                    {
                    case WhitelistingResult.Added:
                        added++;
                        break;

                    case WhitelistingResult.Updated:
                        updated++;
                        break;

                    case WhitelistingResult.Exists:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                catch
                {
                    if (singleFile)
                    {
                        ShowNotification(new GUIContent("Selected file is not a valid .NET assembly!"));
                    }
                }
            }

            if (!singleFile)
            {
                ShowNotification(new GUIContent("Assemblies added: " + added + ", updated: " + updated));
            }

            return(added > 0 || updated > 0);
        }
Exemplo n.º 2
0
        private WhitelistingResult TryWhitelistAssemblyName(AssemblyName assName, bool singleFile)
        {
            WhitelistingResult result = WhitelistingResult.Exists;

            string name = assName.Name;
            int    hash = ActEditorGlobalStuff.GetAssemblyHash(assName);

            AllowedAssembly allowed = whiteList.FirstOrDefault(allowedAssembly => allowedAssembly.name == name);

            if (allowed != null)
            {
                if (allowed.AddHash(hash))
                {
                    if (singleFile)
                    {
                        ShowNotification(new GUIContent("New hash added!"));
                    }
                    result = WhitelistingResult.Updated;
                }
                else
                {
                    if (singleFile)
                    {
                        ShowNotification(new GUIContent("Assembly already exists!"));
                    }
                }
            }
            else
            {
                allowed = new AllowedAssembly(name, new[] { hash });
                whiteList.Add(allowed);

                if (singleFile)
                {
                    ShowNotification(new GUIContent("Assembly added!"));
                }
                result = WhitelistingResult.Added;
            }

            return(result);
        }
Exemplo n.º 3
0
        private bool TryWhitelistAssemblies(string[] libraries, bool singleFile)
        {
            int added   = 0;
            int updated = 0;

            int count = libraries.Length;

            for (int i = 0; i < count; i++)
            {
                string libraryPath = libraries[i];
                try
                {
                    AssemblyName       assName            = AssemblyName.GetAssemblyName(libraryPath);
                    WhitelistingResult whitelistingResult = TryWhitelistAssemblyName(assName, singleFile);
                    if (whitelistingResult == WhitelistingResult.Added)
                    {
                        added++;
                    }
                    else if (whitelistingResult == WhitelistingResult.Updated)
                    {
                        updated++;
                    }
                }
                catch
                {
                    if (singleFile)
                    {
                        ShowNotification(new GUIContent("Selected file is not a valid .NET assembly!"));
                    }
                }
            }

            if (!singleFile)
            {
                ShowNotification(new GUIContent("Assemblies added: " + added + ", updated: " + updated));
            }

            return(added > 0 || updated > 0);
        }
Exemplo n.º 4
0
        private void OnGUI()
        {
            if (whiteList == null)
            {
                whiteList = new List <AllowedAssembly>();
                LoadAndParseWhiteList();
            }

            GUIStyle tmpStyle = new GUIStyle(EditorStyles.largeLabel);

            tmpStyle.alignment = TextAnchor.MiddleCenter;
            tmpStyle.fontStyle = FontStyle.Bold;
            GUILayout.Label("User-defined Whitelist of Assemblies trusted by Injection Detector", tmpStyle);

            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
            bool whitelistUpdated = false;

            int count = whiteList.Count;

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    AllowedAssembly assembly = whiteList[i];
                    GUILayout.BeginHorizontal();
                    GUILayout.Label(assembly.ToString());
                    if (GUILayout.Button(new GUIContent("-", "Remove Assembly from Whitelist"), GUILayout.Width(30)))
                    {
                        whiteList.Remove(assembly);
                        whitelistUpdated = true;
                    }
                    GUILayout.EndHorizontal();
                }
            }
            else
            {
                tmpStyle           = new GUIStyle(EditorStyles.largeLabel);
                tmpStyle.alignment = TextAnchor.MiddleCenter;
                GUILayout.Label("- no Assemblies added so far (use buttons below to add) -", tmpStyle);
            }

            if (manualAssemblyWhitelisting)
            {
                manualAssemblyWhitelistingName = EditorGUILayout.TextField(manualAssemblyWhitelistingName);

                GUILayout.BeginHorizontal();
                if (GUILayout.Button("Save"))
                {
                    try
                    {
                        AssemblyName       assName = new AssemblyName(manualAssemblyWhitelistingName);
                        WhitelistingResult res     = TryWhitelistAssemblyName(assName, true);
                        if (res != WhitelistingResult.Exists)
                        {
                            whitelistUpdated = true;
                        }
                        manualAssemblyWhitelisting     = false;
                        manualAssemblyWhitelistingName = INITIAL_CUSTOM_NAME;
                    }
                    catch (FileLoadException error)
                    {
                        ShowNotification(new GUIContent(error.Message));
                    }

                    GUI.FocusControl("");
                }

                if (GUILayout.Button("Cancel"))
                {
                    manualAssemblyWhitelisting     = false;
                    manualAssemblyWhitelistingName = INITIAL_CUSTOM_NAME;
                    GUI.FocusControl("");
                }
                GUILayout.EndHorizontal();
            }

            EditorGUILayout.EndScrollView();

            GUILayout.BeginHorizontal();
            GUILayout.Space(20);
            if (GUILayout.Button("Add Assembly"))
            {
                string assemblyPath = EditorUtility.OpenFilePanel("Choose an Assembly to add", "", "dll");
                if (!string.IsNullOrEmpty(assemblyPath))
                {
                    whitelistUpdated |= TryWhitelistAssemblies(new[] { assemblyPath }, true);
                }
            }

            if (GUILayout.Button("Add Assemblies from folder"))
            {
                string selectedFolder = EditorUtility.OpenFolderPanel("Choose a folder with Assemblies", "", "");
                if (!string.IsNullOrEmpty(selectedFolder))
                {
                    string[] libraries = ActEditorGlobalStuff.FindLibrariesAt(selectedFolder);
                    whitelistUpdated |= TryWhitelistAssemblies(libraries);
                }
            }

            if (!manualAssemblyWhitelisting)
            {
                if (GUILayout.Button("Add Assembly manually"))
                {
                    manualAssemblyWhitelisting = true;
                }
            }

            if (count > 0)
            {
                if (GUILayout.Button("Clear"))
                {
                    if (EditorUtility.DisplayDialog("Please confirm", "Are you sure you wish to completely clear your Injection Detector whitelist?", "Yes", "No"))
                    {
                        whiteList.Clear();
                        whitelistUpdated = true;
                    }
                }
            }
            GUILayout.Space(20);
            GUILayout.EndHorizontal();

            GUILayout.Space(20);

            if (whitelistUpdated)
            {
                WriteWhiteList();
            }
        }