Skip to content

Commit

Permalink
Merge pull request #310 from cmu-sei/v8
Browse files Browse the repository at this point in the history
V8
  • Loading branch information
sei-dupdyke authored Apr 4, 2024
2 parents a46d12e + 0d70304 commit 02f569f
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 92 deletions.
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;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
Expand All @@ -17,7 +18,9 @@
using ghosts.api.Infrastructure.Services;
using Ghosts.Domain;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using NLog;
using Npgsql.Internal.TypeHandlers.DateTimeHandlers;
using RestSharp;

namespace ghosts.api.Areas.Animator.Infrastructure.Animations.AnimationDefinitions;
Expand Down Expand Up @@ -147,37 +150,49 @@ private async void Step()

if (_configuration.AnimatorSettings.Animations.SocialSharing.IsSendingTimelinesToGhostsApi)
{
var formValues = new StringBuilder();
formValues.Append('{')
.Append("\\\"").Append(userFormValue).Append("\\\":\\\"").Append(agent.NpcProfile.Email).Append("\\\"")
.Append(",\\\"").Append(messageFormValue).Append("\\\":\\\"").Append(tweetText).Append("\\\"");
for (var i = 0; i < AnimatorRandom.Rand.Next(0, 6); i++)
var payload = new
{
formValues
.Append(",\\\"").Append(Lorem.GetWord().ToLower()).Append("\\\":\\\"")
.Append(AnimatorRandom.Rand.NextDouble()).Append("\\\"");
}
formValues.Append('}');

var postPayload = await File.ReadAllTextAsync("config/socializer_post.json", _cancellationToken);
postPayload = postPayload.Replace("{id}", Guid.NewGuid().ToString());
postPayload = postPayload.Replace("{user}", agent.NpcProfile.Email);
postPayload = postPayload.Replace("{payload}", formValues.ToString());
postPayload = postPayload.Replace("{url}", _configuration.AnimatorSettings.Animations.SocialSharing.PostUrl);
postPayload = postPayload.Replace("{now}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
Uri = _configuration.AnimatorSettings.Animations.SocialSharing.PostUrl,
Category = "social",
Method = "POST",
Headers = new Dictionary<string, string>
{
{ "u", agent.NpcProfile.Email }
},
FormValues = new Dictionary<string, string>
{
{ userFormValue, agent.NpcProfile.Email },
{ messageFormValue, tweetText }
}
};

var t = new Timeline();
t.Id = Guid.NewGuid();
t.Status = Timeline.TimelineStatus.Run;
var th = new TimelineHandler();
th.HandlerType = HandlerType.BrowserFirefox;
th.Initial = "about:blank";
th.UtcTimeOn = new TimeSpan(0, 0, 0);
th.UtcTimeOff = new TimeSpan(23, 59, 59);
th.HandlerArgs = new Dictionary<string, object>();
th.HandlerArgs.Add("isheadless", "false");
th.Loop = false;
var te = new TimelineEvent();
te.Command = "browse";
te.CommandArgs = new List<object>();
te.CommandArgs.Add(JsonConvert.SerializeObject(payload));
te.DelayAfter = 0;
te.DelayBefore = 0;
th.TimeLineEvents.Add(te);
t.TimeLineHandlers.Add(th);

var machineUpdate = new MachineUpdate();
if (agent.MachineId.HasValue)
{
postPayload = postPayload.Replace("{machineId}", agent.MachineId.Value.ToString());
machineUpdate.MachineId = agent.MachineId.Value;
}
else
{
postPayload = postPayload.Replace("{machineId}", Guid.Empty.ToString());
}

machineUpdate.Update = postPayload;
machineUpdate.Update = JsonConvert.SerializeObject(t);
machineUpdate.Username = agent.NpcProfile.Email;
machineUpdate.Status = StatusType.Active;
machineUpdate.Type = UpdateClientConfig.UpdateType.TimelinePartial;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public async Task<string> GenerateTweet(NpcRecord agent)

tweetText = tweetText.ReplaceDoubleQuotesWithSingleQuotes(); // else breaks csv file, //TODO should replace this with a proper csv library

tweetText = Clean(tweetText);

_log.Info($"{agent.NpcProfile.Name} said: {tweetText}");
}
catch (Exception e)
Expand All @@ -102,4 +104,12 @@ public async Task<string> GenerateTweet(NpcRecord agent)
}
return tweetText;
}

private string Clean(string raw)
{
raw = raw.Replace("`", "");
raw = raw.Replace("\"", "");
raw = raw.Replace("'", "");
return raw;
}
}
7 changes: 7 additions & 0 deletions src/Ghosts.Api/Infrastructure/Models/MachineUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ public class MachineUpdate
public StatusType Status { get; set; }

public string Update { get; set; }

