Skip to content

Commit

Permalink
7.2.0 adds download timeline at init
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-dupdyke committed Dec 21, 2023
1 parent 44682ab commit f4003cc
Show file tree
Hide file tree
Showing 11 changed files with 760 additions and 60 deletions.
52 changes: 34 additions & 18 deletions src/Ghosts.Client/Comms/CheckId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,34 @@ public class CheckId
/// </summary>
public string IdFile = ApplicationDetails.InstanceFiles.Id;

private DateTime _lastChecked = DateTime.Now;
private string _id = string.Empty;

public CheckId()
public CheckId(bool checkInitialTimeline = false)
{
_log.Trace($"CheckId instantiated with ID: {Id}");

try
{
if (checkInitialTimeline)
{
using var client = GetClient();
TimelineBuilder.CheckForUrlTimeline(client, Program.Configuration.TimelineConfiguration.Location);
}
}
catch (Exception e)
{
_log.Error("configuration doesn't have timeline location, update your application.json to latest please");
}
}

private WebClient GetClient()
{
var machine = new ResultMachine();
GuestInfoVars.Load(machine);
return WebClientBuilder.Build(machine, !string.IsNullOrEmpty(_id));
}


/// <summary>
/// Gets the agent's current id from local instance, and if it does not exist, gets an id from the server and saves it locally
/// </summary>
Expand All @@ -44,13 +64,13 @@ public string Id
{
if (!File.Exists(IdFile))
{
if (DateTime.Now > _lastChecked.AddMinutes(5))
if (DateTime.Now < Program.LastChecked.AddMinutes(5))
{
_log.Error("Skipping Check for ID from server, too many requests in a short amount of time...");
return string.Empty;
}

_lastChecked = DateTime.Now;
Program.LastChecked = DateTime.Now;
return Run();
}
Id = File.ReadAllText(IdFile);
Expand Down Expand Up @@ -81,13 +101,11 @@ private string Run()
return s;
}

var machine = new ResultMachine();
GuestInfoVars.Load(machine);
using var client = GetClient();

try
{
//call home
using var client = WebClientBuilder.BuildNoId(machine);
try
{
using var reader = new StreamReader(client.OpenRead(Program.Configuration.IdUrl) ?? throw new InvalidOperationException("CheckID client is null"));
Expand All @@ -113,23 +131,21 @@ private string Run()
catch (Exception e)
{
_log.Error($"Cannot connect to API: {e.Message}");
return string.Empty;
}

s = s.Replace("\"", "");

if (!Directory.Exists(ApplicationDetails.InstanceFiles.Path))
if (!string.IsNullOrEmpty(s))
{
Directory.CreateDirectory(ApplicationDetails.InstanceFiles.Path);
}
s = s.Replace("\"", "");

if (string.IsNullOrEmpty(s))
{
return string.Empty;
if (!Directory.Exists(ApplicationDetails.InstanceFiles.Path))
{
Directory.CreateDirectory(ApplicationDetails.InstanceFiles.Path);
}

//save returned id
File.WriteAllText(IdFile, s);
}

//save returned id
File.WriteAllText(IdFile, s);
return s;
}
}
15 changes: 3 additions & 12 deletions src/Ghosts.Client/Infrastructure/WebClientHeaders.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2017 Carnegie Mellon University. All Rights Reserved. See LICENSE.md file for terms.

using System.Net;
using System.Web.UI;
using Ghosts.Domain;
using Ghosts.Domain.Code;

Expand All @@ -11,21 +12,11 @@ namespace Ghosts.Client.Infrastructure;
/// </summary>
public static class WebClientBuilder
{
public static WebClient Build(ResultMachine machine)
{
return BuildEx(machine);
}

public static WebClient BuildNoId(ResultMachine machine)
{
return BuildEx(machine, false);
}

private static WebClient BuildEx(ResultMachine machine, bool hasId = true)
public static WebClient Build(ResultMachine machine, bool useId = true)
{
var client = new WebClient();
client.Headers.Add(HttpRequestHeader.UserAgent, "Ghosts Client");
if (hasId)
if (Program.CheckId != null && !string.IsNullOrEmpty(Program.CheckId.Id) && useId)
{
client.Headers.Add("ghosts-id", Program.CheckId.Id);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Ghosts.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Ghosts.Client.Comms;
using Ghosts.Client.Infrastructure;
using Ghosts.Client.TimelineManager;
using Ghosts.Domain;
using Ghosts.Domain.Code;
using Ghosts.Domain.Models;
using NLog;
Expand All @@ -36,6 +37,7 @@ class Program

internal static List<ThreadJob> ThreadJobs { get; set; }
internal static ClientConfiguration Configuration { get; set; }
internal static DateTime LastChecked = DateTime.Now.AddHours(-1);
internal static Options OptionFlags;
internal static bool IsDebug;
internal static IScheduler Scheduler;
Expand Down Expand Up @@ -133,8 +135,8 @@ private static void Run(string[] args)
Console.ReadLine();
return;
}

Program.CheckId = new CheckId();
Program.CheckId = new CheckId(true);

DebugManager.Evaluate();

Expand Down Expand Up @@ -175,7 +177,7 @@ private static void Run(string[] args)

//do we have client id? or is this first run?
_log.Trace($"CheckID: {Program.CheckId.Id}");

//connect to command server for 1) client id 2) get updates and 3) sending logs/surveys
Updates.Run();

Expand Down
4 changes: 2 additions & 2 deletions src/Ghosts.Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("7.1.0.0")]
[assembly: AssemblyVersion("7.2.0.0")]
[assembly: AssemblyInformationalVersion("7.1.0.0")]
[assembly: AssemblyFileVersion("7.1.0.0")]
[assembly: AssemblyFileVersion("7.2.0.0")]
4 changes: 4 additions & 0 deletions src/Ghosts.Client/config/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"OutputFormat": "indent",
"PostUrl": "http://ghosts-api:52388/api/clientsurvey"
},
"TimelineConfiguration": {
"useThisForLocalLocation": "config/timeline.json",
"Location": "https://raw.githubusercontent.com/cmu-sei/GHOSTS/6e04885809dfdceb138fbf9cdf1a1f795ea907ef/src/Ghosts.Client/config/timeline.json"
},
"Content": {
"EmailsMax": 20,
"EmailContent": "",
Expand Down

0 comments on commit f4003cc

Please sign in to comment.