Creating a Thread as a different user?

Creating a Thread as a different user?

Secure Home | Search | About
 Microsoft Applications Security    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content add this group's latest topics to your Google content
Subject Author Date
Creating a Thread as a different user? Jason Viers 09-13-2006
Posted by Jason Viers on September 13, 2006, 1:44 pm
If you were  Registered and logged in, you could reply and use other advanced thread options
I know that when using CreateThread, the default security descriptor
comes from the primary token of the creator. How can I create a thread
with a different token?

I'm in IIS land (which is running as "NT AUTHORITY\NETWORK SERVICE"), in
an ISAPI Extension that's been authenticated and invoked as a different
user. I want to launch a thread as that user, but by default the new
thread inherits NETWORK SERVICE from IIS, and I can't find specifics on
how to form the LPSECURITY_ATTRIBUTE paramter for CreateThread. All the
MSDN articles I've found contain vague hand-waving about access tokens
and lpvoid parameters.

Thanks
Jason

Posted by Alun Jones [MS-MVP - Windows S on September 15, 2006, 3:31 pm
If you were  Registered and logged in, you could reply and use other advanced thread options
>I know that when using CreateThread, the default security descriptor comes
>from the primary token of the creator. How can I create a thread with a
>different token?

You can't. You can create a new thread that runs code which takes a
parameter that contains a handle that the new thread impersonates, but as
you say, the security context of the new thread is given restricted access
if you call CreateThread from a thread that's impersonating another token.

> I'm in IIS land (which is running as "NT AUTHORITY\NETWORK SERVICE"), in
> an ISAPI Extension that's been authenticated and invoked as a different
> user. I want to launch a thread as that user, but by default the new
> thread inherits NETWORK SERVICE from IIS, and I can't find specifics on
> how to form the LPSECURITY_ATTRIBUTE paramter for CreateThread. All the
> MSDN articles I've found contain vague hand-waving about access tokens and
> lpvoid parameters.

LPSECURITY_ATTRIBUTE is a red herring for your purposes. This value does
not describe any rights or tokens that the thread executes in, it specifies
whether the thread handle will be inherited by its children, and what rights
other security contexts have to access the newly created thread through its
handle. To form this value, you build a Security Descriptor containing an
ACL (Access Control List) for discretionary access (the DACL), an ACL for
auditing (the SACL), owner, group, etc. Then you package those into the
Security Attributes structure along with a boolean value to declare whether
the token is inherited by child processes.

Much of the time, it is appropriate to leave the LPSECURITY_ATTRIBUTE at
NULL. Nothing in your post suggests that you want it non-NULL.

The "hand-waving" that you are talking about indicates that you are confused
by what's being asked of you.

Here's an example of how you might achieve your purpose.

Create a structure, with three elements:
typedef struct {
HANDLE hToken;
LPTHREAD_START_ROUTINE lpFunction;
LPVOID lpArgument;
} IMPERSONATION_THREAD_ARGS;

Write a function, that takes a single argument:
DWORD WINAPI ImpersonationThreadProc( LPVOID lpParameter)
{
IMPERSONATION_THREAD_ARGS *pArgs = (IMPERSONATION_THREAD_ARGS
*)lpParameter;
if ( ! ImpersonateLoggedOnUser(pArgs->hToken) )
return GetLastError(); // Failed to impersonate the token we were
given.
// We are impersonated. Now call the function.
DWORD rval=(pArgs->lpFunction)(pArgs->lpArgument); // Call the original
function, with the original argument.
RevertToSelf(); // Cleanup
return rval;
}

Now you should be able to write a CreateThreadAsUser function that creates
the IMPERSONATION_THREAD_ARGS structure, populates it with the appropriate
data, and then calls CreateThread to run ImpersonationThreadProc.

This is still a little bit hand-waving, and of course I haven't compiled or
tested any of the code posted here - if this is still confusing, you may
want to consider whether it's worth hiring a more experienced programmer to
do this job.

Alun.
~~~~
--
Texas Imperial Software | Web: http://www.wftpd.com/
23921 57th Ave SE | Blog: http://msmvps.com/alunj/
Woodinville WA 98072-8661 | WFTPD, WFTPD Pro are Windows FTP servers.
Fax/Voice +1(425)807-1787 | Try our NEW client software, WFTPD Explorer.



Posted by Jason Viers on September 18, 2006, 4:02 pm
If you were  Registered and logged in, you could reply and use other advanced thread options
Alun Jones [MS-MVP - Windows Security] wrote:
> if ( ! ImpersonateLoggedOnUser(pArgs->hToken) )

ImpersonateLoggedOnUser! That, in combination with OpenThreadToken and
GetCurrentThread, does everything I need. I already had a complex
structure being passed in to the thread func, so adding an extra handle
to it was easy.

LPSECURITY_ATTRIBUTE was definitely a red herring for my problem; a
round hole for my square peg of a problem, where ImpersonateLoggedOnUser
is the proper square hole.

Thanks for the explanation and example!
Jason

Similar ThreadsPosted
Creating local user account from ASP.NET (C#) April 24, 2007, 8:02 am
Creating a very limited user account to run a service September 6, 2006, 11:04 am
HOW can i subscribe to a Thread? February 6, 2006, 9:11 am
Inability to start new thread inXP newsgroup December 31, 2005, 12:13 am
Re: creating password policies October 16, 2008, 7:42 am
RE: creating password policies November 12, 2008, 2:28 pm
Creating a Security guidebook for my department. October 17, 2006, 9:03 am
Creating Certificate for a wireless projector February 8, 2008, 7:12 am
creating PKI certificates without using a FQDN in the Name field October 30, 2008, 6:05 pm
Creating a recovery agent on local computer January 12, 2006, 9:40 pm

The site map in XML format XML site map

Contact Us | Privacy Policy