-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenGenerator.cpp
More file actions
2460 lines (2218 loc) · 118 KB
/
Copy pathTokenGenerator.cpp
File metadata and controls
2460 lines (2218 loc) · 118 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
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
/*
compileoptions : -Ofast -Wall -Wextra -fexec-charset=GBK -static -lwtsapi32 -luserenv -lntdll -ladvapi32 -lgdi32 -lcomctl32 -lcomdlg32 -luuid -lole32
*/
#undef UNICODE
#include <windows.h>
#include <wtsapi32.h>
#include <userenv.h>
#include <tlhelp32.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <vector>
#include <sstream>
#include <sddl.h>
#include <richedit.h>
#include <ntdef.h>
#include <shobjidl.h>
#include <winternl.h>
// ==========================================
// 1. ID
// ==========================================
#define ID_LISTVIEW_GROUPS 2001
#define ID_BTN_ADD_GRP 2002
#define ID_BTN_DEL_GRP 2003
#define ID_BTN_EDIT_GRP 2004
#define ID_LISTVIEW_PRIVS 3001
#define ID_BTN_RESET 3002
#define ID_BTN_DISABLE 3003
#define ID_BTN_REMOVE 3004
#define ID_BTN_RUN 1001
#define ID_BTN_CANCEL 1002
#define ID_EDIT_CMD 1003
#define ID_COMBO_USER 1004
#define ID_COMBO_IL 1005
#define ID_CHECK_UIACCESS 1007
#define ID_CHECK_DEBUG 1009
#define ID_EDIT_DESKTOP 1010
#define ID_EDIT_DIR 1011
#define ID_COMBO_MODE 1012
#define ID_EDIT_LOG 1013
#define ID_EDIT_GROUPS 1014
#define ID_EDIT_PRIVS 1015
#define ID_BTN_CMD_BROWSE 1016
#define ID_BTN_DIR_BROWSE 1017
#define ID_COMBO_PRESET 1018
#define ID_BTN_EDIT_GRP_BTN 1019
#define ID_BTN_EDIT_PRIV_BTN 1020
#define ID_PREVIEW_LOG 1021
// ==========================================
// 2.
// ==========================================
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
typedef NTSTATUS(NTAPI* PNtCreateToken)(
PHANDLE, ACCESS_MASK, PVOID, TOKEN_TYPE, PLUID, PLARGE_INTEGER,
PTOKEN_USER, PTOKEN_GROUPS, PTOKEN_PRIVILEGES, PTOKEN_OWNER,
PTOKEN_PRIMARY_GROUP, PTOKEN_DEFAULT_DACL, PVOID);
typedef HRESULT(WINAPI* PDwmSetWindowAttribute)(HWND, DWORD, LPCVOID, DWORD);
typedef HRESULT(WINAPI* PSetWindowTheme)(HWND, LPCWSTR, LPCWSTR);
enum LogLevel { LOG_ERROR, LOG_WARN, LOG_INFO, LOG_SUCCESS, LOG_DEBUG };
struct Preset {
LPSTR identityStr;
std::vector<std::string> extraGroups;
std::vector<int> RemovePrivilege;
};
struct PrivInfo {
const char* name;
const char* desc;
};
// ==========================================
// 3.
// ==========================================
#define COLOR_BG_DARK RGB(24, 24, 24) //
#define COLOR_CARD_DARK RGB(32, 32, 32) //
#define COLOR_CTRL_BG_DARK RGB(45, 45, 45) //
#define COLOR_TEXT_DARK RGB(240, 240, 240) //
#define COLOR_TEXT_MUTED_DARK RGB(160, 160, 160) //
#define COLOR_ACCENT_DARK RGB(0, 120, 215) // (WinUI 3 )
#define COLOR_ACCENT_HOVER_DARK RGB(20, 140, 235) //
#define COLOR_ACCENT_PUSH_DARK RGB(0, 100, 180) //
#define COLOR_SECONDARY_DARK RGB(60, 60, 60) //
#define COLOR_SEC_HOVER_DARK RGB(80, 80, 80) //
#define COLOR_SEC_PUSH_DARK RGB(45, 45, 45) //
#define COLOR_BORDER_DARK RGB(70, 70, 70) //
#define COLOR_BG_LIGHT RGB(243, 243, 243) //
#define COLOR_CARD_LIGHT RGB(255, 255, 255) //
#define COLOR_CTRL_BG_LIGHT RGB(255, 255, 255) //
#define COLOR_TEXT_LIGHT RGB(32, 32, 32) //
#define COLOR_TEXT_MUTED_LIGHT RGB(110, 110, 110) //
#define COLOR_ACCENT_LIGHT RGB(0, 90, 158) //
#define COLOR_ACCENT_HOVER_LIGHT RGB(16, 110, 190) //
#define COLOR_ACCENT_PUSH_LIGHT RGB(0, 74, 127) //
#define COLOR_SECONDARY_LIGHT RGB(225, 229, 235) //
#define COLOR_SEC_HOVER_LIGHT RGB(210, 215, 222) //
#define COLOR_SEC_PUSH_LIGHT RGB(190, 195, 202) //
#define COLOR_BORDER_LIGHT RGB(210, 210, 210) //
COLORREF g_ColorBg = 0;
COLORREF g_ColorCard = 0;
COLORREF g_ColorCtrlBg = 0;
COLORREF g_ColorText = 0;
COLORREF g_ColorTextMuted = 0;
COLORREF g_ColorAccent = 0;
COLORREF g_ColorAccentHover = 0;
COLORREF g_ColorAccentPush = 0;
COLORREF g_ColorSecondary = 0;
COLORREF g_ColorSecHover = 0;
COLORREF g_ColorSecPush = 0;
COLORREF g_ColorBorder = 0;
BOOL g_bDarkMode = FALSE;
// ==========================================
// 4.
// ==========================================
bool g_bDebug = false, g_bUIAccess = false;
std::vector<std::string> extraGroups;
std::vector<int> RemovePrivilege, DisabledPrivilege;
int g_WindowCreateMode = SW_SHOWNORMAL;
DWORD Integrity_Level = SECURITY_MANDATORY_SYSTEM_RID;
HWND g_hLogEdit = NULL, g_hGroupEditor = NULL, g_hPrivEditor = NULL, g_hPreviewEdit = NULL; //
LPSTR g_desktop = NULL, g_runCommand = (LPSTR)"cmd.exe", g_identityStr = (LPSTR)"NT AUTHORITY\\SYSTEM";
//
HBRUSH hBrushBg = NULL;
HBRUSH hBrushCard = NULL;
HBRUSH hBrushCtrlBg = NULL;
HFONT hFontTitle = NULL;
HFONT hFontSubtitle = NULL;
HFONT hFontNormal = NULL;
HFONT hFontLog = NULL;
const char* AllPrivileges[] = {
"SeCreateTokenPrivilege", "SeAssignPrimaryTokenPrivilege", "SeLockMemoryPrivilege",
"SeIncreaseQuotaPrivilege", "SeMachineAccountPrivilege", "SeTcbPrivilege",
"SeSecurityPrivilege", "SeTakeOwnershipPrivilege", "SeLoadDriverPrivilege",
"SeSystemProfilePrivilege", "SeSystemtimePrivilege", "SeProfileSingleProcessPrivilege",
"SeIncreaseBasePriorityPrivilege", "SeCreatePagefilePrivilege", "SeCreatePermanentPrivilege",
"SeBackupPrivilege", "SeRestorePrivilege", "SeShutdownPrivilege", "SeDebugPrivilege",
"SeAuditPrivilege", "SeSystemEnvironmentPrivilege", "SeChangeNotifyPrivilege",
"SeRemoteShutdownPrivilege", "SeUndockPrivilege", "SeSyncAgentPrivilege",
"SeEnableDelegationPrivilege", "SeManageVolumePrivilege", "SeImpersonatePrivilege",
"SeCreateGlobalPrivilege", "SeTimeZonePrivilege", "SeCreateSymbolicLinkPrivilege",
"SeRelabelPrivilege", "SeIncreaseWorkingSetPrivilege", "SeTrustedCredManAccessPrivilege",
"SeDelegateSessionUserImpersonatePrivilege"
};
PrivInfo g_PrivInfos[35] = {
{ "SeCreateTokenPrivilege", "\xb4\xb4\xbd\xa8\xd2\xbb\xb8\xf6\xc1\xee\xc5\xc6\xb6\xd4\xcf\xf3" },
{ "SeAssignPrimaryTokenPrivilege", "\xcc\xe6\xbb\xbb\xd2\xbb\xb8\xf6\xbd\xf8\xb3\xcc\xbc\xb6\xc1\xee\xc5\xc6" },
{ "SeLockMemoryPrivilege", "\xcb\xf8\xb6\xa8\xc4\xda\xb4\xe6\xd2\xb3" },
{ "SeIncreaseQuotaPrivilege", "\xce\xaa\xbd\xf8\xb3\xcc\xb5\xf7\xd5\xfb\xc4\xda\xb4\xe6\xc5\xe4\xb6\xee" },
{ "SeMachineAccountPrivilege", "\xbd\xab\xb9\xa4\xd7\xf7\xd5\xbe\xcc\xed\xbc\xd3\xb5\xbd\xd3\xf2" },
{ "SeTcbPrivilege", "\xd2\xd4\xb2\xd9\xd7\xf7\xcf\xb5\xcd\xb3\xb7\xbd\xca\xbd\xd6\xb4\xd0\xd0" },
{ "SeSecurityPrivilege", "\xb9\xdc\xc0\xed\xc9\xf3\xba\xcb\xba\xcd\xb0\xb2\xc8\xab\xc8\xd5\xd6\xbe" },
{ "SeTakeOwnershipPrivilege", "\xc8\xa1\xb5\xc3\xce\xc4\xbc\xfe\xbb\xf2\xc6\xe4\xcb\xfb\xb6\xd4\xcf\xf3\xb5\xc4\xcb\xf9\xd3\xd0\xc8\xa8" },
{ "SeLoadDriverPrivilege", "\xbc\xd3\xd4\xd8\xba\xcd\xd0\xb6\xd4\xd8\xc9\xe8\xb1\xb8\xc7\xfd\xb6\xaf\xb3\xcc\xd0\xf2" },
{ "SeSystemProfilePrivilege", "\xc5\xe4\xd6\xc3\xce\xc4\xbc\xfe\xcf\xb5\xcd\xb3\xd0\xd4\xc4\xdc" },
{ "SeSystemtimePrivilege", "\xb8\xfc\xb8\xc4\xcf\xb5\xcd\xb3\xca\xb1\xbc\xe4" },
{ "SeProfileSingleProcessPrivilege", "\xc5\xe4\xd6\xc3\xce\xc4\xbc\xfe\xb5\xa5\xd2\xbb\xbd\xf8\xb3\xcc" },
{ "SeIncreaseBasePriorityPrivilege", "\xcc\xe1\xb8\xdf\xbc\xc6\xbb\xae\xd3\xc5\xcf\xc8\xbc\xb6" },
{ "SeCreatePagefilePrivilege", "\xb4\xb4\xbd\xa8\xd2\xbb\xb8\xf6\xd2\xb3\xc3\xe6\xce\xc4\xbc\xfe" },
{ "SeCreatePermanentPrivilege", "\xb4\xb4\xbd\xa8\xd3\xc0\xbe\xc3\xb9\xb2\xcf\xed\xb6\xd4\xcf\xf3" },
{ "SeBackupPrivilege", "\xb1\xb8\xb7\xdd\xce\xc4\xbc\xfe\xba\xcd\xc4\xbf\xc2\xbc" },
{ "SeRestorePrivilege", "\xbb\xb9\xd4\xad\xce\xc4\xbc\xfe\xba\xcd\xc4\xbf\xc2\xbc" },
{ "SeShutdownPrivilege", "\xb9\xd8\xb1\xd5\xcf\xb5\xcd\xb3" },
{ "SeDebugPrivilege", "\xb5\xf7\xca\xd4\xb3\xcc\xd0\xf2" },
{ "SeAuditPrivilege", "\xc9\xfa\xb3\xc9\xb0\xb2\xc8\xab\xc9\xf3\xba\xcb" },
{ "SeSystemEnvironmentPrivilege", "\xd0\xde\xb8\xc4\xb9\xcc\xbc\xfe\xbb\xb7\xbe\xb3\xd6\xb5" },
{ "SeChangeNotifyPrivilege", "\xc8\xc6\xb9\xfd\xb1\xe9\xc0\xfa\xbc\xec\xb2\xe9" },
{ "SeRemoteShutdownPrivilege", "\xb4\xd3\xd4\xb6\xb3\xcc\xcf\xb5\xcd\xb3\xc7\xbf\xd6\xc6\xb9\xd8\xbb\xfa" },
{ "SeUndockPrivilege", "\xb4\xd3\xc0\xa9\xd5\xb9\xce\xeb\xc9\xcf\xc8\xa1\xcf\xc2\xbc\xc6\xcb\xe3\xbb\xfa" },
{ "SeSyncAgentPrivilege", "\xcd\xac\xb2\xbd\xc4\xbf\xc2\xbc\xb7\xfe\xce\xf1\xca\xfd\xbe\xdd" },
{ "SeEnableDelegationPrivilege", "\xd0\xc5\xc8\xce\xbc\xc6\xcb\xe3\xbb\xfa\xba\xcd\xd3\xc3\xbb\xa7\xd5\xcb\xbb\xa7\xbf\xc9\xd2\xd4\xd6\xb4\xd0\xd0\xce\xaf\xc5\xc9" },
{ "SeManageVolumePrivilege", "\xd6\xb4\xd0\xd0\xbe\xed\xce\xac\xbb\xa4\xc8\xce\xce\xf1" },
{ "SeImpersonatePrivilege", "\xc9\xed\xb7\xdd\xd1\xe9\xd6\xa4\xba\xf3\xc4\xa3\xc4\xe2\xbf\xcd\xbb\xa7\xb6\xcb" },
{ "SeCreateGlobalPrivilege", "\xb4\xb4\xbd\xa8\xc8\xab\xbe\xd6\xb6\xd4\xcf\xf3" },
{ "SeTimeZonePrivilege", "\xb8\xfc\xb8\xc4\xca\xb1\xc7\xf8" },
{ "SeCreateSymbolicLinkPrivilege", "\xb4\xb4\xbd\xa8\xb7\xfb\xba\xc5\xc1\xb4\xbd\xd3" },
{ "SeRelabelPrivilege", "\xd0\xde\xb8\xc4\xd2\xbb\xb8\xf6\xb6\xd4\xcf\xf3\xb1\xea\xc7\xa9" },
{ "SeIncreaseWorkingSetPrivilege", "\xd4\xf6\xbc\xd3\xbd\xf8\xb3\xcc\xb9\xa4\xd7\xf7\xbc\xaf" },
{ "SeTrustedCredManAccessPrivilege", "\xd7\xf7\xce\xaa\xca\xdc\xd0\xc5\xc8\xce\xb5\xc4\xba\xf4\xbd\xd0\xb7\xbd\xb7\xc3\xce\xca\xc6\xbe\xbe\xdd\xb9\xdc\xc0\xed\xc6\xf7" },
{ "SeDelegateSessionUserImpersonatePrivilege", "\xbb\xf1\xc8\xa1\xcd\xac\xd2\xbb\xbb\xe1\xbb\xb0\xd6\xd0\xc1\xed\xd2\xbb\xb8\xf6\xd3\xc3\xbb\xa7\xb5\xc4\xc4\xa3\xc4\xe2\xc1\xee\xc5\xc6" }
};
// ==========================================
// 5. (Forward Declarations)
// ==========================================
void WriteToStdOut(const char* str);
void ShowUsage_Detailed(const char* prog);
void ShowUsage_Brief(const char* prog);
void UpdateThemeColors();
void InitializeThemeResources();
void CleanThemeResources();
void EnableImmersiveDarkMode(HWND hwnd, BOOL bEnable);
void ApplyThemeToControl(HWND hwnd);
void MakeButtonModern(HWND hBtn);
void DrawModernButton(LPDRAWITEMSTRUCT pdis);
void DrawCardFrame(HDC hdc, int x, int y, int w, int h, const char* title);
void UpdateGuiSummaries(HWND hEditGroups, HWND hEditPrivs);
void UpdateTokenPreview(HWND hwnd);
void ShowGroupEditor(HWND hParent);
void ShowPrivilegeEditor(HWND hParent);
void AddNewGroupItem(HWND hList);
void DeleteSelectedGroup(HWND hList);
void EditSelectedGroup(HWND hList);
BOOL IsSystemInDarkMode();
PSID ResolveIdentity(const char* identityStr);
void Log(LogLevel level, const char* format, ...);
void Log_ErrorCode(LogLevel level, DWORD ErrorCode);
BOOL IsUserAnAdmin();
LPSTR GetCurrentLpDesktop();
DWORD GetPidByNameA(LPCSTR processName);
PSID GetSidFromString(LPCSTR str);
PSID GetSidForAccountName(LPCSTR accountName);
PSID GetLogonSid();
PSID DupSid(PSID src);
BOOL EnablePrivilege(HANDLE hToken, LPCSTR privilegeName);
int ResolvePrivilegeId(const char* name);
long ResolveILlevel(const char* level);
Preset ResolvePreset(const char* arg);
int ResolveWindowCreateMode(const char* arg);
void TerminateParent(int parentPid, DWORD exitCode);
HANDLE GetLsassToken();
HANDLE CreateCustomToken(DWORD targetSessionId, PSID pUserSid, const std::vector<std::string>& extraGroups);
BOOL ExecuteSudoOperation(LPSTR cmdLine, LPSTR identityStr, LPSTR desktop, const std::string& workingDir, int windowMode, DWORD integrityLevel, BOOL bUIAccess, const std::vector<std::string>& extraGroups, const std::vector<int>& DisabledPrivilege, const std::vector<int>& RemovePrivilege, PROCESS_INFORMATION* pOutPI);
int GetDisplayWidth(const std::string& str);
std::string PadRight(const std::string& str, int width);
void GetILInfo(DWORD il, std::string& name, std::string& sid);
void SetRichEditDefaultColor(HWND hEdit, COLORREF color);
void UpdatePrivStatus(HWND hList, int iItem, const char* status);
void SetSelectedPrivs(HWND hList, const char* status);
void DeleteDisabledPrivileges(HANDLE& hToken);
DWORD GetActiveSessionID();
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK GroupEditorWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK PrivilegeEditorWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
// ==========================================
// 5.5.
// ==========================================
BOOL IsUserAnAdmin() {
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
BOOL b = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup);
if (b) {
if (!CheckTokenMembership(NULL, AdministratorsGroup, &b)) b = FALSE;
FreeSid(AdministratorsGroup);
}
return b;
}
void DeleteDisabledPrivileges(HANDLE& hToken) {
DWORD len = 0;
GetTokenInformation(hToken, TokenPrivileges, nullptr, 0, &len);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return;
PTOKEN_PRIVILEGES tp = (PTOKEN_PRIVILEGES)malloc(len);
if (!tp) return;
if (!GetTokenInformation(hToken, TokenPrivileges, tp, len, &len)) {
free(tp);
return;
}
std::vector<LUID_AND_ATTRIBUTES> toRemove;
toRemove.reserve(tp->PrivilegeCount);
for (DWORD i = 0; i < tp->PrivilegeCount; ++i) {
const auto& p = tp->Privileges[i];
if ((p.Attributes & SE_PRIVILEGE_ENABLED) == 0) {
toRemove.push_back({ p.Luid, 0 });
}
}
free(tp);
if (toRemove.empty()) return;
HANDLE hNewToken = nullptr;
if (!CreateRestrictedToken(hToken, 0, 0, nullptr, (DWORD)toRemove.size(), toRemove.data(), 0, nullptr, &hNewToken)) return;
CloseHandle(hToken);
hToken = hNewToken;
}
DWORD GetActiveSessionID() {
DWORD count = 0;
PWTS_SESSION_INFOA pSessionInfo = NULL;
DWORD activeSessionId = (DWORD)-1;
if (WTSEnumerateSessionsA(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &count)) {
for (DWORD i = 0; i < count; ++i) {
if (pSessionInfo[i].State == WTSActive) {
activeSessionId = pSessionInfo[i].SessionId;
break;
}
}
WTSFreeMemory(pSessionInfo);
}
if (activeSessionId == (DWORD)-1)
ProcessIdToSessionId(GetCurrentProcessId(), &activeSessionId);
return activeSessionId;
}
BOOL EnablePrivilege(HANDLE hToken, LPCSTR privilegeName) {
HANDLE hTokenToUse = hToken;
BOOL bMyToken = FALSE;
if (hTokenToUse == NULL) {
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hTokenToUse))
return FALSE;
bMyToken = TRUE;
}
LUID luid;
if (!LookupPrivilegeValueA(NULL, privilegeName, &luid)) {
if (bMyToken) CloseHandle(hTokenToUse);
return FALSE;
}
TOKEN_PRIVILEGES tp = { 1, {{ luid, SE_PRIVILEGE_ENABLED }} };
BOOL r = AdjustTokenPrivileges(hTokenToUse, FALSE, &tp, sizeof(tp), NULL, NULL);
DWORD err = GetLastError();
if (bMyToken) CloseHandle(hTokenToUse);
if (r && (err == ERROR_SUCCESS)) return TRUE;
else {
Log(LOG_ERROR, "\xc6\xf4\xd3\xc3\xcc\xd8\xc8\xa8 %s \xca\xa7\xb0\xdc\xa3\xac\xb4\xed\xce\xf3\xb4\xfa\xc2\xeb\xa3\xba%lu", privilegeName, err);
return FALSE;
}
}
void ShowUsage_Brief(const char* prog) {
printf("\x0a");
printf("Token-Generator v1.0.0 (\xb8\xdf\xbc\xb6\xb0\xb2\xc8\xab\xc1\xee\xc5\xc6\xb4\xdb\xb8\xc4\xd3\xeb\xbd\xf8\xb3\xcc\xc6\xf4\xb6\xaf\xc6\xf7)\x0a");
printf("====================================================\x0a\x0a");
printf("\xd3\xc3\xb7\xa8: %s [\xd1\xa1\xcf\xee] [\xd6\xb4\xd0\xd0\xc3\xfc\xc1\xee (\xc4\xac\xc8\xcf: cmd.exe)]\x0a\x0a", prog);
printf("\xd4\xa4\xc9\xe8\xd1\xa1\xcf\xee:\x0a");
printf(" -Use:<\xc3\xfb\xb3\xc6> \xd1\xa1\xd4\xf1\xb0\xb2\xc8\xab\xd6\xf7\xcc\xe5\xd4\xa4\xc9\xe8 (\xd6\xa7\xb3\xd6 S/S+, A/A+, TI/TI+, LS/LS+, NS/NS+, DWM/DWM+)\x0a");
printf("\xc9\xed\xb7\xdd\xd1\xa1\xcf\xee:\x0a");
printf(" -U:<\xd3\xc3\xbb\xa7\xc3\xfb|SID> \xca\xd6\xb6\xaf\xd6\xb8\xb6\xa8\xd4\xcb\xd0\xd0\xc4\xbf\xb1\xea\xd3\xc3\xbb\xa7\xc9\xcf\xcf\xc2\xce\xc4\xc9\xed\xb7\xdd (\xc4\xac\xc8\xcf: System)\x0a");
printf("\xc1\xee\xc5\xc6\xb4\xdb\xb8\xc4\xd1\xa1\xcf\xee:\x0a");
printf(" -G:<\xb8\xbd\xbc\xd3\xd3\xc3\xbb\xa7\xd7\xe9> \xd7\xa2\xc8\xeb\xb6\xee\xcd\xe2\xb5\xc4\xb0\xb2\xc8\xab\xd7\xe9\xbb\xf2 SID \xb5\xbd\xc9\xfa\xb3\xc9\xb5\xc4\xc1\xee\xc5\xc6\xd6\xd0\x0a");
printf(" --UIAccess \xce\xaa\xc9\xfa\xb3\xc9\xb5\xc4\xc1\xee\xc5\xc6\xbf\xaa\xc6\xf4 UI Access \xcc\xd8\xc8\xa8\xb1\xea\xd6\xbe\x0a");
printf(" -IL:<\xcd\xea\xd5\xfb\xd0\xd4\xbc\xb6\xb1\xf0> \xc9\xe8\xd6\xc3\xc1\xee\xc5\xc6\xc7\xbf\xd6\xc6\xcd\xea\xd5\xfb\xd0\xd4 (Untrusted, Low, Medium, Medium+, High, System)\x0a");
printf("\xcc\xd8\xc8\xa8\xd1\xa1\xcf\xee:\x0a");
printf(" -Remove:<\xcc\xd8\xc8\xa8> \xb4\xd3\xc9\xfa\xb3\xc9\xb5\xc4\xc1\xee\xc5\xc6\xd6\xd0\xb3\xb9\xb5\xd7\xc7\xbf\xd6\xc6\xd2\xc6\xb3\xfd\xd6\xb8\xb6\xa8\xcc\xd8\xc8\xa8\x0a");
printf(" -Disabled:<\xcc\xd8\xc8\xa8> \xc7\xbf\xd6\xc6\xbd\xab\xd6\xb8\xb6\xa8\xcc\xd8\xc8\xa8\xd6\xc3\xce\xaa\xbd\xfb\xd3\xc3\xd7\xb4\xcc\xac\x0a");
printf("\xd4\xcb\xd0\xd0\xbf\xd8\xd6\xc6\xd1\xa1\xcf\xee:\x0a");
printf(" --Debug \xbf\xaa\xc6\xf4\xb5\xf7\xca\xd4\xc8\xd5\xd6\xbe\xa3\xac\xca\xe4\xb3\xf6\xcf\xea\xcf\xb8\xb5\xc4\xc1\xee\xc5\xc6\xce\xb1\xd4\xec\xd0\xd0\xce\xaa\x0a");
printf(" -GUI \xc6\xf4\xb6\xaf\xbe\xab\xd0\xc4\xc9\xe8\xbc\xc6\xb5\xc4 Windows 11 Fluent \xb7\xe7\xb8\xf1\xb8\xdf\xbc\xb6\xbd\xe7\xc3\xe6\x0a");
printf(" -d:<\xd7\xc0\xc3\xe6> \xd6\xb8\xb6\xa8\xc6\xf4\xb6\xaf\xb5\xc4 Desktop \xb0\xb2\xc8\xab\xd7\xc0\xc3\xe6 (\xc4\xac\xc8\xcf: \xb5\xb1\xc7\xb0\xbb\xee\xb6\xaf\xd7\xc0\xc3\xe6)\x0a");
printf(" -C:<\xc2\xb7\xbe\xb6> \xd6\xb8\xb6\xa8\xbd\xf8\xb3\xcc\xc6\xf4\xb6\xaf\xb5\xc4\xb3\xf5\xca\xbc\xb9\xa4\xd7\xf7\xc4\xbf\xc2\xbc\x0a");
printf(" -M:<\xcf\xd4\xca\xbe\xc4\xa3\xca\xbd> \xc9\xe8\xd6\xc3\xc6\xf4\xb6\xaf\xb4\xb0\xbf\xda\xd7\xb4\xcc\xac: Inline (\xc4\xda\xc1\xaa\xb5\xb1\xc7\xb0\xbf\xd8\xd6\xc6\xcc\xa8), Hide (\xd2\xfe\xb2\xd8), Max (\xd7\xee\xb4\xf3\xbb\xaf), Min (\xd7\xee\xd0\xa1\xbb\xaf)\x0a");
}
void ShowUsage_Detailed(const char* prog) {
printf("\x0a");
printf("Token-Generator v1.0.0 (\xb8\xdf\xbc\xb6\xb0\xb2\xc8\xab\xc1\xee\xc5\xc6\xb4\xdb\xb8\xc4\xd3\xeb\xbd\xf8\xb3\xcc\xc6\xf4\xb6\xaf\xc6\xf7)\x0a");
printf("====================================================\x0a\x0a");
printf("\xd3\xc3\xb7\xa8: %s [\xd1\xa1\xcf\xee] [\xd6\xb4\xd0\xd0\xc3\xfc\xc1\xee (\xc4\xac\xc8\xcf: cmd.exe)]\x0a\x0a", prog);
printf("\xd4\xa4\xc9\xe8\xb2\xce\xca\xfd\xd1\xa1\xd4\xf1:\x0a");
printf(" -Use:<\xc3\xfb\xb3\xc6> \xca\xb9\xd3\xc3\xd2\xd4\xcf\xc2\xbe\xad\xb5\xe4\xb5\xc4\xd4\xa4\xc9\xe8\xc6\xbe\xd6\xa4\xd6\xf7\xcc\xe5\xbb\xb7\xbe\xb3\xd6\xae\xd2\xbb\xa3\xba\x0a");
printf(" S, System, S+, System+ (NT AUTHORITY\\SYSTEM)\x0a");
printf(" A, Admin, A+, Admin+ (BUILTIN\\Administrators)\x0a");
printf(" TI, TrustedInstaller, TI+, TrustedInstaller+ (NT SERVICE\\TrustedInstaller)\x0a");
printf(" LS, LOCAL SERVICE, LS+, LOCAL SERVICE+ (NT AUTHORITY\\LOCAL SERVICE)\x0a");
printf(" NS, NETWORK SERVICE, NS+, NETWORK SERVICE+ (NT AUTHORITY\\NETWORK SERVICE)\x0a");
printf(" DWM, DWM+ (Window Manager\\DWM-1)\x0a");
printf("\xc9\xed\xb7\xdd\xd1\xa1\xcf\xee:\x0a");
printf(" -U:<\xd3\xc3\xbb\xa7\xc3\xfb|SID> \xca\xd6\xb6\xaf\xd6\xb8\xb6\xa8\xd4\xcb\xd0\xd0\xc4\xbf\xb1\xea\xd3\xc3\xbb\xa7\xc9\xcf\xcf\xc2\xce\xc4\xc9\xed\xb7\xdd\x0a");
printf("\xc1\xee\xc5\xc6\xb4\xdb\xb8\xc4\xd1\xa1\xcf\xee:\x0a");
printf(" -G:<\xb8\xbd\xbc\xd3\xd7\xe9/SID> \xd7\xa2\xc8\xeb\xb6\xee\xcd\xe2\xb0\xb2\xc8\xab\xd7\xe9 (\xba\xac\xbf\xd5\xb8\xf1\xb5\xc4\xd7\xe9\xc3\xfb\xb1\xd8\xd0\xeb\xbc\xd3\xd2\xfd\xba\xc5)\x0a");
printf(" --UIAccess \xce\xaa\xc1\xee\xc5\xc6\xbf\xaa\xc6\xf4 UI Access \xb1\xea\xd6\xbe\x0a");
printf(" -IL:<\xcd\xea\xd5\xfb\xd0\xd4\xbc\xb6\xb1\xf0> \xd0\xde\xb8\xc4\xc1\xee\xc5\xc6\xc7\xbf\xd6\xc6\xcd\xea\xd5\xfb\xd0\xd4\xbc\xb6\xb1\xf0:\x0a");
printf(" U, Untrusted \xb7\xc7\xd0\xc5\xc8\xce\xbc\xb6 (SECURITY_MANDATORY_UNTRUSTED_RID)\x0a");
printf(" L, Low \xb5\xcd\xcd\xea\xd5\xfb\xd0\xd4\xbc\xb6 (SECURITY_MANDATORY_LOW_RID)\x0a");
printf(" M, Medium \xd6\xd0\xb5\xc8\xcd\xea\xd5\xfb\xd0\xd4\xbc\xb6 (SECURITY_MANDATORY_MEDIUM_RID)\x0a");
printf(" M+, Medium+ \xd6\xd0\xb5\xc8\xd4\xf6\xc7\xbf\xbc\xb6 (SECURITY_MANDATORY_MEDIUM_PLUS_RID)\x0a");
printf(" H, High \xb8\xdf\xcd\xea\xd5\xfb\xd0\xd4\xbc\xb6 (SECURITY_MANDATORY_HIGH_RID)\x0a");
printf(" S, System \xcf\xb5\xcd\xb3\xcd\xea\xd5\xfb\xd0\xd4\xbc\xb6 (SECURITY_MANDATORY_SYSTEM_RID)\x0a");
printf("\xd4\xcb\xd0\xd0\xbf\xd8\xd6\xc6\xd1\xa1\xcf\xee:\x0a");
printf(" --Debug \xbf\xaa\xc6\xf4\xb5\xf7\xca\xd4\xc8\xd5\xd6\xbe\xa3\xac\xca\xe4\xb3\xf6\xb5\xd7\xb2\xe3 NtCreateToken \xd6\xb4\xd0\xd0\xc1\xf7\xb3\xcc\x0a");
printf(" -GUI \xc6\xf4\xb6\xaf\xb8\xfa\xcb\xe6\xcf\xb5\xcd\xb3\xd6\xf7\xcc\xe2\xb5\xc4\xcf\xd6\xb4\xfa GUI \xb9\xdc\xc0\xed\xc6\xf7\x0a");
printf(" -d:<\xd7\xc0\xc3\xe6> Winsta\\Desktop \xc2\xb7\xbe\xb6\x0a");
printf(" -C:<\xc2\xb7\xbe\xb6> \xd0\xde\xb8\xc4\xbd\xf8\xb3\xcc\xb9\xa4\xd7\xf7\xc4\xbf\xc2\xbc\xc2\xb7\xbe\xb6\x0a");
printf(" -M:<\xcf\xd4\xca\xbe\xc4\xa3\xca\xbd> \xc6\xf4\xb6\xaf\xb4\xb0\xbf\xda\xd7\xb4\xcc\xac: I (\xc4\xda\xc1\xaa), H (\xd2\xfe\xb2\xd8), Max (\xd7\xee\xb4\xf3\xbb\xaf), Min (\xd7\xee\xd0\xa1\xbb\xaf)\x0a\x0a");
printf("\xcc\xd8\xc8\xa8\xd1\xa1\xcf\xee:\x0a");
printf(" -Remove:<\xcc\xd8\xc8\xa8> \xb3\xb9\xb5\xd7\xc7\xbf\xd6\xc6\xd2\xc6\xb3\xfd\xd6\xb8\xb6\xa8\xcc\xd8\xc8\xa8\x0a");
printf(" -Disabled:<\xcc\xd8\xc8\xa8> \xc7\xbf\xd6\xc6\xd6\xc3\xd6\xb8\xb6\xa8\xcc\xd8\xc8\xa8\xd7\xb4\xcc\xac\xce\xaa\xbd\xfb\xd3\xc3\x0a\x0a");
printf("\xbf\xc9\xd3\xc3\xb5\xc4\xcc\xd8\xc8\xa8\xd0\xf2\xba\xc5\xd3\xeb\xc3\xfb\xb3\xc6\xb6\xd4\xd5\xd5\xb1\xed:\x0a");
printf(" SeCreateTokenPrivilege:1\x0a SeAssignPrimaryTokenPrivilege:2\x0a SeLockMemoryPrivilege:3\x0a");
printf(" SeIncreaseQuotaPrivilege:4\x0a SeMachineAccountPrivilege:5\x0a SeTcbPrivilege:6\x0a");
printf(" SeSecurityPrivilege:7\x0a SeTakeOwnershipPrivilege:8\x0a SeLoadDriverPrivilege:9\x0a");
printf(" SeSystemProfilePrivilege:10\x0a SeSystemtimePrivilege:11\x0a SeProfileSingleProcessPrivilege:12\x0a");
printf(" SeIncreaseBasePriorityPrivilege:13\x0a SeCreatePagefilePrivilege:14\x0a SeCreatePermanentPrivilege:15\x0a");
printf(" SeBackupPrivilege:16\x0a SeRestorePrivilege:17\x0a SeShutdownPrivilege:18\x0a");
printf(" SeDebugPrivilege:19\x0a SeAuditPrivilege:20\x0a SeSystemEnvironmentPrivilege:21\x0a");
printf(" SeChangeNotifyPrivilege:22\x0a SeRemoteShutdownPrivilege:23\x0a SeUndockPrivilege:24\x0a");
printf(" SeSyncAgentPrivilege:25\x0a SeEnableDelegationPrivilege:26\x0a SeManageVolumePrivilege:27\x0a");
printf(" SeImpersonatePrivilege:28\x0a SeCreateGlobalPrivilege:29\x0a SeTimeZonePrivilege:30\x0a");
printf(" SeCreateSymbolicLinkPrivilege:31\x0a SeRelabelPrivilege:32\x0a SeIncreaseWorkingSetPrivilege:33\x0a");
printf(" SeTrustedCredManAccessPrivilege:34\x0a SeDelegateSessionUserImpersonatePrivilege:35\x0a\x0a");
printf("\xca\xb9\xd3\xc3\xca\xbe\xc0\xfd:\x0a");
printf(" %s -Use:TI cmd.exe\x0a", prog);
printf(" %s -GUI\x0a", prog);
printf(" %s -IL:Medium cmd.exe\x0a", prog);
printf(" %s -G:\"CONSOLE LOGON\" cmd.exe\x0a", prog);
}
// ==========================================
// 6.
// ==========================================
void WriteToStdOut(const char* str) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE || hOut == NULL) return;
DWORD written;
if (!WriteConsoleA(hOut, str, strlen(str), &written, NULL)) {
WriteFile(hOut, str, strlen(str), &written, NULL);
}
}
void Log(LogLevel level, const char* format, ...) {
if (g_hLogEdit && IsWindow(g_hLogEdit)) {
if (!g_bDebug && level == LOG_DEBUG) return;
std::string msg;
COLORREF color;
if (g_bDarkMode) {
color = RGB(220, 220, 220);
switch (level) {
case LOG_ERROR: msg = "[!] "; color = RGB(255, 65, 54); break; //
case LOG_WARN: msg = "[-] "; color = RGB(255, 133, 27); break; //
case LOG_INFO: msg = "[*] "; color = RGB(0, 116, 217); break; //
case LOG_SUCCESS: msg = "[+] "; color = RGB(46, 204, 64); break; //
case LOG_DEBUG: msg = "[D] "; color = RGB(170, 170, 170); break; //
}
} else {
color = RGB(32, 32, 32);
switch (level) {
case LOG_ERROR: msg = "[!] "; color = RGB(192, 0, 0); break; //
case LOG_WARN: msg = "[-] "; color = RGB(180, 80, 0); break; //
case LOG_INFO: msg = "[*] "; color = RGB(0, 90, 158); break; //
case LOG_SUCCESS: msg = "[+] "; color = RGB(0, 128, 0); break; //
case LOG_DEBUG: msg = "[D] "; color = RGB(110, 110, 110); break; //
}
}
char buffer[2048];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
msg += buffer;
msg += "\x0d\x0a";
int len = GetWindowTextLengthA(g_hLogEdit);
SendMessageA(g_hLogEdit, EM_SETSEL, (WPARAM)len, (LPARAM)len);
CHARFORMAT2 cf;
ZeroMemory(&cf, sizeof(cf));
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR;
cf.crTextColor = color;
cf.dwEffects = 0;
SendMessage(g_hLogEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
SendMessage(g_hLogEdit, EM_REPLACESEL, 0, (LPARAM)msg.c_str());
SendMessage(g_hLogEdit, WM_VSCROLL, SB_BOTTOM, 0);
}
else {
if (!g_bDebug && level != LOG_ERROR && level != LOG_WARN) return;
const char* prefix = "";
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut != INVALID_HANDLE_VALUE && hOut != NULL) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
WORD wOldColor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
if (GetConsoleScreenBufferInfo(hOut, &csbi)) {
wOldColor = csbi.wAttributes;
}
WORD wColor = wOldColor;
switch (level) {
case LOG_ERROR: prefix = "[!] "; wColor = FOREGROUND_RED | FOREGROUND_INTENSITY; break;
case LOG_WARN: prefix = "[-] "; wColor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case LOG_INFO: prefix = "[*] "; wColor = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case LOG_SUCCESS: prefix = "[+] "; wColor = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case LOG_DEBUG: prefix = "[D] "; wColor = FOREGROUND_INTENSITY; break;
}
SetConsoleTextAttribute(hOut, wColor);
WriteToStdOut(prefix);
char buffer[4096];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
WriteToStdOut(buffer);
WriteToStdOut("\x0d\x0a");
SetConsoleTextAttribute(hOut, wOldColor);
}
}
}
void Log_ErrorCode(LogLevel level, DWORD ErrorCode) {
CHAR MessageBuf[2048] = {};
if (FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
ErrorCode,
0,
MessageBuf,
2048,
NULL
)) {
Log(level, "\xb4\xed\xce\xf3\xcf\xb5\xcd\xb3\xc3\xe8\xca\xf6\xa3\xba%s", MessageBuf);
}
}
LPSTR GetCurrentLpDesktop() {
HDESK hDesk = GetThreadDesktop(GetCurrentThreadId());
if (!hDesk) return NULL;
HWINSTA hWinsta = GetProcessWindowStation();
if (!hWinsta) return NULL;
char desk[256], winsta[256];
DWORD needed;
if (!GetUserObjectInformationA(hDesk, UOI_NAME, desk, sizeof(desk), &needed)) return NULL;
if (!GetUserObjectInformationA(hWinsta, UOI_NAME, winsta, sizeof(winsta), &needed)) return NULL;
size_t len = strlen(winsta) + strlen(desk) + 2;
LPSTR p = (LPSTR)LocalAlloc(LMEM_FIXED, len);
if (!p) return NULL;
_snprintf(p, len, "%s\\%s", winsta, desk);
return p;
}
DWORD GetPidByNameA(LPCSTR processName) {
DWORD pid = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE) {
PROCESSENTRY32 pe = {};
pe.dwSize = sizeof(pe);
if (Process32First(hSnapshot, &pe)) {
do {
if (_stricmp(pe.szExeFile, processName) == 0) {
pid = pe.th32ProcessID;
break;
}
} while (Process32Next(hSnapshot, &pe));
}
CloseHandle(hSnapshot);
}
return pid;
}
PSID DupSid(PSID src) {
if (!src) return NULL;
DWORD len = GetLengthSid(src);
PSID dst = (PSID)HeapAlloc(GetProcessHeap(), 0, len);
if (dst) CopySid(len, dst, src);
return dst;
}
PSID GetSidFromString(LPCSTR str) {
PSID sid = NULL;
if (ConvertStringSidToSidA(str, &sid)) {
PSID dup = DupSid(sid);
LocalFree(sid);
return dup;
}
return NULL;
}
PSID GetSidForAccountName(LPCSTR accountName) {
DWORD sidLen = 0, domainLen = 0;
SID_NAME_USE use;
LookupAccountNameA(NULL, accountName, NULL, &sidLen, NULL, &domainLen, &use);
if (sidLen > 0) {
PSID sid = (PSID)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidLen);
char* domain = (char*)HeapAlloc(GetProcessHeap(), 0, domainLen);
if (sid && domain && LookupAccountNameA(NULL, accountName, sid, &sidLen, domain, &domainLen, &use)) {
HeapFree(GetProcessHeap(), 0, domain);
return sid;
}
if (domain) HeapFree(GetProcessHeap(), 0, domain);
if (sid) HeapFree(GetProcessHeap(), 0, sid);
}
return NULL;
}
PSID ResolveIdentity(const char* identityStr) {
if (_stricmp(identityStr, "S") == 0 || _stricmp(identityStr, "System") == 0) {
Log(LOG_DEBUG, "\xd3\xb3\xc9\xe4\xc9\xed\xb7\xdd\xc9\xcf\xcf\xc2\xce\xc4: %s -> SYSTEM", identityStr);
return GetSidFromString("S-1-5-18");
}
if (_stricmp(identityStr, "A") == 0 || _stricmp(identityStr, "Admin") == 0) {
Log(LOG_DEBUG, "\xd3\xb3\xc9\xe4\xc9\xed\xb7\xdd\xc9\xcf\xcf\xc2\xce\xc4: %s -> Administrators", identityStr);
return GetSidFromString("S-1-5-32-544");
}
if (_stricmp(identityStr, "TI") == 0 || _stricmp(identityStr, "TrustedInstaller") == 0) {
Log(LOG_DEBUG, "\xd3\xb3\xc9\xe4\xc9\xed\xb7\xdd\xc9\xcf\xcf\xc2\xce\xc4: %s -> TrustedInstaller", identityStr);
return GetSidForAccountName("NT SERVICE\\TrustedInstaller");
}
if (_stricmp(identityStr, "LS") == 0 || _stricmp(identityStr, "LOCAL SERVICE") == 0) {
Log(LOG_DEBUG, "\xd3\xb3\xc9\xe4\xc9\xed\xb7\xdd\xc9\xcf\xcf\xc2\xce\xc4: %s -> LOCAL SERVICE", identityStr);
return GetSidFromString("S-1-5-19");
}
if (_stricmp(identityStr, "NS") == 0 || _stricmp(identityStr, "NETWORK SERVICE") == 0) {
Log(LOG_DEBUG, "\xd3\xb3\xc9\xe4\xc9\xed\xb7\xdd\xc9\xcf\xcf\xc2\xce\xc4: %s -> NETWORK SERVICE", identityStr);
return GetSidFromString("S-1-5-20");
}
if (_stricmp(identityStr, "DWM") == 0) {
Log(LOG_DEBUG, "\xd3\xb3\xc9\xe4\xc9\xed\xb7\xdd\xc9\xcf\xcf\xc2\xce\xc4: %s -> Window Manager\\DWM-1", identityStr);
return GetSidForAccountName("Window Manager\\DWM-1");
}
PSID sid = GetSidFromString(identityStr);
if (sid) return sid;
return GetSidForAccountName(identityStr);
}
long ResolveILlevel(const char* level) {
if (_stricmp(level, "untrusted") == 0 || _stricmp(level, "u") == 0) return SECURITY_MANDATORY_UNTRUSTED_RID;
if (_stricmp(level, "low") == 0 || _stricmp(level, "l") == 0) return SECURITY_MANDATORY_LOW_RID;
if (_stricmp(level, "medium") == 0 || _stricmp(level, "m") == 0) return SECURITY_MANDATORY_MEDIUM_RID;
if (_stricmp(level, "medium+") == 0 || _stricmp(level, "m+") == 0) return SECURITY_MANDATORY_MEDIUM_PLUS_RID;
if (_stricmp(level, "high") == 0 || _stricmp(level, "h") == 0) return SECURITY_MANDATORY_HIGH_RID;
if (_stricmp(level, "system") == 0 || _stricmp(level, "s") == 0) return SECURITY_MANDATORY_SYSTEM_RID;
return -1;
}
Preset ResolvePreset(const char* arg) {
if (_stricmp(arg, "Normal") == 0) {
return { (LPSTR)"NT AUTHORITY\\SYSTEM", {}, {} };
}
if (_stricmp(arg, "A") == 0 || _stricmp(arg, "Admin") == 0) {
return { (LPSTR)"BUILTIN\\Administrators", {}, {} };
}
if (_stricmp(arg, "A+") == 0 || _stricmp(arg, "Admin+") == 0) {
return { (LPSTR)"BUILTIN\\Administrators", {}, { 1, 3, 5, 6, 15, 20, 25, 26, 32, 34 } };
}
if (_stricmp(arg, "S") == 0 || _stricmp(arg, "System") == 0) {
return { (LPSTR)"NT AUTHORITY\\SYSTEM", {}, {} };
}
if (_stricmp(arg, "S+") == 0 || _stricmp(arg, "System+") == 0) {
return { (LPSTR)"NT AUTHORITY\\SYSTEM", {}, { 1, 3, 5, 10, 11, 14, 23, 25, 26, 30, 31, 32, 33, 35 } };
}
if (_stricmp(arg, "TI") == 0 || _stricmp(arg, "TrustedInstaller") == 0) {
return { (LPSTR)"NT AUTHORITY\\SYSTEM", { "NT AUTHORITY\\SERVICE", "NT SERVICE\\TrustedInstaller" }, {} };
}
if (_stricmp(arg, "TI+") == 0 || _stricmp(arg, "TrustedInstaller+") == 0) {
return { (LPSTR)"NT AUTHORITY\\SYSTEM", { "NT AUTHORITY\\SERVICE", "NT SERVICE\\TrustedInstaller" }, { 1, 5, 23, 25, 26, 32, 34 } };
}
if (_stricmp(arg, "NS") == 0 || _stricmp(arg, "NETWORK SERVICE") == 0) {
return { (LPSTR)"NT AUTHORITY\\NETWORK SERVICE", { "NT AUTHORITY\\SERVICE" }, {} };
}
if (_stricmp(arg, "NS+") == 0 || _stricmp(arg, "NETWORK SERVICE+") == 0) {
return { (LPSTR)"NT AUTHORITY\\NETWORK SERVICE", { "NT AUTHORITY\\SERVICE" },
{ 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 21, 23, 25, 26, 27, 31, 32, 34, 35 } };
}
if (_stricmp(arg, "LS") == 0 || _stricmp(arg, "LOCAL SERVICE") == 0) {
return { (LPSTR)"NT AUTHORITY\\LOCAL SERVICE", { "NT AUTHORITY\\SERVICE" }, {} };
}
if (_stricmp(arg, "LS+") == 0 || _stricmp(arg, "LOCAL SERVICE+") == 0) {
return { (LPSTR)"NT AUTHORITY\\LOCAL SERVICE", { "NT AUTHORITY\\SERVICE" },
{ 1, 3, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 19, 21, 23, 25, 26, 27, 31, 32, 34, 35 } };
}
if (_stricmp(arg, "DWM") == 0) {
return { (LPSTR)"Window Manager\\DWM-1", { "S-1-5-4", "S-1-5-19", "S-1-5-90-0" }, {} };
}
if (_stricmp(arg, "DWM+") == 0) {
return { (LPSTR)"Window Manager\\DWM-1",
{ "NT AUTHORITY\\INTERACTIVE", "NT AUTHORITY\\LOCAL SERVICE", "Window Manager\\Window Manager Group" },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 34, 35 } };
}
return {NULL, {}, {}};
}
int ResolvePrivilegeId(const char* name) {
if (_stricmp(name, "SeCreateTokenPrivilege") == 0 || strcmp(name, "1") == 0) return 1;
if (_stricmp(name, "SeAssignPrimaryTokenPrivilege") == 0 || strcmp(name, "2") == 0) return 2;
if (_stricmp(name, "SeLockMemoryPrivilege") == 0 || strcmp(name, "3") == 0) return 3;
if (_stricmp(name, "SeIncreaseQuotaPrivilege") == 0 || strcmp(name, "4") == 0) return 4;
if (_stricmp(name, "SeMachineAccountPrivilege") == 0 || strcmp(name, "5") == 0) return 5;
if (_stricmp(name, "SeTcbPrivilege") == 0 || strcmp(name, "6") == 0) return 6;
if (_stricmp(name, "SeSecurityPrivilege") == 0 || strcmp(name, "7") == 0) return 7;
if (_stricmp(name, "SeTakeOwnershipPrivilege") == 0 || strcmp(name, "8") == 0) return 8;
if (_stricmp(name, "SeLoadDriverPrivilege") == 0 || strcmp(name, "9") == 0) return 9;
if (_stricmp(name, "SeSystemProfilePrivilege") == 0 || strcmp(name, "10") == 0) return 10;
if (_stricmp(name, "SeSystemtimePrivilege") == 0 || strcmp(name, "11") == 0) return 11;
if (_stricmp(name, "SeProfileSingleProcessPrivilege") == 0 || strcmp(name, "12") == 0) return 12;
if (_stricmp(name, "SeIncreaseBasePriorityPrivilege") == 0 || strcmp(name, "13") == 0) return 13;
if (_stricmp(name, "SeCreatePagefilePrivilege") == 0 || strcmp(name, "14") == 0) return 14;
if (_stricmp(name, "SeCreatePermanentPrivilege") == 0 || strcmp(name, "15") == 0) return 15;
if (_stricmp(name, "SeBackupPrivilege") == 0 || strcmp(name, "16") == 0) return 16;
if (_stricmp(name, "SeRestorePrivilege") == 0 || strcmp(name, "17") == 0) return 17;
if (_stricmp(name, "SeShutdownPrivilege") == 0 || strcmp(name, "18") == 0) return 18;
if (_stricmp(name, "SeDebugPrivilege") == 0 || strcmp(name, "19") == 0) return 19;
if (_stricmp(name, "SeAuditPrivilege") == 0 || strcmp(name, "20") == 0) return 20;
if (_stricmp(name, "SeSystemEnvironmentPrivilege") == 0 || strcmp(name, "21") == 0) return 21;
if (_stricmp(name, "SeChangeNotifyPrivilege") == 0 || strcmp(name, "22") == 0) return 22;
if (_stricmp(name, "SeRemoteShutdownPrivilege") == 0 || strcmp(name, "23") == 0) return 23;
if (_stricmp(name, "SeUndockPrivilege") == 0 || strcmp(name, "24") == 0) return 24;
if (_stricmp(name, "SeSyncAgentPrivilege") == 0 || strcmp(name, "25") == 0) return 25;
if (_stricmp(name, "SeEnableDelegationPrivilege") == 0 || strcmp(name, "26") == 0) return 26;
if (_stricmp(name, "SeManageVolumePrivilege") == 0 || strcmp(name, "27") == 0) return 27;
if (_stricmp(name, "SeImpersonatePrivilege") == 0 || strcmp(name, "28") == 0) return 28;
if (_stricmp(name, "SeCreateGlobalPrivilege") == 0 || strcmp(name, "29") == 0) return 29;
if (_stricmp(name, "SeTimeZonePrivilege") == 0 || strcmp(name, "30") == 0) return 30;
if (_stricmp(name, "SeCreateSymbolicLinkPrivilege") == 0 || strcmp(name, "31") == 0) return 31;
if (_stricmp(name, "SeRelabelPrivilege") == 0 || strcmp(name, "32") == 0) return 32;
if (_stricmp(name, "SeIncreaseWorkingSetPrivilege") == 0 || strcmp(name, "33") == 0) return 33;
if (_stricmp(name, "SeTrustedCredManAccessPrivilege") == 0 || strcmp(name, "34") == 0) return 34;
if (_stricmp(name, "SeDelegateSessionUserImpersonatePrivilege") == 0 || strcmp(name, "35") == 0) return 35;
return -1;
}
int ResolveWindowCreateMode(const char* arg) {
if (_stricmp(arg, "I") == 0 || _stricmp(arg, "Inline") == 0) return -1;
if (_stricmp(arg, "H") == 0 || _stricmp(arg, "Hide") == 0) return SW_HIDE;
if (_stricmp(arg, "Max") == 0 || _stricmp(arg, "Maximize") == 0) return SW_SHOWMAXIMIZED;
if (_stricmp(arg, "Min") == 0 || _stricmp(arg, "Minimize") == 0) return SW_SHOWMINIMIZED;
return -2; //
}
PSID GetLogonSid() {
HANDLE h;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h)) return NULL;
DWORD l = 0;
GetTokenInformation(h, TokenGroups, 0, 0, &l);
std::vector<BYTE> b(l);
GetTokenInformation(h, TokenGroups, b.data(), l, &l);
CloseHandle(h);
PTOKEN_GROUPS g = (PTOKEN_GROUPS)b.data();
for (DWORD i = 0; i < g->GroupCount; i++) {
if ((g->Groups[i].Attributes & SE_GROUP_LOGON_ID) == SE_GROUP_LOGON_ID) {
return DupSid(g->Groups[i].Sid);
}
}
return NULL;
}
void TerminateParent(int parentPid, DWORD exitCode) {
if (parentPid > 0) {
HANDLE hParent = OpenProcess(PROCESS_TERMINATE, FALSE, (DWORD)parentPid);
if (hParent) {
TerminateProcess(hParent, exitCode);
CloseHandle(hParent);
}
}
}
// ==========================================
// 7.
// ==========================================
HANDLE GetLsassToken() {
EnablePrivilege(NULL, "SeDebugPrivilege");
DWORD lsassPid = GetPidByNameA("lsass.exe");
if (lsassPid == 0) {
Log(LOG_ERROR, "\xce\xde\xb7\xa8\xb6\xa8\xce\xbb lsass.exe \xcf\xb5\xcd\xb3\xb7\xfe\xce\xf1\xbd\xf8\xb3\xcc");
return NULL;
}
Log(LOG_DEBUG, "\xd5\xfd\xd4\xda\xb4\xf2\xbf\xaa LSASS \xbd\xf8\xb3\xcc (PID: %d)...", lsassPid);
HANDLE hLsassProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, lsassPid);
if (!hLsassProc) {
Log(LOG_ERROR, "\xb4\xf2\xbf\xaa LSASS \xbd\xf8\xb3\xcc\xca\xa7\xb0\xdc\xa3\xac\xbe\xdc\xbe\xf8\xb7\xc3\xce\xca");
return NULL;
}
HANDLE hLsassToken = NULL;
if (!OpenProcessToken(hLsassProc, TOKEN_DUPLICATE | TOKEN_QUERY | TOKEN_IMPERSONATE, &hLsassToken)) {
Log(LOG_ERROR, "\xce\xde\xb7\xa8\xbb\xf1\xc8\xa1 LSASS \xbd\xf8\xb3\xcc\xc1\xee\xc5\xc6");
CloseHandle(hLsassProc);
return NULL;
}
Log(LOG_SUCCESS, "\xd2\xd1\xb3\xc9\xb9\xa6\xd7\xa5\xc8\xa1 LSASS \xcf\xb5\xcd\xb3\xb5\xc4\xb0\xb2\xc8\xab\xc6\xbe\xd6\xa4\xc9\xcf\xcf\xc2\xce\xc4");
CloseHandle(hLsassProc);
return hLsassToken;
}
HANDLE CreateCustomToken(DWORD targetSessionId, PSID pUserSid, const std::vector<std::string>& extraGroups) {
Log(LOG_DEBUG, "CreateCustomToken: \xbb\xee\xb6\xaf Session \xbb\xe1\xbb\xb0 ID = %d", targetSessionId);
PNtCreateToken NtCreateToken = (PNtCreateToken)(void*)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtCreateToken");
if (!NtCreateToken) {
Log(LOG_ERROR, "\xbc\xd3\xd4\xd8\xc4\xda\xba\xcb\xbd\xd3\xbf\xda NtCreateToken \xca\xa7\xb0\xdc (ntdll.dll)");
return NULL;
}
if (!pUserSid || !IsValidSid(pUserSid)) {
Log(LOG_ERROR, "\xce\xde\xd0\xa7\xb5\xc4\xd3\xc3\xbb\xa7\xb0\xb2\xc8\xab\xd6\xf7\xcc\xe5 SID");
return NULL;
}
LPSTR sidStr = NULL;
if (ConvertSidToStringSidA(pUserSid, &sidStr)) Log(LOG_INFO, "\xc4\xbf\xb1\xea\xce\xb1\xd4\xec\xb5\xc4\xb0\xb2\xc8\xab\xc9\xcf\xcf\xc2\xce\xc4\xd3\xc3\xbb\xa7: %s", sidStr ? sidStr : "\xce\xb4\xd6\xaa\xc9\xed\xb7\xdd");
if (sidStr) LocalFree(sidStr);
PSID pSidAdmins = GetSidFromString("S-1-5-32-544");
PSID pSidAuth = GetSidFromString("S-1-5-11");
PSID pSidEveryone = GetSidFromString("S-1-1-0");
PSID pSidIntegrity = GetSidFromString("S-1-16-16384");
PSID pLogonSid = GetLogonSid();
HANDLE hThreadToken = NULL;
OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hThreadToken);
EnablePrivilege(hThreadToken, "SeCreateTokenPrivilege");
EnablePrivilege(hThreadToken, "SeTcbPrivilege");
EnablePrivilege(hThreadToken, "SeAssignPrimaryTokenPrivilege");
CloseHandle(hThreadToken);
std::vector<SID_AND_ATTRIBUTES> groups;
groups.push_back({ pUserSid, SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_OWNER });
if (pLogonSid) groups.push_back({ pLogonSid, SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_LOGON_ID });
if (pSidAdmins) groups.push_back({ pSidAdmins, SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY });
if (pSidAuth) groups.push_back({ pSidAuth, SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY });
if (pSidEveryone) groups.push_back({ pSidEveryone, SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_MANDATORY });
if (pSidIntegrity) groups.push_back({ pSidIntegrity, SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED });
for (const auto& groupName : extraGroups) {
PSID sid = GetSidFromString(groupName.c_str());
if (!sid) sid = GetSidForAccountName(groupName.c_str());
if (sid) {
groups.push_back({ sid, SE_GROUP_ENABLED | SE_GROUP_ENABLED_BY_DEFAULT });
Log(LOG_DEBUG, "\xd7\xa2\xc8\xeb\xb8\xbd\xbc\xd3\xb0\xb2\xc8\xab\xd7\xe9: %s", groupName.c_str());
}
else {
Log(LOG_WARN, "\xce\xde\xb7\xa8\xbd\xe2\xce\xf6\xb8\xbd\xbc\xd3\xd7\xe9\xc3\xfb\xb3\xc6: %s", groupName.c_str());
}
}
DWORD groupsSize = sizeof(TOKEN_GROUPS) + groups.size() * sizeof(SID_AND_ATTRIBUTES);
PTOKEN_GROUPS pGroups = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, groupsSize);
pGroups->GroupCount = (DWORD)groups.size();
for (size_t i = 0; i < groups.size(); i++) pGroups->Groups[i] = groups[i];
DWORD privCount = 35;
DWORD privSize = sizeof(TOKEN_PRIVILEGES) + privCount * sizeof(LUID_AND_ATTRIBUTES);
PTOKEN_PRIVILEGES pPrivs = (PTOKEN_PRIVILEGES)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, privSize);
pPrivs->PrivilegeCount = privCount;
for (DWORD i = 0; i < privCount; i++) {
pPrivs->Privileges[i].Luid.LowPart = i + 2;
pPrivs->Privileges[i].Attributes = SE_PRIVILEGE_ENABLED_BY_DEFAULT | SE_PRIVILEGE_ENABLED;
}
TOKEN_USER tUser = { { pUserSid, 0 } };
TOKEN_OWNER tOwner = { pUserSid };
TOKEN_PRIMARY_GROUP tPrim = { pUserSid };
TOKEN_SOURCE tSource;
memcpy(tSource.SourceName, "TOKENGEN", 8);
AllocateLocallyUniqueId(&tSource.SourceIdentifier);
LUID authId = { 0x3e7, 0 };
LARGE_INTEGER exp; exp.QuadPart = -1;
OBJECT_ATTRIBUTES oa = {};
oa.Length = sizeof(OBJECT_ATTRIBUTES);
Log(LOG_DEBUG, "\xb7\xa2\xc6\xf0\xcf\xb5\xcd\xb3\xd3\xb2\xd6\xd0\xb6\xcf\xb5\xf7\xd3\xc3 NtCreateToken API...");
HANDLE hNewToken = NULL;
NTSTATUS status = NtCreateToken(
&hNewToken, TOKEN_ALL_ACCESS, &oa, TokenPrimary, &authId, &exp,
&tUser, pGroups, pPrivs, &tOwner, &tPrim, NULL, &tSource);
if (status != STATUS_SUCCESS) {
Log(LOG_ERROR, "\xce\xb1\xd4\xec NtCreateToken \xca\xa7\xb0\xdc: 0x%08X", status);
RevertToSelf();
HeapFree(GetProcessHeap(), 0, pGroups);
HeapFree(GetProcessHeap(), 0, pPrivs);
return NULL;
}
Log(LOG_SUCCESS, "\xd7\xd4\xb6\xa8\xd2\xe5\xc8\xa8\xcf\xde\xc1\xee\xc5\xc6\xd2\xd1\xb1\xbb\xbe\xab\xcf\xb8\xb5\xd8\xb6\xcd\xd4\xec\xb3\xc9\xd0\xcd");
if (!SetTokenInformation(hNewToken, TokenSessionId, &targetSessionId, sizeof(DWORD))) {
Log(LOG_WARN, "\xb0\xf3\xb6\xa8 Session ID \xd6\xc1\xc4\xbf\xb1\xea\xc1\xee\xc5\xc6\xb7\xa2\xc9\xfa\xd2\xec\xb3\xa3");
}
HeapFree(GetProcessHeap(), 0, pGroups);
HeapFree(GetProcessHeap(), 0, pPrivs);
return hNewToken;
}
BOOL ExecuteSudoOperation(
LPSTR cmdLine,
LPSTR identityStr,
LPSTR desktop,
const std::string& workingDir,
int windowMode,
DWORD integrityLevel,
BOOL bUIAccess,
const std::vector<std::string>& extraGroups,
const std::vector<int>& DisabledPrivilege,
const std::vector<int>& RemovePrivilege,
PROCESS_INFORMATION* pOutPI //
) {
Log(LOG_INFO, "\xd5\xfd\xd4\xda\xbd\xf8\xc8\xeb\xbd\xf8\xb3\xcc\xb6\xcd\xd4\xec\xc6\xf4\xb6\xaf\xc6\xf7...");
Log(LOG_DEBUG, "\xd6\xb4\xd0\xd0\xc3\xfc\xc1\xee: %s", cmdLine);
Log(LOG_DEBUG, "\xb0\xb2\xc8\xab\xd7\xc0\xc3\xe6: %s", desktop ? desktop : "\xb5\xb1\xc7\xb0\xbb\xee\xb6\xaf\xd7\xc0\xc3\xe6");
Log(LOG_DEBUG, "\xbb\xb7\xbe\xb3\xc9\xed\xb7\xdd: %s", identityStr);
if (workingDir.empty()) {
char tmp[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, tmp);
Log(LOG_DEBUG, "\xd4\xcb\xd0\xd0\xc2\xb7\xbe\xb6: %s", tmp);
}
else Log(LOG_DEBUG, "\xd4\xcb\xd0\xd0\xc2\xb7\xbe\xb6: %s", workingDir.c_str());
if (bUIAccess) Log(LOG_DEBUG, "UI Access \xd4\xf6\xc7\xbf\xb7\xc3\xce\xca\xcc\xd8\xc8\xa8\xd2\xd1\xd7\xb0\xd4\xd8");
if (integrityLevel != (DWORD)-1) Log(LOG_DEBUG, "\xcd\xea\xd5\xfb\xd0\xd4\xc7\xbf\xd6\xc6\xb1\xea\xc7\xa9\xbc\xb6\xb1\xf0: 0x%06x", integrityLevel);
DWORD sess = GetActiveSessionID();
PSID pTargetSid = ResolveIdentity(identityStr);
if (!pTargetSid) {
Log(LOG_ERROR, "\xce\xde\xb7\xa8\xbd\xe2\xce\xf6\xb4\xcb\xc9\xed\xb7\xdd\xc9\xcf\xcf\xc2\xce\xc4: %s", identityStr);
return FALSE;
}
HANDLE hLsassToken = GetLsassToken();
if (hLsassToken == NULL) return FALSE;
if (!ImpersonateLoggedOnUser(hLsassToken)) {
DWORD ErrCode = GetLastError();
Log(LOG_ERROR, "\xc6\xbe\xd6\xa4\xc4\xa3\xc4\xe2 LSASS \xca\xa7\xb0\xdc: %lu", ErrCode);
Log_ErrorCode(LOG_WARN, ErrCode);
CloseHandle(hLsassToken);
return FALSE;
}
HANDLE hToken = CreateCustomToken(sess, pTargetSid, extraGroups);
if (!hToken) {
RevertToSelf();
CloseHandle(hLsassToken);
return FALSE;
}
if (bUIAccess) {
BOOL UIAccess = TRUE;
if (SetTokenInformation(hToken, TokenUIAccess, &UIAccess, sizeof(BOOL))) {
Log(LOG_SUCCESS, "\xc1\xee\xc5\xc6\xd2\xd1\xbf\xaa\xc6\xf4 UI Access \xbf\xd8\xd6\xc6\xb1\xea\xd6\xbe");
} else {
DWORD ErrCode = GetLastError();
Log(LOG_WARN, "\xd0\xb4\xc8\xeb UI Access \xb1\xea\xbc\xc7\xca\xa7\xb0\xdc: %lu", ErrCode);
Log_ErrorCode(LOG_WARN, ErrCode);
}
}
if (integrityLevel != (DWORD)-1) {
SID sid = {};
sid.Revision = SID_REVISION;
sid.SubAuthorityCount = 1;
sid.IdentifierAuthority = SECURITY_MANDATORY_LABEL_AUTHORITY;
sid.SubAuthority[0] = integrityLevel;
TOKEN_MANDATORY_LABEL tml = {};
tml.Label.Attributes = SE_GROUP_INTEGRITY;
tml.Label.Sid = &sid;
if (SetTokenInformation(hToken, TokenIntegrityLevel, &tml, sizeof(TOKEN_MANDATORY_LABEL) + sizeof(DWORD))) {
Log(LOG_SUCCESS, "\xc1\xee\xc5\xc6\xc7\xbf\xd6\xc6\xb0\xb2\xc8\xab\xcd\xea\xd5\xfb\xd0\xd4\xb1\xea\xc7\xa9 (IL) \xd0\xde\xb8\xc4\xcd\xea\xb3\xc9");
DeleteDisabledPrivileges(hToken);
} else {
DWORD ErrCode = GetLastError();
Log(LOG_WARN, "\xc9\xe8\xd6\xc3\xcd\xea\xd5\xfb\xd0\xd4\xb5\xc8\xbc\xb6\xca\xa7\xb0\xdc: %lu", ErrCode);
Log_ErrorCode(LOG_WARN, ErrCode);
}
}
if(!RemovePrivilege.empty()) {
for(int id : RemovePrivilege) {
LUID_AND_ATTRIBUTES Privilege{{DWORD(id + 1), 0}, SE_PRIVILEGE_REMOVED};
HANDLE tmpToken = NULL;
if (CreateRestrictedToken(hToken, 0, 0, NULL, 1, &Privilege, 0, NULL, &tmpToken)) {
hToken = tmpToken;
Log(LOG_SUCCESS, "\xc7\xbf\xd6\xc6\xb4\xd3\xc1\xee\xc5\xc6\xd6\xd0\xc7\xd0\xb3\xfd\xcc\xd8\xc8\xa8 ID %d \xb3\xc9\xb9\xa6", id);
} else {
DWORD ErrCode = GetLastError();
Log(LOG_WARN, "\xc7\xbf\xd6\xc6\xc7\xd0\xb3\xfd\xcc\xd8\xc8\xa8 ID %d \xca\xa7\xb0\xdc: %lu", id, ErrCode);
Log_ErrorCode(LOG_WARN, ErrCode);
}
}
}
if(!DisabledPrivilege.empty()) {
for(int id : DisabledPrivilege) {
TOKEN_PRIVILEGES tp{1, {{{DWORD(id + 1), 0}, 0L}}};
if (AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL)) {
Log(LOG_SUCCESS, "\xbd\xfb\xd3\xc3\xcc\xd8\xc8\xa8 ID %d \xb3\xc9\xb9\xa6", id);
} else {
DWORD ErrCode = GetLastError();
Log(LOG_WARN, "\xbd\xfb\xd3\xc3\xcc\xd8\xc8\xa8 ID %d \xca\xa7\xb0\xdc: %lu", id, ErrCode);
Log_ErrorCode(LOG_WARN, ErrCode);
}
}
}
LPVOID lpEnv = NULL;
CreateEnvironmentBlock(&lpEnv, hToken, FALSE);
STARTUPINFOA si = {};
si.cb = sizeof(si);
si.lpDesktop = desktop;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = windowMode;
PROCESS_INFORMATION pi = {};
DWORD dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
if (windowMode != -1) dwCreationFlags |= CREATE_NEW_CONSOLE;
LPCSTR lpCurrentDir = workingDir.empty() ? NULL : workingDir.c_str();
if (windowMode == -1) {
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
}
BOOL success = CreateProcessAsUserA(
hToken, NULL, cmdLine, NULL, NULL, (windowMode == -1) ? TRUE : FALSE,
dwCreationFlags, lpEnv, lpCurrentDir, &si, &pi
);
RevertToSelf();
CloseHandle(hLsassToken);
if (lpEnv) DestroyEnvironmentBlock(lpEnv);