/// <summary> /// Update the subdomain if is a new service with open port /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="agent">The Agent</param> /// <param name="scriptOutput">The terminal output one line</param> /// <param name="activateNotification">If the notification is active</param> /// <param name="cancellationToken">Notification that operations should be canceled</param> /// <returns>A task</returns> private async Task UpdateSubdomainService(Subdomain subdomain, Agent agent, ScriptOutput scriptOutput, bool activateNotification, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(scriptOutput.Service)) { return; } var service = new Service { Name = scriptOutput.Service.ToLower(), Port = scriptOutput.Port.Value }; if (subdomain.Services == null) { subdomain.Services = new List <Service>(); } if (!subdomain.Services.Any(s => s.Name == service.Name && s.Port == service.Port)) { subdomain.Services.Add(service); if (activateNotification && agent.NotifyNewFound && agent.AgentNotification != null && !string.IsNullOrEmpty(agent.AgentNotification.ServicePayload)) { var payload = agent.AgentNotification.ServicePayload.Replace("{{domain}}", subdomain.Name).Replace("{{service}}", service.Name).Replace("{{port}}", service.Port.ToString()); await this.notificationService.SendAsync(payload, cancellationToken); } } }
public static ScriptOutput GetNowTime(ScriptInput si) { ScriptOutput so = new ScriptOutput(); so.SetValue("NowTime", DateTime.Now); return(so); }
public static ScriptOutput SetBoolValue(ScriptInput idx) { ScriptOutput so = new ScriptOutput(); so.SetValue("result", Convert.ToBoolean(idx.GetValue("value"))); return(so); }
/// <summary> /// Add or update the subdomain belong to the target /// </summary> /// <param name="domain">The domain</param> /// <param name="agent">The agent</param> /// <param name="scriptOutput">The terminal output one line</param> /// <returns>A Task</returns> private async Task AddOrUpdateSubdomainAsync(RootDomain domain, Agent agent, ScriptOutput scriptOutput, bool activateNotification, CancellationToken cancellationToken = default) { if (Uri.CheckHostName(scriptOutput.Subdomain) == UriHostNameType.Unknown) { return; } var subdomain = await this.subdomainService.GetAllQueryableByCriteria(d => d.Name == scriptOutput.Subdomain && d.Domain == domain) .Include(s => s.Services) .FirstOrDefaultAsync(); if (subdomain == null) { subdomain = new Subdomain { Name = scriptOutput.Subdomain, Domain = domain }; subdomain = await this.subdomainService.AddAsync(subdomain); if (activateNotification && agent.NotifyNewFound && agent.AgentNotification != null && !string.IsNullOrEmpty(agent.AgentNotification.SubdomainPayload)) { var payload = agent.AgentNotification.SubdomainPayload.Replace("{{domain}}", subdomain.Name); await this.notificationService.SendAsync(payload, cancellationToken); } } await this.SaveScriptOutputAsync(domain, subdomain, agent, scriptOutput, activateNotification, cancellationToken); }
/// <summary> /// Update the subdomain with directory discovery /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="scriptOutput">The terminal output one line</param> /// <returns><see cref="ISubdomainService"/></returns> private SubdomainService UpdateSubdomainDirectory(Subdomain subdomain, ScriptOutput scriptOutput) { if (!string.IsNullOrEmpty(scriptOutput.HttpDirectory)) { var httpDirectory = scriptOutput.HttpDirectory.TrimEnd('/').TrimEnd(); if (subdomain.ServiceHttp == null) { subdomain.ServiceHttp = new ServiceHttp(); } if (subdomain.ServiceHttp.Directories == null) { subdomain.ServiceHttp.Directories = new List <ServiceHttpDirectory>(); } if (subdomain.ServiceHttp.Directories.Any(d => d.Directory == httpDirectory)) { return(this); } var directory = new ServiceHttpDirectory() { Directory = httpDirectory, StatusCode = scriptOutput.HttpDirectoryStatusCode, Method = scriptOutput.HttpDirectoryMethod, Size = scriptOutput.HttpDirectorySize }; subdomain.ServiceHttp.Directories.Add(directory); } return(this); }
/// <summary> /// Update the subdomain if is a new service with open port /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="scriptOutput">The terminal output one line</param> /// <returns><see cref="ISubdomainService"/></returns> private SubdomainService UpdateSubdomainService(Subdomain subdomain, ScriptOutput scriptOutput) { if (string.IsNullOrWhiteSpace(scriptOutput.Service)) { return(this); } var service = new Service { Name = scriptOutput.Service.ToLower(), Port = scriptOutput.Port.Value }; if (subdomain.Services == null) { subdomain.Services = new List <Service>(); } if (!subdomain.Services.Any(s => s.Name == service.Name && s.Port == service.Port)) { subdomain.Services.Add(service); } return(this); }
private static ScriptOutput Execute(string scriptBlock, IDictionary <string, object> parameters = null, string workingDirectory = null) { if (string.IsNullOrWhiteSpace(scriptBlock)) { throw new ArgumentNullException(nameof(scriptBlock)); } using (var shell = PowerShell.Create()) { if (!string.IsNullOrEmpty(workingDirectory)) { shell.AddScript($"{Set_Location} \"{workingDirectory}\""); } var script = shell.AddScript(scriptBlock); if (parameters != null) { script.AddParameters(parameters.ToDictionary(k => k.Key, v => v.Value)); } var output = ScriptOutput.ExecuteAndOutput(script); if (shell.Streams.Error.Count > 0) { throw new AggregateException(shell.Streams.Error.Select(e => e.Exception)); } return(output); } }
protected ServerSessionBase() { SessionId = Guid.NewGuid().ToString("N"); TimeCreated = DateTime.UtcNow; CacheSpan = TimeSpan.FromHours(8); LifeSpan = TimeSpan.FromHours(12); Output = new ScriptOutput(); _log = new Lazy <ILog>(() => LogManager.GetLogger(GetType())); hostList = new ConcurrentDictionary <string, DomainAgentSessionHostBase>(StringComparer.Ordinal); PushedProjectPackageList = new List <PackageReference>(); WorkDirectory = Path.Combine(Configuration.WorkDirectory, SessionId); BinDirectory = Path.Combine(WorkDirectory, "bin"); ContentDirectory = Path.Combine(WorkDirectory, "content"); projectPackages = new ProjectPackageManager { PackageDirectory = Configuration.ProjectPackageDirectory, }; applicationPackages = new ApplicationPackageManager { PackageDirectory = Configuration.ApplicationPackageDirectory, }; ConnectionFactory = new DomainConnectionFactory(); ConnectionFactory.OnConnectionRequest += ConnectionFactory_OnConnectionRequest; PackageClient = new DomainPackageClient(); PackageClient.OnPushProjectPackage += PackageClient_OnPushProjectPackage; PackageClient.OnPushApplicationPackage += PackageClient_OnPushApplicationPackage; PackageClient.OnPullProjectPackage += PackageClient_OnPullProjectPackage; PackageClient.OnPullApplicationPackage += PackageClient_OnPullApplicationPackage; TokenSource = new CancellationTokenSource(); }
public static ScriptOutput AddFloat(ScriptInput idx) { ScriptOutput so = new ScriptOutput(); so.SetValue("result", Convert.ToDouble(idx.GetValue("f1")) + Convert.ToDouble(idx.GetValue("f2"))); idx.Write("AddFloat结果:" + so.GetValue("result").ToString()); return(so); }
public ScriptOutput Run(ScriptInput input) { var co = new ScriptOutput(); co.Code = input.Code; co.Output = CompileHelper.CompileAndRun(string.Format(input.Language == Language.CSharp?CodeSamples.ScriptWrapperCSharp:CodeSamples.ScriptWrapperVBNet, input.Code), input.Language); return(co); }
/// <summary> /// 脚本检测是否断点 /// </summary> /// <param name="item"></param> /// <param name="si"></param> /// <param name="so"></param> protected void checkScriptRunForStopPoint(IItemBox item, ScriptInput si, ScriptOutput so) { if (item.IsHasBreakPoint || isDebugMode) { ScriptBreakPoint?.Invoke(item, si, null); runStop(); } }
public static ScriptOutput AddInt(ScriptInput idx) { ScriptOutput so = new ScriptOutput(); so.SetValue("result", (Convert.ToInt32(idx.GetValue("d1")) + Convert.ToInt32(idx.GetValue("d2")))); idx.Write("AddInt结果:" + so.GetValue("result").ToString()); return(so); }
public void Output_Test() { ScriptOutput target = new ScriptOutput(); string expected = "Foo"; string actual; target.Output = expected; actual = target.Output; Assert.AreEqual(expected, actual); }
public void ContentType_Test() { ScriptOutput target = new ScriptOutput(); string expected = "Foo"; string actual; target.ContentType = expected; actual = target.ContentType; Assert.AreEqual(expected, actual); }
public SessionOutput(MessageTransceiver transceiver, string serverSessionId, string sessionClientId) { this.transceiver = transceiver; this.serverSessionId = serverSessionId; this.sessionClientId = sessionClientId; Writer = new ScriptOutput(); Writer.Changed += Output_OnChanged; }
/// <summary> /// Update the subdomain if is Alive /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="scriptOutput">The terminal output one line</param> /// <returns><see cref="ISubdomainService"/></returns> private SubdomainService UpdateSubdomainIsAlive(Subdomain subdomain, ScriptOutput scriptOutput) { if (scriptOutput.IsAlive != null && subdomain.IsAlive != scriptOutput.IsAlive) { subdomain.IsAlive = scriptOutput.IsAlive.Value; } return(this); }
/// <summary> /// Assign Ip address to the subdomain /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="scriptOutput">The terminal output one line</param> /// <returns><see cref="ISubdomainService"/></returns> private SubdomainService UpdateSubdomainIpAddress(Subdomain subdomain, ScriptOutput scriptOutput) { if (scriptOutput.Ip != null && Helpers.Helpers.ValidateIPv4(scriptOutput.Ip) && subdomain.IpAddress != scriptOutput.Ip) { subdomain.IpAddress = scriptOutput.Ip; } return(this); }
public void FromCache_Test() { ScriptOutput target = new ScriptOutput(); bool expected = false; bool actual; target.FromCache = expected; actual = target.FromCache; Assert.AreEqual(expected, actual); }
public static ScriptOutput Add(ScriptInput scriptInput) { int add1 = Convert.ToInt32(scriptInput.GetValue("add1")); int add2 = Convert.ToInt32(scriptInput.GetValue("add2")); ScriptOutput scriptOutput = new ScriptOutput(); scriptOutput.SetValue("result", add1 + add2); return(scriptOutput); }
/// <summary> /// Update the subdomain if it can be takeover /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="scriptOutput">The terminal output one line</param> /// <returns><see cref="ISubdomainService"/></returns> private SubdomainService UpdateSubdomainTakeover(Subdomain subdomain, ScriptOutput scriptOutput) { if (scriptOutput.Takeover != null && subdomain.Takeover != scriptOutput.Takeover.Value) { subdomain.Takeover = scriptOutput.Takeover.Value; } return(this); }
/// <summary> /// Update the subdomain if it has http port open /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="scriptOutput">The terminal output one line</param> /// <returns><see cref="ISubdomainService"/></returns> private SubdomainService UpdateSubdomainHasHttpOpen(Subdomain subdomain, ScriptOutput scriptOutput) { if (scriptOutput.HasHttpOpen != null && subdomain.HasHttpOpen != scriptOutput.HasHttpOpen.Value) { subdomain.HasHttpOpen = scriptOutput.HasHttpOpen.Value; } return(this); }
/// <summary> /// 脚本返回回调 /// </summary> /// <param name="obj"></param> private void ScriptReponse(ScriptOutput obj, string name) { if (PluginManager == null) { return; } foreach (var item in PluginManager.PluginItems) { item.ScriptPlugin.ReciveResponseFunction(obj, name); } }
/// <summary> /// <see cref="IAgentBackgroundService.SaveOutputParseOnScopeAsync(AgentRunner, ScriptOutput, CancellationToken)"/> /// </summary> public async Task SaveOutputParseOnScopeAsync(AgentRunner agentRun, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default) { using (var scope = this.serviceProvider.CreateScope()) { var targetService = scope.ServiceProvider .GetRequiredService <ITargetService>(); await targetService.SaveTerminalOutputParseAsync(agentRun, terminalOutputParse, cancellationToken); } }
/// <summary> /// <see cref="ISaveTerminalOutputParseService.SaveTerminalOutputParseAsync(agentRunnerner, ScriptOutput, CancellationToken)"/> /// </summary> public async Task SaveTerminalOutputParseAsync(AgentRunner agentRunner, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (!string.IsNullOrEmpty(terminalOutputParse.Ip)) { await this.UpdateSubdomainIpAddressAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (terminalOutputParse.IsAlive != null) { await this.UpdateSubdomainIsAliveAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (terminalOutputParse.HasHttpOpen != null) { await this.UpdateSubdomainHasHttpOpenAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (terminalOutputParse.Takeover != null) { await this.UpdateSubdomainTakeoverAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (!string.IsNullOrEmpty(terminalOutputParse.HttpDirectory)) { await this.UpdateSubdomainDirectoryAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (!string.IsNullOrEmpty(terminalOutputParse.Service)) { await this.UpdateSubdomainServiceAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (!string.IsNullOrEmpty(terminalOutputParse.Note)) { await this.UpdateSubdomainNoteAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (!string.IsNullOrEmpty(terminalOutputParse.Technology)) { await this.UpdateSubdomainTechnologyAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } if (!string.IsNullOrEmpty(terminalOutputParse.HttpScreenshotFilePath) || !string.IsNullOrEmpty(terminalOutputParse.HttpsScreenshotFilePath)) { await this.UpdateSubdomainScreenshotAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse); } if (!string.IsNullOrWhiteSpace(terminalOutputParse.Label)) { await this.UpdateSubdomainLabelAsync(agentRunner.Subdomain, agentRunner, terminalOutputParse, cancellationToken); } }
/// <summary> /// <see cref="ISaveTerminalOutputParseService.SaveTerminalOutputParseAsync(AgentRunner, ScriptOutput, CancellationToken)"/> /// </summary> public async Task SaveTerminalOutputParseAsync(AgentRunner agentRunner, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (await this.NeedAddNewRootDomain(agentRunner, terminalOutputParse.RootDomain, cancellationToken)) { agentRunner.RootDomain = await this.AddTargetNewRootDomainAsync(agentRunner.Target, terminalOutputParse.RootDomain, cancellationToken); if (agentRunner.ActivateNotification) { await this.notificationService.SendAsync(NotificationType.SUBDOMAIN, new[]
/// <summary> /// 添加一个属性 /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> private bool AddProperty(string propertyName, ScriptOutput value) { if (propertys.ContainsKey(propertyName) == false) { propertys.Add(propertyName, value); return(true); } else { return(false); } }
public static ScriptOutput PrintObject(ScriptInput si) { ScriptOutput scriptOutput = new ScriptOutput(); object obj = si.GetValue("obj") as object; string message = si.GetValue("message").ToString(); string content = ""; content += obj == null ? "" : obj.ToString(); content += message; scriptOutput.Write(content); return(scriptOutput); }
/// <summary> /// 初始化 /// </summary> protected override void Init(IScriptLayout ml) { if (ml.IPropertys.Count > 0) { foreach (var item in ml.IPropertys) { ScriptOutput so = new ScriptOutput(); so.SetValue(item.Name, item.Value); manager.SetValue(item.Id, so); } } }
/// <summary> /// 设置属性值 /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> /// <returns></returns> public bool SetValue(string propertyName, ScriptOutput value) { if (propertys.ContainsKey(propertyName) == true) { propertys[propertyName] = null; propertys[propertyName] = value; return(true); } else { return(AddProperty(propertyName, value)); } }
/// <summary> /// <see cref="ISubdomainService.UpdateSubdomainAsync(Subdomain, Agent, ScriptOutput, bool)"/> /// </summary> public void UpdateSubdomain(Subdomain subdomain, Agent agent, ScriptOutput scriptOutput) { this.UpdateSubdomainIpAddress(subdomain, scriptOutput) .UpdateSubdomainIsAlive(subdomain, scriptOutput) .UpdateSubdomainHasHttpOpen(subdomain, scriptOutput) .UpdateSubdomainTakeover(subdomain, scriptOutput) .UpdateSubdomainScreenshot(subdomain, scriptOutput) .UpdateSubdomainDirectory(subdomain, scriptOutput) .UpdateSubdomainService(subdomain, scriptOutput) .UpdateSubdomainAgent(subdomain, agent); this.UnitOfWork.Repository <Subdomain>().Update(subdomain); }
/// <summary> /// Update the subdomain if is Alive /// </summary> /// <param name="subdomain">The subdomain</param> /// <param name="agent">The Agent</param> /// <param name="scriptOutput">The terminal output one line</param> /// <param name="activateNotification">If the notification is active</param> /// <param name="cancellationToken">Notification that operations should be canceled</param> /// <returns>A task</returns> private async Task UpdateSubdomainIsAlive(Subdomain subdomain, Agent agent, ScriptOutput scriptOutput, bool activateNotification, CancellationToken cancellationToken = default) { if (scriptOutput.IsAlive != null && subdomain.IsAlive != scriptOutput.IsAlive) { subdomain.IsAlive = scriptOutput.IsAlive.Value; if (activateNotification && agent.NotifyNewFound && agent.AgentNotification != null && !string.IsNullOrEmpty(agent.AgentNotification.IsAlivePayload)) { var payload = agent.AgentNotification.IsAlivePayload.Replace("{{domain}}", subdomain.Name).Replace("{{isAlive}}", scriptOutput.IsAlive.Value.ToString()); await this.notificationService.SendAsync(payload, cancellationToken); } } }
/// <summary> /// 运行带url的函数 /// </summary> /// <param name="itemBox"></param> /// <param name="scriptInput"></param> /// <returns></returns> private ScriptOutput RunScript(IItemBox itemBox, ScriptInput scriptInput) { ScriptRequest?.Invoke(scriptInput, itemBox.Name); string json = ScriptClient.PostStringAsync(itemBox.ScriptUrl, (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(scriptInput))); ScriptOutput scriptOutput = JsonConvert.DeserializeObject <ScriptOutput>(json); ScriptReponse?.Invoke(scriptOutput, itemBox.Name); if (scriptOutput != null && scriptOutput.IsExecption) { throw new Exception("服务器给出停止信号!"); } return(scriptOutput); }
/// <summary> /// 设置值 /// </summary> /// <param name="ib"></param> /// <param name="sm"></param> /// <returns></returns> protected static IItemBox DoFunction(ItemBox itembox, ScriptObjectManager sm, WriteStreamCallBack wrs) { LineItemBox ib = itembox as LineItemBox; if (ib.BoxType == ItemBoxEnum.IF) { ScriptInput si = new ScriptInput(); //si.WriteStream += wrs; foreach (var item in ib.InputDatas) { if (item.PIEnum != ParaItemEnum.OUTPUT && item.PIEnum != ParaItemEnum.INPUT && item.PIEnum != ParaItemEnum.NULL) { si.SetValue(item.Name, getValue(item as ParatItem, sm)); } } if (Convert.ToBoolean(si.GetFirst())) { return(ib.Next(0)); } else { return(ib.Next(1)); } } else if (ib.BoxType == ItemBoxEnum.SET) { ScriptOutput so = new ScriptOutput(); so.SetValue(ib.InputDatas[1].Name, getValue(ib.InputDatas[1] as ParatItem, sm)); //so.SetValue(ib.InputDatas[1].Name, ib.InputDatas[1].Value); sm.SetValue(ib.Id, so); return(ib.Next(0)); } else { ScriptInput si = new ScriptInput(); si.WriteStream += wrs; foreach (var item in ib.InputDatas) { if (item.PIEnum != ParaItemEnum.OUTPUT && item.PIEnum != ParaItemEnum.INPUT && item.PIEnum != ParaItemEnum.NULL) { si.SetValue(item.Name, getValue(item as ParatItem, sm)); } } ScriptOutput so = (ib as ItemBox).DoScriptFunction(si); if (so != null) { sm.SetValue(ib.Id, so); } return(ib.Next(0)); } }
public void ScriptOutputConstructor_Test() { ScriptOutput target = new ScriptOutput(); }