00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
#include <nt.h>
00031
#include <ntrtl.h>
00032
00033
00034
00035
00036
00037
00038
#include <stdio.h>
00039
#include <string.h>
00040
00041
00042
00043
00044
00045
00046
00047 BOOLEAN
SwitchEnable =
FALSE;
00048 BOOLEAN
SwitchDisable =
FALSE;
00049 BOOLEAN
SwitchReset =
FALSE;
00050 BOOLEAN
SwitchAll =
FALSE;
00051
00052
#ifndef SHIFT
00053 #define SHIFT(c,v) {c--; v++;}
00054
#endif //SHIFT
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
VOID
00065
Usage ( VOID );
00066
00067 BOOLEAN
00068
OpenAppropriateToken(
00069 OUT PHANDLE Token
00070 );
00071
00072
VOID
00073
EnableAllPrivileges( VOID );
00074
00075
VOID
00076
ResetAllPrivileges( VOID );
00077
00078
VOID
00079
DisableAllPrivileges( VOID );
00080
00081
int
00082
PrivMain (
00083 IN
int c,
00084 IN PCHAR v[]
00085 );
00086
00087
00088
00089
00090
VOID
00091
Usage (
00092 VOID
00093 )
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 {
00111
00112 printf(
"\n");
00113 printf(
"\n");
00114
00115 printf(
"Usage: priv [/EDRA] {PrivilegeName}\n");
00116 printf(
" /E - Enable Privilege(s)\n");
00117 printf(
" /D - Disable Privilege(s)\n");
00118 printf(
" /R - Reset to default setting(s)\n");
00119 printf(
" /A - Apply To All Privileges\n");
00120 printf(
"\n");
00121
00122 printf(
" The qualifiers /E and /D are mutually exclusive and can not\n");
00123 printf(
" be used in the same command.\n");
00124 printf(
" If /A is specified, then the PrivilegeName is ignored.\n");
00125 printf(
"\n");
00126 printf(
"\n");
00127 printf(
"Examples:\n");
00128 printf(
"\n");
00129 printf(
" priv /ae\n");
00130 printf(
" (enables all held privileges.\n");
00131 printf(
"\n");
00132 printf(
" priv /ad\n");
00133 printf(
" disables all held privileges.\n");
00134 printf(
"\n");
00135 printf(
" priv /ar\n");
00136 printf(
" (returns all privileges to their default setting.\n");
00137 printf(
"\n");
00138 printf(
" priv /e SeSetTimePrivilege\n");
00139 printf(
" (enables the privileges called: SeSetTimePrivilege\n");
00140 printf(
"\n");
00141 printf(
"\n");
00142
00143
return;
00144 }
00145
00146
00147 BOOLEAN
00148 OpenAppropriateToken(
00149 OUT PHANDLE Token
00150 )
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 {
00180
NTSTATUS Status, IgnoreStatus;
00181 OBJECT_ATTRIBUTES ProcessAttributes;
00182 HANDLE Process;
00183 PTEB CurrentTeb;
00184
00185 CurrentTeb = NtCurrentTeb();
00186 InitializeObjectAttributes(&ProcessAttributes,
NULL, 0,
NULL,
NULL);
00187
Status =
NtOpenProcess(
00188 &Process,
00189 PROCESS_QUERY_INFORMATION,
00190 &ProcessAttributes,
00191 &CurrentTeb->ClientId
00192 );
00193
00194
if (
NT_SUCCESS(
Status)) {
00195
00196
Status =
NtOpenProcessToken(
00197 Process,
00198 TOKEN_ADJUST_PRIVILEGES |
00199 TOKEN_QUERY,
00200
Token
00201 );
00202
00203 IgnoreStatus =
NtClose( Process );
00204
00205
if (
NT_SUCCESS(
Status) ) {
00206
00207
return TRUE;
00208
00209 }
00210
00211 }
00212
00213 printf(
"\n");
00214 printf(
"\n");
00215 printf(
"You are not allowed to change your own privilege settings.\n");
00216 printf(
"Operation failed.\n");
00217
00218
return FALSE;
00219
00220 }
00221
00222
00223
00224
VOID
00225 EnableAllPrivileges(
00226 VOID
00227 )
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 {
00245
NTSTATUS Status;
00246 HANDLE
Token;
00247 ULONG ReturnLength,
Index;
00248 PTOKEN_PRIVILEGES NewState;
00249
00250
00251
if ( !
OpenAppropriateToken(&
Token) ) {
00252
return;
00253 }
00254
00255
00256
00257
00258
00259
Status =
NtQueryInformationToken(
00260
Token,
00261 TokenPrivileges,
00262 NewState,
00263 0,
00264 &ReturnLength
00265 );
00266
ASSERT(
Status == STATUS_BUFFER_TOO_SMALL );
00267
00268 NewState =
RtlAllocateHeap( RtlProcessHeap(), 0, ReturnLength );
00269
ASSERT( NewState !=
NULL );
00270
00271
00272
Status =
NtQueryInformationToken(
00273
Token,
00274 TokenPrivileges,
00275 NewState,
00276 ReturnLength,
00277 &ReturnLength
00278 );
00279
ASSERT(
NT_SUCCESS(
Status) ||
NT_INFORMATION(
Status) );
00280
00281
00282
00283
00284
00285
00286
if (NewState->PrivilegeCount > 0) {
00287
Index = NewState->PrivilegeCount;
00288
00289
while (
Index < NewState->PrivilegeCount) {
00290 NewState->Privileges[
Index].Attributes = SE_PRIVILEGE_ENABLED;
00291
Index += 1;
00292 }
00293 }
00294
00295
00296
00297
00298
00299
00300
Status =
NtAdjustPrivilegesToken(
00301
Token,
00302
FALSE,
00303 NewState,
00304 ReturnLength,
00305
NULL,
00306 &ReturnLength
00307 );
00308
ASSERT(
NT_SUCCESS(
Status) ||
NT_INFORMATION(
Status) );
00309
00310
00311
00312
RtlFreeHeap( RtlProcessHeap(), 0, NewState );
00313
Status =
NtClose(
Token );
00314
00315
return;
00316
00317 }
00318
00319
00320
00321
VOID
00322 ResetAllPrivileges(
00323 VOID
00324 )
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 {
00342
NTSTATUS Status;
00343 HANDLE
Token;
00344 ULONG ReturnLength,
Index;
00345 PTOKEN_PRIVILEGES NewState;
00346
00347
00348
if ( !
OpenAppropriateToken(&
Token) ) {
00349 printf(
"\n");
00350 printf(
"\n");
00351 printf(
"You are not allowed to change your own privilege settings.\n");
00352 printf(
"Operation failed.\n");
00353
return;
00354 }
00355
00356
00357
00358
00359
00360
Status =
NtQueryInformationToken(
00361
Token,
00362 TokenPrivileges,
00363 NewState,
00364 0,
00365 &ReturnLength
00366 );
00367
ASSERT( STATUS_BUFFER_TOO_SMALL );
00368
00369 NewState =
RtlAllocateHeap( RtlProcessHeap(), 0, ReturnLength );
00370
ASSERT( NewState !=
NULL );
00371
00372
00373
Status =
NtQueryInformationToken(
00374
Token,
00375 TokenPrivileges,
00376 NewState,
00377 ReturnLength,
00378 &ReturnLength
00379 );
00380
ASSERT(
NT_SUCCESS(
Status) ||
NT_INFORMATION(
Status) );
00381
00382
00383
00384
00385
00386
00387
00388
if (NewState->PrivilegeCount > 0) {
00389
Index = NewState->PrivilegeCount;
00390
00391
while (
Index < NewState->PrivilegeCount) {
00392
if (NewState->Privileges[
Index].Attributes ==
00393 SE_PRIVILEGE_ENABLED_BY_DEFAULT) {
00394 NewState->Privileges[
Index].Attributes = SE_PRIVILEGE_ENABLED;
00395 }
00396
else {
00397 NewState->Privileges[
Index].Attributes = 0;
00398 }
00399
00400
Index += 1;
00401 }
00402 }
00403
00404
00405
00406
00407
00408
00409
Status =
NtAdjustPrivilegesToken(
00410
Token,
00411
FALSE,
00412 NewState,
00413 ReturnLength,
00414
NULL,
00415 &ReturnLength
00416 );
00417
ASSERT(
NT_SUCCESS(
Status) ||
NT_INFORMATION(
Status) );
00418
00419
00420
00421
RtlFreeHeap( RtlProcessHeap(), 0, NewState );
00422
Status =
NtClose(
Token );
00423
00424
return;
00425
00426 }
00427
00428
00429
00430
00431
VOID
00432 DisableAllPrivileges(
00433 VOID
00434 )
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 {
00452 ULONG IgnoredReturnLength;
00453 HANDLE
Token;
00454
NTSTATUS Status;
00455
00456
if ( !
OpenAppropriateToken(&
Token) ) {
00457 printf(
"\n");
00458 printf(
"\n");
00459 printf(
"You are not allowed to change your own privilege settings.\n");
00460 printf(
"Operation failed.\n");
00461
return;
00462 }
00463
00464
00465
00466
00467
00468
00469
Status =
NtAdjustPrivilegesToken(
00470
Token,
00471
TRUE,
00472
NULL,
00473 0,
00474
NULL,
00475 &IgnoredReturnLength
00476 );
00477
ASSERT(
NT_SUCCESS(
Status) ||
NT_INFORMATION(
Status) );
00478
00479
Status =
NtClose(
Token );
00480
return;
00481
00482 }
00483
00484
00485
int
00486 PrivMain (
00487 IN
int c,
00488 IN PCHAR v[]
00489 )
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506 {
00507 PCHAR p;
00508
CHAR ch;
00509 ULONG DispositionDirectives;
00510
00511
00512
try {
00513 DispositionDirectives = 0;
00514
SHIFT (
c,v);
00515
while ((
c > 0) && ((ch = *v[0]))) {
00516 p = *v;
00517
if (ch ==
'/') {
00518
while (*++p !=
'\0') {
00519
if (*p ==
'E') {
00520
SwitchEnable =
TRUE;
00521 DispositionDirectives += 1;
00522 }
00523
if (*p ==
'D') {
00524
SwitchDisable =
TRUE;
00525 DispositionDirectives += 1;
00526 }
00527
if (*p ==
'R') {
00528
SwitchReset =
TRUE;
00529 DispositionDirectives += 1;
00530 }
00531
else if (*p ==
'A') {
00532
SwitchAll =
TRUE;
00533 }
00534
else {
00535
Usage();
00536 }
00537 }
00538
SHIFT(
c,v);
00539 }
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
if (!
SwitchAll && (
c == 0)) {
00554 printf(
"\n");
00555 printf(
"\n");
00556 printf(
"You must provide privilege name or use the /A switch.\n");
00557
Usage();
00558
return ( 0 );
00559 }
00560
00561
if (DispositionDirectives != 1) {
00562 printf(
"\n");
00563 printf(
"\n");
00564 printf(
"You must provide one and only one of the following");
00565 printf(
"switches: /E, /D, /R\n");
00566
Usage();
00567
return ( 0 );
00568
00569 }
00570
00571
00572
00573
00574
00575
00576
if (
SwitchAll) {
00577
00578
00579
00580
00581
00582
if (
SwitchEnable) {
00583
EnableAllPrivileges();
00584 }
00585
else if (
SwitchDisable) {
00586
DisableAllPrivileges();
00587 }
00588
else {
00589
ResetAllPrivileges();
00590 }
00591 }
00592
00593
00594
00595
00596
00597
else {
00598 printf(
"\n");
00599 printf(
"I'm sorry, but due to the lack of time and interest,\n");
00600 printf(
"individual privilege selection is not yet supported.\n");
00601 printf(
"Please use the /A qualifier for the time being.\n");
00602 printf(
"\n");
00603 }
00604
00605 } finally {
00606
return ( 0 );
00607 }
00608
00609
return( 0 );
00610
00611 }