/// <summary> /// Get the working directory for a given Plugin Pointer /// </summary> /// <param name="pointer">Plugin pointer</param> /// <returns></returns> public string GetChildWorkingDirectory(PluginPointerModel pointer) { // Configuration: // <storage root>/<workspace>/<resource>/<plugin>.<item hash> if (string.IsNullOrEmpty(WorkingDirectory)) { throw new Exception( "Attempted to create child in ephemeral workspace; this operation is not allowed without a persistent working directory"); } var _workingDirectory = ""; if (pointer.Resource == null) { _workingDirectory = Path.Combine(WorkingDirectory, "no_resource_given", $"{pointer.Plugin}.{pointer.Fingerprint(serializeEphemeralData: true)}"); } else { _workingDirectory = Path.Combine(WorkingDirectory, pointer.Resource.ResourceId, $"{pointer.Plugin}.{pointer.Fingerprint(serializeEphemeralData: true)}"); } if (!Directory.Exists(_workingDirectory)) { Directory.CreateDirectory(_workingDirectory); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { new UnixFileInfo(_workingDirectory).FileAccessPermissions = FileAccessPermissions.AllPermissions; } } return(_workingDirectory); }
/// <summary> /// Execute the command, wrapped by a Task /// </summary> /// <returns></returns> public Task Start(PluginPointerModel pluginPointer) { if (string.IsNullOrEmpty(Command)) { throw new Exception("Command has not been set but execution has been requested"); } PluginPointer = pluginPointer; RuntimeTask = Task.Run(() => { var sw = new Stopwatch(); sw.Start(); Process.StartInfo = GetStartInfo(Command); Process.Start(); Process.BeginOutputReadLine(); Process.BeginErrorReadLine(); Process.OutputDataReceived += (s, e) => StdOut += e + Environment.NewLine; Process.ErrorDataReceived += (s, e) => StdErr += e + Environment.NewLine; Process.WaitForExit(); sw.Stop(); ExecutionTime = sw.Elapsed; }); return(RuntimeTask); }
public PluginResult(PluginPointerModel pluginPointer, string workingDirectory) { PluginPointer = pluginPointer; WorkingDirectory = workingDirectory; SaveResultArtifact(Path.Combine(WorkingDirectory, POINTER_FILE), pluginPointer); }
/// <summary> /// Validate a Plugin Pointer to ensure its target is acceptable /// </summary> /// <param name="pointer">Plugin Pointer to validate</param> /// <returns></returns> public bool Validate(PluginPointerModel pointer) { var instance = PluginManager.Instance.GetInstance(pointer); if (instance == null) { return(false); } return(instance.Validate(pointer)); }
/// <summary> /// Reprocess a single Plugin Pointer /// </summary> /// <param name="pointer">Plugin pointer</param> /// <returns></returns> public Task <PluginResult> Reprocess(PluginPointerModel pointer) { Logger.LogInformation("Resolving plugin pointer id {0}", pointer.Plugin); var plugin = PluginManager.Instance.GetInstance(pointer); if (plugin == null) { throw new Exception($"Plugin '{pointer.Plugin}' is not loaded"); } return(TaskManager.Instance.Enqueue(plugin.Reprocess(GetChildWorkingDirectory(pointer)))); }
public void PrintNode(PluginPointerModel model, string indent = "", bool isLast = false) { indent = PrintNodeBase(indent, isLast); if (model.Resource != null) { var resource = ResourceManager.Instance.GetById(model.Resource.ResourceId); Console.WriteLine(Output.BrightMagenta(model.Plugin) + " -> " + Output.BrightYellow(resource.ToString())); } else { Console.WriteLine(Output.BrightMagenta(model.Plugin) + " -> " + Output.Yellow("<No Resource>")); } AfterPluginPointerDrawn?.Invoke(indent, isLast, model); }
public override async Task <PluginResult> Execute(PluginPointerModel pointer, string workingDirectory) { Logger = Extensions.LogFactory.CreateLogger("OwaspZap@" + pointer.Resource.ResourceId); Logger.LogInformation("Starting OWASP ZAP plugin against Resource {0} with working directory {1}", pointer.Resource?.ResourceId, workingDirectory); var httpTarget = ResourceManager.Instance.GetByPointer(pointer.Resource) as HttpResource; var wrapper = new ExecutionWrapper(); string pyScript; if (GetOption(pointer.PluginParameters, "aggressive") == "false") { pyScript = "zap-baseline.py"; } else { pyScript = "zap-full-scan.py"; } Logger.LogDebug("Using scanner script {0} (aggressive option is {1})", pyScript, GetOption(pointer.PluginParameters, "aggressive")); var dockerCommandTemplate = "docker run " + "-v {0}:/zap/wrk:rw " + "-u zap " + "-i ictu/zap2docker-weekly " + "{1} -t {2} -J {3} -r {4} "; if (GetOption(pointer.PluginParameters, "useajaxspider") == "true") { dockerCommandTemplate += "-j "; } Logger.LogDebug("Invoking command " + dockerCommandTemplate, workingDirectory, pyScript, httpTarget.Url, JSON_REPORT_FILE, HTML_REPORT_FILE); wrapper.Command = string.Format(dockerCommandTemplate, workingDirectory, pyScript, httpTarget.Url, JSON_REPORT_FILE, HTML_REPORT_FILE); wrapper.Process.OutputDataReceived += (s, e) => Logger.LogInformation(e.Data); wrapper.Process.ErrorDataReceived += (s, e) => Logger.LogDebug(e.Data); await wrapper.Start(pointer); Logger.LogInformation("Completed OWASP ZAP scanner runtime execution in {0}", wrapper.ExecutionTime); return(new OwaspZapPluginResult(pointer, workingDirectory)); }
/// <summary> /// Execute a single Plugin from its Plugin Pointer /// </summary> /// <param name="pointer">Plugin pointer</param> /// <returns></returns> public Task <PluginResult> Execute(PluginPointerModel pointer) { Logger.LogInformation("Resolving plugin pointer id {0}", pointer.Plugin); var plugin = PluginManager.Instance.GetInstance(pointer); if (plugin == null) { throw new Exception($"Plugin '{pointer.Plugin}' is not loaded"); } if (pointer.Resource != null) { Logger.LogInformation("Resolving resource pointer id {0}", pointer.Resource.ResourceId); if (!ResourceManager.Instance.ContainsPointer(pointer.Resource)) { throw new Exception($"Resource '{pointer.Resource.ResourceId}' does not exist in resource map"); } } return(TaskManager.Instance.Enqueue(plugin.GetValidatedExecutionTask(pointer))); }
public override Task <PluginResult> Execute(PluginPointerModel pointer, string workingDirectory) { var sw = new Stopwatch(); sw.Start(); var rng = new Random((int)DateTime.Now.Ticks); var v = 0; var g = 0; while (v < 50) { g++; Thread.Sleep(rng.Next(500, 2000)); v += rng.Next(2, 10); Logger.LogInformation("Generation {0} -- {1}/100", g, v); var exec = new ExecutionWrapper("echo This is a test of the echo back"); exec.Process.OutputDataReceived += (s, e) => { if (e.Data != null) { Logger.LogCritical("RECEIVED DATA: {1}", e.Data); } }; var task = exec.Start(pointer); task.Wait(); } sw.Stop(); Logger.LogInformation("Completed in {0}", sw.Elapsed); return(Task.FromResult((PluginResult) new DummyPluginResult(pointer, workingDirectory) { Generations = g, Duration = sw.Elapsed })); }
/// <summary> /// Retrieve a Task that will execute this Plugin (inside a wrapper) /// This Task will be tagged with its Plugin name /// </summary> /// <param name="pointer">Pointer that describes the Plugin and its options</param> /// <returns></returns> public Task <PluginResult> GetValidatedExecutionTask(PluginPointerModel pointer) { var logger = Extensions.LogFactory.CreateLogger <Plugin>(); try { Validate(pointer); } catch (Exception ex) { logger.LogCritical(ex, "Error validating Plugin Pointer for execution"); } Task <PluginResult> generatedTask = null; generatedTask = new Task <PluginResult>(() => { try { Logger = Extensions.LogFactory.CreateLogger($"{Identifier}@{pointer.Resource?.ResourceId}"); logger.LogDebug("Starting scheduled task -- Plugin: {0} -- Resource: {1}", pointer.Plugin, pointer.Resource?.ResourceId); var result = Execute(pointer, Workspace.Instance.GetChildWorkingDirectory(pointer)).Result; logger.LogDebug("Completed scheduled task -- Plugin: {0} -- Resource: {1}", pointer.Plugin, pointer.Resource?.ResourceId); return(result); } catch (Exception ex) { logger.LogCritical(ex, "Exception thrown while executing scheduled task"); return(null); } }); return(generatedTask); }
/// <summary> /// Validate that the settings provided in a given pointer are valid /// </summary> /// <param name="pointer">Plugin pointer to validate</param> /// <returns></returns> public bool Validate(PluginPointerModel pointer) { var coalescedOptions = GetOptions(pointer.PluginParameters); if (ConfigurableOptions.Any(o => o.Required && (!coalescedOptions.ContainsKey(o.OptionName) || string.IsNullOrEmpty(coalescedOptions[o.OptionName])))) { Logger.LogCritical("One or more required configuration elements are missing!"); return(false); } if (ValidResourceTypes == null && pointer.Resource != null) { Logger.LogCritical("Plugin does not take any Resource inputs"); return(false); } if (pointer.Resource != null) { var targetResource = ResourceManager.Instance.GetByPointer(pointer.Resource); // Check if this resolves if (targetResource == null) { Logger.LogCritical("Resource {0} does not resolve", pointer.Resource.ResourceId); return(false); } if (!ValidResourceTypes.Any(t => t.IsInstanceOfType(targetResource))) { Logger.LogCritical("One or more resources are not supported by plugin"); return(false); } } return(true); }
public OwaspZapPluginResult(PluginPointerModel pluginPointer, string workingDirectory) : base(pluginPointer, workingDirectory) { SaveResultArtifact(Path.Combine(workingDirectory, ".ptr"), pluginPointer); }
internal bool Exists(PluginPointerModel pointer) { return(Exists(pointer.Plugin)); }
public DummyPluginResult(PluginPointerModel pluginPointer, string workingDirectory) : base(pluginPointer, workingDirectory) { }
internal Type GetLibraryType(PluginPointerModel pointer) { return(LoadedLibraryTypes.FirstOrDefault(p => GetTypeIdentifier(p) == pointer.Plugin)); }
internal Plugin GetInstance(PluginPointerModel pointer) { return(GetInstance(pointer.Plugin)); }
internal Plugin GetInstance(PluginPointerModel pointer, params object[] parameters) { return(GetInstance(pointer.Plugin, parameters)); }
/// <summary> /// Execute any tasks that the plugin requires to generate some output (i.e. run a scanner) /// </summary> /// <param name="pointer">Pointer to plugin and its configuration</param> /// <param name="workingDirectory">Working directory to store artifacts</param> /// <returns></returns> public abstract Task <PluginResult> Execute(PluginPointerModel pointer, string workingDirectory);