21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

* Create a restricted token with all privileges removed */<br />

CreateRestrictedToken(hThreadToken, DISABLE_MAX_PRIVILEGE, 0, 0, 0, 0, 0, 0,<br />

&hRestrictedToken);<br />

/* Set the thread's impersonation token to the new restricted token */<br />

SetThreadToken(&hThread, hRestrictedToken);<br />

/* ... perform work here */<br />

/* Revert the thread's impersonation token back to its original */<br />

SetThreadToken(&hThread, 0);<br />

/* Stop impersonating */<br />

RevertToSelf( );<br />

/* Cleanup */<br />

CloseHandle(hRestrictedToken);<br />

CloseHandle(hThreadToken);<br />

Modifying a process’s primary token<br />

Beginning with Windows .NET Server 2003, support for a new flag has been added<br />

to the function AdjustTokenPrivileges( ); it allows a privilege to be removed from a<br />

token, rather than simply disabled. Once the privilege has been removed, it cannot<br />

be added back to the token. In older versions of Windows, privileges could only be<br />

enabled or disabled using AdjustTokenPrivileges( ), and there was no way to remove<br />

privileges from a token without duplicating it. There is no way to substitute another<br />

token for a process’s primary token—the best you can do in older versions of Windows<br />

is to use restricted impersonation tokens.<br />

BOOL AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges,<br />

PTOKEN_PRIVILEGES NewState, DWORD BufferLength,<br />

PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength);<br />

This function has the following arguments:<br />

TokenHandle<br />

Handle to the token that is to have its privileges adjusted. The handle must have<br />

been opened with TOKEN_ADJUST_PRIVILEGES access; in addition, if PreviousState<br />

is to be filled in, it must have TOKEN_QUERY access.<br />

DisableAllPrivileges<br />

Boolean argument that specifies whether all privileges held by the token are to be<br />

disabled. If specified as TRUE, all privileges are disabled, and the NewState argument<br />

is ignored. If specified as FALSE, privileges are adjusted according to the<br />

information in the NewState argument.<br />

NewState<br />

List of privileges that are to be adjusted, along with the adjustment that is to be<br />

made for each. Privileges can be enabled, disabled, and removed. The TOKEN_<br />

PRIVILEGES structure contains two fields: PrivilegeCount and Privileges.<br />

PrivilegeCount is simply a DWORD that indicates how many elements are in the<br />

12 | Chapter 1: Safe Initialization<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!