views:

40

answers:

2

I know I can impersonate a windows account using: http://msdn.microsoft.com/en-us/library/chf6fbt4.aspx

But how do I go about impersonating a WellKnownSidType account?

SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null);
NTAccount account = sid.Translate(typeof(NTAccount)) as NTAccount;
if (account != null)
{
    //Impersonate here???
}
+1  A: 

First off, you will not be able to impersonate built-in accounts like 'Network Service' or 'Local Service' unless you are running under the 'Local Service' account, which as far as I know can only by done be the OS.

The following post provides a possible solution to the point above (I did not try it, just googled it before I started writting all this my self), but it looks reasonable.

http://geek.hubkey.com/2008/02/impersonating-built-in-service-account.html

Once are running under the 'Local Service' account, you simply need to use LogonUser (also shown in the link above) to Logon using the account name of the well known SID and then impersonate the token.

I see the link above runs 'CMD.EXE' as a service which can is allowed to interact with the desktop, note that the 'interact with desktop' functionality is being deprecated.

Chris Taylor
I think you're not quite correct about what sorts of impersonation are possible. You might want to take a look at the second link in my answer, as it points to a particularly comprehensive article on the subject.
Steven Sudit
@Steven, that is for ASP.NET applications which are running under a system account, the IIS service runs as a windows service and communicates with w3wp.exe or aspnet_iis.exe using named pipes so both processes are running under the service account. What I am refering to is for interactive user applications, started from the command prompt.
Chris Taylor
Then the CLI app would need to first impersonate a highly-trusted, non-service account, which would then give it the ability to use delegation to impersonate the local system. Of course, once it has the ability to ability to do the former, there's little need for the latter.
Steven Sudit
@Steven, impersonating a logged on user is a common thing to do, the problem here is, as I understand it, the OP is wanting to impersonate a system account in a normal windows application. The Local Service does not have a password, so that impersonation requires serious juice.
Chris Taylor
Yes, which is why I suggested the two-step approach. But I'm still unclear on what the *purpose* is, so I've asked the OP to clarify.
Steven Sudit
I'm still not sure about the OP's reasoning, but the code you linked to shows how to do the deed, once you're already running under a highly-trusted account, so I'll toss you the upvote.
Steven Sudit
A: 

Use the WindowsIdentity constructor and follow the directions here. You're going to need "Act as part of the operating system", among other things.

Steven Sudit