views:

393

answers:

1

After a successful call to both LogonUser and ImpersonateLoggedOnUser it doesn't appear that my process is running as the new user...

system("whoami");

prints out: Chris-PC\Chris

when it should be: Chris-PC\LimitedGuy

Is there a function I'm not calling or something?

My code:

if(argc == 6) // impersonate
  {

   printf("[~] Logging in as %ws\\\\%ws..\n", argv[3], argv[4]);
   if(!LogonUser(argv[4], argv[3], argv[5], LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &logonToken))
   {
    printf("[!] Failed to login as %ws. Error Code: %X\n", argv[4], GetLastError());
    return 1;
   }


   if(!ImpersonateLoggedOnUser(logonToken))
   {
    printf("[!] ImpersonateLoggedOnUser failed with error code: %X\n", GetLastError());
    return 1;
   }

   LoadUserProfile(logonToken, &plinfo);
   system("whoami");
   printf("[~] Login successful!\n");
}
+2  A: 

When you use the system call a new process is created to execute the command but in Windows the new process is always created with the token from the parent process not the thread (unless you specifically use one of the CreateProcessAsUser, CreateProcessWithLogonW, etc. calls). So in your case 'whoami' is executed in the context of the original user not the one impersonating. To check the name of the user being impersonated call GetUserName.

Stephen Martin
When I use CreateProcessAsUser to launch cmd.exe it still fails when i type "whoami". Is there anyway to force the launched process to take on the security attributes I want?
Chris T
I just tried a quick test using CreateProcessWithLogonW and it worked as expected - whoami gave the user name of the account the process was launched under not that of the original user. It may have something to do with environment blocks or something along those lines though it seems unlikely. Maybe you could post your CreateProcessAsUser code.
Stephen Martin