Skip to content

Commit

Permalink
Merge pull request #287 from cmu-sei/v8
Browse files Browse the repository at this point in the history
V8
  • Loading branch information
sei-dupdyke authored Mar 14, 2024
2 parents 0c038f2 + 55c5a1a commit ef1594b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 75 deletions.
15 changes: 12 additions & 3 deletions src/Ghosts.Client/Ghosts.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,22 @@
<PackageReference Include="Microsoft.Win32.Primitives">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="NetOfficeFw.Excel">
<PackageReference Include="NetOffice.Excel">
<Version>1.7.4.11</Version>
</PackageReference>
<PackageReference Include="NetOfficeFw.PowerPoint">
<PackageReference Include="NetOffice.Excel.Net45">
<Version>1.7.4.11</Version>
</PackageReference>
<PackageReference Include="NetOfficeFw.Word">
<PackageReference Include="NetOffice.PowerPoint">
<Version>1.7.4.11</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>
<PackageReference Include="NETStandard.Library">
Expand Down
69 changes: 33 additions & 36 deletions src/Ghosts.Client/Handlers/Outlook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Exception = System.Exception;
using MAPIFolder = Microsoft.Office.Interop.Outlook.MAPIFolder;
using ReportItem = Ghosts.Domain.Code.ReportItem;
using Newtonsoft.Json;

namespace Ghosts.Client.Handlers;

Expand All @@ -38,8 +39,12 @@ public Outlook(TimelineHandler handler)
var currentDir = new FileInfo(GetType().Assembly.Location).Directory;
RedemptionLoader.DllLocation64Bit = Path.GetFullPath(currentDir + @"\lib\redemption64.dll");
RedemptionLoader.DllLocation32Bit = Path.GetFullPath(currentDir + @"\lib\redemption.dll");

Log.Trace("Redemption64 loaded from " + Path.GetFullPath(currentDir + @"\lib\redemption64.dll"));
Log.Trace("Redemption loaded from " + Path.GetFullPath(currentDir + @"\lib\redemption.dll"));

//Create a Redemption object and use it
Log.Trace("Creating new RDO session");
Log.Trace("Creating new RDO session...");
var session = RedemptionLoader.new_RDOSession();
Log.Trace("Attempting RDO session logon...");
session.Logon(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Expand All @@ -55,7 +60,7 @@ public Outlook(TimelineHandler handler)
_oMapiNamespace = _app.GetNamespace("MAPI");
_folderInbox = _oMapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
_folderOutbox = _oMapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderOutbox);
Log.Trace("Launching Outlook");
Log.Trace("Launching Outlook...");
_folderInbox.Display();

if (handler.Loop)
Expand Down Expand Up @@ -354,8 +359,8 @@ private bool ReplyViaOutlook(EmailConfiguration emailConfig)

