BOOL MyLogonUser(HANDLE hLsa, ULONG uPackageId, LPWSTR lpszUserName, LPWSTR lpszPassword, LPWSTR lpszDomainName, HANDLE *phToken,MSV1_0_INTERACTIVE_PROFILE *pmsvProfile,ULONG *uProfileLength,QUOTA_LIMITS *limits) { LUID luid; ULONG uBufferLength; //ULONG uProfileLength; LPBYTE lp; NTSTATUS ns; NTSTATUS nsSub; LSA_STRING lsaOriginal; TOKEN_SOURCE tokenSource; MSV1_0_INTERACTIVE_LOGON msvLogon; PMSV1_0_INTERACTIVE_LOGON pmsvLogon; InitString(&lsaOriginal, "origial");
uBufferLength = sizeof(MSV1_0_INTERACTIVE_LOGON); uBufferLength += InitUnicodeString(&msvLogon.UserName, lpszUserName); uBufferLength += InitUnicodeString(&msvLogon.Password, lpszPassword); uBufferLength += InitUnicodeString(&msvLogon.LogonDomainName, lpszDomainName); msvLogon.MessageType = MsV1_0InteractiveLogon;
lp = (LPBYTE)LocalAlloc(LPTR, uBufferLength); CopyMemory(lp, (PVOID)&msvLogon, sizeof(MSV1_0_INTERACTIVE_LOGON));
pmsvLogon = (PMSV1_0_INTERACTIVE_LOGON)lp; lp += sizeof(MSV1_0_INTERACTIVE_LOGON);
FormatBuffer(&lp, &pmsvLogon->UserName); lp += pmsvLogon->UserName.Length;
FormatBuffer(&lp, &pmsvLogon->Password); lp += pmsvLogon->Password.Length; FormatBuffer(&lp, &pmsvLogon->LogonDomainName); lstrcpyA(tokenSource.SourceName, "sample");
AllocateLocallyUniqueId(&tokenSource.SourceIdentifier);
ns = LsaLogonUser(hLsa, &lsaOriginal, Interactive, uPackageId, (PVOID)pmsvLogon, uBufferLength, NULL, &tokenSource, (PVOID *)pmsvProfile, uProfileLength, &luid, phToken, limits, &nsSub); if (LsaNtStatusToWinError(ns) != ERROR_SUCCESS) { LocalFree(pmsvLogon); return FALSE; }
LocalFree(pmsvLogon); return TRUE; } void FormatBuffer(LPBYTE *lp, PUNICODE_STRING pString) { if (pString->Buffer != NULL) { CopyMemory(*lp, pString->Buffer, pString->Length); pString->Buffer = (LPWSTR)*lp; } } ULONG InitString(PLSA_STRING plsaString, LPSTR lpszString) { if (lpszString == NULL) { plsaString->Length = 0; plsaString->MaximumLength = 0; plsaString->Buffer = NULL; } else { plsaString->Length = (USHORT)(lstrlenA(lpszString) * sizeof(CHAR)); plsaString->MaximumLength = plsaString->Length + sizeof(CHAR); plsaString->Buffer = lpszString; } return plsaString->Length; } ULONG InitUnicodeString(PLSA_UNICODE_STRING plsaString, LPWSTR lpszString) { if (lpszString == NULL) { plsaString->Length = 0; plsaString->MaximumLength = 0; plsaString->Buffer = NULL; } else { plsaString->Length = (USHORT)(lstrlenW(lpszString) * sizeof(WCHAR)); plsaString->MaximumLength = plsaString->Length + sizeof(WCHAR); plsaString->Buffer = lpszString; } return plsaString->Length; } BOOL EnablePrivilege(LPTSTR lpszPrivilege, BOOL bEnable) { BOOL bResult; LUID luid; HANDLE hToken; TOKEN_PRIVILEGES tokenPrivileges; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) return FALSE; if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) { CloseHandle(hToken); return FALSE; } tokenPrivileges.PrivilegeCount = 1; tokenPrivileges.Privileges[0].Luid = luid; tokenPrivileges.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL); CloseHandle(hToken); return bResult && GetLastError() == ERROR_SUCCESS; } __declspec(dllexport) BOOL WINAPI LogonUserExW( wchar_t* lpszUsername, wchar_t* lpszDomain, wchar_t* lpszPassword, DWORD dwLogonType, DWORD dwLogonProvider, PHANDLE phToken, PSID *ppLogonSid, PVOID ppProfileBuffer, PULONG pdwProfileLength, PQUOTA_LIMITS pQuotaLimits ){ ULONG uPackageId; HANDLE hLsa; NTSTATUS ns; LSA_STRING lsaString; HTOKEN hToken;
ns = LsaConnectUntrusted(&hLsa); if (LsaNtStatusToWinError(ns) != ERROR_SUCCESS) { return 0; } InitString(&lsaString, MSV1_0_PACKAGE_NAME); ns = LsaLookupAuthenticationPackage(hLsa, &lsaString, &uPackageId); if (LsaNtStatusToWinError(ns) != ERROR_SUCCESS) { LsaDeregisterLogonProcess(hLsa); return 0; } if (! MyLogonUser(hLsa, uPackageId, lpszUsername,lpszPassword, lpszDomain, &hToken, (struct _MSV1_0_INTERACTIVE_PROFILE *)ppProfileBuffer,pdwProfileLength,pQuotaLimits)) { LsaDeregisterLogonProcess(hLsa); return 0; }
if(ppLogonSid) { DWORD req; TOKEN_USER *sid; SID *sid2; req=0; GetTokenInformation(hToken,TokenUser,0,0,&req); if(req){ sid=(TOKEN_USER*)LocalAlloc(0x40,req); GetTokenInformation(hToken,TokenUser,sid,req,&req); DWORD zz=GetLengthSid(sid->User.Sid); sid2=(SID*)LocalAlloc(0x40,zz); memcpy(sid2,sid->User.Sid,zz); LocalFree(sid); *ppLogonSid=sid2; } } if(phToken) { *phToken = hToken; }
LsaDeregisterLogonProcess(hLsa); return 1; } |
Comments