/// <summary> /// <see cref="ISubdomainService.GetAllWithIncludesAsync(Target RootDomain, string, CancellationToken)"/> /// </summary> public async Task <List <Subdomain> > GetAllWithIncludesAsync(Target target, RootDomain rootDomain, string subdomain, CancellationToken cancellationToken = default) { if (target == null && rootDomain == null) { return(new List <Subdomain>()); } IQueryable <Subdomain> query; if (string.IsNullOrEmpty(subdomain)) { query = this.GetAllQueryableByCriteria(s => s.Target == target && s.RootDomain == rootDomain, cancellationToken); } else { query = this.GetAllQueryableByCriteria(s => s.Target == target && s.RootDomain == rootDomain && s.Name == subdomain, cancellationToken); } return(await query .Include(t => t.Services) .Include(t => t.Notes) .Include(t => t.ServiceHttp) .ThenInclude(sh => sh.Directories) .Include(t => t.Labels) .ThenInclude(ac => ac.Label) .OrderByDescending(s => s.CreatedAt) .ToListAsync(cancellationToken)); }
/// <summary> /// If we need to skip this RootDomain /// </summary> /// <param name="rootDomain">The rootDomain</param> /// <param name="agentTrigger">Agent trigger configuration</param> /// <returns>If we need to skip this RootDomain</returns> private static bool SkipRootDomain(RootDomain rootDomain, AgentTrigger agentTrigger) { if (agentTrigger.RootdomainHasBounty && (rootDomain == null || !rootDomain.HasBounty)) { return(true); } if (!string.IsNullOrEmpty(agentTrigger.RootdomainIncExcName) && !string.IsNullOrEmpty(agentTrigger.RootdomainName)) { if (INCLUDE.Equals(agentTrigger.RootdomainIncExcName, StringComparison.OrdinalIgnoreCase)) { var match = Regex.Match(rootDomain.Name, agentTrigger.RootdomainName); // if match success dont skip this rootdomain return(!match.Success); } else if (EXCLUDE.Equals(agentTrigger.RootdomainIncExcName, StringComparison.OrdinalIgnoreCase)) { var match = Regex.Match(rootDomain.Name, agentTrigger.RootdomainName); // if match success skip this rootdomain return(match.Success); } } return(false); }
protected virtual void Initialize(RootDomain domain) { BasicDomainConfiguration configuration; Stream stream; configuration = new BasicDomainConfiguration(); stream = this.GetResourceAsStream( "BaseSettings/RootDomain.xml", true); try { configuration.Load(stream); } finally { stream.Close(); } stream = this.GetResourceAsStream( "LocalSettings/RootDomain.xml", false); if (stream != null) { try { configuration.Load(stream); } finally { stream.Close(); } } domain.Initialize(configuration); }
public async Task <ActionResult> StopAgent([FromBody] AgentRunnerDto agentRunnerDto, CancellationToken cancellationToken) { var agent = await agentService.GetByCriteriaAsync(a => a.Name == agentRunnerDto.Agent, cancellationToken); if (agent == null) { return(BadRequest()); } Target target = default; if (!string.IsNullOrWhiteSpace(agentRunnerDto.Target)) { target = await this.targetService.GetTargetNotTrackingAsync(t => t.Name == agentRunnerDto.Target, cancellationToken); if (target == null) { return(BadRequest()); } } RootDomain rootDomain = default; if (!string.IsNullOrWhiteSpace(agentRunnerDto.RootDomain)) { rootDomain = await this.rootDomainService.GetRootDomainNoTrackingAsync(t => t.Name == agentRunnerDto.RootDomain && t.Target == target, cancellationToken); if (rootDomain == null) { return(NotFound()); } } Subdomain subdomain = default; if (!string.IsNullOrWhiteSpace(agentRunnerDto.Subdomain)) { subdomain = await this.subdomainService .GetAllQueryableByCriteria(s => s.RootDomain == rootDomain && s.Name == agentRunnerDto.Subdomain) .AsNoTracking() .SingleOrDefaultAsync(cancellationToken); if (subdomain == null) { return(NotFound()); } } var agentRunner = new AgentRunner { Agent = agent, Target = target, RootDomain = rootDomain, Subdomain = subdomain }; await this.agentRunnerService.StopAgentAsync(agentRunner, cancellationToken); return(NoContent()); }
public async Task <ActionResult> StopAgent([FromBody] AgentRunnerDto agentRunnerDto, CancellationToken cancellationToken) { var agent = await agentService.GetByCriteriaAsync(a => a.Name == agentRunnerDto.Agent, cancellationToken); if (agent == null) { return(BadRequest()); } Target target = default; if (!string.IsNullOrWhiteSpace(agentRunnerDto.Target)) { target = await this.targetService.GetByCriteriaAsync(t => t.Name == agentRunnerDto.Target, cancellationToken); if (target == null) { return(BadRequest()); } } RootDomain rootDomain = default; if (!string.IsNullOrWhiteSpace(agentRunnerDto.RootDomain)) { rootDomain = await this.rootDomainService.GetByCriteriaAsync(t => t.Name == agentRunnerDto.RootDomain && t.Target == target, cancellationToken); if (rootDomain == null) { return(NotFound()); } } Subdomain subdomain = null; if (!string.IsNullOrWhiteSpace(agentRunnerDto.Subdomain)) { subdomain = await this.subdomainService.GetByCriteriaAsync(s => s.RootDomain == rootDomain && s.Name == agentRunnerDto.Subdomain, cancellationToken); if (subdomain == null) { return(NotFound()); } } var agentRunner = new AgentRunner { Agent = agent, Target = target, RootDomain = rootDomain, Subdomain = subdomain }; var channel = AgentRunnerHelpers.GetChannel(agentRunner); await this.agentRunnerService.StopAgentAsync(agentRunner, channel, cancellationToken); return(NoContent()); }
/// <summary> /// Run the Agent /// </summary> /// <param name="target">The target</param> /// <param name="rootDomain"></param> /// <param name="subdomain"></param> /// <param name="agent"></param> /// <param name="command"></param> /// <param name="channel"></param> /// <param name="activateNotification"></param> /// <param name="cancellationToken"></param> /// <returns></returns> private async Task RunAgentAsync(Target target, RootDomain rootDomain, Subdomain subdomain, Agent agent, string command, string channel, bool activateNotification, CancellationToken cancellationToken) { var commandToRun = this.GetCommand(target, rootDomain, subdomain, agent, command); await this.connectorService.SendAsync("logs_" + channel, $"RUN: {command}"); await this.RunBashAsync(rootDomain, subdomain, agent, commandToRun, channel, activateNotification, cancellationToken); }
public async Task <IActionResult> RunAgent([FromBody] AgentRunnerDto agentRunnerDto, CancellationToken cancellationToken) { var agent = await agentService.GetAgentToRunAsync(a => a.Name == agentRunnerDto.Agent, cancellationToken); if (agent == null) { return(BadRequest()); } Target target = default; if (!string.IsNullOrWhiteSpace(agentRunnerDto.Target)) { target = await this.targetService.GetTargetNotTrackingAsync(t => t.Name == agentRunnerDto.Target, cancellationToken); if (target == null) { return(BadRequest()); } } RootDomain rootDomain = default; if (!string.IsNullOrWhiteSpace(agentRunnerDto.RootDomain)) { rootDomain = await this.rootDomainService.GetRootDomainNoTrackingAsync(t => t.Target == target && t.Name == agentRunnerDto.RootDomain, cancellationToken); if (rootDomain == null) { return(NotFound()); } } Subdomain subdomain = default; if (rootDomain != null && !string.IsNullOrWhiteSpace(agentRunnerDto.Subdomain)) { subdomain = await this.subdomainService.GetSubdomainAsync(s => s.RootDomain == rootDomain && s.Name == agentRunnerDto.Subdomain, cancellationToken); if (subdomain == null) { return(NotFound()); } } await this.agentRunnerService.RunAgentAsync( new AgentRunner { Agent = agent, Target = target, RootDomain = rootDomain, Subdomain = subdomain, ActivateNotification = agentRunnerDto.ActivateNotification, Command = agentRunnerDto.Command }, cancellationToken); return(NoContent()); }
/// <summary> /// <see cref="ISubdomainService.GetSubdomainsByTargetAsync(RootDomain, CancellationToken)"/> /// </summary> public async Task <List <Subdomain> > GetSubdomainsByTargetAsync(RootDomain domain, CancellationToken cancellationToken = default) { return(await this.GetAllQueryableByCriteria(s => s.Domain == domain, cancellationToken) .Include(t => t.Services) .Include(t => t.Notes) .Include(t => t.Labels) .ThenInclude(ac => ac.Label) .OrderByDescending(s => s.CreatedAt) .ToListAsync(cancellationToken)); }
/// <summary> /// Obtain the command to run on bash /// </summary> /// <param name="target">The target</param> /// <param name="rootDomain">The domain</param> /// <param name="subdomain">The subdomain</param> /// <param name="agent">The agent</param> /// <param name="command">The command to run</param> /// <returns>The command to run on bash</returns> private string GetCommand(Target target, RootDomain rootDomain, Subdomain subdomain, Agent agent, string command) { if (string.IsNullOrWhiteSpace(command)) { command = agent.Command; } return($"{command.Replace("{{domain}}", subdomain == null ? rootDomain.Name : subdomain.Name)}" .Replace("{{target}}", target.Name) .Replace("{{rootDomain}}", rootDomain.Name) .Replace("\"", "\\\"")); }
public List <OrderEf> Resolve(RootDomain source, RootEf destination, List <OrderEf> destMember, ResolutionContext context) { var mappedOnlineOrders = new List <OrderEf>(destination.Orders); var mappedMailOrders = new List <OrderEf>(destination.Orders); context.Mapper.Map(source.OnlineOrders, mappedOnlineOrders, context); context.Mapper.Map(source.MailOrders, mappedMailOrders, context); var efOrders = mappedOnlineOrders.Union(mappedMailOrders).ToList(); return(efOrders); }
public void Map_FromEfToDomain_And_AddAnOnlineOrderInTheDomainObject_And_ThenMapBackToEf_Should_UseTheSameReferenceInTheEfCollection() { var mapper = CreateMapper(); //arrange var onlineOrderEf = new OnlineOrderEf { Id = "Id", Key = "Key" }; var mailOrderEf = new MailOrderEf { Id = "MailOrderId" }; var rootEf = new RootEf { Orders = { onlineOrderEf, mailOrderEf } }; //act RootDomain mappedRootDomain = mapper.Map <RootEf, RootDomain>(rootEf); //assert OnlineOrderDomain onlineOrderDomain = mappedRootDomain.OnlineOrders[0]; onlineOrderDomain.Should().BeOfType <OnlineOrderDomain>(); onlineOrderEf.Id.ShouldBeEquivalentTo(onlineOrderEf.Id); // IMPORTANT ASSERT ------------------------------------------------------------- IMPORTANT ASSERT // //arrange again mappedRootDomain.OnlineOrders.Add(new OnlineOrderDomain { Id = "NewOnlineOrderId", Key = "NewKey" }); mappedRootDomain.MailOrders.Add(new MailOrderDomain { Id = "NewMailOrderId", }); onlineOrderDomain.Id = "Hi"; //act again mapper.Map(mappedRootDomain, rootEf); //assert again OrderEf existingMailOrderEf = rootEf.Orders.Single(orderEf => orderEf.Id == mailOrderEf.Id); OrderEf existingOnlineOrderEf = rootEf.Orders.Single(orderEf => orderEf.Id == onlineOrderEf.Id); OrderEf newOnlineOrderEf = rootEf.Orders.Single(orderEf => orderEf.Id == "NewOnlineOrderId"); OrderEf newMailOrderEf = rootEf.Orders.Single(orderEf => orderEf.Id == "NewMailOrderId"); rootEf.Orders.Count.ShouldBeEquivalentTo(4); onlineOrderEf.Should().BeSameAs(existingOnlineOrderEf); mailOrderEf.Should().BeSameAs(existingMailOrderEf); newOnlineOrderEf.Should().BeOfType <OnlineOrderEf>(); newMailOrderEf.Should().BeOfType <MailOrderEf>(); }
/// <summary> /// <see cref="ISaveTerminalOutputParseService.SaveTerminalOutputParseAsync(Target, string, bool, ScriptOutput, CancellationToken)"/> /// </summary> public async Task SaveTerminalOutputParseAsync(Target target, string agentName, bool activateNotification, ScriptOutput terminalOutputParse, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); RootDomain rootDomain = default; if (await this.NeedAddNewRootDomain(target, terminalOutputParse.RootDomain, cancellationToken)) { rootDomain = await this.AddTargetNewRootDomainAsync(target, terminalOutputParse.RootDomain, cancellationToken); if (activateNotification) { await this.notificationService.SendAsync(NotificationType.SUBDOMAIN, new[]
/// <summary> /// <see cref="IAgentService.RunAsync(Target, RootDomain, Subdomain, Agent, string, CancellationToken)"></see> /// </summary> public async Task RunAsync(Target target, RootDomain rootDomain, Subdomain subdomain, Agent agent, string command, bool activateNotification, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); var channel = this.GetChannel(target, rootDomain, subdomain, agent); this.runnerProcess.Stopped = false; if (this.NeedToRunInEachSubdomain(subdomain, agent)) { // wait 1 sec to avoid broke the frontend modal Thread.Sleep(1000); foreach (var sub in rootDomain.Subdomains.ToList()) { if (cancellationToken.IsCancellationRequested) { this.runnerProcess.Stopped = true; this.runnerProcess.KillProcess(); cancellationToken.ThrowIfCancellationRequested(); } if (this.runnerProcess.Stopped) { break; } var needToSkip = this.NeedToSkipSubdomain(agent, sub); if (needToSkip) { await this.connectorService.SendAsync("logs_" + channel, $"Skip subdomain: {sub.Name}"); continue; } await this.RunAgentAsync(target, rootDomain, sub, agent, command, channel, activateNotification, cancellationToken); } } else { await this.RunAgentAsync(target, rootDomain, subdomain, agent, command, channel, activateNotification, cancellationToken); } await this.SendAgentDoneNotificationAsync(channel, agent, activateNotification, cancellationToken); // update the last time that we run this agent agent.LastRun = DateTime.Now; await this.UpdateAsync(agent, cancellationToken); }
public async Task <ActionResult> RunningAgent(string targetName, string rootDomainName, string subdomainName, CancellationToken cancellationToken) { Target target = default; if (!string.IsNullOrWhiteSpace(targetName)) { target = await this.targetService.GetTargetNotTrackingAsync(t => t.Name == targetName, cancellationToken); if (target == null) { return(BadRequest()); } } RootDomain rootDomain = default; if (!string.IsNullOrWhiteSpace(rootDomainName) && !"undefined".Equals(rootDomainName)) { rootDomain = await this.rootDomainService.GetRootDomainNoTrackingAsync(t => t.Target == target && t.Name == rootDomainName, cancellationToken); if (rootDomain == null) { return(NotFound()); } } Subdomain subdomain = default; if (!string.IsNullOrWhiteSpace(subdomainName) && !"undefined".Equals(subdomainName)) { subdomain = await this.subdomainService .GetAllQueryableByCriteria(s => s.RootDomain == rootDomain && s.Name == subdomainName) .AsNoTracking() .SingleOrDefaultAsync(cancellationToken); if (subdomain == null) { return(NotFound()); } } var agentsRunning = await this.agentRunnerService.RunningAgentsAsync(new AgentRunner { Target = target, RootDomain = rootDomain, Subdomain = subdomain }, cancellationToken); return(Ok(agentsRunning)); }
/// <summary> /// Obtain the command to run on bash /// </summary> /// <param name="target">The target</param> /// <param name="rootDomain">The domain</param> /// <param name="subdomain">The subdomain</param> /// <param name="agent">The agent</param> /// <param name="command">The command to run</param> /// <returns>The command to run on bash</returns> private string GetCommand(Target target, RootDomain rootDomain, Subdomain subdomain, Agent agent, string command) { if (string.IsNullOrWhiteSpace(command)) { command = agent.Command; } var envUserName = Environment.GetEnvironmentVariable("ReconnessUserName") ?? Environment.GetEnvironmentVariable("ReconnessUserName", EnvironmentVariableTarget.User); var envPassword = Environment.GetEnvironmentVariable("ReconnessPassword") ?? Environment.GetEnvironmentVariable("ReconnessPassword", EnvironmentVariableTarget.User); return($"{command.Replace("{{domain}}", subdomain == null ? rootDomain.Name : subdomain.Name)}" .Replace("{{target}}", target.Name) .Replace("{{rootDomain}}", rootDomain.Name) .Replace("{{userName}}", envUserName) .Replace("{{password}}", envPassword) .Replace("\"", "\\\"")); }
/// <summary> /// <see cref="IAgentService.StopAsync(Target, RootDomain, Subdomain, Agent, CancellationToken)"></see> /// </summary> public async Task StopAsync(Target target, RootDomain rootDomain, Subdomain subdomain, Agent agent, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); var channel = subdomain == null ? $"{target.Name}_{rootDomain.Name}_{agent.Name}" : $"{target.Name}_{rootDomain.Name}_{subdomain.Name}_{agent.Name}"; if (this.runnerProcess.IsRunning()) { try { this.runnerProcess.KillProcess(); } catch (Exception ex) { await this.connectorService.SendAsync(channel, ex.Message, cancellationToken); } } this.runnerProcess.Stopped = true; await this.connectorService.SendAsync(channel, "Agent stopped!", cancellationToken); }
/// <inheritdoc/> public async Task SaveRootdomainNotesAsync(RootDomain rootDomain, string notesContent, CancellationToken cancellationToken = default) { var notes = rootDomain.Notes; if (notes == null) { notes = new Note { Notes = notesContent, RootDomain = rootDomain }; await this.AddAsync(notes, cancellationToken); } else { notes.Notes = notesContent; await this.UpdateAsync(notes, cancellationToken); } }
public void Map_Should_ReturnOnlineOrderEf_When_ListIsOfTypeOrderEf() { var mapper = CreateMapper(); //arrange var orderDomain = new OnlineOrderDomain { Id = "Id", Key = "Key" }; var rootDomain = new RootDomain { OnlineOrders = { orderDomain } }; //act RootEf mappedRootEf = mapper.Map <RootDomain, RootEf>(rootDomain); //assert OrderEf orderEf = mappedRootEf.Orders[0]; orderEf.Should().BeOfType <OnlineOrderEf>(); orderEf.Id.ShouldBeEquivalentTo(orderDomain.Id); var onlineOrderEf = (OnlineOrderEf)orderEf; onlineOrderEf.Key.ShouldBeEquivalentTo(orderDomain.Key); // ------------------------------------------------------------- // //arrange again mappedRootEf.Orders.Add(new OnlineOrderEf { Id = "NewId" }); mapper.Map(mappedRootEf, rootDomain); //assert again rootDomain.OnlineOrders.Count.ShouldBeEquivalentTo(2); rootDomain.OnlineOrders.Last().Should().BeOfType <OnlineOrderDomain>(); //Assert.AreSame(rootDomain.OnlineOrders.First(), orderDomain); that doesn't matter when we map from EF to Domain }
/// <summary> /// Method to run a bash command /// </summary> /// <param name="channel">The channel to send the menssage</param> /// <param name="command">The command to run on bash</param> /// <returns>A Task</returns> private async Task RunBashAsync(RootDomain rootDomain, Subdomain subdomain, Agent agent, string command, string channel, bool activateNotification, CancellationToken cancellationToken) { try { this.runnerProcess.StartProcess(command); this.scriptEngineService.InintializeAgent(agent); int lineCount = 1; while (this.runnerProcess.IsRunning() && !this.runnerProcess.EndOfStream) { cancellationToken.ThrowIfCancellationRequested(); var terminalLineOutput = this.runnerProcess.TerminalLineOutput(); var scriptOutput = await this.scriptEngineService.ParseInputAsync(terminalLineOutput, lineCount ++); await this.connectorService.SendAsync("logs_" + channel, $"Output #: {lineCount}"); await this.connectorService.SendAsync("logs_" + channel, $"Output: {terminalLineOutput}"); await this.connectorService.SendAsync("logs_" + channel, $"Result: {JsonConvert.SerializeObject(scriptOutput)}"); await this.rootDomainService.SaveScriptOutputAsync(rootDomain, subdomain, agent, scriptOutput, activateNotification, cancellationToken); await this.connectorService.SendAsync("logs_" + channel, $"Output #: {lineCount} processed"); await this.connectorService.SendAsync("logs_" + channel, "-----------------------------------------------------"); await this.connectorService.SendAsync(channel, terminalLineOutput, cancellationToken); } } catch (Exception ex) { await SendLogException(channel, ex); } finally { this.runnerProcess.KillProcess(); } }
public DummyItemService(RootDomain domain) { this._domain = domain; }
public RealItemModel(RootDomain domain) { this._domain = domain; }
public RealItemService(RootDomain domain) { this._domain = domain; }
public DummyItemService(RootDomain domain) { this._domain = domain; }
public RealItemService(RootDomain domain) { this._domain = domain; }
public DummyItemModel(RootDomain domain) { this._domain = domain; }
public Bootstrap() { this._rootDomain = new RootDomain(); }
/// <summary> /// Obtain the channel to send the menssage /// </summary> /// <param name="rootDomain">The domain</param> /// <param name="subdomain">The subdomain</param> /// <param name="agent">The agent</param> /// <returns>The channel to send the menssage</returns> private string GetChannel(Target target, RootDomain rootDomain, Subdomain subdomain, Agent agent) { return(subdomain == null ? $"{target.Name}_{rootDomain.Name}_{agent.Name}" : $"{target.Name}_{rootDomain.Name}_{subdomain.Name}_{agent.Name}"); }
/// <summary> /// <see cref="IRootDomainService.UploadRootDomainAsync(RootDomain, RootDomain, CancellationToken)"/> /// </summary> public async Task UploadRootDomainAsync(Target target, RootDomain newRootdomain, CancellationToken cancellationToken = default) { target.RootDomains.Add(newRootdomain); await this.UpdateAsync(target, cancellationToken); }
/// <inheritdoc/> public async Task <PagedResult <Subdomain> > GetPaginateAsync(RootDomain rootDomain, string query, int page, int limit, CancellationToken cancellationToken = default) { IQueryable <Subdomain> queryable = default; if (string.IsNullOrEmpty(query)) { queryable = this.GetAllQueryableByCriteria(s => s.RootDomain == rootDomain) .Select(subdomain => new Subdomain { Id = subdomain.Id, Name = subdomain.Name, CreatedAt = subdomain.CreatedAt, IpAddress = subdomain.IpAddress, AgentsRanBefore = subdomain.AgentsRanBefore, HasHttpOpen = subdomain.HasHttpOpen, IsAlive = subdomain.IsAlive, IsMainPortal = subdomain.IsMainPortal, Takeover = subdomain.Takeover, Labels = subdomain.Labels .Select(label => new Label { Name = label.Name, Color = label.Color }) .ToList(), Services = subdomain.Services .Select(service => new Service { Name = service.Name }).ToList() }) .OrderByDescending(s => s.CreatedAt) .AsNoTracking(); } else { queryable = this.GetAllQueryableByCriteria(s => s.RootDomain == rootDomain && s.Name.Contains(query)) .Select(subdomain => new Subdomain { Name = subdomain.Name, CreatedAt = subdomain.CreatedAt, IpAddress = subdomain.IpAddress, AgentsRanBefore = subdomain.AgentsRanBefore, HasHttpOpen = subdomain.HasHttpOpen, IsAlive = subdomain.IsAlive, IsMainPortal = subdomain.IsMainPortal, Takeover = subdomain.Takeover, Labels = subdomain.Labels .Select(label => new Label { Name = label.Name, Color = label.Color }) .ToList(), Services = subdomain.Services .Select(service => new Service { Name = service.Name }).ToList() }) .OrderByDescending(s => s.CreatedAt) .AsNoTracking(); } return(await queryable.GetPageAsync <Subdomain>(page, limit, cancellationToken)); }
public DummyItemModel(RootDomain domain) { this._domain = domain; }
public RealItemModel(RootDomain domain) { this._domain = domain; }