Access is denied в WindowsIdentity.GetCurrentToken()
От: twirpx  
Дата: 25.07.05 08:34
Оценка:
Для выполнения некоторых операций на веб-сервере
делается импесонализация WindowsIdentity.

writer.WriteLine("[-] Impersonating web application account...");
writer.WriteLine("&tab;Previous account name: '{0}'", WindowsIdentity.GetCurrent().Name);
try {
  bool result = false;
  result = Win32.LogonUser(
    user_name, domain, password, 
    LOGON32_LOGON_TYPE.LOGON32_LOGON_INTERACTIVE, 
    LOGON32_PROVIDER.LOGON32_PROVIDER_DEFAULT, 
    ref existing_token_handle
  );
  if (!result) {
    int error_code = Marshal.GetLastWin32Error();
    throw new Win32Exception(error_code);
  }
  result = Win32.DuplicateToken(
    existing_token_handle, 
    SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, 
    ref duplicate_token_handle
  );
  if (!result) {
    int error_code = Marshal.GetLastWin32Error();
    throw new Win32Exception(error_code);
  } else {
    // create new identity using new primary token
    WindowsIdentity new_identity = new WindowsIdentity(duplicate_token_handle);
    WindowsImpersonationContext impersonation_context = new_identity.Impersonate();
    return impersonation_context;
  }
} catch (Exception ex) {
  throw ex;
} finally {
   // close handle(s)
   if (existing_token_handle != IntPtr.Zero) Win32.CloseHandle(existing_token_handle);
   if (duplicate_token_handle != IntPtr.Zero) Win32.CloseHandle(duplicate_token_handle);
}
writer.WriteLine("&tab;Current account name: '{0}'", WindowsIdentity.GetCurrent().Name);
writer.WriteLine();


На некоторых серверах такой код выбрасывает исключение на втором WindowsIdentity.GetCurrent()

ExceptionType="System.ApplicationException"
Message="Access is denied."
TargetSite="IntPtr _GetCurrentToken()"
Source="mscorlib"
at System.Security.Principal.WindowsIdentity._GetCurrentToken()
at System.Security.Principal.WindowsIdentity.GetCurrent()
...

В рефлекторе получаем, что

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IntPtr _GetCurrentToken();

Я думаю, что это вызов внутреннего метода CLR

Поиск в google дал, что возможно не получается открыть
security token после имперсанализации, а как это
можно победить?
Posted via RSDN NNTP Server 1.9
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.