public MachineUpdate()
{
var now = DateTime.UtcNow;
this.ActiveUtc = now;
this.CreatedUtc = now;
}
}
}
15 changes: 10 additions & 5 deletions src/Ghosts.Api/Infrastructure/Services/MachineUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,25 @@ public async Task UpdateGroupAsync(int groupId, MachineUpdateViewModel machineUp

public async Task<MachineUpdate> GetAsync(Guid machineId, string currentUsername, CancellationToken ct)
{
if (machineId == Guid.Empty && string.IsNullOrEmpty(currentUsername))
return new MachineUpdate();

// Build the base query with conditions that are always true
var query = _context.MachineUpdates
.Where(m => m.ActiveUtc < DateTime.UtcNow && m.Status == StatusType.Active);
.Where(m => m.ActiveUtc <= DateTime.UtcNow && m.Status == StatusType.Active);

// Adjust the query based on the provided arguments
if (machineId != Guid.Empty)
{
query = query.Where(m => m.MachineId == machineId);
}

if (!string.IsNullOrEmpty(currentUsername))
else
{
var usernameLower = currentUsername.ToLower();
query = query.Where(m => m.Username.ToLower().StartsWith(usernameLower));
if (!string.IsNullOrEmpty(currentUsername))
{
var usernameLower = currentUsername.ToLower();
query = query.Where(m => m.Username.ToLower().StartsWith(usernameLower));
}
}

var update = await query.FirstOrDefaultAsync(ct);
Expand Down
10 changes: 0 additions & 10 deletions src/Ghosts.Api/config/socializer_post.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Ghosts.Client.Comms.ClientSocket;

public class Connection
{
private int _attempts = 0;
private HubConnection _connection;
private readonly CancellationToken _ct = new();
public readonly BackgroundTaskQueue Queue = new();
Expand All @@ -31,6 +32,7 @@ public async Task Run()
while (_connection == null)
{
await EstablishConnection(url);
_attempts++;
}

Console.WriteLine($"Connected to {url}");
Expand Down Expand Up @@ -134,7 +136,8 @@ async Task EstablishConnection(string url)
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred at {url} while connecting: {ex.Message}");
if(_attempts > 1)
Console.WriteLine($"An error occurred at {url} while connecting: {ex.Message}");
}
}

Expand Down
22 changes: 8 additions & 14 deletions src/Ghosts.Client/Ghosts.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -440,23 +440,17 @@
<PackageReference Include="Microsoft.Win32.Primitives">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="NetOffice.Excel">
<Version>1.7.4.11</Version>
<PackageReference Include="NetOfficeFw.Core">
<Version>1.9.5</Version>
</PackageReference>
<PackageReference Include="NetOffice.Excel.Net45">
<Version>1.7.4.11</Version>
<PackageReference Include="NetOfficeFw.Excel">
<Version>1.9.5</Version>
</PackageReference>
<PackageReference Include="NetOffice.PowerPoint">
<Version>1.7.4.11</Version>
<PackageReference Include="NetOfficeFw.PowerPoint">
<Version>1.9.5</Version>
</PackageReference>
<PackageReference Include="NetOffice.PowerPoint.Net45">
<Version>1.7.4.11</Version>
</PackageReference>
<PackageReference Include="NetOffice.Word">
<Version>1.7.4.11</Version>
</PackageReference>
<PackageReference Include="NetOffice.Word.Net45">
<Version>1.7.4.11</Version>
<PackageReference Include="NetOfficeFw.Word">
<Version>1.9.5</Version>
</PackageReference>
<PackageReference Include="NETStandard.Library">
<Version>2.0.3</Version>
Expand Down
2 changes: 1 addition & 1 deletion src/Ghosts.Client/Handlers/BaseBrowserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public string MakeRequest(RequestConfiguration config)
script += "xhr.onload = function() {";
script += "document.write(this.responseText);";
script += "};";
script += $"xhr.send('{config.FormValues.ToFormValueString()}');";
script += $"xhr.send('{config.FormValues.ToFormValueString().Replace("'", "").Replace("\"", "")}');";

var javaScriptExecutor = (IJavaScriptExecutor)Driver;
javaScriptExecutor.ExecuteScript(script);
Expand Down
9 changes: 6 additions & 3 deletions src/Ghosts.Client/Handlers/Excel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ private void ExecuteEvents(Timeline timeline, TimelineHandler handler)
{
document = excelApplication.Workbooks.Add();
Log.Trace($"{handler.HandlerType} adding new...");

}

Log.Trace("Excel adding worksheet");

for(var i = 0; i < _random.Next(1,8); i++)
document.Worksheets.Add(Type.Missing, document.Worksheets[document.Worksheets.Count]);

var workSheet = (Excel.Worksheet)document.Worksheets[1];

