Skip to content

Commit bf2a766

Browse files
committed
Fix handling of bad password attempts and improve task cancellation logic so we finish quick on quick scans
1 parent cf6e300 commit bf2a766

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

TheSprayer/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"TheSprayer": {
44
"commandName": "Project",
5-
"commandLineArgs": "-p vagrant -U vagrant -P vagrant -d sevenkingdoms.local -s 192.168.56.10"
5+
"commandLineArgs": "-p Heartsbane -U vagrant -P vagrant -d north.sevenkingdoms.local -s 192.168.56.11"
66
}
77
}
88
}

TheSprayer/Services/ActiveDirectoryService.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,17 @@ public List<ActiveDirectoryUser> GetAllDomainUsers(IEnumerable<string> userSamAc
227227
if(lastBadPwdTime == null)
228228
{
229229
var intEntry = entry.Attributes.GetIfExists<int?>("badPasswordTime");
230-
if (intEntry == 0)
230+
if (intEntry == 0 || intEntry == null)
231231
{
232232
lastBadPwdTime = entry.Attributes.GetIfExists<DateTime?>("createTimeStamp") ?? DateTime.MinValue;
233233
}
234234
}
235235

236+
if (badPwdCount == null)
237+
{
238+
badPwdCount = 0;
239+
}
240+
236241
if (badPwdCount == null || lastBadPwdTime == null)
237242
{
238243
var sam = entry.Attributes.GetIfExists("sAMAccountName");
@@ -369,11 +374,15 @@ public async void SprayPasswords(
369374
var cancellationTokenSource = new CancellationTokenSource();
370375
var saveTask = Task.Run(async () =>
371376
{
372-
while (!cancellationTokenSource.Token.IsCancellationRequested)
377+
try
373378
{
374-
await Task.Delay(TimeSpan.FromSeconds(10));
375-
SaveUnsavedAttempts(unsavedAttempts, noDb);
379+
while (!cancellationTokenSource.Token.IsCancellationRequested)
380+
{
381+
await Task.Delay(TimeSpan.FromSeconds(10), cancellationTokenSource.Token);
382+
SaveUnsavedAttempts(unsavedAttempts, noDb);
383+
}
376384
}
385+
catch (OperationCanceledException) { }
377386
}, cancellationTokenSource.Token);
378387

379388
try
@@ -516,7 +525,7 @@ public async void SprayPasswords(
516525
finally
517526
{
518527
cancellationTokenSource.Cancel();
519-
saveTask.Wait();
528+
try { saveTask.Wait(); } catch (AggregateException) { }
520529
SaveUnsavedAttempts(unsavedAttempts, noDb);
521530
}
522531
}

0 commit comments

Comments
 (0)