This commit is contained in:
manojampalam 2016-05-14 14:27:26 -07:00
parent bf41884f36
commit 64b305b3b2
3 changed files with 713 additions and 715 deletions

View File

@ -58,15 +58,15 @@ NTSTATUS LsaAllocUnicodeString(PUNICODE_STRING *lsaStr, DWORD maxLen)
FAIL(lsaStr == NULL);
*lsaStr = (PUNICODE_STRING) LsaApi.AllocateLsaHeap(sizeof(UNICODE_STRING));
*lsaStr = (PUNICODE_STRING)LsaApi.AllocateLsaHeap(sizeof(UNICODE_STRING));
FAIL((*lsaStr) == NULL);
(*lsaStr) -> Buffer = (WCHAR *) LsaApi.AllocateLsaHeap(sizeof(maxLen));
(*lsaStr) -> Length = 0;
(*lsaStr) -> MaximumLength = maxLen;
(*lsaStr)->Buffer = (WCHAR *)LsaApi.AllocateLsaHeap(sizeof(maxLen));
(*lsaStr)->Length = 0;
(*lsaStr)->MaximumLength = maxLen;
FAIL((*lsaStr) -> Buffer == NULL);
FAIL((*lsaStr)->Buffer == NULL);
ntStat = 0;
@ -76,7 +76,7 @@ fail:
{
if (lsaStr && (*lsaStr))
{
LsaApi.FreeLsaHeap((*lsaStr) -> Buffer);
LsaApi.FreeLsaHeap((*lsaStr)->Buffer);
LsaApi.FreeLsaHeap((*lsaStr));
}
@ -96,9 +96,9 @@ void LsaFreeUnicodeString(PUNICODE_STRING lsaStr)
{
if (lsaStr)
{
if (lsaStr -> Buffer)
if (lsaStr->Buffer)
{
LsaApi.FreeLsaHeap(lsaStr -> Buffer);
LsaApi.FreeLsaHeap(lsaStr->Buffer);
}
LsaApi.FreeLsaHeap(lsaStr);
@ -126,7 +126,7 @@ NTSTATUS FillUnicodeString(UNICODE_STRING *lsaStr, const Char *str)
FAIL(lsaStr == NULL);
FAIL(lsaStr -> Buffer == NULL);
FAIL(lsaStr->Buffer == NULL);
FAIL(str == NULL);
@ -136,21 +136,21 @@ NTSTATUS FillUnicodeString(UNICODE_STRING *lsaStr, const Char *str)
cbSize = strlen(str);
FAIL(cbSize >= lsaStr -> MaximumLength);
FAIL(cbSize >= lsaStr->MaximumLength);
//
// Fill string buffer.
//
#ifdef __VS_BUILD__
_swprintf(lsaStr -> Buffer, L"%hs", str);
_swprintf(lsaStr->Buffer, L"%hs", str);
#else
swprintf(lsaStr->Buffer, L"%hs", str);
#endif
lsaStr -> Length = cbSize * 2;
lsaStr->Length = cbSize * 2;
lsaStr -> Buffer[cbSize * 2] = 0x0000;
lsaStr->Buffer[cbSize * 2] = 0x0000;
ntStat = STATUS_SUCCESS;

View File

@ -37,57 +37,41 @@
extern "C" {
#endif
//
// Handle to 'ntdll.dll' module and address of 'RtlInitUnicodeString()'
// function.
//
RtlInitUnicodeStringPtr RtlInitUnicodeString = NULL;
HMODULE NtDll = NULL;
#ifdef DYNAMIC_OPENSSL
//
// Handle to 'libcrypto.dll' and 'libssl.dll' modules.
// Handle to 'ntdll.dll' module and address of 'RtlInitUnicodeString()'
// function.
//
HMODULE LibCrypto = NULL;
HMODULE LibSSL = NULL;
RtlInitUnicodeStringPtr RtlInitUnicodeString = NULL;
HMODULE NtDll = NULL;
//
// This is global struct with dynamic loaded libssl and libcrypto
// functions.
// This is table with addresses of LSA API functions.
// We retrieve this table from system at package initialization
// moment.
//
SSLFuncList DynSSL;
#endif
LSA_SECPKG_FUNCTION_TABLE LsaApi;
//
// This is table with addresses of LSA API functions.
// We retrieve this table from system at package initialization
// moment.
//
//
// Called once to initialize package at system startup.
//
// pkgId - our package's ID given by LSA (IN)
// func - table with adresses of LSA functions (IN)
// database - uunsed / reserved (IN)
// confident - unused / reserved (IN)
// pkgName - name of our package (OUT)
//
// RETURNS: STATUSS_SUCCESS if OK.
//
LSA_SECPKG_FUNCTION_TABLE LsaApi;
//
// Called once to initialize package at system startup.
//
// pkgId - our package's ID given by LSA (IN)
// func - table with adresses of LSA functions (IN)
// database - uunsed / reserved (IN)
// confident - unused / reserved (IN)
// pkgName - name of our package (OUT)
//
// RETURNS: STATUSS_SUCCESS if OK.
//
NTSTATUS NTAPI LsaApInitializePackage(ULONG pkgId,
NTSTATUS NTAPI LsaApInitializePackage(ULONG pkgId,
PLSA_SECPKG_FUNCTION_TABLE func,
PLSA_STRING database,
PLSA_STRING confident,
PLSA_STRING *pkgName)
{
{
//
// Save table with adresses of LSA API functions.
@ -100,36 +84,36 @@ NTSTATUS NTAPI LsaApInitializePackage(ULONG pkgId,
//
*pkgName = (PLSA_STRING) LsaApi.AllocateLsaHeap(sizeof(LSA_STRING));
*pkgName = (PLSA_STRING)LsaApi.AllocateLsaHeap(sizeof(LSA_STRING));
(*pkgName) -> Buffer = (PCHAR) LsaApi.AllocateLsaHeap(PKG_NAME_SIZE);
(*pkgName)->Buffer = (PCHAR)LsaApi.AllocateLsaHeap(PKG_NAME_SIZE);
//
// Fill buffer with our name.
//
memcpy((*pkgName) -> Buffer, PKG_NAME, PKG_NAME_SIZE);
memcpy((*pkgName)->Buffer, PKG_NAME, PKG_NAME_SIZE);
(*pkgName) -> Length = PKG_NAME_SIZE - 1;
(*pkgName)->Length = PKG_NAME_SIZE - 1;
(*pkgName) -> MaximumLength = PKG_NAME_SIZE;
(*pkgName)->MaximumLength = PKG_NAME_SIZE;
return STATUS_SUCCESS;
}
}
//
// Allocate new buffer in LSA address space and copy input SID to it.
//
// dst - pointer that retrieves new allocated copy of input SID (OUT)
// src - input SID to copy (IN)
//
// RETURNS: 0 if OK.
//
//
// Allocate new buffer in LSA address space and copy input SID to it.
//
// dst - pointer that retrieves new allocated copy of input SID (OUT)
// src - input SID to copy (IN)
//
// RETURNS: 0 if OK.
//
Int LsaCopySid(PSID &dst, PSID src)
{
Int LsaCopySid(PSID &dst, PSID src)
{
Int exitCode = 1;
DWORD size = 0;
@ -144,27 +128,27 @@ Int LsaCopySid(PSID &dst, PSID src)
exitCode = 0;
fail:
fail:
if (exitCode)
{
}
return exitCode;
}
}
//
// Allocate LSA_TOKEN_INFORMATION_V1 structure in LSA address space
// and fill it with data from given token.
//
// tokenInfo - new allocated struct with info from given token (OUT)
// token - handle to token (IN)
//
// RETURNS: 0 if OK.
//
//
// Allocate LSA_TOKEN_INFORMATION_V1 structure in LSA address space
// and fill it with data from given token.
//
// tokenInfo - new allocated struct with info from given token (OUT)
// token - handle to token (IN)
//
// RETURNS: 0 if OK.
//
Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
{
Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
{
Int exitCode = 1;
@ -201,14 +185,14 @@ Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
GetTokenInformation(token, TokenUser, NULL, 0, &cbSize);
pUserToken = (PTOKEN_USER) LocalAlloc(LPTR, cbSize);
pUserToken = (PTOKEN_USER)LocalAlloc(LPTR, cbSize);
FAIL(GetTokenInformation(token, TokenUser,
pUserToken, cbSize, &cbSize) == FALSE);
tokenInfo -> User.User.Attributes = pUserToken -> User.Attributes;
tokenInfo->User.User.Attributes = pUserToken->User.Attributes;
FAIL(LsaCopySid(tokenInfo -> User.User.Sid, pUserToken -> User.Sid));
FAIL(LsaCopySid(tokenInfo->User.User.Sid, pUserToken->User.Sid));
//
// Copy TOKEN_GROUPS part from input token.
@ -217,25 +201,25 @@ Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
GetTokenInformation(token, TokenGroups, NULL, 0, &cbSize);
pGroupsToken = (PTOKEN_GROUPS) LocalAlloc(LPTR, cbSize);
pGroupsToken = (PTOKEN_GROUPS)LocalAlloc(LPTR, cbSize);
FAIL(GetTokenInformation(token, TokenGroups,
pGroupsToken, cbSize, &cbSize) == FALSE);
cbSize = pGroupsToken -> GroupCount * sizeof(SID_AND_ATTRIBUTES) + sizeof(DWORD);
cbSize = pGroupsToken->GroupCount * sizeof(SID_AND_ATTRIBUTES) + sizeof(DWORD);
tokenInfo -> Groups = (PTOKEN_GROUPS) LsaApi.AllocateLsaHeap(cbSize);
tokenInfo->Groups = (PTOKEN_GROUPS)LsaApi.AllocateLsaHeap(cbSize);
tokenInfo -> Groups -> GroupCount = pGroupsToken -> GroupCount;
tokenInfo->Groups->GroupCount = pGroupsToken->GroupCount;
for (i = 0; i < pGroupsToken -> GroupCount; i++)
for (i = 0; i < pGroupsToken->GroupCount; i++)
{
FAIL(LsaCopySid(tokenInfo -> Groups -> Groups[i].Sid,
pGroupsToken -> Groups[i].Sid));
FAIL(LsaCopySid(tokenInfo->Groups->Groups[i].Sid,
pGroupsToken->Groups[i].Sid));
tokenInfo -> Groups -> Groups[i].Attributes = pGroupsToken -> Groups[i].Attributes;
tokenInfo->Groups->Groups[i].Attributes = pGroupsToken->Groups[i].Attributes;
}
//
@ -246,10 +230,10 @@ Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
GetTokenInformation(token, TokenPrivileges, NULL, 0, &cbSize);
tokenInfo -> Privileges = (PTOKEN_PRIVILEGES) LsaApi.AllocateLsaHeap(cbSize);
tokenInfo->Privileges = (PTOKEN_PRIVILEGES)LsaApi.AllocateLsaHeap(cbSize);
FAIL(GetTokenInformation(token, TokenPrivileges,
tokenInfo -> Privileges, cbSize, &cbSize) == FALSE);
tokenInfo->Privileges, cbSize, &cbSize) == FALSE);
//
// Copy TOKEN_OWNER part from input token.
@ -258,12 +242,12 @@ Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
GetTokenInformation(token, TokenOwner, NULL, 0, &cbSize);
pOwnerToken = (PTOKEN_OWNER) LocalAlloc(LPTR, cbSize);
pOwnerToken = (PTOKEN_OWNER)LocalAlloc(LPTR, cbSize);
FAIL(GetTokenInformation(token, TokenOwner,
pOwnerToken, cbSize, &cbSize) == FALSE);
FAIL(LsaCopySid(tokenInfo -> Owner.Owner, pOwnerToken -> Owner));
FAIL(LsaCopySid(tokenInfo->Owner.Owner, pOwnerToken->Owner));
//
// Copy TOKEN_PRIMARY_GROUP part from input token.
@ -272,13 +256,13 @@ Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
GetTokenInformation(token, TokenPrimaryGroup, NULL, 0, &cbSize);
pPrimaryGroupToken = (PTOKEN_PRIMARY_GROUP) LocalAlloc(LPTR, cbSize);
pPrimaryGroupToken = (PTOKEN_PRIMARY_GROUP)LocalAlloc(LPTR, cbSize);
FAIL(GetTokenInformation(token, TokenPrimaryGroup,
pPrimaryGroupToken, cbSize, &cbSize) == FALSE);
FAIL(LsaCopySid(tokenInfo -> PrimaryGroup.PrimaryGroup,
pPrimaryGroupToken -> PrimaryGroup));
FAIL(LsaCopySid(tokenInfo->PrimaryGroup.PrimaryGroup,
pPrimaryGroupToken->PrimaryGroup));
//
// Copy TOKEN_DEFAULT_DACL part from input token.
@ -292,18 +276,18 @@ Int LsaAllocTokenInfo(PLSA_TOKEN_INFORMATION_V1 &tokenInfo, HANDLE token)
//FAIL(GetTokenInformation(token, TokenDefaultDacl,
// pDaclToken, cbSize, &cbSize) == FALSE);
tokenInfo -> DefaultDacl.DefaultDacl = NULL;
tokenInfo->DefaultDacl.DefaultDacl = NULL;
//
// Fill expiration time. Our token never expires.
//
tokenInfo -> ExpirationTime.HighPart = 0x7fffffff;
tokenInfo -> ExpirationTime.LowPart = 0xffffffff;
tokenInfo->ExpirationTime.HighPart = 0x7fffffff;
tokenInfo->ExpirationTime.LowPart = 0xffffffff;
exitCode = 0;
fail:
fail:
//
// Clean up.
@ -320,27 +304,27 @@ fail:
return exitCode;
}
}
//
// Called, when client logon process want logon user.
//
// request - internal LSA struct for allocating client buffer (IN)
// logonType - what type of logon client need (e.g. Interactive) (IN)
// authData - buffer with authorization data (we use SshLsaAuth) (IN)
// authDataClient - adress of original authData in client address space (IN)
// authDataSize - size of authData buffer in bytes (IN)
// profile - profile data (we decide what to return) (OUT)
// profileSize - number of bytes returnet in profile (OUT)
// subStat - additional NTSTATUS code used when logon failure (OUT)
// tokenInfoType - what structure we returned to LSA in tokenInfo (OUT)
// tokenInfo - structure with token's parts for LSA (OUT)
// accountName - on which account we try to logon (OUT)
// authority - ?? We use it as domain name and fill with NULL (OUT)
//
//
// Called, when client logon process want logon user.
//
// request - internal LSA struct for allocating client buffer (IN)
// logonType - what type of logon client need (e.g. Interactive) (IN)
// authData - buffer with authorization data (we use SshLsaAuth) (IN)
// authDataClient - adress of original authData in client address space (IN)
// authDataSize - size of authData buffer in bytes (IN)
// profile - profile data (we decide what to return) (OUT)
// profileSize - number of bytes returnet in profile (OUT)
// subStat - additional NTSTATUS code used when logon failure (OUT)
// tokenInfoType - what structure we returned to LSA in tokenInfo (OUT)
// tokenInfo - structure with token's parts for LSA (OUT)
// accountName - on which account we try to logon (OUT)
// authority - ?? We use it as domain name and fill with NULL (OUT)
//
NTSTATUS NTAPI
NTSTATUS NTAPI
LsaApLogonUser(PLSA_CLIENT_REQUEST request, SECURITY_LOGON_TYPE logonType,
PVOID authData, PVOID clientAuthData, ULONG authDataSize,
PVOID *profile, PULONG profileSize, PLUID logonId,
@ -349,7 +333,7 @@ NTSTATUS NTAPI
PVOID *tokenInfo,
PLSA_UNICODE_STRING *accountName,
PLSA_UNICODE_STRING *authority)
{
{
NTSTATUS ntStat = STATUS_LOGON_FAILURE;
@ -400,7 +384,7 @@ NTSTATUS NTAPI
//
inUserName = (wchar_t *) (((char*)authData)+4);
inUserName = (wchar_t *)authData;
@ -437,7 +421,7 @@ NTSTATUS NTAPI
samUserBuf[MAX_ACCOUNT_NAME_SIZE] = 0x00;
RtlInitUnicodeString((PUNICODE_STRING) &samUser, samUserBuf);
RtlInitUnicodeString((PUNICODE_STRING)&samUser, samUserBuf);
NTFAIL(LsaApi.GetAuthDataForUser(&samUser, SecNameFlat, NULL,
&userAuth, &userAuthSize, flatName));
@ -448,7 +432,7 @@ NTSTATUS NTAPI
//
memcpy (tokenSource.SourceName, "_sshlsa_", 8);
memcpy(tokenSource.SourceName, "_sshlsa_", 8);
AllocateLocallyUniqueId(&tokenSource.SourceIdentifier);
@ -502,7 +486,7 @@ NTSTATUS NTAPI
exitCode = 0;
fail:
fail:
if (exitCode)
{
@ -530,25 +514,25 @@ fail:
return ntStat;
}
//
// This functions is called, after session closed. This is only
// information for package and we don't need to do anything here.
//
VOID NTAPI LsaApLogonTerminated(PLUID logonId)
{
}
//
// DllMain function (called when DLL is loaded or unloaded)
//
//
// This functions is called, after session closed. This is only
// information for package and we don't need to do anything here.
//
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpRes)
{
VOID NTAPI LsaApLogonTerminated(PLUID logonId)
{
}
//
// DllMain function (called when DLL is loaded or unloaded)
//
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpRes)
{
BOOL exitCode = FALSE;
switch (dwReason)
@ -601,7 +585,7 @@ BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpRes)
exitCode = TRUE;
fail:
fail:
if (exitCode == FALSE)
{
@ -610,52 +594,52 @@ fail:
}
return exitCode;
}
}
//
// For compatibility only.
//
//
// For compatibility only.
//
NTSTATUS NTAPI LsaApCallPackagePassthrough(PLSA_CLIENT_REQUEST request,
NTSTATUS NTAPI LsaApCallPackagePassthrough(PLSA_CLIENT_REQUEST request,
PVOID submitBuf,
PVOID clientBufBase,
ULONG submitBufSize,
PVOID *outBuf,
PULONG outBufSize,
PNTSTATUS status)
{
{
return STATUS_NOT_IMPLEMENTED;
}
}
//
// For compatibility only.
//
//
// For compatibility only.
//
NTSTATUS NTAPI LsaApCallPackageUntrusted(PLSA_CLIENT_REQUEST request,
NTSTATUS NTAPI LsaApCallPackageUntrusted(PLSA_CLIENT_REQUEST request,
PVOID submitBuf,
PVOID clientBufBase,
ULONG submitBufSize,
PVOID *outBuf,
PULONG outBufSize,
PNTSTATUS status)
{
{
return STATUS_NOT_IMPLEMENTED;
}
}
//
// For compatibility only.
//
//
// For compatibility only.
//
NTSTATUS NTAPI LsaApCallPackage(PLSA_CLIENT_REQUEST request, PVOID submitBuf,
NTSTATUS NTAPI LsaApCallPackage(PLSA_CLIENT_REQUEST request, PVOID submitBuf,
PVOID clientBufBase, ULONG submitBufSize,
PVOID *outBuf, PULONG outBufSize,
PNTSTATUS status)
{
{
return STATUS_NOT_IMPLEMENTED;
}
}
#ifdef __cplusplus
}

View File

@ -56,7 +56,7 @@ generate_user_token(wchar_t* user) {
LSA_OPERATIONAL_MODE mode;
ULONG auth_package_id;
NTSTATUS ret, subStatus;
KERB_S4U_LOGON *s4u_logon = NULL;
void * logon_info = NULL;
size_t logon_info_size;
LSA_STRING logon_process_name, auth_package_name, originName;
TOKEN_SOURCE sourceContext;
@ -64,10 +64,14 @@ generate_user_token(wchar_t* user) {
LUID logonId;
QUOTA_LIMITS quotas;
DWORD cbProfile;
BOOL domain_user = (wcschr(user, L'@') != NULL)? TRUE : FALSE;
InitLsaString(&logon_process_name, "ssh-agent");
//InitLsaString(&auth_package_name, MICROSOFT_KERBEROS_NAME_A);
InitLsaString(&auth_package_name, "Negotiate");
if (domain_user)
InitLsaString(&auth_package_name, MICROSOFT_KERBEROS_NAME_A);
else
InitLsaString(&auth_package_name, "SSH-LSA");
InitLsaString(&originName, "sshd");
if (ret = LsaRegisterLogonProcess(&logon_process_name, &lsa_handle, &mode) != STATUS_SUCCESS)
goto done;
@ -75,12 +79,14 @@ generate_user_token(wchar_t* user) {
if (ret = LsaLookupAuthenticationPackage(lsa_handle, &auth_package_name, &auth_package_id) != STATUS_SUCCESS)
goto done;
if (domain_user) {
KERB_S4U_LOGON *s4u_logon;
logon_info_size = sizeof(KERB_S4U_LOGON);
logon_info_size += (wcslen(user) * 2 + 2);
s4u_logon = malloc(logon_info_size);
if (s4u_logon == NULL)
logon_info = malloc(logon_info_size);
if (logon_info == NULL)
goto done;
s4u_logon = (KERB_S4U_LOGON*)logon_info;
s4u_logon->MessageType = KerbS4ULogon;
s4u_logon->Flags = 0;
s4u_logon->ClientUpn.Length = wcslen(user) * 2;
@ -90,8 +96,16 @@ generate_user_token(wchar_t* user) {
s4u_logon->ClientRealm.Length = 0;
s4u_logon->ClientRealm.MaximumLength = 0;
s4u_logon->ClientRealm.Buffer = 0;
}
else {
logon_info_size = (wcslen(user) + 1)*sizeof(wchar_t);
logon_info = malloc(logon_info_size);
if (logon_info == NULL)
goto done;
memcpy(logon_info, user, logon_info_size);
}
memcpy(sourceContext.SourceName,".Jobs ", sizeof(sourceContext.SourceName));
memcpy(sourceContext.SourceName,"sshagent", sizeof(sourceContext.SourceName));
if (AllocateLocallyUniqueId(&sourceContext.SourceIdentifier) != TRUE)
goto done;
@ -100,7 +114,7 @@ generate_user_token(wchar_t* user) {
&originName,
Network,
auth_package_id,
s4u_logon,
logon_info,
logon_info_size,
NULL,
&sourceContext,
@ -115,8 +129,8 @@ generate_user_token(wchar_t* user) {
done:
if (lsa_handle)
LsaDeregisterLogonProcess(lsa_handle);
if (s4u_logon)
free(s4u_logon);
if (logon_info)
free(logon_info);
if (pProfile)
LsaFreeReturnBuffer(pProfile);