private bool SendEmailViaOutlook(EmailConfiguration emailConfig)
{
ClientConfiguration.EmailSettings config = Program.Configuration.Email;
bool wasSuccessful = false;
var config = Program.Configuration.Email;
var wasSuccessful = false;

try
{
Expand Down Expand Up @@ -394,7 +399,7 @@ private bool SendEmailViaOutlook(EmailConfiguration emailConfig)
if (emailConfig.Attachments.Count > 0)
{
//Add attachments
foreach (string path in emailConfig.Attachments)
foreach (var path in emailConfig.Attachments)
{
mailItem.Attachments.Add(path);
Log.Trace($"Adding attachment from: {path}");
Expand Down Expand Up @@ -437,25 +442,25 @@ private bool SendEmailViaOutlook(EmailConfiguration emailConfig)
mailItem.SendUsingAccount = acc;
}
}

if (config.SaveToOutbox)
{
Log.Trace("Saving mailItem to outbox...");
mailItem.Move(_folderOutbox);
mailItem.Save();
}

Log.Trace("Attempting new Redemtion SafeMailItem...");
var rdoMail = new SafeMailItem
{
Item = mailItem
};

//Parse To
if (emailConfig.To.Count > 0)
Log.Trace($"Email configuration from timeline is currently: {JsonConvert.SerializeObject(emailConfig)}...");
Log.Trace($"Attempting add of To addresses...");
if (emailConfig.To.Any())
{
var list = emailConfig.To.Distinct();
foreach (var a in list)
foreach (var a in emailConfig.To)
{
var r = rdoMail.Recipients.AddEx(a.Trim());
r.Resolve();
Expand All @@ -467,41 +472,33 @@ private bool SendEmailViaOutlook(EmailConfiguration emailConfig)
throw new Exception("Must specify to-address");
}

//Parse Cc
if (emailConfig.Cc.Count > 0)
foreach (var a in emailConfig.Cc)
{
var list = emailConfig.Cc.Distinct();
foreach (var a in list)
var r = rdoMail.Recipients.AddEx(a.Trim());
r.Resolve();
if (r.Resolved)
{
var r = rdoMail.Recipients.AddEx(a.Trim());
r.Resolve();
if (r.Resolved)
{
r.Type = 2; //CC
}

Log.Trace($"RdoMail CC {a.Trim()}");
r.Type = 2; //CC
}
}

if (emailConfig.Bcc.Count > 0)
Log.Trace($"RdoMail CC {a.Trim()}");
}

foreach (var a in emailConfig.Bcc)
{
var list = emailConfig.Bcc.Distinct();
foreach (var a in list)
var r = rdoMail.Recipients.AddEx(a.Trim());
r.Resolve();
if (r.Resolved)
{
var r = rdoMail.Recipients.AddEx(a.Trim());
r.Resolve();
if (r.Resolved)
{
r.Type = 3; //BCC
}

Log.Trace($"RdoMail BCC {a.Trim()}");
r.Type = 3; //BCC
}

Log.Trace($"RdoMail BCC {a.Trim()}");
}


rdoMail.Recipients.ResolveAll();

Log.Trace("Attempting to send Redemtion SafeMailItem...");
rdoMail.Send();

Expand Down
65 changes: 31 additions & 34 deletions src/Ghosts.Client/Infrastructure/Email/EmailConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
using System.DirectoryServices.AccountManagement;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using Ghosts.Domain.Code;
using Ghosts.Domain.Code.Helpers;
using Newtonsoft.Json;
using NLog;
// ReSharper disable InconsistentNaming

namespace Ghosts.Client.Infrastructure.Email;

Expand Down Expand Up @@ -56,6 +54,8 @@ public enum EmailBodyType

public EmailConfiguration(IList<object> args)
{
_log.Trace($"Building email configuration from timeline {JsonConvert.SerializeObject(args)}...");

var settings = Program.Configuration.Email;
var emailConfigArray = args;
if (emailConfigArray.Count != 8)
Expand All @@ -71,7 +71,7 @@ public EmailConfiguration(IList<object> args)
this.Attachments = new List<string>();

this.From = emailConfigArray[0].ToString();

// just use the first account we find already registered in outlook
//if (this.From.Equals("CurrentUser", StringComparison.CurrentCultureIgnoreCase))
//{
Expand All @@ -81,7 +81,7 @@ public EmailConfiguration(IList<object> args)
this.To = ParseEmail(emailConfigArray[1].ToString(), settings.RecipientsToMin, settings.RecipientsToMax);
this.Cc = ParseEmail(emailConfigArray[2].ToString(), settings.RecipientsCcMin, settings.RecipientsCcMax);
this.Bcc = ParseEmail(emailConfigArray[3].ToString(), settings.RecipientsBccMin, settings.RecipientsBccMax);

var emailContent = new EmailContentManager();

this.Subject = emailConfigArray[4].ToString();
Expand Down Expand Up @@ -122,6 +122,8 @@ public EmailConfiguration(IList<object> args)
_log.Debug($"Can't add attachment {o} - file was not found");
}
}

_log.Trace($"Built email configuration from timeline {JsonConvert.SerializeObject(this)}...");
}

