How to change owner of a process or thread?

Hi,

I have a Windows Service that run with SYSTEM user.
When i create a new process in this process (using ShellExecute function) the new process got SYSTEM privilege too.
I want it'll run as current user privilege.
How can i done Any help

Hieu


Answer this question

How to change owner of a process or thread?

  • dpilcher

    I've find out the solution.
    By using Security function:
     - OpenProcess
     - SetSecurityInfo
    So i can write the owner of the process.
    But i when i user SetSecurityInfo function, it failed with error is ERROR_INVALID_OWNER
    Here is my little code

     

    HANDLE hCurrent = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetProcessIdFromImageName(_T(
    "explorer.exe")));
    PSID pSid;
    DWORD dwPID = GetProcessIdFromImageName(_T("explorer.exe"
    ));
    HANDLE hToken;
    OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken);
    BYTE buf[512];
    DWORD cb =
    sizeof
    (buf);
    GetTokenInformation(hToken, TokenUser, buf, cb, &cb);
    TCHAR szName[255];
    DWORD dwName = 255;
    TCHAR szDomain[255];
    DWORD dwDomain = 255;
    SID_NAME_USE snu;
    LookupAccountSid(NULL, ((TOKEN_USER *)(buf))->User.Sid, szName, &dwName, szDomain, &dwDomain, &snu);
    AfxMessageBox(szName);
    DWORD dwError = SetSecurityInfo(hCurrent, SE_KERNEL_OBJECT, OWNER_SECURITY_INFORMATION, ((TOKEN_USER *)(buf))->User.Sid, NULL, NULL, NULL);
    if
    (dwError != ERROR_SUCCESS)
    {
    CString sz;
    sz.Format(_T(
    "%d"
    ), dwError);
    ::MessageBox(NULL, sz,
    "E2"
    , MB_OK);
    }

    HINSTANCE hInstance = ShellExecute(NULL, NULL, "http://www.yahoo.com", NULL, NULL, SW_SHOW);



     



  • muyiwa taiwo

    In my eyes this code doesn't make any sense. You are searching for an existing process an you try to set its security context to a new sid.

    But you want to create a new process in a specific security context. You have to use CreateProcessAsUser. You can use AssocQueryString to find the application for a file to launch, like ShellExecute does.

  • -steinar-

    I have successfully try the CreateProcessAsUser and AssocQueryString function.
    But with application that open with DDE command, how can i pass to CreateProcessAsUser function (Ex: like Visual Studio)



  • Pumkiny

    I'm searching for a way to use same as CreateProcessAsUser function but for ShellExecute function.

    Because i want to be able to open a document instead of create a process.

    Hieu

  • JustDizzy

    Are you searching for CreateProcessAsUser



  • David Chicks - MSFT

    I would try posting at the win32 newsgroups at http://msdn.microsoft.com/newsgroups/topic.aspx url=/MSDN-FILES/028/201/015/topic.xml where you might be able to find answers.

    Thanks,
      Ayman Shoukry
      VC++ Team

  • David Rogers

    I am not sure what AssocQueryString returns for this. But the registry shows the executable that has to be started in the Command entry in the registry.

    I have no other idea, that you start the process with CreateProcessAsUser than to initiate a DDE connection with the topic defined under topic in and perform the specific actions that are defined under DDEExec in Application. All this stuff is defined in the registry for the application.

    So you only have a data file name and you want to launch it with the associated application like ShellExecute does, but with a different user. Sorry I do not now a complete function like ShellExecuteAsUser...

  • How to change owner of a process or thread?