-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
actions.c
5163 lines (4458 loc) · 135 KB
/
actions.c
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* wj32 2010-2016
* dmex 2017-2023
*
*/
/*
* These are a set of consistent functions which will perform actions on objects such as processes,
* threads and services, while displaying any necessary prompts and error messages. Automatic
* elevation can also easily be added if necessary.
*/
#include <phapp.h>
#include <actions.h>
#include <winsta.h>
#include <kphuser.h>
#include <ksisup.h>
#include <mapldr.h>
#include <secedit.h>
#include <secwmi.h>
#include <settings.h>
#include <svcsup.h>
#include <apiimport.h>
#include <bcd.h>
#include <emenu.h>
#include <hndlprv.h>
#include <memprv.h>
#include <modprv.h>
#include <netprv.h>
#include <phsvccl.h>
#include <procprv.h>
#include <srvprv.h>
#include <thrdprv.h>
static PH_STRINGREF DangerousProcesses[] =
{
PH_STRINGREF_INIT(L"csrss.exe"), PH_STRINGREF_INIT(L"dwm.exe"), PH_STRINGREF_INIT(L"logonui.exe"),
PH_STRINGREF_INIT(L"lsass.exe"), PH_STRINGREF_INIT(L"lsm.exe"), PH_STRINGREF_INIT(L"services.exe"),
PH_STRINGREF_INIT(L"smss.exe"), PH_STRINGREF_INIT(L"wininit.exe"), PH_STRINGREF_INIT(L"winlogon.exe")
};
static PPH_STRING DebuggerCommand = NULL;
static volatile LONG PhSvcReferenceCount = 0;
static PH_PHSVC_MODE PhSvcCurrentMode;
static PH_QUEUED_LOCK PhSvcStartLock = PH_QUEUED_LOCK_INIT;
HRESULT CALLBACK PhpElevateActionCallbackProc(
_In_ HWND hwnd,
_In_ UINT uNotification,
_In_ WPARAM wParam,
_In_ LPARAM lParam,
_In_ LONG_PTR dwRefData
)
{
switch (uNotification)
{
case TDN_CREATED:
SendMessage(hwnd, TDM_SET_BUTTON_ELEVATION_REQUIRED_STATE, IDYES, TRUE);
break;
}
return S_OK;
}
_Success_(return)
BOOLEAN PhpShowElevatePrompt(
_In_ HWND hWnd,
_In_ PWSTR Message,
_Out_ PINT Button
)
{
TASKDIALOGCONFIG config = { sizeof(config) };
TASKDIALOG_BUTTON buttons[1];
INT button;
// Currently the error dialog box is similar to the one displayed
// when you try to label a drive in Windows Explorer. It's much better
// than the clunky dialog in PH 1.x.
config.hwndParent = hWnd;
config.hInstance = PhInstanceHandle;
config.dwFlags = IsWindowVisible(hWnd) ? TDF_POSITION_RELATIVE_TO_WINDOW : 0;
config.pszWindowTitle = PhApplicationName;
config.pszMainIcon = TD_ERROR_ICON;
config.pszMainInstruction = PhaConcatStrings2(Message, L".")->Buffer;
config.pszContent = L"You will need to provide administrator permission. "
L"Click Continue to complete this operation.";
config.dwCommonButtons = TDCBF_CANCEL_BUTTON;
buttons[0].nButtonID = IDYES;
buttons[0].pszButtonText = L"Continue";
config.cButtons = 1;
config.pButtons = buttons;
config.nDefaultButton = IDYES;
config.pfCallback = PhpElevateActionCallbackProc;
if (SUCCEEDED(TaskDialogIndirect(
&config,
&button,
NULL,
NULL
)))
{
*Button = button;
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Shows an error, prompts for elevation, and executes a command.
*
* \param hWnd The window to display user interface components on.
* \param Message A message describing the operation that failed.
* \param Status A NTSTATUS value.
* \param Command The arguments to pass to the new instance of
* the application, if required.
* \param Success A variable which receives TRUE if the elevated
* action succeeded or FALSE if the action failed.
*
* \return TRUE if the user was prompted for elevation, otherwise
* FALSE, in which case you need to show your own error message.
*/
_Success_(return)
BOOLEAN PhpShowErrorAndElevateAction(
_In_ HWND hWnd,
_In_ PWSTR Message,
_In_ NTSTATUS Status,
_In_ PWSTR Command,
_Out_opt_ PBOOLEAN Success
)
{
NTSTATUS status = STATUS_SUCCESS;
PH_ACTION_ELEVATION_LEVEL elevationLevel;
INT button = IDNO;
if (!(
Status == STATUS_ACCESS_DENIED ||
Status == STATUS_PRIVILEGE_NOT_HELD ||
(NT_NTWIN32(Status) && WIN32_FROM_NTSTATUS(Status) == ERROR_ACCESS_DENIED)
))
return FALSE;
if (PhGetOwnTokenAttributes().Elevated)
return FALSE;
elevationLevel = PhGetIntegerSetting(L"ElevationLevel");
if (elevationLevel == NeverElevateAction)
return FALSE;
if (elevationLevel == PromptElevateAction)
{
if (!PhpShowElevatePrompt(hWnd, Message, &button))
return FALSE;
}
if (elevationLevel == AlwaysElevateAction || button == IDYES)
{
HANDLE processHandle;
LARGE_INTEGER timeout;
PROCESS_BASIC_INFORMATION basicInfo;
if (PhShellProcessHacker(
hWnd,
Command,
SW_SHOW,
PH_SHELL_EXECUTE_ADMIN,
PH_SHELL_APP_PROPAGATE_PARAMETERS,
0,
&processHandle
))
{
timeout.QuadPart = -(LONGLONG)UInt32x32To64(10, PH_TIMEOUT_SEC);
status = NtWaitForSingleObject(processHandle, FALSE, &timeout);
if (
status == STATUS_WAIT_0 &&
NT_SUCCESS(status = PhGetProcessBasicInformation(processHandle, &basicInfo))
)
{
status = basicInfo.ExitStatus;
}
NtClose(processHandle);
}
}
if (Success)
*Success = NT_SUCCESS(status);
if (!NT_SUCCESS(status))
PhShowStatus(hWnd, Message, status, 0);
return TRUE;
}
/**
* Shows an error, prompts for elevation, and connects to phsvc.
*
* \param hWnd The window to display user interface components on.
* \param Message A message describing the operation that failed.
* \param Status A NTSTATUS value.
* \param Connected A variable which receives TRUE if the user
* elevated the action and phsvc was started, or FALSE if the user
* cancelled elevation. If the value is TRUE, you need to
* perform any necessary phsvc calls and use PhUiDisconnectFromPhSvc()
* to disconnect from phsvc.
*
* \return TRUE if the user was prompted for elevation, otherwise
* FALSE, in which case you need to show your own error message.
*/
BOOLEAN PhpShowErrorAndConnectToPhSvc(
_In_ HWND hWnd,
_In_ PWSTR Message,
_In_ NTSTATUS Status,
_Out_ PBOOLEAN Connected
)
{
PH_ACTION_ELEVATION_LEVEL elevationLevel;
INT button = IDNO;
*Connected = FALSE;
if (!(
Status == STATUS_ACCESS_DENIED ||
Status == STATUS_PRIVILEGE_NOT_HELD ||
(NT_NTWIN32(Status) && WIN32_FROM_NTSTATUS(Status) == ERROR_ACCESS_DENIED)
))
return FALSE;
if (PhGetOwnTokenAttributes().Elevated)
return FALSE;
elevationLevel = PhGetIntegerSetting(L"ElevationLevel");
if (elevationLevel == NeverElevateAction)
return FALSE;
// Try to connect now so we can avoid prompting the user.
if (PhUiConnectToPhSvc(hWnd, TRUE))
{
*Connected = TRUE;
return TRUE;
}
if (elevationLevel == PromptElevateAction)
{
if (!PhpShowElevatePrompt(hWnd, Message, &button))
return FALSE;
}
if (elevationLevel == AlwaysElevateAction || button == IDYES)
{
*Connected = PhUiConnectToPhSvc(hWnd, FALSE);
}
return TRUE;
}
/**
* Connects to phsvc.
*
* \param hWnd The window to display user interface components on.
* \param ConnectOnly TRUE to only try to connect to phsvc, otherwise
* FALSE to try to elevate and start phsvc if the initial connection
* attempt failed.
*/
BOOLEAN PhUiConnectToPhSvc(
_In_opt_ HWND hWnd,
_In_ BOOLEAN ConnectOnly
)
{
return PhUiConnectToPhSvcEx(hWnd, ElevatedPhSvcMode, ConnectOnly);
}
VOID PhpGetPhSvcPortName(
_In_ PH_PHSVC_MODE Mode,
_Out_ PUNICODE_STRING PortName
)
{
switch (Mode)
{
case ElevatedPhSvcMode:
if (!PhIsExecutingInWow64())
RtlInitUnicodeString(PortName, PHSVC_PORT_NAME);
else
RtlInitUnicodeString(PortName, PHSVC_WOW64_PORT_NAME);
break;
case Wow64PhSvcMode:
RtlInitUnicodeString(PortName, PHSVC_WOW64_PORT_NAME);
break;
default:
PhRaiseStatus(STATUS_INVALID_PARAMETER);
break;
}
}
BOOLEAN PhpStartPhSvcProcess(
_In_opt_ HWND hWnd,
_In_ PH_PHSVC_MODE Mode
)
{
switch (Mode)
{
case ElevatedPhSvcMode:
if (PhShellProcessHacker(
hWnd,
L"-phsvc",
SW_HIDE,
PH_SHELL_EXECUTE_ADMIN,
0,
0,
NULL
))
{
return TRUE;
}
break;
case Wow64PhSvcMode:
{
static PH_STRINGREF relativeFileNames[] =
{
PH_STRINGREF_INIT(L"\\x86\\"),
#ifdef DEBUG
PH_STRINGREF_INIT(L"\\..\\Debug32\\"),
PH_STRINGREF_INIT(L"\\..\\Release32\\")
#endif
};
ULONG i;
PPH_STRING applicationDirectory;
PPH_STRING applicationFileName;
if (!(applicationDirectory = PhGetApplicationDirectoryWin32()))
return FALSE;
if (!(applicationFileName = PhGetApplicationFileNameWin32()))
return FALSE;
PhMoveReference(&applicationFileName, PhGetBaseName(applicationFileName));
for (i = 0; i < RTL_NUMBER_OF(relativeFileNames); i++)
{
PPH_STRING fileName;
PPH_STRING fileFullPath;
fileName = PhConcatStringRef3(
&applicationDirectory->sr,
&relativeFileNames[i],
&applicationFileName->sr
);
if (NT_SUCCESS(PhGetFullPath(PhGetString(fileName), &fileFullPath, NULL)))
{
PhMoveReference(&fileName, fileFullPath);
}
if (PhDoesFileExistWin32(PhGetString(fileName)))
{
if (PhShellProcessHackerEx(
hWnd,
PhGetString(fileName),
L"-phsvc",
SW_HIDE,
PH_SHELL_EXECUTE_DEFAULT,
0,
0,
NULL
))
{
PhDereferenceObject(fileName);
PhDereferenceObject(applicationFileName);
PhDereferenceObject(applicationDirectory);
return TRUE;
}
}
PhDereferenceObject(fileName);
}
PhDereferenceObject(applicationFileName);
PhDereferenceObject(applicationDirectory);
}
break;
}
return FALSE;
}
/**
* Connects to phsvc.
*
* \param hWnd The window to display user interface components on.
* \param Mode The type of phsvc instance to connect to.
* \param ConnectOnly TRUE to only try to connect to phsvc, otherwise
* FALSE to try to elevate and start phsvc if the initial connection
* attempt failed.
*/
BOOLEAN PhUiConnectToPhSvcEx(
_In_opt_ HWND hWnd,
_In_ PH_PHSVC_MODE Mode,
_In_ BOOLEAN ConnectOnly
)
{
NTSTATUS status;
BOOLEAN started;
UNICODE_STRING portName;
if (_InterlockedIncrementNoZero(&PhSvcReferenceCount))
{
if (PhSvcCurrentMode == Mode)
{
started = TRUE;
}
else
{
_InterlockedDecrement(&PhSvcReferenceCount);
started = FALSE;
}
}
else
{
PhAcquireQueuedLockExclusive(&PhSvcStartLock);
if (_InterlockedExchange(&PhSvcReferenceCount, 0) == 0)
{
started = FALSE;
PhpGetPhSvcPortName(Mode, &portName);
// Try to connect first, then start the server if we failed.
status = PhSvcConnectToServer(&portName, 0);
if (NT_SUCCESS(status))
{
started = TRUE;
PhSvcCurrentMode = Mode;
_InterlockedIncrement(&PhSvcReferenceCount);
}
else if (!ConnectOnly)
{
// Prompt for elevation, and then try to connect to the server.
if (PhpStartPhSvcProcess(hWnd, Mode))
started = TRUE;
if (started)
{
ULONG attempts = 50;
// Try to connect several times because the server may take
// a while to initialize.
do
{
status = PhSvcConnectToServer(&portName, 0);
if (NT_SUCCESS(status))
break;
PhDelayExecution(100);
} while (--attempts != 0);
// Increment the reference count even if we failed.
// We don't want to prompt the user again.
PhSvcCurrentMode = Mode;
_InterlockedIncrement(&PhSvcReferenceCount);
}
}
}
else
{
if (PhSvcCurrentMode == Mode)
{
started = TRUE;
_InterlockedIncrement(&PhSvcReferenceCount);
}
else
{
started = FALSE;
}
}
PhReleaseQueuedLockExclusive(&PhSvcStartLock);
}
return started;
}
/**
* Disconnects from phsvc.
*/
VOID PhUiDisconnectFromPhSvc(
VOID
)
{
PhAcquireQueuedLockExclusive(&PhSvcStartLock);
if (_InterlockedDecrement(&PhSvcReferenceCount) == 0)
{
PhSvcDisconnectFromServer();
}
PhReleaseQueuedLockExclusive(&PhSvcStartLock);
}
BOOLEAN PhUiLockComputer(
_In_ HWND hWnd
)
{
if (LockWorkStation())
return TRUE;
else
PhShowStatus(hWnd, L"Unable to lock the computer.", 0, GetLastError());
return FALSE;
}
BOOLEAN PhUiLogoffComputer(
_In_ HWND hWnd
)
{
if (ExitWindowsEx(EWX_LOGOFF, 0))
return TRUE;
else
PhShowStatus(hWnd, L"Unable to log off the computer.", 0, GetLastError());
return FALSE;
}
BOOLEAN PhUiSleepComputer(
_In_ HWND hWnd
)
{
NTSTATUS status;
if (NT_SUCCESS(status = NtInitiatePowerAction(
PowerActionSleep,
PowerSystemSleeping1,
0,
FALSE
)))
return TRUE;
else
PhShowStatus(hWnd, L"Unable to sleep the computer.", status, 0);
return FALSE;
}
BOOLEAN PhUiHibernateComputer(
_In_ HWND hWnd
)
{
NTSTATUS status;
if (NT_SUCCESS(status = NtInitiatePowerAction(
PowerActionHibernate,
PowerSystemSleeping1,
0,
FALSE
)))
return TRUE;
else
PhShowStatus(hWnd, L"Unable to hibernate the computer.", status, 0);
return FALSE;
}
BOOLEAN PhUiRestartComputer(
_In_ HWND WindowHandle,
_In_ PH_POWERACTION_TYPE Action,
_In_ ULONG Flags
)
{
switch (Action)
{
case PH_POWERACTION_TYPE_WIN32:
{
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
NULL,
FALSE
))
{
ULONG status = PhInitiateShutdown(PH_SHUTDOWN_RESTART | Flags);
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(WindowHandle, L"Unable to restart the computer.", 0, status);
//if (ExitWindowsEx(EWX_REBOOT | EWX_BOOTOPTIONS, 0))
// return TRUE;
//else
// PhShowStatus(WindowHandle, L"Unable to restart the computer.", 0, GetLastError());
}
}
break;
case PH_POWERACTION_TYPE_NATIVE:
{
PPH_STRING messageText;
messageText = PhaFormatString(
L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.",
L"preforms a hard",
L"restart");
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
messageText->Buffer,
TRUE
))
{
NTSTATUS status;
status = NtShutdownSystem(ShutdownReboot);
if (NT_SUCCESS(status))
return TRUE;
PhShowStatus(WindowHandle, L"Unable to restart the computer.", status, 0);
}
}
break;
case PH_POWERACTION_TYPE_CRITICAL:
{
PPH_STRING messageText;
messageText = PhaFormatString(
L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.",
L"forces a critical",
L"restart");
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
messageText->Buffer,
TRUE
))
{
NTSTATUS status;
status = NtSetSystemPowerState(
PowerActionShutdownReset,
PowerSystemShutdown,
POWER_ACTION_CRITICAL
);
//status = NtInitiatePowerAction(
// PowerActionShutdownReset,
// PowerSystemShutdown,
// POWER_ACTION_CRITICAL,
// FALSE
// );
if (NT_SUCCESS(status))
return TRUE;
PhShowStatus(WindowHandle, L"Unable to restart the computer.", status, 0);
}
}
break;
case PH_POWERACTION_TYPE_ADVANCEDBOOT:
{
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
NULL,
FALSE
))
{
NTSTATUS status;
status = PhBcdSetAdvancedOptionsOneTime(
TRUE
);
if (NT_SUCCESS(status))
{
status = PhInitiateShutdown(PH_SHUTDOWN_RESTART);
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(WindowHandle, L"Unable to configure the advanced boot options.", 0, status);
}
else
{
PhShowStatus(WindowHandle, L"Unable to configure the advanced boot options.", status, 0);
}
}
}
break;
case PH_POWERACTION_TYPE_FIRMWAREBOOT:
{
if (!PhGetOwnTokenAttributes().Elevated)
{
PhShowMessage2(
WindowHandle,
TD_OK_BUTTON,
TD_ERROR_ICON,
L"Unable to restart to firmware options.",
L"Make sure System Informer is running with administrative privileges."
);
break;
}
if (!NT_SUCCESS(PhAdjustPrivilege(NULL, SE_SYSTEM_ENVIRONMENT_PRIVILEGE, TRUE)))
{
PhShowMessage2(
WindowHandle,
TD_OK_BUTTON,
TD_ERROR_ICON,
L"Unable to restart to firmware options.",
L"Make sure System Informer is running with administrative privileges."
);
break;
}
if (!PhIsFirmwareSupported())
{
PhShowMessage2(
WindowHandle,
TD_OK_BUTTON,
TD_ERROR_ICON,
L"Unable to restart to firmware options.",
L"This machine does not have UEFI support."
);
break;
}
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer",
NULL,
FALSE
))
{
NTSTATUS status;
status = PhSetSystemEnvironmentBootToFirmware();
if (NT_SUCCESS(status))
{
status = PhInitiateShutdown(PH_SHUTDOWN_RESTART);
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(WindowHandle, L"Unable to restart the computer.", 0, status);
}
else
{
PhShowStatus(WindowHandle, L"Unable to restart the computer.", status, 0);
}
}
}
break;
case PH_POWERACTION_TYPE_UPDATE:
{
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"update and restart",
L"the computer",
NULL,
FALSE
))
{
ULONG status = PhInitiateShutdown(PH_SHUTDOWN_RESTART | PH_SHUTDOWN_INSTALL_UPDATES);
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(WindowHandle, L"Unable to restart the computer.", 0, status);
}
}
break;
case PH_POWERACTION_TYPE_WDOSCAN:
{
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"restart",
L"the computer for Windows Defender Offline Scan",
NULL,
FALSE
))
{
HRESULT status = PhRestartDefenderOfflineScan();
if (status == S_OK)
return TRUE;
if ((status & 0xFFFF0000) == MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, 0))
{
PhShowStatus(WindowHandle, L"Unable to restart the computer.", 0, HRESULT_CODE(status));
}
else
{
PhShowStatus(WindowHandle, L"Unable to restart the computer.", STATUS_UNSUCCESSFUL, 0);
}
}
}
break;
}
return FALSE;
}
BOOLEAN PhUiShutdownComputer(
_In_ HWND WindowHandle,
_In_ PH_POWERACTION_TYPE Action,
_In_ ULONG Flags
)
{
switch (Action)
{
case PH_POWERACTION_TYPE_WIN32:
{
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"shut down",
L"the computer",
NULL,
FALSE
))
{
ULONG status = PhInitiateShutdown(PH_SHUTDOWN_POWEROFF | Flags);
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(WindowHandle, L"Unable to shut down the computer.", 0, status);
//if (ExitWindowsEx(EWX_POWEROFF | EWX_HYBRID_SHUTDOWN, 0))
// return TRUE;
//else if (ExitWindowsEx(EWX_SHUTDOWN | EWX_HYBRID_SHUTDOWN, 0))
// return TRUE;
//else
// PhShowStatus(WindowHandle, L"Unable to shut down the computer.", 0, GetLastError());
}
}
break;
case PH_POWERACTION_TYPE_NATIVE:
{
PPH_STRING messageText;
messageText = PhaFormatString(
L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.",
L"preforms a hard",
L"shut down");
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"shut down",
L"the computer",
messageText->Buffer,
TRUE
))
{
NTSTATUS status;
status = NtShutdownSystem(ShutdownPowerOff);
if (NT_SUCCESS(status))
return TRUE;
PhShowStatus(WindowHandle, L"Unable to shut down the computer.", status, 0);
}
}
break;
case PH_POWERACTION_TYPE_CRITICAL:
{
PPH_STRING messageText;
messageText = PhaFormatString(
L"This option %s %s in an disorderly manner and may cause corrupted files or instability in the system.",
L"forces a critical",
L"shut down");
// Ignore the EnableWarnings preference and always show the warning prompt. (dmex)
if (PhShowConfirmMessage(
WindowHandle,
L"shut down",
L"the computer",
messageText->Buffer,
TRUE
))
{
NTSTATUS status;
status = NtSetSystemPowerState(
PowerActionShutdownOff,
PowerSystemShutdown,
POWER_ACTION_CRITICAL
);
//status = NtInitiatePowerAction(
// PowerActionShutdownReset,
// PowerSystemShutdown,
// POWER_ACTION_CRITICAL,
// FALSE
// );
if (NT_SUCCESS(status))
return TRUE;
PhShowStatus(WindowHandle, L"Unable to shut down the computer.", status, 0);
}
}
break;
case PH_POWERACTION_TYPE_UPDATE:
{
if (!PhGetIntegerSetting(L"EnableWarnings") || PhShowConfirmMessage(
WindowHandle,
L"update and shutdown",
L"the computer",
NULL,
FALSE
))
{
ULONG status = PhInitiateShutdown(PH_SHUTDOWN_POWEROFF | PH_SHUTDOWN_INSTALL_UPDATES);
if (status == ERROR_SUCCESS)
return TRUE;
PhShowStatus(WindowHandle, L"Unable to shut down the computer.", 0, status);
}
}
break;
}
return FALSE;
}
PVOID PhUiCreateComputerBootDeviceMenu(
_In_ BOOLEAN DelayLoadMenu
)
{
PPH_EMENU_ITEM menuItem;
PPH_LIST bootApplicationList;
menuItem = PhCreateEMenuItem(PH_EMENU_DISABLED, ID_COMPUTER_RESTARTBOOTDEVICE, L"Restart to boot application", NULL, NULL);
if (!PhGetOwnTokenAttributes().Elevated)
return menuItem;
if (!DelayLoadMenu)
{
BOOLEAN bootEnumerateAllObjects = !!PhGetIntegerSetting(L"EnableBootObjectsEnumerate");
if (bootApplicationList = PhBcdQueryBootApplicationList(bootEnumerateAllObjects))
{
for (ULONG i = 0; i < bootApplicationList->Count; i++)
{
PPH_BCD_OBJECT_LIST entry = bootApplicationList->Items[i];
PPH_EMENU_ITEM menuItemNew;
menuItemNew = PhCreateEMenuItem(
PH_EMENU_TEXT_OWNED,
ID_COMPUTER_RESTARTBOOTDEVICE,
PhAllocateCopy(entry->ObjectName->Buffer, entry->ObjectName->Length + sizeof(UNICODE_NULL)),
NULL,
UlongToPtr(i)
);
PhInsertEMenuItem(menuItem, menuItemNew, ULONG_MAX);
}
if (bootApplicationList->Count)
PhSetEnabledEMenuItem(menuItem, TRUE);
PhBcdDestroyBootApplicationList(bootApplicationList);
}
}
return menuItem;
}
PVOID PhUiCreateComputerFirmwareDeviceMenu(