Win XP SP2上的用户模拟 [英] User Impersonation on Win XP SP2

查看:62
本文介绍了Win XP SP2上的用户模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些问题,当我尝试在win xp sp2框上使用

Process.Start(ProcessStartInfo psi)启动另一个进程时(其他版本

of xp没有问题。


以下是详细信息。

主应用程序在启动时检查更新,如果有更新,则

启动单独的exe来复制文件。


在启动新进程(exe)之前,我冒充管理员用户为

主应用程序由非-admin用户。


用户身份在模拟后更改,但在更新期间,

访问被拒绝当主代码尝试启动复印机exe时,会抛出win32Exception。


使用以下win32 api实现模拟。

< ;代码>

字符串domainName = string.Empty;

尝试

{

//获取当前的Windows身份

string currentWindowsIdentity = WindowsIdentity.GetCurrent()。Name;


domainName = currentWindowsIdentity.Substring(0,

currentWindowsIdentity.IndexOf (''\\''));


const int LOGON32_PROVIDER_DEFAULT = 0;

//此参数使LogonUser创建主令牌。

const int LOGON32_LOGON_INTERACTIVE = 2;

const int SecurityImpersonation = 2;

tokenHandle = IntPtr.Zero;

dupeTokenHandle = IntPtr.Zero;

//调用LogonUser获取访问令牌的句柄。

bool returnValue = LogonUser(_impersonationUsername,

域名,_impersonationPassword ,LOGON32_LOGON_INTERACTIVE,

LOGON32_PROVIDER_DEFAULT,ref tokenHandle);


if(false == returnValue)

{

int ret = Marshal.GetLastWin32Error();

int errorCode = 0x5; // ERROR_ACCESS_DENIED

抛出新的System.ComponentModel.Win32Exception(errorCode);

}


//检查身份。

bool retVal = DuplicateToken(tokenHandle,SecurityImpersonation,ref

dupeTokenHandle);

if(false == retVal)

{

CloseHandle(tokenHandle);

返回;

}


//令牌传递给下面的构造函数必须

//才能使用它进行模拟。

WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);

_impersonatedUser = newId.Impersonate();

}

catch(例外情况)

{


}

< / code>

我为非管理员用户启用了以下安全策略:


1)替换进程级令牌。


2)调试程序


3)调整进程的内存配额


注意:''Launc如果没有用户模仿,则使用另一个exe''工作正常




任何想法,可能出现什么问题?

~ViPuL

I am having some issues, when I try to launch another process using
Process.Start(ProcessStartInfo psi) on win xp sp2 box (Other versions
of xp have no issue).

Here is the detail.
Main app checks for updates on startup and if updates are available, it
launches separate exe to copy files.

Before launching new process(exe), I am impersonating admin user as
main app is being launched by non-admin user.

User identity is changed after impersonation, but during update,
"Access is denied" win32Exception is being thrown when main code tries
to launch copier exe.

Impersonation is implemented using following win32 api.
<code>
string domainName = string.Empty;
try
{
// Get current windows identity
string currentWindowsIdentity = WindowsIdentity.GetCurrent().Name;

domainName = currentWindowsIdentity.Substring(0,
currentWindowsIdentity.IndexOf(''\\''));

const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
const int SecurityImpersonation = 2;
tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(_impersonationUsername,
domainName,_impersonationPassword, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,ref tokenHandle);

if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
int errorCode = 0x5; //ERROR_ACCESS_DENIED
throw new System.ComponentModel.Win32Exception(errorCode);
}

// Check the identity.
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref
dupeTokenHandle);
if (false == retVal)
{
CloseHandle(tokenHandle);
return;
}

// The token that is passed to the following constructor must
// be a primary token in order to use it for impersonation.
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
_impersonatedUser = newId.Impersonate();
}
catch(Exception ex)
{

}
</code>
I have enabled following security policies for non-admin user:

1) Replace a process level token.

2) Debug programs

3) Adjust memory quotas for a process

NOTE: ''Launching another exe'' works fine, if no user impersonation is
used.

Any ideas, what might be wrong ?

~ViPuL

推荐答案

我无法回答你的问题,但是你考虑过使用

ClickOnce部署而不是您当前检查

更新的方法?然后你甚至不需要为这整个问题烦恼。

I can''t answer your question for you, but have you considered using
ClickOnce Deployment instead of your current method of checking for
updates? Then you wouldnt even need to bother with this whole issue.


好吧,我们仍在使用.net 1.1 sp1。因此,ClickOnce不是一个选项... :(

Well, we are still using .net 1.1 sp1. So ClickOnce is not a option...:(


以下是详细信息例外:


异常:System.ComponentModel.Win32Exception

消息:访问被拒绝

来源:System

at System.Diagnostics.ProcessManager.OpenProcess(Int3 2 processId,

Int32 access,Boolean throwIfExited)

在System.Diagnostics.NtProcessManager.GetModuleInfos(Int32

processId)

at System.Diagnostics.Process.get_Modules()

在System.Diagnostics.Process.get_MainModule()


帮助!!!!!! !!!!!!!

~ViPuL

Following is the Detail Exception ia m getting:

Exception: System.ComponentModel.Win32Exception
Message: Access is denied
Source: System
at System.Diagnostics.ProcessManager.OpenProcess(Int3 2 processId,
Int32 access, Boolean throwIfExited)
at System.Diagnostics.NtProcessManager.GetModuleInfos (Int32
processId)
at System.Diagnostics.Process.get_Modules()
at System.Diagnostics.Process.get_MainModule()

Help!!!!!!!!!!!!!
~ViPuL


这篇关于Win XP SP2上的用户模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