是否有可能为Windows服务模拟用户没有密码?有可能、密码、用户、Windows

2023-09-02 23:47:35 作者:东风寄千愁

是否有可能为一个C#Windows服务运行的本地系统或本地服务来模拟其他用户的无需为用户的密码的?

Is it possible for a C# Windows service running as Local System or Local Service to impersonate another user without needing a password for that user?

这将如何做?

注意:我这样做的动机是为了能够在服务运行用户特定的WMI查询。该WMI调用我正在(对OfflineFiles WMI API)是用户敏感,他们只当我运行我的服务,我要查询其数据的用户使用。我不希望用户必须输入安装服务时的用户名和密码,所以我想只要运行服务作为本地系统或某事,并模仿我关心用户。

Note: My motivation for this is to be able to run user specific WMI queries in a service. The WMI calls I'm making (to the OfflineFiles WMI API) are user sensitive, and they only work when I run my service as the user whose data I want to query. I don't want users to have to enter their usernames and passwords when installing the service, so I'd like to just run the service as Local System or something, and impersonate the user I care about.

推荐答案

假设你只需要启动的模拟,同时相关的用户登录,您可以:

Assuming you only need start impersonation whilst the relevant user is logged on, you could:

在使用EnumProcesses(如的 http://msdn.microsoft.com/en-us/library/windows/desktop/ms682623(V = vs.85)的.aspx )[WINAPI] OpenProcessToken()相关用户进程[WINAPI] DuplicateToken()与模拟特权[WINAPI] 使用DuplicateToken的结果创建一个新的WindowsIdentity() 从第4步呼叫.Impersonate你的新身份 Locate relevant user session using EnumProcesses (eg http://msdn.microsoft.com/en-us/library/windows/desktop/ms682623(v=vs.85).aspx) [winapi] OpenProcessToken() on relevant user process [winapi] DuplicateToken() with impersonation privileges [winapi] Create a new WindowsIdentity() using the result of DuplicateToken Call .Impersonate on your new identity from step 4

在令牌已被复制,它不如果用户登录无关紧要 - 保留在服务的​​模拟

Once the token has been duplicated, it doesn't matter if the user logs of - the impersonation in your service remains.

显然,API的无证ZwCreateToken WINAPI功能,可以实现这一点,虽然还可以,但我从来没有使用它,可以在任何时间在未来的突破。

Apparently the API the undocumented ZwCreateToken winapi function can achieve this although also, but I have never used it and may break at anytime in future.