冒充Windows用户

冒充Windows用户,第1张

概述冒充Windows用户

我正在使用代码来模拟用户帐户来访问文件共享。

public class Impersonator : Idisposable { #region Public methods. // ------------------------------------------------------------------ /// <summary> /// Constructor. Starts the impersonation with the given credentials. /// Please note that the account that instantiates the Impersonator class /// needs to have the 'Act as part of operating system' privilege set. /// </summary> /// <param name="username">The name of the user to act as.</param> /// <param name="domainname">The domain name of the user to act as.</param> /// <param name="password">The password of the user to act as.</param> public Impersonator( string username,string domainname,string password ) { ImpersonateValIDUser( username,domainname,password ); } // ------------------------------------------------------------------ #endregion #region Idisposable member. // ------------------------------------------------------------------ public voID dispose() { UndoImpersonation(); } // ------------------------------------------------------------------ #endregion #region P/Invoke. // ------------------------------------------------------------------ [Dllimport("advAPI32.dll",SetLastError=true)] private static extern int logonUser( string lpszUsername,string lpszDomain,string lpszPassword,int DWlogonType,int DWlogonProvIDer,ref IntPtr phToken); [Dllimport("advAPI32.dll",CharSet=CharSet.auto,SetLastError=true)] private static extern int Duplicatetoken( IntPtr hToken,int impersonationLevel,ref IntPtr hNewToken); [Dllimport("advAPI32.dll",SetLastError=true)] private static extern bool RevertToSelf(); [Dllimport("kernel32.dll",CharSet=CharSet.auto)] private static extern bool CloseHandle( IntPtr handle); private const int logoN32_logoN_INteraCTIVE = 2; private const int logoN32_PROVIDER_DEFAulT = 0; // ------------------------------------------------------------------ #endregion #region Private member. // ------------------------------------------------------------------ /// <summary> /// Does the actual impersonation. /// </summary> /// <param name="username">The name of the user to act as.</param> /// <param name="domainname">The domain name of the user to act as.</param> /// <param name="password">The password of the user to act as.</param> private voID ImpersonateValIDUser( string username,string domain,string password ) { windowsIDentity tempwindowsIDentity = null; IntPtr token = IntPtr.Zero; IntPtr tokenDuplicate = IntPtr.Zero; try { if ( RevertToSelf() ) { if ( logonUser( username,domain,password,logoN32_logoN_INteraCTIVE,logoN32_PROVIDER_DEFAulT,ref token ) != 0 ) { if ( Duplicatetoken( token,2,ref tokenDuplicate ) != 0 ) { tempwindowsIDentity = new windowsIDentity( tokenDuplicate ); impersonationContext = tempwindowsIDentity.Impersonate(); } else { throw new Win32Exception( Marshal.GetLastWin32Error() ); } } else { throw new Win32Exception( Marshal.GetLastWin32Error() ); } } else { throw new Win32Exception( Marshal.GetLastWin32Error() ); } } finally { if ( token!= IntPtr.Zero ) { CloseHandle( token ); } if ( tokenDuplicate!=IntPtr.Zero ) { CloseHandle( tokenDuplicate ); } } } /// <summary> /// Reverts the impersonation. /// </summary> private voID UndoImpersonation() { if ( impersonationContext!=null ) { impersonationContext.Undo(); } } private windowsImpersonationContext impersonationContext = null; // ------------------------------------------------------------------ #endregion }

然后使用:

using (new Impersonator("username","domain","password")) { Process.Start("explorer.exe",@"/root,\server01-Prodabc"); }

我得到一个“访问被拒绝”的错误。

这个用户可以直接访问这个共享。 我可以映射一个驱动器,使用“净使用”,但这个代码将无法正常工作。 现在我想这是代码。 有人看到什么吗? 有没有更好的方法来做到这一点?

如何通过非交互式login模拟获得提升权限(UAC)?

如何从服务中正确模拟用户?

调用ImpersonateSelf()是否取消对同一线程进行的所有安全令牌调整?

在Python脚本中以windows(不是admin)上的不同用户身份运行程序

linux的windows Mobile模拟器

windows模仿:软膏中的一个缺陷

windows模拟networking电缆丢失

babun:从windows剪贴板复制/粘贴到VIM?

使用advAPI32.dll:logonUserA()模拟远程计算机的本地用户

CreateProcessWithUser无法模拟错误87,1349的用户

尝试这个 :

[Dllimport("advAPI32.dll",SetLastError = true)] public static extern bool logonUser( string lpszUsername,out IntPtr phToken);

用法:

IntPtr userToken = IntPtr.Zero; bool success = External.logonUser( "john.doe","domain.com","MyPassword",(int) AdvAPI32Utility.logonType.logoN32_logoN_INteraCTIVE,//2 (int) AdvAPI32Utility.logonProvIDer.logoN32_PROVIDER_DEFAulT,//0 out userToken); if (!success) { throw new SecurityException("logon user Failed"); } using (windowsIDentity.Impersonate(userToken)) { Process.Start("explorer.exe",\server01-Prodabc"); }

如果我理解正确,你的意图是在模拟上下文中运行这个过程。

来自CreateProcess的文档(由Process.Start使用)表示:如果调用进程正在模拟另一个用户,则新进程使用该调用进程的标记,而不是模拟标记。 要在由模拟令牌表示的用户的安全上下文中运行新进程,请使用CreateProcessAsUser或CreateProcessWithlogonW函数。

所以,你使用错误的API来做到这一点。

而不是使用你的Impersonator类,当你调用Process.Start并传入一个包含用户名,密码和域名的processstartinfo实例时,会发生什么情况?

也许,如果这样的话,那么你的Impersonator类应该创建一个processstartinfo实例并使用它来创建新的进程(在类中封装它)。

var psi = new processstartinfo("explorer.exe",\server01-Prodabc"); psi.Domain = domain; psi.Username = username; psi.Password = password; psi.WorkingDirectory = workingDir; Process.Start(psi);

另外,根据MSDN文档 …

在processstartinfo对象中设置Domain,Username和Password属性是使用用户凭据启动进程的推荐做法。

您还应该在启动具有不同用户信誉的进程时设置工作目录。

总结

以上是内存溢出为你收集整理的冒充Windows用户全部内容,希望文章能够帮你解决冒充Windows用户所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1289065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存