-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sync.cs
598 lines (494 loc) · 23.6 KB
/
Sync.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using ASTSync.BatchTableHelper;
using Azure.Data.Tables;
using Azure.Identity;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Graph.Beta;
using Microsoft.Graph.Beta.Models;
using Microsoft.Graph.Beta.Models.ODataErrors;
namespace ASTSync;
public static class Sync
{
// If to pull entra users
private static bool _pullEntraUsers =
bool.Parse(Environment.GetEnvironmentVariable("SyncEntra", EnvironmentVariableTarget.Process) ?? "true");
/// <summary>
/// How many table rows to send up in a batch
/// </summary>
private static int _maxTableBatchSize = 100;
/// <summary>
/// How frequent to sync users (default 7 days)
/// </summary>
private static TimeSpan _ageUserSync = TimeSpan.FromDays(7);
/// <summary>
/// Maintains a list of users we have already synced
/// </summary>
private static ConcurrentDictionary<string, bool> userListSynced = new();
/// <summary>
/// Logger
/// </summary>
private static ILogger _log { get; set; }
/// <summary>
/// Batch table processor for Entra Users
/// </summary>
private static BatchTable _batchUsers { get; set; }
/// <summary>
/// Batch table processor for Simulations
/// </summary>
private static BatchTable _batchSimulations { get; set; }
/// <summary>
/// Batch table processor for Simulation Users
/// </summary>
private static BatchTable _batchSimulationUsers { get; set; }
/// <summary>
/// Batch table processor for Simulation User Events
/// </summary>
private static BatchTable _batchSimulationUserEvents { get; set; }
/// <summary>
/// Batch table processor for Trainings
/// </summary>
private static BatchTable _batchTrainings { get; set; }
/// <summary>
/// Batch table processor for Payloads
/// </summary>
private static BatchTable _batchPayloads { get; set; }
[FunctionName("Sync")]
public static async Task RunAsync([TimerTrigger("0 0 * * * *")] TimerInfo myTimer, ILogger log)
{
Stopwatch sw = Stopwatch.StartNew();
_log = log;
// Get graph client
var GraphClient = GetGraphServicesClient();
_log.LogInformation($"AST Sync started at: {DateTime.UtcNow}");
// Spin up the batch queue processors
_batchUsers = new BatchTable(GetStorageConnection(), "Users", _maxTableBatchSize, log);
_batchSimulations = new BatchTable(GetStorageConnection(), "Simulations", _maxTableBatchSize, log);
_batchSimulationUsers = new BatchTable(GetStorageConnection(), "SimulationUsers", _maxTableBatchSize, log);
_batchSimulationUserEvents = new BatchTable(GetStorageConnection(), "SimulationUserEvents", _maxTableBatchSize, log);
_batchTrainings = new BatchTable(GetStorageConnection(), "Trainings", _maxTableBatchSize, log);
_batchPayloads = new BatchTable(GetStorageConnection(), "Payloads", _maxTableBatchSize, log);
// Sync Tenant Simulations to perform sync whilst sync'ing simulations to table
// This can probably be moved to an async foreach below.
// However, need to figure out how to do the async foreach return in a lambda (graph services client paging func.)
HashSet<string> simulationIds;
try
{
simulationIds = await GetTenantSimulations(GraphClient);
}
catch (Exception e)
{
_log.LogError($"Failed to get simulations: {e}");
throw;
}
// Sync Tenant Simulation Users
foreach (string id in simulationIds)
{
try
{
await GetTenantSimulationUsers(GraphClient, id);
}
catch (Exception e)
{
_log.LogError($"Failed to get simulation users for simulation {id}: {e}");
}
}
// Remaining syncs
try
{
await GetTrainings(GraphClient);
}
catch (Exception e)
{
_log.LogError($"Failed to get trainings: {e}");
}
// Get tenant payloads
try
{
await GetPayloads(GraphClient, SourceType.Tenant);
}
catch (Exception e)
{
_log.LogError($"Failed to get tenant payloads: {e}");
}
// Get global payloads
try
{
await GetPayloads(GraphClient, SourceType.Global);
}
catch (Exception e)
{
_log.LogError($"Failed to get global payloads: {e}");
}
// Dispose of all batch processors
await _batchUsers.DisposeAsync();
await _batchSimulations.DisposeAsync();
await _batchSimulationUsers.DisposeAsync();
await _batchSimulationUserEvents.DisposeAsync();
await _batchTrainings.DisposeAsync();
await _batchPayloads.DisposeAsync();
_log.LogInformation($"AST sync completed synchronising {simulationIds.Count} simulations in {sw.Elapsed}");
}
/// <summary>
/// Get Simulations for Tenant
/// </summary>
/// <param name="GraphClient"></param>
private static async Task<HashSet<string>> GetTenantSimulations(GraphServiceClient GraphClient)
{
// Simulation Ids
HashSet<string> SimulationIds = new HashSet<string>();
// Get table client for table
TableClient tableClient = new TableClient(GetStorageConnection(), "Simulations");
// Get simulation results
var results = await GraphClient
.Security
.AttackSimulation
.Simulations
.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Top = 1000;
});
var pageIterator = Microsoft.Graph.PageIterator<Simulation,SimulationCollectionResponse>
.CreatePageIterator(GraphClient, results, async (sim) =>
{
// Get the table row item for this simulation
var SimulationExistingTableItem = await tableClient.GetEntityIfExistsAsync<TableEntity>("Simulations", sim.Id);
// For determining the last user sync
DateTime LastUserSync = DateTime.SpecifyKind(new DateTime(1986,1,1), DateTimeKind.Utc);
if (SimulationExistingTableItem.HasValue && SimulationExistingTableItem.Value.ContainsKey("LastUserSync"))
LastUserSync = DateTime.SpecifyKind(DateTime.Parse(SimulationExistingTableItem.Value["LastUserSync"].ToString()), DateTimeKind.Utc);
// Perform a user sync (if)
// - We have never performed a sync
// - The simulation finished within the past 7 days
// - Or the simulation is running
// - Last user sync is more than a month ago
if (!SimulationExistingTableItem.HasValue ||
sim.Status == SimulationStatus.Running ||
sim.CompletionDateTime > DateTime.UtcNow.AddDays(-7) ||
(LastUserSync < DateTime.UtcNow.AddMonths(-1)))
{
_log.LogInformation($"Perform full synchronisation of simulation '{sim.DisplayName}' status {SimulationStatus.Running}");
SimulationIds.Add(sim.Id);
}
// Add the table item
_batchSimulations.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpdateReplace, new TableEntity("Simulations", sim.Id)
{
{"AttackTechnique", sim.AttackTechnique.ToString()},
{"AttackType", sim.AttackType.ToString()},
{"AutomationId", sim.AutomationId},
{"CompletionDateTime", sim.CompletionDateTime},
{"CreatedBy_Id", sim.CreatedBy?.Id},
{"CreatedBy_DisplayName", sim.CreatedBy?.DisplayName},
{"CreatedBy_Email", sim.CreatedBy?.Email},
{"CreatedDateTime", sim.CreatedDateTime},
{"Description",sim.Description},
{"DisplayName",sim.DisplayName},
{"DurationInDays", sim.DurationInDays},
{"IsAutomated", sim.IsAutomated},
{"LastModifiedBy_Id", sim.LastModifiedBy?.Id},
{"LastModifiedBy_DisplayName", sim.LastModifiedBy?.DisplayName},
{"LastModifiedBy_Email", sim.LastModifiedBy?.Email},
{"LastModifiedDateTime", sim.LastModifiedDateTime},
{"Payload_Id", sim.Payload?.Id},
{"Payload_DisplayName", sim.Payload?.DisplayName},
{"Payload_Platform", sim.Payload?.Platform?.ToString()},
{"Status", sim.Status.ToString()},
{"AutomationId", sim.AutomationId},
{"LastUserSync", LastUserSync}
}));
return true;
});
await pageIterator.IterateAsync();
// Flush batch simulations
await _batchSimulations.FlushBatchAsync(TimeSpan.FromMinutes(5));
return SimulationIds;
}
/// <summary>
/// Get Trainings
/// </summary>
/// <param name="GraphClient"></param>
private static async Task<bool> GetTrainings(GraphServiceClient GraphClient)
{
Stopwatch sw = Stopwatch.StartNew();
_log.LogInformation("Synchronising trainings");
// Get simulation results
var results = await GraphClient
.Security
.AttackSimulation
.Trainings
.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Top = 1000;
});
var pageIterator = Microsoft.Graph.PageIterator<Training,TrainingCollectionResponse>
.CreatePageIterator(GraphClient, results, async (training) =>
{
// Add the table item
_batchTrainings.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpdateReplace, new TableEntity("Trainings", training.Id)
{
{"TrainingId", training.Id},
{"DisplayName", training.DisplayName},
{"Description", training.Description},
{"DurationInMinutes", training.DurationInMinutes},
{"Source", training.Source.ToString()},
{"Type", training.Type?.ToString()},
{"availabilityStatus", training.AvailabilityStatus?.ToString()},
{"HasEvaluation", training.HasEvaluation},
{"CreatedBy_Id", training.CreatedBy?.Id},
{"CreatedBy_DisplayName", training.CreatedBy?.DisplayName},
{"CreatedBy_Email", training.CreatedBy?.Email},
{"LastModifiedBy_Id", training.LastModifiedBy?.Id},
{"LastModifiedBy_DisplayName", training.LastModifiedBy?.DisplayName},
{"LastModifiedBy_Email", training.LastModifiedBy?.Email},
{"LastModifiedDateTime", training.LastModifiedDateTime},
}));
return true;
});
await pageIterator.IterateAsync();
// Flush remaining trainings
await _batchTrainings.FlushBatchAsync(TimeSpan.FromMinutes(5));
_log.LogInformation($"Synchronising trainings complete in {sw.Elapsed}");
return true;
}
/// <summary>
/// Get Payloads
/// </summary>
/// <param name="GraphClient"></param>
private static async Task<bool> GetPayloads(GraphServiceClient GraphClient, SourceType Source)
{
Stopwatch sw = Stopwatch.StartNew();
_log.LogInformation("Synchronising payloads");
string? filter = null;
if (Source == SourceType.Global)
filter = "source eq 'global'";
if (Source == SourceType.Tenant)
filter = "source eq 'tenant'";
// Get simulation results
var results = await GraphClient
.Security
.AttackSimulation
.Payloads
.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Top = 1000;
requestConfiguration.QueryParameters.Filter = filter;
});
var pageIterator = Microsoft.Graph.PageIterator<Payload,PayloadCollectionResponse>
.CreatePageIterator(GraphClient, results, async (payload) =>
{
// Add the table item
_batchPayloads.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpdateReplace, new TableEntity("Payloads", payload.Id)
{
{"PayloadId", payload.Id},
{"DisplayName", payload.DisplayName},
{"Description", payload.Description},
{"SimulationAttackType", payload.SimulationAttackType?.ToString()},
{"Platform", payload.Platform?.ToString()},
{"Status", payload.Status?.ToString()},
{"Source", payload.Source?.ToString()},
{"PredictedCompromiseRate", payload.PredictedCompromiseRate},
{"Complexity", payload.Complexity?.ToString()},
{"Technique", payload.Technique?.ToString()},
{"Theme", payload.Theme?.ToString()},
{"Brand", payload.Brand?.ToString()},
{"Industry", payload.Industry?.ToString()},
{"IsCurrentEvent", payload.IsCurrentEvent},
{"IsControversial", payload.IsControversial},
{"CreatedBy_Id", payload.CreatedBy?.Id},
{"CreatedBy_DisplayName", payload.CreatedBy?.DisplayName},
{"CreatedBy_Email", payload.CreatedBy?.Email},
{"LastModifiedBy_Id", payload.LastModifiedBy?.Id},
{"LastModifiedBy_DisplayName", payload.LastModifiedBy?.DisplayName},
{"LastModifiedBy_Email", payload.LastModifiedBy?.Email},
{"LastModifiedDateTime", payload.LastModifiedDateTime},
}));
return true;
});
await pageIterator.IterateAsync();
// Flush remaining payloads
await _batchPayloads.FlushBatchAsync(TimeSpan.FromMinutes(5));
_log.LogInformation($"Synchronising payloads complete in {sw.Elapsed}");
return true;
}
/// <summary>
/// Get Simulations Users
/// </summary>
/// <param name="GraphClient"></param>
private static async Task GetTenantSimulationUsers(GraphServiceClient GraphClient, string SimulationId)
{
Stopwatch sw = Stopwatch.StartNew();
_log.LogInformation($"Performing full user synchronisation of {SimulationId}");
var requestInformation =
GraphClient.Security.AttackSimulation.Simulations[SimulationId].ToGetRequestInformation();
requestInformation.URI = new Uri(requestInformation.URI.ToString() + "/report/simulationUsers");
requestInformation.QueryParameters["Top"] = 1000;
var results = await GraphClient.RequestAdapter.SendAsync<UserSimulationDetailsCollectionResponse>(requestInformation, UserSimulationDetailsCollectionResponse.CreateFromDiscriminatorValue);
var pageIterator = Microsoft.Graph.PageIterator<UserSimulationDetails,UserSimulationDetailsCollectionResponse>
.CreatePageIterator(GraphClient, results, async (userSimDetail) =>
{
// Create an identifier for the SimulationUser_Id
string id = $"{SimulationId}-{userSimDetail.SimulationUser?.UserId}";
// Add the table item
_batchSimulationUsers.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpdateReplace, new TableEntity(SimulationId, userSimDetail.SimulationUser?.UserId)
{
{"SimulationUser_Id", id},
{"SimulationId", SimulationId},
{"SimulationUser_UserId", userSimDetail.SimulationUser?.UserId},
{"SimulationUser_Email", userSimDetail.SimulationUser?.Email},
{"CompromisedDateTime", userSimDetail.CompromisedDateTime},
{"ReportedPhishDateTime", userSimDetail.ReportedPhishDateTime},
{"AssignedTrainingsCount", userSimDetail.AssignedTrainingsCount},
{"CompletedTrainingsCount", userSimDetail.CompletedTrainingsCount},
{"InProgressTrainingsCount", userSimDetail.InProgressTrainingsCount},
{"IsCompromised", userSimDetail.IsCompromised},
{"HasReported", userSimDetail.ReportedPhishDateTime is not null},
}));
// Determine if should sync user
if (await ShouldSyncUser(userSimDetail.SimulationUser?.UserId))
{
await SyncUser(GraphClient, userSimDetail.SimulationUser?.UserId);
}
// Add simulation user events in to table
if (userSimDetail.SimulationEvents is not null)
{
foreach (var simulationUserEvents in userSimDetail.SimulationEvents)
{
_batchSimulationUserEvents.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpdateReplace, new TableEntity(SimulationId, $"{userSimDetail.SimulationUser?.UserId}_{simulationUserEvents.EventName}_{simulationUserEvents.EventDateTime.Value.ToUnixTimeSeconds()}")
{
{"SimulationUser_Id", id},
{"SimulationUser_UserId", userSimDetail.SimulationUser?.UserId},
{"SimulationUserEvent_EventName", simulationUserEvents.EventName},
{"SimulationUserEvent_EventDateTime", simulationUserEvents.EventDateTime},
{"SimulationUserEvent_Browser", simulationUserEvents.Browser},
{"SimulationUserEvent_IpAddress", simulationUserEvents.IpAddress},
{"SimulationUserEvent_OsPlatformDeviceDetails", simulationUserEvents.OsPlatformDeviceDetails},
}));
}
}
return true;
});
await pageIterator.IterateAsync();
// update in the Simulations table that this has been syncd
_batchSimulations.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpsertMerge, new TableEntity("Simulations", SimulationId)
{
{"LastUserSync", DateTime.UtcNow},
}));
// Flush batch simulations for users and events
await _batchSimulationUsers.FlushBatchAsync(TimeSpan.FromMinutes(5));
await _batchSimulationUserEvents.FlushBatchAsync(TimeSpan.FromMinutes(5));
_log.LogInformation($"Full user synchronisation of {SimulationId} completed in {sw.Elapsed}");
}
/// <summary>
/// Get the Graph Client for Tenant
/// </summary>
/// <returns></returns>
private static GraphServiceClient GetGraphServicesClient()
{
// Use default azure credential
var tokenCredential = new DefaultAzureCredential();
// Default graph scope
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Return graph services client
return new GraphServiceClient(tokenCredential, scopes);
}
/// <summary>
/// Get Storage Connection from App settings
/// </summary>
/// <returns></returns>
private static string GetStorageConnection() => Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process);
/// <summary>
/// Synchronise user
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private static async Task SyncUser(GraphServiceClient GraphClient, string id)
{
// Set in dictionary
userListSynced[id] = true;
try
{
var User = await GraphClient.Users[id].GetAsync();
if (User is not null)
{
_batchUsers.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpdateReplace, new TableEntity("Users", id)
{
{"DisplayName", User.DisplayName},
{"GivenName", User.GivenName},
{"Surname", User.Surname},
{"Country", User.Country},
{"Mail", User.Mail},
{"Department", User.Department},
{"CompanyName", User.CompanyName},
{"City", User.City},
{"Country", User.Country},
{"JobTitle", User.JobTitle},
{"LastUserSync", DateTime.UtcNow},
{"Exists", "true"},
}));
}
}
catch (ODataError e)
{
if (e.Error is not null && e.Error.Code == "Request_ResourceNotFound")
{
// User no longer exists, update table entity
_batchUsers.EnqueueUpload(new TableTransactionAction(TableTransactionActionType.UpsertMerge, new TableEntity("Users", id)
{
{"Exists", "false"},
{"LastUserSync", DateTime.UtcNow},
}));
}
else
{
_log.LogError($"Failed to sync user {id}: {e}");
}
}
}
/// <summary>
/// Determine if should sync user
///
/// This prevents continously syncing the user
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private static async Task<bool> ShouldSyncUser(string id)
{
// Return false if set not to sync users
if (!_pullEntraUsers)
return false;
// Return false if already synchronised
if (userListSynced.ContainsKey(id))
return false;
// Get the table entity to determine how long ago the user has been pulled
TableClient tableClient = new TableClient(GetStorageConnection(), "Users");
var UserTableItem = await tableClient.GetEntityIfExistsAsync<TableEntity>("Users", id);
// Get last sync time
DateTime LastUserSync = new DateTime(1986,1,1);
if (UserTableItem.HasValue && UserTableItem.Value.ContainsKey("LastUserSync"))
LastUserSync = DateTime.SpecifyKind(DateTime.Parse(UserTableItem.Value["LastUserSync"].ToString()), DateTimeKind.Utc);
// If no sync or days is older than a week
if (LastUserSync < DateTime.UtcNow.Subtract(_ageUserSync))
return true;
// Add to userSyncList so we don't need to check again
userListSynced[id] = true;
return false;
}
}
/// <summary>
/// Source Type, Global or Tenant - for filters
/// </summary>
public enum SourceType
{
/// <summary>
/// Global (default payloads)
/// </summary>
Global,
/// <summary>
/// Tenant specific
/// </summary>
Tenant
}