internal static AgentPlugin Create(AgentPluginMetadata metadata, IAgentPlugin plugin, ILogger logger) { logger.LogMessage(LogLevel.Verbose, $"Loading: {metadata.RootPath}"); MethodInfo[] methodInfos = plugin.GetType().GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod | BindingFlags.Instance); if (methodInfos == null || methodInfos.Length == 0) { return(null); } ImmutableDictionary <string, ImmutableDictionary <string, PluginMethod> > methodByVerb = methodInfos .Select(m => PluginMethod.Create(plugin, m)) .Where(m => m != null) .GroupBy(m => m.HttpMethod) .ToImmutableDictionary(g => g.Key, g => g.ToImmutableDictionary(m => m.Name)); if (methodByVerb.Count == 0) { logger.LogMessage(LogLevel.Warning, "No valid methods found."); return(null); } foreach (var mbv in methodByVerb) { foreach (var method in mbv.Value) { logger.LogMessage(LogLevel.Verbose, $"Exporting: {mbv.Key} {method.Key}"); } } return(new AgentPlugin(metadata, methodByVerb)); }
public void LoadPluginsFromDirectory(string pluginDirectory) { foreach (string assemblyName in System.IO.Directory.GetFiles(pluginDirectory)) { System.Reflection.Assembly assembly = null; try { assembly = System.Reflection.Assembly.LoadFile(assemblyName); } catch (Exception ex) { OnLoadPluginExceptionRaised(assemblyName, null, ex, PluginExceptionSource.Init); } if (assembly != null) { foreach (Type type in assembly.GetTypes()) { if (type.IsPublic && !type.IsAbstract) { Type typeInterface = type.GetInterface(typeof(IAgentPlugin).Name, false); //Make sure the interface we want to use actually exists if (typeInterface != null) { try { IAgentPlugin objectInstance = (IAgentPlugin)Activator.CreateInstance(type); if (objectInstance != null) { this.GetAgentConfig(assemblyName, type.Name, (objectInstance as IAgentPlugin).Configuration.ConfigurationSettingsTypeName); } } catch (Exception exception) { OnLoadPluginExceptionRaised(assemblyName, type.Name, exception, PluginExceptionSource.Load); } } } } } } }
public AgentProxy(string assemblyFilename, string typeName) { try { agent = (IAgentPlugin)Activator.CreateInstanceFrom(assemblyFilename, typeName).Unwrap(); } catch (Exception ex) { throw ex; } agent.AttackDetected += new AttackDetectedHandler(agent_AttackDetected); }
private PluginMethod(IAgentPlugin plugin, string httpMethod, MethodInfo methodInfo) { this.HttpMethod = httpMethod; this.MethodInfo = methodInfo; this.parameters = this.MethodInfo.GetParameters(); this.parameterConverters = CreateParameterConverters(); this.returnParameter = this.MethodInfo.ReturnParameter; this.Plugin = plugin; }
internal static PluginMethod Create(IAgentPlugin plugin, MethodInfo methodInfo) { PluginMethodAttribute methodAttribute = methodInfo.GetCustomAttribute <PluginMethodAttribute>(); if (!IsValidMethodAttribute(methodAttribute)) { return(null); } if (!IsValidSignature(methodInfo)) { return(null); } return(new PluginMethod(plugin, methodAttribute.HttpMethod, methodInfo)); }
public static void RemoveInstance(IAgentPlugin agent) { lock (pluginLock) { if (agent == null) { throw new ArgumentNullException("agent"); } Guid pluginId = Guid.Empty; if (!pluginInstanceToPluginId.TryGetValue(agent, out pluginId)) { return; } try { agent.StopApplication(); } catch (Exception) { } pluginInstanceToPluginId.Remove(agent); int usage = 0; typeChacheUsage.TryGetValue(pluginId, out usage); if (usage != 0) { if (usage == 1) { PluginData pluginData = knownPluginData[pluginId]; AppDomain.Unload(pluginData.PluginDomain); typeChacheUsage.Remove(pluginId); knownPluginData.Remove(pluginId); } else { typeChacheUsage[pluginId] = usage - 1; } } } }
internal void Load(string assemblyName) { Type pInterfaceType = typeof(IAgentPlugin); Assembly assembly; try { assembly = Assembly.LoadFile(assemblyName); foreach (Type type in assembly.GetTypes()) { if (type.IsPublic) // Just the public ones { if (!type.IsAbstract) // ignore abstract classes { Type typeInterface = type.GetInterface(pInterfaceType.ToString(), false); //Make sure the interface we want to use actually exists if (typeInterface != null) { try { IAgentPlugin objectInstance = (IAgentPlugin)Activator.CreateInstance(type); if (objectInstance != null) { Agent orange = new Agent(assembly.FullName); orange.Assembly = objectInstance; orange.Name = type.Name; this.Add(orange); } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception); throw exception; } } typeInterface = null; } } } } catch (Exception ex) { throw ex; } assembly = null; }
public List <SecurityAgent> GetSecurityAgents(string fileName) { Assembly assembly = Assembly.LoadFile(fileName); List <SecurityAgent> result = new List <SecurityAgent>(); foreach (Type type in assembly.GetTypes()) { if (type.IsPublic && !type.IsAbstract) { Type typeInterface = type.GetInterface(typeof(IAgentPlugin).ToString(), false); //Make sure the interface we want to use actually exists if (typeInterface != null) { try { IAgentPlugin agentPlugin = (IAgentPlugin)Activator.CreateInstanceFrom(fileName, type.FullName.ToString()).Unwrap(); if (agentPlugin != null) { SecurityAgent securityAgent = new SecurityAgent(); securityAgent.AssemblyName = assembly.FullName; if (agentPlugin is IExtendedInformation) { IExtendedInformation exInfo = (IExtendedInformation)agentPlugin; securityAgent.DisplayName = exInfo.DisplayName; securityAgent.UnselectedIcon = exInfo.UnselectedIcon; securityAgent.SelectedIcon = exInfo.SelectedIcon; securityAgent.Icon = exInfo.Icon; securityAgent.Id = exInfo.Id; } else { securityAgent.DisplayName = type.FullName; securityAgent.UnselectedIcon = global::Cyberarms.IntrusionDetection.Shared.Resources.agent15px_default_dark; securityAgent.SelectedIcon = global::Cyberarms.IntrusionDetection.Shared.Resources.agent15px_default_white; securityAgent.Icon = global::Cyberarms.IntrusionDetection.Shared.Resources.agent15px_default_dark; } securityAgent.Name = type.FullName; securityAgent.Enabled = false; securityAgent.FailedLogins = 0; securityAgent.HardLockAttempts = 10; securityAgent.HardLocks = 0; securityAgent.HardLockTimeHours = 24; securityAgent.AssemblyFilename = fileName; securityAgent.SoftLockAttempts = 3; securityAgent.SoftLocks = 0; securityAgent.SoftLockTimeMinutes = 20; securityAgent.OverrideConfig = false; if (agentPlugin.Configuration.AgentSettings != null) { securityAgent.CustomConfiguration = GetCustomConfigurationObjects(agentPlugin.Configuration.AgentSettings); } result.Add(securityAgent); } } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); throw exception; } } } } return(result); }