private string GetFooter()
Expand Down Expand Up @@ -149,7 +151,7 @@ private string GetFooter()
{
_log.Trace($"Can't get current userprinciple for the email footer, skipping... {e}");
}

var f = File.ReadAllText(ApplicationDetails.ConfigurationFiles.EmailsFooter);
f = f.Replace("{{from}}", this.From);
f = f.Replace("{{now}}", DateTime.Now.ToLongDateString());
Expand All @@ -169,49 +171,43 @@ public override string ToString()

private static List<string> ParseEmail(string raw, int min, int max)
{
_log.Trace($"Parsing email - raw {raw} min {min} max {max}");
var list = new List<string>();
if (string.IsNullOrEmpty(raw)) return list;
if (string.IsNullOrEmpty(raw)) raw = "";

var rnd = new Random();
var numberOfRecipients = rnd.Next(min, max);
var numberOfRecipients = rnd.Next(min, max + 1);

if (numberOfRecipients < 1)
{
_log.Trace("numberOfRecipients was less than 1, exiting...");
return list;
}

if (raw.StartsWith("random", StringComparison.InvariantCultureIgnoreCase))
{
var o = raw.Split(Convert.ToChar(":"));

if (o.GetUpperBound(0) > 0) //supplied list
{
var l = o[1];
var emails = l.Split(Convert.ToChar(","));

for (var i = 0; i < numberOfRecipients; i++)
list.Add(emails.PickRandom());
}
else //build list
{
//add domain
var emails = EmailListManager.GetDomainList();

for (var i = 0; i < numberOfRecipients; i++)
list.Add(emails.PickRandom());

//add outside
var x = rnd.Next(Program.Configuration.Email.RecipientsOutsideMin, Program.Configuration.Email.RecipientsOutsideMax + 1);
if (x < 1) return list;

var outsideEmails = EmailListManager.GetOutsideList();
for (var i = 0; i < x; i++)
list.Add(outsideEmails.PickRandom());
}
//add domain
var emails = EmailListManager.GetDomainList();
_log.Trace($"Building domain email list: {emails.Count}...");

for (var i = 0; i <= numberOfRecipients; i++)
list.Add(emails.PickRandom());

//add outside
var numberOfOutsideRecipients = rnd.Next(Program.Configuration.Email.RecipientsOutsideMin, Program.Configuration.Email.RecipientsOutsideMax + 1);
var outsideEmails = EmailListManager.GetOutsideList();
_log.Trace($"Building outside email list: {outsideEmails.Count}...");
for (var i = 0; i <= numberOfOutsideRecipients; i++)
list.Add(outsideEmails.PickRandom());
}
else
{
var a = raw.Split(Convert.ToChar(","));
_log.Trace($"Building non-random list: {a.Length}...");
list.AddRange(a.Where(IsValidEmail));
}

_log.Trace($"List generated: {list.Count}...");
return list;
}

Expand All @@ -224,6 +220,7 @@ private static bool IsValidEmail(string email)
}
catch
{
_log.Trace($"Invalid email address: {email}...");
return false;
}
}
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.7.0")]
[assembly: AssemblyFileVersion("8.0.7.45")]
2 changes: 1 addition & 1 deletion src/Ghosts.Client/config/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"SetAccountFromConfig": false,
"SetAccountFromLocal": false,
"SetForcedSendReceive": true,
"SaveToOutbox": false,
"SaveToOutbox": true,
"EmailDomainSearchString": "Get-ADUser -filter * -searchbase \"CN=USERS,DC=JRSS,DC=GOV\" -properties UserPrincipalName | select -expand UserPrincipalName"
},
"Listener": {
Expand Down

0 comments on commit ef1594b

Please sign in to comment.