Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Software > Win32 Programmer Kernel > Re: OpenProcess...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 1841 of 7319
Post > Topic >>

Re: OpenProcessToken failed Access Denied

by m_pll@[EMAIL PROTECTED] (Pavel Lebedinsky) Oct 25, 2004 at 10:46 PM

Make sure you're not impersonating while calling OpenProcessToken.

Rene <root@[EMAIL PROTECTED]
> wrote:

> Hello,
> 
> We have an process which acts as a watchdog for other process. If one of

> the process fails, the watchdog process will kill it and restart.
> 
> The watchdog process runs as a ISAPI DLL (DLLHOST.EXE) on a Windows 2000

> Advanced Server machine, with all service packs and patches applied. At 
> the end of this message you can see the source-code of the part which 
> tries to kill the failing process. The watchdog process runs under a 
> user which is part of the Administrators group.
> 
> The first step is to open the ProcessToken, in order to set the needed 
> privileges. See the code-fragment below.
> 
> HANDLE             hToken;
> LUID               DebugValue;
> TOKEN_PRIVILEGES   tkp;
> 
> 
> 
>   if (0 == OpenProcessToken(GetCurrentProcess(),
>       TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
>       DWORD error = GetLastError();
>       cd.LogError("OpenProcessToken failed.");
>       cd.LogWin32Error(error);
>       return false;
>      }
> 
> The call too OpenProcessToken() fails with error 'Access Denied'. I 
> think that the user under which the watchdog process runs does not have 
> the needed rights to open the Process Token.
> 
> The watchdog process run as an user member of group 'Administrators'. I 
> also tried to run the watchdog process as user 'Administrator'. I am 
> aware of the Local Security Policy. I am unsure if I need to change 
> settings in the Local Security Policy, and if so, which settings.
> 
> My questions are:
> Under which user should the watchdog process run in order to succeed the

> call OpenProcessToken()?
> If I need to change Local Security Policy, how can I map enums like 
> 'TOKEN_ADJUST_PRIVILEGES' too settings in the Policy? Which settings 
> should I change?
> 
> Thanks in advance.
> 
> The complete sourcecode is below this line.
> 
> -------------------------------------------------
> 
> HANDLE             hToken;
> LUID               DebugValue;
> TOKEN_PRIVILEGES   tkp;
> 
> 
> 
> if (0 == OpenProcessToken(GetCurrentProcess(),
>     TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
>     DWORD error = GetLastError();
>     cd.LogError("OpenProcessToken failed.");
>     cd.LogWin32Error(error);
>     return false;
>      }
> 
> //
> // Enable the SE_DEBUG_NAME privilege
> //
> if (0 == LookupPrivilegeValue((LPWSTR) NULL,SE_DEBUG_NAME,
>           &DebugValue)) {
>      cd.LogError("LookupPrivilegeValue failed.");
>      cd.LogWin32Error(GetLastError());
>      ::CloseHandle(hToken);
>      return false;
>      }
> 
>   tkp.PrivilegeCount = 1;
>   tkp.Privileges[0].Luid = DebugValue;
>   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
> 
>   AdjustTokenPrivileges(hToken, FALSE, &tkp,
>        sizeof(TOKEN_PRIVILEGES),
>        (PTOKEN_PRIVILEGES) NULL,
>        (PDWORD) NULL);
> 
>   //
>   // The return value of AdjustTokenPrivileges can't be tested
>   //
>   DWORD dwResult = GetLastError();
>   if (dwResult != ERROR_SUCCESS) {
>       cd.LogError("AdjustTokenPrivileges failed.");
>       cd.LogWin32Error(dwResult);
>       ::CloseHandle(hToken);
>       return false;
>      }
> 
> HANDLE ps = OpenProcess( PROCESS_TERMINATE, FALSE, pid );
> 
> int result = -1;
> if (TerminateProcess(ps, (unsigned)-1)) {
>        result = 1;
>      }
>       else {
>        result = -1;
>      }
> 
> tkp.PrivilegeCount = 1;
> tkp.Privileges[0].Luid = DebugValue;
> 
> AdjustTokenPrivileges(hToken,
>       FALSE,
>       &tkp,
>       sizeof(TOKEN_PRIVILEGES),
>       (PTOKEN_PRIVILEGES) NULL,
>       (PDWORD) NULL);
> 
>    //
>    // The return value of AdjustTokenPrivileges can't be tested
>    //
>    if (dwResult != ERROR_SUCCESS) {
>      cd.LogError("AdjustTokenPrivileges failed.");
>      cd.LogWin32Error(dwResult);
>      ::CloseHandle(hToken);
>      ::CloseHandle(ps);
>      return false;
>     }
>  
>    ::CloseHandle(hToken);
>    ::CloseHandle(ps);
 




 2 Posts in Topic:
OpenProcessToken failed Access Denied
Rene <root@[EMAIL PROT  2004-10-25 20:15:07 
Re: OpenProcessToken failed Access Denied
m_pll@[EMAIL PROTECTED]   2004-10-25 22:46:47 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Sun Jul 6 14:58:00 CDT 2008.