for (var i = 2; i < 10; i++)
Expand Down Expand Up @@ -264,7 +267,7 @@ private void ExecuteEvents(Timeline timeline, TimelineHandler handler)
{
//sleep and leave the app open
Log.Trace($"Sleep after for {timelineEvent.DelayAfterActual}");
Thread.Sleep(timelineEvent.DelayAfterActual - writeSleep);
Thread.Sleep(timelineEvent.DelayAfterActual.GetSafeSleepTime(writeSleep));
}

workSheet.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion src/Ghosts.Client/Handlers/PowerPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private void ExecuteEvents(Timeline timeline, TimelineHandler handler)
{
//sleep and leave the app open
Log.Trace($"Sleep after for {timelineEvent.DelayAfterActual}");
Thread.Sleep(timelineEvent.DelayAfterActual - writeSleep);
Thread.Sleep(timelineEvent.DelayAfterActual.GetSafeSleepTime(writeSleep));
}

document.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion src/Ghosts.Client/Handlers/Word.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private void ExecuteEvents(Timeline timeline, TimelineHandler handler)
{
//sleep and leave the app open
Log.Trace($"Sleep after for {timelineEvent.DelayAfterActual}");
Thread.Sleep(timelineEvent.DelayAfterActual - writeSleep);
Thread.Sleep(timelineEvent.DelayAfterActual.GetSafeSleepTime(writeSleep));
}

document.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion src/Ghosts.Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("8.0.0.0")]
[assembly: AssemblyInformationalVersion("8.0.0.0")]
[assembly: AssemblyFileVersion("8.0.8.0")]
[assembly: AssemblyFileVersion("8.0.9.9")]
20 changes: 7 additions & 13 deletions src/Ghosts.Client/TimelineManager/Orchestrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
using System.Security.Permissions;
using Ghosts.Domain.Code;
using Ghosts.Domain.Models;
using Microsoft.VisualBasic.Logging;
using Quartz;
// ReSharper disable RedundantAssignment

namespace Ghosts.Client.TimelineManager
Expand Down Expand Up @@ -74,11 +72,9 @@ public void Run()
}
if (_stopfileWatcher == null && dirName != null)
{

_log.Trace("Stopfile watcher is starting");
_stopfileWatcher = new FileSystemWatcher(dirName);
var stopFile = "stop.txt";
_stopfileWatcher.Filter = stopFile;
_stopfileWatcher.Filter = "stop.txt";
_stopfileWatcher.EnableRaisingEvents = true;
_stopfileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes;
_stopfileWatcher.Changed += StopFileChanged;
Expand Down Expand Up @@ -244,17 +240,15 @@ private static void SafetyNet(object defaultTimeline)
{
try
{
using (var proc = Process.GetCurrentProcess())
{
Console.WriteLine($"Minimizing footprint and memory. Current is {proc.PrivateMemorySize64 / (1024 * 1024)}...");
Program.MinimizeFootprint();
Program.MinimizeMemory();
Console.WriteLine($"Minimized footprint and memory. Current is {proc.PrivateMemorySize64 / (1024 * 1024)}...");
}
using var proc = Process.GetCurrentProcess();
var was = proc.PrivateMemorySize64 / (1024 * 1024);
Program.MinimizeFootprint();
Program.MinimizeMemory();
_log.Trace($"Minimized footprint and memory. Was: {was}. Current: {proc.PrivateMemorySize64 / (1024 * 1024)}");
}
catch (Exception e)
{
Console.WriteLine(e);
_log.Trace(e);
}


Expand Down
17 changes: 1 addition & 16 deletions src/Ghosts.Client/config/timeline.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Ghosts.Client/nlog.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
internalLogLevel="Error"
>
<targets>
<target name="logfile" xsi:type="File" fileName="logs/app.log" layout="${Date:universalTime=true}|${callsite}|${message}" archiveAboveSize ="1000000" maxArchiveFiles="2" />
<target name="logfile" xsi:type="File" fileName="logs/app.log" layout="${Date:universalTime=true}|${level}|${callsite}|${message}" archiveAboveSize ="1000000" maxArchiveFiles="2" />
<target name="clientupdates" xsi:type="File" fileName="logs/clientupdates.log" layout="${message}" archiveAboveSize ="500000" maxArchiveFiles="20" />
</targets>
<rules>
Expand Down
6 changes: 5 additions & 1 deletion src/Ghosts.Domain/Code/Jitter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2017 Carnegie Mellon University. All Rights Reserved. See LICENSE.md file for terms.

using System;
using System.Runtime.InteropServices;

namespace Ghosts.Domain.Code
{
Expand All @@ -13,6 +12,11 @@ public static class Jitter
{
private static readonly Random _random = new Random();

public static int GetSafeSleepTime(this int x, int y)
{
return x - y < 0 ? 0 : x - y;
}

public static int Randomize(object baseSleepValue, object lowJitter, object highJitter)
{
var newSleepValue = Convert.ToInt32(baseSleepValue);
Expand Down
Loading

0 comments on commit 02f569f

Please sign in to comment.