murrayju / CreateProcessAsUser

Creates a process in a different Windows session
MIT License
369 stars 114 forks source link

The Demo project isn't working for me (GetSessionUserToken failed) #33

Open gte2723 opened 3 years ago

gte2723 commented 3 years ago

I'm sure I'm not doing something correct, here is what I've done. I downloaded the file CreateProcessAsUser-master.zip and opened it in VS2015. I tried running it unmodified and received the message "Cannot start service from the command line or a debugger. A Windows Service must first be installed and then started ..."

So I created a service with Powershell and changed the first line below to the second:

this.ServiceName = "Service1"; this.ServiceName = "AdminService";

and I get the same error. I wanted to get the demo working before I tried to use this with an MVC website and wrap it in an Invoke-Command powershell command similar to this https://rzander.azurewebsites.net/create-a-process-as-loggedon-user/ . I've searched and it appears the demo works for everyone.

Could my (work) environment be configured incorrectly, not allowing this demo project to work? I am an admin on this box.

if (!GetSessionUserToken(ref hUserToken)) is equal to {0}

[edit: a little more information]

When using the powershell link above and inside of a powershell IDE, I get the following error

Exception calling "StartProcessAsCurrentUser" with "1" argument(s): "StartProcessAsCurrentUser: GetSessionUserToken failed." At C:\Users\xxx\AppData\Local\Temp\2\6ca92ace-e967-4153-a04c-eab859a2430f.ps1:292 char:1

gte2723 commented 3 years ago

For anyone that sees this in a search, I was able to follow these steps and get it to work

_1. Open a cmd.exe as Administrator

  1. git clone https://github.com/murrayju/CreateProcessAsUser
  2. cd CreateProcessAsUser
  3. %windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe solution.sln
  4. cd DemoService
  5. createService.bat
  6. You should see calc.exe open_

Which means the recommended next step is below, although I'm not sure how to do that.

You need SE_TCB_NAME privilege. If you ran from a service (LocalSystem) it's held by default, if you are running it from command line, like you do, it's up to you to obtain it.

Yes, what @AndrewSav said. If you intend to run your code from the system account, you should just do your testing in that same environment and it should work. Otherwise, as an admin you will be able to acquire the necessary permissions, but you'll have to figure out the necessary win32 calls to do so.

Also, that is not the only privilege that you will need, the CreateProcessAsUser doc lists others.

giesalabs commented 3 years ago

how elevate a console application (run as administrator) with the SE_TCB_NAME?

after some search i found this: to have SE_TCB_NAME the process must have the "Act as part of the operating system" right but i don't find any example of that