这是一些实现非托管DLL的C#源代码(advapi32).
public void AddPrivileges(string account, string privilege)
{
IntPtr pSid = GetSIDInformation(account);
LSA_UNICODE_STRING[] privileges = new LSA_UNICODE_STRING[1];
privileges[0] = InitLsaString(privilege);
uint ret = Win32Sec.LsaAddAccountRights(lsaHandle, pSid, privileges, 1);
if (ret == 0)
return;
if (ret == STATUS_ACCESS_DENIED)
{
throw new UnauthorizedAccessException();
}
if ((ret == STATUS_INSUFFICIENT_RESOURCES) || (ret == STATUS_NO_MEMORY))
{
throw new OutOfMemoryException();
}
int error = Win32Sec.LsaNtStatusToWinError((int)ret);
throw new Win32Exception(error);
}
Run Code Online (Sandbox Code Playgroud)
运行时的变量值如下:
privilege: "SeServiceLogonRight"
account: "named"
ret: 3221225485 (STATUS_INVALID_PARAMETER)
error: 87
Run Code Online (Sandbox Code Playgroud)
捕获时,Win32Exception中的消息是:"参数不正确"
代码在Windows Web Server 2008上运行.我可以验证该帐户是否存在,并且此代码在另一台服务器上正常工作......我不确定这是否可能是由Windows 2008 SP2引起的.我在想我忘记安装一些东西,但我想不出来......
代码来自: …
与大多数"遗留"MSDN页面一样,ReportEvent页面的信息太少,我无法理解它.我试过搜索,但找不到一个好的,干净的,简单的函数用法示例.有谁能建议吗?
它是一个指向LSA_UNICODE_STRING结构数组的指针.我找到了一些反向的代码,即LSA_UNICODE_STRING从C#字符串创建一个.您可以在下面的帮助程序代码部分中看到.
我LsaEnumerateAccountRights()所做的和包括呼叫似乎工作得很好.为数组指针和计数返回合理的值.
我手足无措,如何获得在那些抨击字符串.请帮忙?好吗?
更新: nobugz的帮助函数在下面的答案中几乎是正确的,你只需要将长度除以UnicodeEncoding.CharSize.多亏了他,我现在可以看到数组中的FIRST字符串了.请参阅下面两个代码部分末尾的更新.
现在,我如何做地狱世界的指针运算?
更新2.5:查看功能代码的答案.我丢失了旧的"错误"代码.
我正在与第三方库(Poco C ++)链接,并从链接器中收到以下未解决的符号错误。它似乎找不到“ CryptAcquireContextW”,“ CryptReleaseContext”和“ CryptGenRandom”。
根据此处的Microsoft信息,可以使用“ Advapi32.lib”链接这些功能。我已将其添加到链接器输入中,但符号仍未解析。
1>PocoFoundationCLR.lib(RandomStream.obj) : error LNK2019: unresolved external symbol __imp__CryptAcquireContextW@20 referenced in function "public: virtual int __thiscall Poco::RandomBuf::readFromDevice(char *,__int64)" (?readFromDevice@RandomBuf@Poco@@UAEHPAD_J@Z)
1>PocoFoundationCLR.lib(RandomStream.obj) : error LNK2019: unresolved external symbol __imp__CryptReleaseContext@8 referenced in function "public: virtual int __thiscall Poco::RandomBuf::readFromDevice(char *,__int64)" (?readFromDevice@RandomBuf@Poco@@UAEHPAD_J@Z)
1>PocoFoundationCLR.lib(RandomStream.obj) : error LNK2019: unresolved external symbol __imp__CryptGenRandom@12 referenced in function "public: virtual int __thiscall Poco::RandomBuf::readFromDevice(char *,__int64)" (?readFromDevice@RandomBuf@Poco@@UAEHPAD_J@Z)
Run Code Online (Sandbox Code Playgroud)
我已经确认Advapi32.lib在搜索路径上,而Advapi32.dll在Windows目录中,所以我不确定该错误如何继续发生。
想法,有人吗?
谢谢!
我正在开发一个ASP.net应用程序,我正在尝试冒充用户
我正在使用令牌创建一个windowsIdentity
WindowsIdentity winId = new WindowsIdenty( token );
Run Code Online (Sandbox Code Playgroud)
这个令牌是通过调用un托管代码获得的
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来获取令牌而不使用此advapi32.dll非托管代码?
TKS
我正在尝试使用此代码读取Windows中文件和目录的访问权限(在Tim Golden建议的os.access补丁之后进行模式化以使其从Windows上的ACL读取):
from ctypes import(
windll,
wintypes,
c_char_p,
c_void_p,
byref
)
from win32api import GetCurrentThread
from win32security import (
GetFileSecurity,
DACL_SECURITY_INFORMATION,
ImpersonateSelf,
SecurityImpersonation,
OpenThreadToken,
TOKEN_ALL_ACCESS,
MapGenericMask
)
from ntsecuritycon import (
FILE_READ_DATA,
FILE_WRITE_DATA,
FILE_EXECUTE,
FILE_ALL_ACCESS
)
import pywintypes
import winnt
TRUE = 1
def CheckAccess(path,AccessDesired):
result = wintypes.BOOL()
granted = wintypes.DWORD(0)
privsetlength = wintypes.DWORD(0)
fileSD = GetFileSecurity(path, DACL_SECURITY_INFORMATION)
if not fileSD.IsValid():
raise Exception("Invalid security descriptor")
ImpersonateSelf(SecurityImpersonation)
token = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE)
mapping = wintypes.DWORD(MapGenericMask(AccessDesired,
(FILE_READ_DATA, FILE_WRITE_DATA, FILE_EXECUTE, FILE_ALL_ACCESS))) …Run Code Online (Sandbox Code Playgroud) 我使用advapi32.dll的logonuser方法通过我们的网络访问数据.
我知道它将线程的用户改为我给它的信息,但我想知道是否有办法扭转它.
我想访问数据,然后返回本地用户凭据.
我正在使用该包在 Go 中编写 Windows 服务golang.org/x/sys/windows/svc。
到目前为止,一切都很顺利,而且很容易上手,我喜欢它。
我已经编写了一些自动更新功能,我希望该服务在完成更新后自行重新启动。
我尝试生成一个进程,该进程将使用 重新启动服务SCM,但它记录了一条错误消息,这似乎与在作为本地系统运行时尝试控制服务有关。
The service process could not connect to the service controller.
Run Code Online (Sandbox Code Playgroud)
更好/更简单的方法似乎是将os.Exit(1)服务Failure Actions设置为Restart on Failure,效果非常好!
唯一的麻烦是,似乎没有使用 Go 以编程方式配置这些选项的功能。
我做了一些挖掘,看起来它们是通过将结构传递给ChangeServiceConfig2in advapi32.dll- How to create service which restarts on crash 来配置的
在golang/sys/blob/master/windows/svc/mgr/config.go中-func updateDescription(handle windows.Handle, desc string) error
代码已经调用了windows.ChangeServiceConfig2DLL 调用的链接。
该SERVICE_FAILURE_ACTIONS结构的 Microsoft 文档位于此处。
我无法弄清楚如何使用 Go 构建和传递该结构 - 有人有任何见解吗?
我不明白RevertToSelf().net应用程序中有什么用.检查MSDN定义,它将定义读取为
The RevertToSelf function terminates the impersonation of a client application.
Run Code Online (Sandbox Code Playgroud)
那么,通过停止客户端的上下文,当前用户上下文是否会更改为sysadmin上下文?通过调用RevertToSelf(),我的代码将在sys管理模式下运行吗?
更新
好的,如果我在ASP.NET应用程序中调用RevertToSelf()会发生什么?让我们考虑一下我不会开始任何模仿.所以如果我调用RevertToSelf()它会恢复到应用程序池标识吗?
您能建议我如何获取特定Windows服务的“登录身份”参数吗?我需要在升级项目中重新注册服务,并且该服务必须使用与最初设置相同的帐户运行。我在返回的结构中的lpServiceStartName中找到了advapi32.dll中的QueryServiceConfig,但是我无法从Inno Setup中使它正常工作。
当我调用GetNamedSecurityInfo API 并且成功时,MSDN 指出我需要调用LocalFree,ppSecurityDescriptor但是 和ppDacl呢ppSacl?
我已经在 Windows 7 64 位中很好地设置了 GitforWindows,并将凭据管理器设置为“Wincred”。
然而,当我运行一些命令(例如git remote prune origin在 GitBash 中)时,它会在控制台中给出以下错误,但运行命令:
Failed to load advapi32.dll
Run Code Online (Sandbox Code Playgroud)
这是为什么 ?如果需要对凭据管理器设置进行某些操作,那么我需要完整解释如何进行这些设置以及每个设置的含义。
注意:我仅在某些命令上遇到此问题,其他命令运行正常,没有任何错误。