-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
814 lines (659 loc) · 27.1 KB
/
main.cpp
File metadata and controls
814 lines (659 loc) · 27.1 KB
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#include <stdio.h>
#include <Windows.h>
#include <tchar.h>
#include <NTSecAPI.h>
#include <ntstatus.h>
#include <fileapi.h>
#include <strsafe.h>
#include <processthreadsapi.h>
#include <sddl.h>
#include <TlHelp32.h>
#include <winreg.h>
#include "main.h"
#pragma comment(lib, "secur32")
#define assert(expression) if (!(expression)) { printf("assert on %d", __LINE__); /*TODO assert when in the service needs to be intelligent */ExitProcess(250); }
/*
* Macro to fail starting the service and actually report this back to the Service Manager
* This is a messy macro, but I don't want to call another function in these failure cases
* and have to manage the scope of &serviceStatus differently.
*/
#define BailOnServiceStart ZeroMemory(&serviceStatus, sizeof(serviceStatus));\
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;\
serviceStatus.dwCurrentState = SERVICE_STOPPED;\
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;\
serviceStatus.dwWin32ExitCode = GetLastError();\
serviceStatus.dwServiceSpecificExitCode = GetLastError();\
serviceStatus.dwCheckPoint = 0;\
serviceStatus.dwWaitHint = 0;\
if (!SetServiceStatus(statusHandle, &serviceStatus)) {\
wprintf(L"SetServiceStatus returned FALSE -- error %d\n", GetLastError());\
return;\
}\
if (INVALID_HANDLE_VALUE != logHandle) {\
CloseHandle(logHandle);\
}\
return;
SERVICE_STATUS serviceStatus = { 0 };
SERVICE_STATUS_HANDLE statusHandle = nullptr;
HANDLE logHandle = INVALID_HANDLE_VALUE;
WCHAR logBuffer[MAX_PATH] = { 0 };
#define LOG_BUFFER_SIZE (MAX_PATH * sizeof(WCHAR))
#define SERVICE_NAME L"ShutdownBuddy"
// Default time to wait between evaluations -- seconds
#define DEFAULT_WAIT_TIMER_INTERVAL_SECONDS 60
// Number of milliseconds in 1 second
#define MS_IN_S 1000
// Number of hundred nanosecond increments in 1ms
#define HUNDREDNS_IN_MS 10000
// Default time to wait before shutdown -- seconds
#define DEFAULT_WAIT_BEFORE_IDLE_SHUTDOWN 3600
//#define DEFAULT_WAIT_BEFORE_IDLE_SHUTDOWN 120
// Registry root key for settings
#define SHUTDOWN_BUDDY_REG_ROOT L"SOFTWARE\\upfold.org.uk\\ShutdownBuddy"
/* Synchronisation and looping
*
* We will have an Event which can be signalled to stop the worker thread
* and also a WaitableTimer for the worker thread so that we do not poll
* user session state too frequently.
*
* We will WaitForMultipleObjects on either of these to be signalled in the
* worker thread. When signalled, we will check to see if the Event was the reason
* for the signal and, if so, stop the worker thread in an orderly fashion.
*/
HANDLE workerWaitableTimer = INVALID_HANDLE_VALUE;
LARGE_INTEGER timerDueTime = { 0 };
HANDLE serviceToStopEvent = INVALID_HANDLE_VALUE;
HANDLE workerThread = INVALID_HANDLE_VALUE;
/// <summary>
/// SIDS of sessions to ignore for the purposes of determining if a real human
/// is signed in.
/// </summary>
WELL_KNOWN_SID_TYPE wellKnownSids[] = {
WinNtAuthoritySid,
WinLocalSystemSid,
WinLocalServiceSid,
WinNetworkServiceSid,
};
// How frequently in seconds we evaluate for sessions.
LONGLONG waitTimerIntervalSeconds = DEFAULT_WAIT_TIMER_INTERVAL_SECONDS;
// How many seconds before we issue a shutdown if all evaluations for interactive sessions were zero.
LONGLONG waitBeforeIdleShutdownSeconds = DEFAULT_WAIT_BEFORE_IDLE_SHUTDOWN;
/// <summary>
/// Should log to a temporary file in %TEMP%
/// </summary>
BOOL shouldDebugLog = FALSE;
/// <summary>
/// Entry point
/// </summary>
/// <param name="argc"></param>
/// <param name="argv"></param>
/// <returns></returns>
int wmain(int argc, WCHAR* argv[]) {
WCHAR serviceName[] = SERVICE_NAME;
SERVICE_TABLE_ENTRY table[] = {
{serviceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL, NULL }
};
if (!(StartServiceCtrlDispatcher(table))) {
wprintf(L"Failed to start service dispatcher: %d", GetLastError());
return GetLastError();
}
return 0;
}
/// <summary>
/// Service entry point
/// </summary>
/// <param name="argc"></param>
/// <param name="argv"></param>
/// <returns></returns>
void WINAPI ServiceMain(DWORD argc, LPTSTR* argv) {
// get settings
LoadSettingsFromRegistry();
// prepare temporary file for logging
WCHAR tempPath[MAX_PATH + 1] = { 0 };
WCHAR tempFileName[MAX_PATH + 1] = { 0 };
logHandle = INVALID_HANDLE_VALUE;
if (shouldDebugLog) {
if (GetTempPath(MAX_PATH, tempPath) == 0) {
wprintf(L"Unable to get temp path name\n");
goto exit;
}
if (GetTempFileName(tempPath, L"SdB", 0, tempFileName) == 0) {// capped to MAX_PATH
wprintf(L"Unable to get temp file name\n");
goto exit;
}
// start logging
logHandle = CreateFile(tempFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (logHandle == INVALID_HANDLE_VALUE) {
wprintf(L"Unable to open temp file for writing: %s (error 0x%x)", tempFileName, GetLastError());
goto exit;
}
}
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Started ShutdownBuddy service\n");
WriteBufferToLog();
statusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
if (statusHandle == NULL) {
wprintf(L"StatusHandle was NULL\n");
return;
}
/*
Service is starting
*/
ZeroMemory(&serviceStatus, sizeof(serviceStatus));
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
if (!SetServiceStatus(statusHandle, &serviceStatus)) {
wprintf(L"SetServiceStatus returned FALSE -- error %d\n", GetLastError());
return;
}
// create synchronisation objects -- timer
workerWaitableTimer = CreateWaitableTimer(NULL, TRUE, NULL);
if (workerWaitableTimer == NULL) {
wprintf(L"Unable to create waitable timer for the worker thread -- error %d\n", GetLastError());
BailOnServiceStart;
}
timerDueTime.QuadPart = -(waitTimerIntervalSeconds * MS_IN_S * HUNDREDNS_IN_MS);
// prepare the stop event
serviceToStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (serviceToStopEvent == NULL) {
wprintf(L"Unable to create the event to notify worker thread on service stop -- error %d", GetLastError());
BailOnServiceStart;
}
// start worker thread
workerThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
if (workerThread == NULL) {
wprintf(L"Failed to create WorkerThread -- error %d", GetLastError());
BailOnServiceStart;
}
// start timer
if (!SetWaitableTimer(workerWaitableTimer, &timerDueTime, 0, NULL, NULL, FALSE)) {
wprintf(L"Failed to set waitiable timer -- error %d", GetLastError());
BailOnServiceStart;
}
// tell the Service Manager we are started
ZeroMemory(&serviceStatus, sizeof(serviceStatus));
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = SERVICE_RUNNING;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
if (!SetServiceStatus(statusHandle, &serviceStatus)) {
wprintf(L"SetServiceStatus returned FALSE -- error %d\n", GetLastError());
return;
}
WaitForSingleObject(workerThread, INFINITE);
exit:
/*
Service has stopped
*/
ZeroMemory(&serviceStatus, sizeof(serviceStatus));
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
if (!SetServiceStatus(statusHandle, &serviceStatus)) {
wprintf(L"SetServiceStatus returned FALSE -- error %d\n", GetLastError());
return;
}
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Service has stopped.");
WriteBufferToLog();
if (INVALID_HANDLE_VALUE != logHandle) {
CloseHandle(logHandle);
}
}
/// <summary>
/// Write the log buffer to the log file.
/// </summary>
/// <param name=""></param>
void WriteBufferToLog(void) {
if (logHandle != INVALID_HANDLE_VALUE) {
if (!(WriteFile(logHandle, logBuffer, (wcslen(logBuffer) + 1) * sizeof(WCHAR), nullptr, nullptr))) {
wprintf(L"Failed to write to logfile Error: %d.\r\n", GetLastError());
}
}
OutputDebugStringW(logBuffer);
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"");
}
/// <summary>
/// Actually do the waiting and sleeping
/// </summary>
/// <param name="lpParam"></param>
/// <returns></returns>
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam) {
NTSTATUS result = -1;
ULONG logonSessionCount = 0;
ULONG originalSessionCount = 0;
PLUID logonSessionListPtr = nullptr;
PSECURITY_LOGON_SESSION_DATA logonSessionData = nullptr;
PLUID nextLogonSessionID = nullptr;
LPWSTR sidString = nullptr;
HANDLE waitableObjects[2] = { serviceToStopEvent, workerWaitableTimer };
DWORD waitResult = MAXDWORD;
// Will hold the path to the HKEY_USERS subkey for a session
WCHAR hkeyUsersSubKey[MAX_PATH + 1] = L"";
HKEY hkeyUsersSubKeyHandle = { 0 };
// how many sessions we consider interactive are running
int interactiveSessionCount = -1;
/* The number of consecutive evaluations for interactive sessions that has returned zero. When this
* reaches a threshold of the shutdown period divided by the interval, we should shut down.
*/
int consecutiveZeroEvaluations = 0;
assert(waitableObjects[0] != INVALID_HANDLE_VALUE);
assert(waitableObjects[0] != NULL);
assert(waitableObjects[1] != INVALID_HANDLE_VALUE);
assert(waitableObjects[1] != NULL);
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Started ShutdownBuddy service worker thread\r\n");
WriteBufferToLog();
//while (!IsDebuggerPresent()) {
// Sleep(1000);
//}
// acquire privileges for shutdown
if (!AdjustTokenPrivilegesForShutdown()) {
return ERROR_ACCESS_DENIED;
}
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Evaluating sessions every %lld seconds. Shutdown after %lld seconds (%lld evaluation runs with 0 sessions)\r\n", waitTimerIntervalSeconds, waitBeforeIdleShutdownSeconds, (waitBeforeIdleShutdownSeconds / waitTimerIntervalSeconds));
WriteBufferToLog();
for (;;) {
// check for the signalled state of the stop event
if (waitResult == WAIT_OBJECT_0) {
// being signalled to stop
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Worker thread signalled by event to stop.\r\n");
WriteBufferToLog();
return ERROR_SUCCESS;
}
// reset timer -- seems to be easier with a negative due QuadPart than an auto interval??
if (!SetWaitableTimer(workerWaitableTimer, &timerDueTime, 0, NULL, NULL, FALSE)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Failed to reset waitiable timer -- error %d\r\n", GetLastError());
WriteBufferToLog();
return GetLastError();
}
result = LsaEnumerateLogonSessions(&logonSessionCount, &logonSessionListPtr);
if (STATUS_SUCCESS != result) {
wprintf(L"Failed to get logon session count: 0x%x. Will retry on next cycle\r\n", result);
// wait for either the timer or the event
waitResult = WaitForMultipleObjects(2, waitableObjects, FALSE, INFINITE);
continue;
}
wprintf(L"logon session count: %d\r\n", logonSessionCount);
interactiveSessionCount = 0;
do {
result = LsaGetLogonSessionData(logonSessionListPtr, &logonSessionData);
sidString = nullptr;
if (STATUS_SUCCESS == result) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"username: %s\r\n", logonSessionData->UserName.Buffer);
WriteBufferToLog();
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"logonType: %d\r\n", logonSessionData->LogonType);
WriteBufferToLog();
if (logonSessionData->Sid != nullptr && IsValidSid(logonSessionData->Sid)) {
if (!ConvertSidToStringSidW(logonSessionData->Sid, &sidString)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Unable to convert SID: %d\r\n", GetLastError());
WriteBufferToLog();
sidString = nullptr;
}
PSID_IDENTIFIER_AUTHORITY authority = GetSidIdentifierAuthority(logonSessionData->Sid);
if (authority != nullptr) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"authority: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n", authority->Value[0], authority->Value[1], authority->Value[2], authority->Value[3], authority->Value[4], authority->Value[5]);
//WriteBufferToLog();
// get sub authorities?
PUCHAR subAuthorityCount = GetSidSubAuthorityCount(logonSessionData->Sid);
if (GetLastError() == ERROR_SUCCESS && subAuthorityCount != nullptr && *subAuthorityCount > 0) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Sub authority count: %d\r\n", *subAuthorityCount);
//WriteBufferToLog();
// sub authority 0 seems to be interesting to us -- matches third element of string sid
for (DWORD i = 0; i < *subAuthorityCount; i++) {
PDWORD subAuthority = GetSidSubAuthority(logonSessionData->Sid, i);
if (subAuthority != nullptr) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Sub authority %d: 0x%x\r\n", i, *subAuthority);
//WriteBufferToLog();
}
}
// heuristic: if sub authority count >4, this is a _real_ session?
if (*subAuthorityCount > 4) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"I: Session %s possibly counts as interactive\r\n", logonSessionData->UserName.Buffer);
WriteBufferToLog();
// determine for sure if this is interactive -- check for Volatile Environment subkey of HKEY_USERS with this SID
StringCbPrintf(hkeyUsersSubKey, MAX_PATH, L"%s\\Volatile Environment", sidString);
LSTATUS regResult = RegOpenKeyExW(HKEY_USERS, hkeyUsersSubKey, 0, KEY_READ, &hkeyUsersSubKeyHandle);
if (regResult == ERROR_SUCCESS) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"I: Return value for Volatile Env for session %s was %d -- interactive if explorer is running\r\n", logonSessionData->UserName.Buffer, regResult);
WriteBufferToLog();
if (ExplorerIsRunningAsSID(logonSessionData->Sid)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"I: Explorer runs as this SID. This is interactive.\r\n");
WriteBufferToLog();
interactiveSessionCount++;
}
RegCloseKey(hkeyUsersSubKeyHandle);
}
else {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"I: Return value for Volatile Env for session %s was %d -- probably not interactive\r\n", logonSessionData->UserName.Buffer, regResult);
WriteBufferToLog();
}
}
}
}
else {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Unable to get authority from sid\r\n");
WriteBufferToLog();
}
}
else {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Not fetching authority from null sid\r\n");
WriteBufferToLog();
}
/* logonType
0
Used only by the System account.
Interactive (2)
Intended for users who are interactively using the machine, such as a user being logged on by a terminal server, remote shell, or similar process.
Network (3)
Intended for high-performance servers to authenticate clear text passwords. LogonUser does not cache credentials for this logon type.
Batch (4)
Intended for batch servers, where processes can be executed on behalf of a user without their direct intervention; or for higher performance servers that process many clear-text authentication attempts at a time, such as mail or web servers. LogonUser does not cache credentials for this logon type.
Service (5)
Indicates a service-type logon. The account provided must have the service privilege enabled.
Proxy (6)
Indicates a proxy-type logon.
Unlock (7)
This logon type is intended for GINA DLLs logging on users who are interactively using the machine. This logon type allows a unique audit record to be generated that shows when the workstation was unlocked.
NetworkCleartext (8)
Preserves the name and password in the authentication packages, allowing the server to make connections to other network servers while impersonating the client. This allows a server to accept clear text credentials from a client, call LogonUser, verify that the user can access the system across the network, and still communicate with other servers.
NewCredentials (9)
Allows the caller to clone its current token and specify new credentials for outbound connections. The new logon session has the same local identify, but uses different credentials for other network connections.
RemoteInteractive (10)
Terminal Services session that is both remote and interactive.
CachedInteractive (11)
Attempt cached credentials without accessing the network.
CachedRemoteInteractive (12)
Same as RemoteInteractive. This is used for internal auditing.
CachedUnlock (13)
Workstation logon.
*/
if (sidString != nullptr) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"sid: %s\r\n", sidString);
WriteBufferToLog();
for (int i = 0; i < (sizeof(wellKnownSids) / sizeof(*wellKnownSids)); i++) {
if (IsWellKnownSid(logonSessionData->Sid, wellKnownSids[i])) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Skipping %s as it is well known %d\r\n", sidString, wellKnownSids[i]);
WriteBufferToLog();
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"\r\n");
WriteBufferToLog();
break;
}
}
}
if (sidString != nullptr) {
LocalFree(sidString);
sidString = nullptr;
}
}
else if (STATUS_ACCESS_DENIED == result) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Access denied on session reverse numbered %d -- running as admin??\r\n", logonSessionCount);
WriteBufferToLog();
}
else {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"LsaGetLogonSession data fail: 0x%x\r\n", result);
WriteBufferToLog();
}
logonSessionListPtr++; //pointer increment is a different operator! -- 8 bytes forward
logonSessionCount--;
LsaFreeReturnBuffer(logonSessionData);
} while (logonSessionCount > 0);
LsaFreeReturnBuffer(logonSessionListPtr);
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"\r\n");
WriteBufferToLog();
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"HEURISTIC: %d sessions are active", interactiveSessionCount);
WriteBufferToLog();
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"\r\n------------------------\r\n");
WriteBufferToLog();
if (interactiveSessionCount == 0) {
consecutiveZeroEvaluations++;
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Consecutive zero session evaluation runs: %d", consecutiveZeroEvaluations);
WriteBufferToLog();
}
else {
consecutiveZeroEvaluations = 0;
}
// should we issue a shutdown command?
if (consecutiveZeroEvaluations > (waitBeforeIdleShutdownSeconds / waitTimerIntervalSeconds)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Time to shut down!");
WriteBufferToLog();
LPWSTR shutdownReason = (LPWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PATH * sizeof(WCHAR));
StringCbPrintf(shutdownReason, MAX_PATH, L"ShutdownBuddy initiated a shutdown after %d consecutive evaluations of no interactive users signed in.", consecutiveZeroEvaluations);
if (!InitiateSystemShutdownExW(NULL, shutdownReason, 0, TRUE, FALSE, SHTDN_REASON_MAJOR_APPLICATION)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Failed to initiate shutdown: 0x%x", GetLastError());
WriteBufferToLog();
}
HeapFree(GetProcessHeap(), 0, shutdownReason);
}
// wait for either the timer or the event
waitResult = WaitForMultipleObjects(2, waitableObjects, FALSE, INFINITE);
}
return ERROR_SUCCESS;
}
/// <summary>
/// Responds to control events from the service manager.
/// </summary>
/// <param name="controlCode"></param>
/// <returns></returns>
VOID WINAPI ServiceCtrlHandler(DWORD controlCode) {
switch (controlCode) {
case SERVICE_CONTROL_STOP:
// note that we are stopping
ZeroMemory(&serviceStatus, sizeof(serviceStatus));
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
if (!SetServiceStatus(statusHandle, &serviceStatus)) {
wprintf(L"SetServiceStatus returned FALSE -- error %d\n", GetLastError());
return;
}
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Stopping service...\n");
WriteBufferToLog();
SetEvent(serviceToStopEvent);
break;
}
}
/// <summary>
/// Receives information about a window station and do something with it.
/// </summary>
/// <param name="windowStation"></param>
/// <param name="param"></param>
/// <returns></returns>
BOOL CALLBACK EnumWindowStationProc(
_In_ LPTSTR windowStation,
_In_ LPARAM param
) {
//StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"%s\r\n", windowStation);
//WriteBufferToLog();
return TRUE;
}
/// <summary>
/// Check for an Explorer process running as the target SID
/// </summary>
/// <param name="sid"></param>
/// <returns>TRUE or FALSE</returns>
BOOL ExplorerIsRunningAsSID(PSID sid) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
HANDLE process = INVALID_HANDLE_VALUE;
HANDLE processToken = INVALID_HANDLE_VALUE;
PROCESSENTRY32 pe32{};
PTOKEN_OWNER processOwner = nullptr;
DWORD tokenInformationLength = 0;
if (snapshot == INVALID_HANDLE_VALUE) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Failed to get Toolhelp snapshot of processes\n");
WriteBufferToLog();
return FALSE;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(snapshot, &pe32)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Process32First failed\n");
WriteBufferToLog();
CloseHandle(snapshot);
return FALSE;
}
do {
if (_wcsicmp(pe32.szExeFile, L"explorer.exe") == 0) {
// open process for query
process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID);
if (process == INVALID_HANDLE_VALUE || process == NULL) {
StringCbPrintf(logBuffer,
LOG_BUFFER_SIZE,
L"Failed to open handle to explorer.exe with PID %d. Error: %d\n",
pe32.th32ProcessID,
GetLastError());
WriteBufferToLog();
CloseHandle(snapshot);
return FALSE;
}
// open a handle to the token for the process
if (!OpenProcessToken(process, TOKEN_READ, &processToken)) {
StringCbPrintf(logBuffer,
LOG_BUFFER_SIZE,
L"Failed to open handle to token for process %d. Error: %d\n",
pe32.th32ProcessID,
GetLastError());
WriteBufferToLog();
CloseHandle(process);
CloseHandle(snapshot);
return FALSE;
}
// get SID of token owner -- first get and check required buffer size
GetTokenInformation(processToken, TokenOwner, processOwner, 0, &tokenInformationLength);
processOwner = (PTOKEN_OWNER)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tokenInformationLength);
if (processOwner == NULL) {
StringCbPrintf(logBuffer,
LOG_BUFFER_SIZE,
L"Failed to allocate memory for process owner info. Error: %d\n",
GetLastError());
WriteBufferToLog();
CloseHandle(processToken);
CloseHandle(process);
CloseHandle(snapshot);
return FALSE;
}
if (!GetTokenInformation(processToken, TokenOwner, processOwner, tokenInformationLength, &tokenInformationLength)) {
StringCbPrintf(logBuffer,
LOG_BUFFER_SIZE,
L"Failed to get token information for process %d. Error: %d\n",
pe32.th32ProcessID,
GetLastError());
WriteBufferToLog();
HeapFree(GetProcessHeap(), 0, processOwner);
CloseHandle(processToken);
CloseHandle(process);
CloseHandle(snapshot);
return FALSE;
}
// now we have the token info
if (EqualSid(processOwner->Owner, sid)) {
StringCbPrintf(logBuffer,
LOG_BUFFER_SIZE,
L"Explorer %d is running as the target SID\n",
pe32.th32ProcessID);
WriteBufferToLog();
//FreeSid(processOwner->Owner); -- this causes a crash, but whose responsibility is freeing these SIDs?? Do they need it?
HeapFree(GetProcessHeap(), 0, processOwner);
CloseHandle(processToken);
CloseHandle(process);
CloseHandle(snapshot);
return TRUE;
}
StringCbPrintf(logBuffer,
LOG_BUFFER_SIZE,
L"Explorer %d is NOT running as the target SID\n",
pe32.th32ProcessID);
WriteBufferToLog();
//FreeSid(processOwner->Owner); -- this causes a crash, but whose responsibility is freeing these SIDs?? Do they need it?
HeapFree(GetProcessHeap(), 0, processOwner);
CloseHandle(processToken);
CloseHandle(process);
continue;
}
} while (Process32Next(snapshot, &pe32));
CloseHandle(snapshot);
return FALSE;
}
/// <summary>
/// Load our settings from the registry.
/// </summary>
/// <param name=""></param>
void LoadSettingsFromRegistry(void) {
HKEY rootKey{};
DWORD dwResult = 0;
DWORD dataSize = sizeof(dwResult);
WCHAR debugString[MAX_PATH] = L"";
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, SHUTDOWN_BUDDY_REG_ROOT, 0, KEY_READ, &rootKey) != ERROR_SUCCESS) {
// log buffer is not yet available.
OutputDebugStringW(L"Unable to open settings from registry. Will use default.");
return;
}
if (RegGetValueW(rootKey, L"", L"DebugLog", RRF_RT_DWORD | RRF_ZEROONFAILURE, NULL, &dwResult, &dataSize) == ERROR_SUCCESS) {
if (dwResult == 1) {
OutputDebugStringW(L"Should debug log to temp file");
shouldDebugLog = TRUE;
}
}
if (RegGetValueW(rootKey, L"", L"EvaluationIntervalSeconds", RRF_RT_DWORD | RRF_ZEROONFAILURE, NULL, &dwResult, &dataSize) == ERROR_SUCCESS) {
waitTimerIntervalSeconds = dwResult;
}
if (RegGetValueW(rootKey, L"", L"ShutdownAfterIdleForSeconds", RRF_RT_DWORD | RRF_ZEROONFAILURE, NULL, &dwResult, &dataSize) == ERROR_SUCCESS) {
waitBeforeIdleShutdownSeconds = dwResult;
}
/*StringCbPrintf(debugString, MAX_PATH, L"Registry result: %d", result);
OutputDebugStringW(debugString);*/
RegCloseKey(rootKey);
}
/// <summary>
/// Adjust our own token privileges to enable the SeShutdownPrivilege
/// </summary>
/// <param name=""></param>
/// <returns></returns>
BOOL AdjustTokenPrivilegesForShutdown(void) {
HANDLE token = INVALID_HANDLE_VALUE;
LUID shutdownPrivLuid{};
TOKEN_PRIVILEGES tp{ };
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Unable to OpenProcessToken for adjusting -- 0x%x", GetLastError());
WriteBufferToLog();
return FALSE;
}
if (!LookupPrivilegeValueW(NULL, SE_SHUTDOWN_NAME, &shutdownPrivLuid)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Unable to get SeShutdownPrivilege LUID -- 0x%x", GetLastError());
WriteBufferToLog();
CloseHandle(token);
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = shutdownPrivLuid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Unable to adjust token privileges to add SeShutdownPrivilege -- 0x%x", GetLastError());
WriteBufferToLog();
CloseHandle(token);
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"The token did not get all the assigned privileges after adjustment");
WriteBufferToLog();
CloseHandle(token);
return FALSE;
}
StringCbPrintf(logBuffer, LOG_BUFFER_SIZE, L"Adjusted token to add SeShutdownPrivilege");
CloseHandle(token);
WriteBufferToLog();
return TRUE;
}