How can I impersonate an admin user from windows service?

92 Views Asked by At

I've been stuck on this question for a long time. Here is my scenario: I create a windows service and I want the service to start an executable installer with interactive GUI. Since the service run under Session0 on my Windows 10 system, I have to find a way to do this properly.
I googled a lot, and found some topics about how to impersonate a user from windows service. And I've tried every one of their ways. None of them actually solved my problem. The procedures are basically clear.
1.Get the token of some user space process, I chose explorer.exe.

Process[] processByName = Process.GetProcessesByName("explorer");
hProcess = processByName[0].Handle;
OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, ref procToken)

2.Duplicate token

DuplicateTokenEx(procToken,
                 TOKEN_ALL_ACCESS,
                 ref sa,
                 (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
                 (int)TOKEN_TYPE.TokenPrimary,
                 ref DupedToken);

3.Create environment block

CreateEnvironmentBlock(ref EnvironmentFromUser, DupedToken, false);

4.Invoke CreateProcessAsUser to start my executable installer with GUI.

CreateProcessAsUser(DupedToken,
                        "myexeinstaller.exe",
                        null,
                        ref sa,
                        ref sa,
                        true,
                        CREATE_NEW_PROCESS_GROUP | CREATE_UNICODE_ENVIRONMENT, 
                        EnvironmentFromUser,
                        null,
                        ref si,
                        out pi);

Everything goes fine until an error box showed up before the GUI show up. I tried two executable installer. And they showed different error.
One is saying: NSIS error writing temporary file. Make sure temp folder is valid.
And the other is saying: error Access denied.
I somehow figured out these two installers' difference. The first one tried to write something into HKEY_CURRENT_USER while the second one tried to write something into HKEY_LOCAL_MACHINE.
And my guess: I've started the installer as some user but without admin privilege. So the installer popped up an error when it needs to do something that need admin privilege.
I didn't give it up here and googled more, and finally found this article.http://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-and-bit-Archite

And it helped me a lot. I can now start the installer running as an Administrator under the System account. For the second installer, it worked perfectly, but for the first installer, it failed silently. I think this maybe because the installer now running under System account while it need to do something under user account like write to HKEY_CURRENT_USER.
So the thing is I finally got admin privilege but my installer had to run under System account.
Can anyone tell me is it even possible to start an installer with GUI from service and running under user account with admin privilege? Any idea is appreciate, many thanks in advance.

0

There are 0 best solutions below