使用具有有限用户帐户的SetSystemTime

时间:2011-05-23 10:50:00

标签: c# security system privileges

操作系统:Windows XP(嵌入式) 语言:c#

问题: 使用受限用户帐户,我尝试使用函数 SetSystemTime()以编程方式更改Windows XP的日期和时间,但它返回false,错误代码为5 :访问被拒绝

在阅读MSDN文章后,我通过使用LogonUser()和Impersonate()函数模拟管理员用户(属于管理员组并有权更改系统时间)的有限用户帐户,并在SetSystemTime()之后调用,但是结果和以前一样。

我尝试将限制用户帐户的权限“SeSystemtimePrivilege”赋予有限的用户帐户,然后在调用AdjustTokenPrivileges()之前模拟它,返回没有错误,但结果与之前相同。

代码:

const int  SE_PRIVILEGE_ENABLED = 2;

// Starting with limited user account
intPtr userToken = WindowsIdentity.GetCurrent(TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query).Token;

IntPtr tokenDuplicate = IntPtr.Zero;
IntPtr token = IntPtr.Zero;

LogonUser("administrator", domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token);
DuplicateToken(token, 2, ref tokenDuplicate);
WindowsImpersonationContext wic = (new WindowsIdentity(tokenDuplicate)).Impersonate();

bool enabled = true;

TOKEN_PRIVILEGES tokenPrivilege = new TOKEN_PRIVILEGES();
tokenPrivilege.PrivilegeCount = 1;
tokenPrivilege.Privileges = new LUID_AND_ATTRIBUTES[tokenPrivilege.PrivilegeCount];
tokenPrivilege.Privileges[0].Attributes = (UInt32)(enabled ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_DISABLED);

if (LookupPrivilegeValue(null, "SeSystemtimePrivilege", out tokenPrivilege.Privileges[0].Luid))
    AdjustTokenPrivileges(userToken, false, ref tokenPrivilege, (UInt32)Marshal.SizeOf(typeof(TOKEN_PRIVILEGES)), IntPtr.Zero, IntPtr.Zero);

// Return to limited user account
wic.Undo();

if(!SetSystemTime(systemTime)) // systemTime in UTC time
   .... Error code here, 5 if I let administrator impersonate or 1314

您是否知道如何解决我的问题?

感谢您的回答, 阿兰

2 个答案:

答案 0 :(得分:1)

我最近发布了一个类似的问题,不同的角度,但基本需求相同,该问题的标题是"如何将本地安全策略权限检查为非管理员"供参考。

无论如何,如果您在本地安全策略中明确授予该标准用户“更改系统时间”权限,则该非管理员可以正常访问SetSystemTime。我在问题中解释了我是如何解决我的特殊需求的,也许它可能对你有所帮助?

祝你好运!

答案 1 :(得分:0)