📄 warwinntlsa.c
字号:
#include "StdAfx.h"#include "WarWinntLsa.h" /////////////////////////////// PUBLIC ///////////////////////////////////////void WarInitLsaString(PLSA_UNICODE_STRING LsaString, LPCWSTR String){ DWORD StringLength; if (String == NULL) { LsaString->Buffer = NULL; LsaString->Length = 0; LsaString->MaximumLength = 0; return; } StringLength = wcslen(String); LsaString->Buffer = (LPWSTR)String; LsaString->Length = (USHORT) StringLength * sizeof(WCHAR); LsaString->MaximumLength = (USHORT) (StringLength + 1) * sizeof(WCHAR); }DWORD WarOpenPolicy(LPWSTR ServerName, DWORD DesiredAccess, PLSA_HANDLE PolicyHandle){ DWORD Error; LSA_OBJECT_ATTRIBUTES ObjectAttributes; LSA_UNICODE_STRING ServerString; PLSA_UNICODE_STRING Server = NULL; // // Always initialize the object attributse to all zeroes // ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes)); if (ServerName != NULL) { // // Make a LSA_UNICODE_STRING out of the LPWSTR passed in // WarInitLsaString(&ServerString, ServerName); Server = &ServerString; } // // Attempt to open the policy for all access // Error = LsaNtStatusToWinError( LsaOpenPolicy( Server, &ObjectAttributes, DesiredAccess, PolicyHandle)); return Error;}PSID WarGetAccountSid(LSA_HANDLE PolicyHandle, LPCWSTR AccountName, DWORD *pError){ PLSA_TRANSLATED_SID TranslatedSid; PSID Sid; PSID DomainSid; PLSA_REFERENCED_DOMAIN_LIST Domains; LSA_UNICODE_STRING AccountString; DWORD NewSidLength; DWORD SubAuthorityCount; *pError = 0; // // Convert the string to a LSA_UNICODE_STRING // WarInitLsaString(&AccountString, AccountName); // // Call the LSA to lookup the name // if ((*pError = LsaNtStatusToWinError( LsaLookupNames( PolicyHandle, 1, &AccountString, &Domains, &TranslatedSid))) != 0) return(NULL); // // Build a SID from the Domain SID and account RID // DomainSid = Domains->Domains[TranslatedSid->DomainIndex].Sid; // // Compute the length of the new SID. This is the length required for the // number of subauthorities in the domain sid plus one for the user RID. // SubAuthorityCount = *GetSidSubAuthorityCount(DomainSid); NewSidLength = GetSidLengthRequired( (UCHAR) (SubAuthorityCount + 1) ); Sid = LocalAlloc(0,NewSidLength); if (Sid == NULL) { LsaFreeMemory(Domains); LsaFreeMemory(TranslatedSid); return(NULL); } // // Build the SID by copying the domain SID and, increasing the sub- // authority count in the new sid by one, and setting the last // subauthority to be the relative id of the user. // CopySid(NewSidLength, Sid, DomainSid); *GetSidSubAuthorityCount(Sid) = (UCHAR) SubAuthorityCount + 1; *GetSidSubAuthority(Sid, SubAuthorityCount) = TranslatedSid->RelativeId; LsaFreeMemory(Domains); LsaFreeMemory(TranslatedSid); return(Sid);}DWORD WarAddUserRightToAccount(LSA_HANDLE PolicyHandle, LPCWSTR AccountName, LPCWSTR UserRightName){ DWORD Error = 0; PSID AccountSid; LSA_UNICODE_STRING UserRightString; AccountSid = WarGetAccountSid(PolicyHandle, AccountName, &Error); if (AccountSid == NULL) return(Error); // // Create a LSA_UNICODE_STRING for the right name // WarInitLsaString(&UserRightString,UserRightName); Error = LsaNtStatusToWinError( LsaAddAccountRights( PolicyHandle, AccountSid, &UserRightString, 1)); if (0 == Error) LocalFree(AccountSid); return(Error);}void WarCloseLsa(PLSA_HANDLE PolicyHandle){ LsaClose(*PolicyHandle); *PolicyHandle = NULL;}/////////////////////////////// PRIVATE ///////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -