2012-03-19 04:30:20 +04:00
|
|
|
/**
|
2012-05-22 06:48:33 +04:00
|
|
|
* WinPR: Windows Portable Runtime
|
2012-03-19 04:30:20 +04:00
|
|
|
* NTLM Security Package (AV_PAIRs)
|
|
|
|
*
|
|
|
|
* Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ntlm.h"
|
|
|
|
#include "../sspi.h"
|
|
|
|
|
2012-05-23 11:08:24 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-05-25 06:40:46 +04:00
|
|
|
#include <winpr/print.h>
|
2012-06-20 02:06:43 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-03-19 06:02:23 +04:00
|
|
|
#include "ntlm_compute.h"
|
|
|
|
|
2012-03-19 04:30:20 +04:00
|
|
|
#include "ntlm_av_pairs.h"
|
|
|
|
|
2012-07-02 01:05:31 +04:00
|
|
|
const char* const AV_PAIR_STRINGS[] =
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
|
|
|
"MsvAvEOL",
|
|
|
|
"MsvAvNbComputerName",
|
|
|
|
"MsvAvNbDomainName",
|
|
|
|
"MsvAvDnsComputerName",
|
|
|
|
"MsvAvDnsDomainName",
|
|
|
|
"MsvAvDnsTreeName",
|
|
|
|
"MsvAvFlags",
|
|
|
|
"MsvAvTimestamp",
|
|
|
|
"MsvAvRestrictions",
|
|
|
|
"MsvAvTargetName",
|
|
|
|
"MsvChannelBindings"
|
|
|
|
};
|
|
|
|
|
2012-07-01 22:33:36 +04:00
|
|
|
void ntlm_av_pair_list_init(NTLM_AV_PAIR* pAvPairList)
|
|
|
|
{
|
|
|
|
NTLM_AV_PAIR* pAvPair = pAvPairList;
|
|
|
|
|
|
|
|
pAvPair->AvId = MsvAvEOL;
|
|
|
|
pAvPair->AvLen = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-02 01:05:31 +04:00
|
|
|
ULONG ntlm_av_pair_list_length(NTLM_AV_PAIR* pAvPairList)
|
|
|
|
{
|
|
|
|
ULONG length;
|
|
|
|
NTLM_AV_PAIR* pAvPair = pAvPairList;
|
|
|
|
|
|
|
|
if (!pAvPair)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (pAvPair->AvId != MsvAvEOL)
|
|
|
|
{
|
|
|
|
pAvPair = ntlm_av_pair_get_next_pointer(pAvPair);
|
|
|
|
}
|
|
|
|
|
|
|
|
length = (pAvPair - pAvPairList) + sizeof(NTLM_AV_PAIR);
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ntlm_print_av_pair_list(NTLM_AV_PAIR* pAvPairList)
|
|
|
|
{
|
|
|
|
NTLM_AV_PAIR* pAvPair = pAvPairList;
|
|
|
|
|
|
|
|
if (!pAvPair)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("AV_PAIRs =\n{\n");
|
|
|
|
|
|
|
|
while (pAvPair->AvId != MsvAvEOL)
|
|
|
|
{
|
|
|
|
printf("\t%s AvId: %d AvLen: %d\n",
|
|
|
|
AV_PAIR_STRINGS[pAvPair->AvId],
|
|
|
|
pAvPair->AvId, pAvPair->AvLen);
|
|
|
|
|
|
|
|
winpr_HexDump(ntlm_av_pair_get_value_pointer(pAvPair), pAvPair->AvLen);
|
|
|
|
|
|
|
|
pAvPair = ntlm_av_pair_get_next_pointer(pAvPair);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
|
2012-07-01 22:33:36 +04:00
|
|
|
ULONG ntlm_av_pair_list_size(ULONG AvPairsCount, ULONG AvPairsValueLength)
|
|
|
|
{
|
|
|
|
/* size of headers + value lengths + terminating MsvAvEOL AV_PAIR */
|
|
|
|
return (AvPairsCount + 1) * sizeof(NTLM_AV_PAIR) + AvPairsValueLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
PBYTE ntlm_av_pair_get_value_pointer(NTLM_AV_PAIR* pAvPair)
|
|
|
|
{
|
|
|
|
return &((PBYTE) pAvPair)[sizeof(NTLM_AV_PAIR)];
|
|
|
|
}
|
|
|
|
|
|
|
|
int ntlm_av_pair_get_next_offset(NTLM_AV_PAIR* pAvPair)
|
|
|
|
{
|
|
|
|
return pAvPair->AvLen + sizeof(NTLM_AV_PAIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
NTLM_AV_PAIR* ntlm_av_pair_get_next_pointer(NTLM_AV_PAIR* pAvPair)
|
|
|
|
{
|
|
|
|
return (NTLM_AV_PAIR*) ((PBYTE) pAvPair + ntlm_av_pair_get_next_offset(pAvPair));
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
NTLM_AV_PAIR* ntlm_av_pair_get(NTLM_AV_PAIR* pAvPairList, NTLM_AV_ID AvId)
|
2012-07-01 22:33:36 +04:00
|
|
|
{
|
|
|
|
NTLM_AV_PAIR* pAvPair = pAvPairList;
|
|
|
|
|
|
|
|
if (!pAvPair)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (pAvPair->AvId == AvId)
|
|
|
|
return pAvPair;
|
|
|
|
|
|
|
|
if (pAvPair->AvId == MsvAvEOL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pAvPair = ntlm_av_pair_get_next_pointer(pAvPair);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
NTLM_AV_PAIR* ntlm_av_pair_add(NTLM_AV_PAIR* pAvPairList, NTLM_AV_ID AvId, PBYTE Value, UINT16 AvLen)
|
2012-07-01 22:33:36 +04:00
|
|
|
{
|
|
|
|
NTLM_AV_PAIR* pAvPair;
|
|
|
|
|
2012-07-02 01:05:31 +04:00
|
|
|
pAvPair = ntlm_av_pair_get(pAvPairList, MsvAvEOL);
|
2012-07-01 22:33:36 +04:00
|
|
|
|
|
|
|
if (!pAvPair)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pAvPair->AvId = AvId;
|
2012-07-02 05:40:33 +04:00
|
|
|
pAvPair->AvLen = AvLen;
|
2012-07-01 22:33:36 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
CopyMemory(ntlm_av_pair_get_value_pointer(pAvPair), Value, AvLen);
|
2012-07-01 22:33:36 +04:00
|
|
|
|
|
|
|
return pAvPair;
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
NTLM_AV_PAIR* ntlm_av_pair_add_copy(NTLM_AV_PAIR* pAvPairList, NTLM_AV_PAIR* pAvPair)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
NTLM_AV_PAIR* pAvPairCopy;
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
pAvPairCopy = ntlm_av_pair_get(pAvPairList, MsvAvEOL);
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (!pAvPairCopy)
|
|
|
|
return NULL;
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
pAvPairCopy->AvId = pAvPair->AvId;
|
|
|
|
pAvPairCopy->AvLen = pAvPair->AvLen;
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
CopyMemory(ntlm_av_pair_get_value_pointer(pAvPairCopy),
|
|
|
|
ntlm_av_pair_get_value_pointer(pAvPair), pAvPair->AvLen);
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
return pAvPairCopy;
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-01 22:33:36 +04:00
|
|
|
void ntlm_get_target_computer_name(PUNICODE_STRING pName, COMPUTER_NAME_FORMAT type)
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
DWORD nSize = 0;
|
|
|
|
|
|
|
|
GetComputerNameExA(type, NULL, &nSize);
|
|
|
|
name = malloc(nSize);
|
|
|
|
GetComputerNameExA(type, name, &nSize);
|
|
|
|
|
|
|
|
if (type == ComputerNameNetBIOS)
|
|
|
|
CharUpperA(name);
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-01 22:33:36 +04:00
|
|
|
pName->Length = strlen(name) * 2;
|
|
|
|
pName->Buffer = (PWSTR) malloc(pName->Length);
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, name, strlen(name),
|
|
|
|
(LPWSTR) pName->Buffer, pName->Length / 2);
|
|
|
|
|
|
|
|
pName->MaximumLength = pName->Length;
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
void ntlm_construct_challenge_target_info(NTLM_CONTEXT* context)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
|
|
|
int length;
|
2012-07-01 22:33:36 +04:00
|
|
|
ULONG AvPairsCount;
|
|
|
|
ULONG AvPairsLength;
|
|
|
|
LONG AvPairListSize;
|
|
|
|
NTLM_AV_PAIR* pAvPairList;
|
|
|
|
UNICODE_STRING NbDomainName;
|
|
|
|
UNICODE_STRING NbComputerName;
|
|
|
|
UNICODE_STRING DnsDomainName;
|
|
|
|
UNICODE_STRING DnsComputerName;
|
|
|
|
|
|
|
|
ntlm_get_target_computer_name(&NbDomainName, ComputerNameNetBIOS);
|
|
|
|
ntlm_get_target_computer_name(&NbComputerName, ComputerNameNetBIOS);
|
|
|
|
ntlm_get_target_computer_name(&DnsDomainName, ComputerNameDnsDomain);
|
|
|
|
ntlm_get_target_computer_name(&DnsComputerName, ComputerNameDnsHostname);
|
|
|
|
|
|
|
|
AvPairsCount = 5;
|
|
|
|
AvPairsLength = NbDomainName.Length + NbComputerName.Length +
|
|
|
|
DnsDomainName.Length + DnsComputerName.Length + 8;
|
|
|
|
|
|
|
|
length = ntlm_av_pair_list_size(AvPairsCount, AvPairsLength);
|
2012-07-02 05:40:33 +04:00
|
|
|
sspi_SecBufferAlloc(&context->ChallengeTargetInfo, length);
|
2012-06-20 02:06:43 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
pAvPairList = (NTLM_AV_PAIR*) context->ChallengeTargetInfo.pvBuffer;
|
|
|
|
AvPairListSize = (ULONG) context->ChallengeTargetInfo.cbBuffer;
|
2012-07-01 22:33:36 +04:00
|
|
|
|
|
|
|
ntlm_av_pair_list_init(pAvPairList);
|
2012-07-02 05:40:33 +04:00
|
|
|
ntlm_av_pair_add(pAvPairList, MsvAvNbDomainName, (PBYTE) NbDomainName.Buffer, NbDomainName.Length);
|
|
|
|
ntlm_av_pair_add(pAvPairList, MsvAvNbComputerName, (PBYTE) NbComputerName.Buffer, NbComputerName.Length);
|
|
|
|
ntlm_av_pair_add(pAvPairList, MsvAvDnsDomainName, (PBYTE) DnsDomainName.Buffer, DnsDomainName.Length);
|
|
|
|
ntlm_av_pair_add(pAvPairList, MsvAvDnsComputerName, (PBYTE) DnsComputerName.Buffer, DnsComputerName.Length);
|
|
|
|
ntlm_av_pair_add(pAvPairList, MsvAvTimestamp, context->Timestamp, sizeof(context->Timestamp));
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
void ntlm_construct_authenticate_target_info(NTLM_CONTEXT* context)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
ULONG size;
|
|
|
|
ULONG AvPairsCount;
|
|
|
|
ULONG AvPairsValueLength;
|
|
|
|
NTLM_AV_PAIR* AvTimestamp;
|
|
|
|
NTLM_AV_PAIR* AvNbDomainName;
|
|
|
|
NTLM_AV_PAIR* AvNbComputerName;
|
|
|
|
NTLM_AV_PAIR* AvDnsDomainName;
|
|
|
|
NTLM_AV_PAIR* AvDnsComputerName;
|
|
|
|
NTLM_AV_PAIR* AvDnsTreeName;
|
|
|
|
NTLM_AV_PAIR* ChallengeTargetInfo;
|
|
|
|
NTLM_AV_PAIR* AuthenticateTargetInfo;
|
|
|
|
|
|
|
|
AvPairsCount = AvPairsValueLength = 0;
|
|
|
|
ChallengeTargetInfo = (NTLM_AV_PAIR*) context->ChallengeTargetInfo.pvBuffer;
|
|
|
|
|
|
|
|
AvNbDomainName = ntlm_av_pair_get(ChallengeTargetInfo, MsvAvNbDomainName);
|
|
|
|
AvNbComputerName = ntlm_av_pair_get(ChallengeTargetInfo, MsvAvNbComputerName);
|
|
|
|
AvDnsDomainName = ntlm_av_pair_get(ChallengeTargetInfo, MsvAvDnsDomainName);
|
|
|
|
AvDnsComputerName = ntlm_av_pair_get(ChallengeTargetInfo, MsvAvDnsComputerName);
|
|
|
|
AvDnsTreeName = ntlm_av_pair_get(ChallengeTargetInfo, MsvAvDnsTreeName);
|
|
|
|
AvTimestamp = ntlm_av_pair_get(ChallengeTargetInfo, MsvAvTimestamp);
|
|
|
|
|
|
|
|
if (AvNbDomainName != NULL)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
AvPairsCount++; /* MsvAvNbDomainName */
|
|
|
|
AvPairsValueLength += AvNbDomainName->AvLen;
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (AvNbComputerName != NULL)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
AvPairsCount++; /* MsvAvNbComputerName */
|
|
|
|
AvPairsValueLength += AvNbComputerName->AvLen;
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (AvDnsDomainName != NULL)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
AvPairsCount++; /* MsvAvDnsDomainName */
|
|
|
|
AvPairsValueLength += AvDnsDomainName->AvLen;
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (AvDnsComputerName != NULL)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
AvPairsCount++; /* MsvAvDnsComputerName */
|
|
|
|
AvPairsValueLength += AvDnsComputerName->AvLen;
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (AvDnsTreeName != NULL)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
AvPairsCount++; /* MsvAvDnsTreeName */
|
|
|
|
AvPairsValueLength += AvDnsTreeName->AvLen;
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
AvPairsCount++; /* MsvAvTimestamp */
|
|
|
|
AvPairsValueLength += 8;
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (context->UseMIC)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
AvPairsCount++; /* MsvAvFlags */
|
|
|
|
AvPairsValueLength += 4;
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 06:13:02 +04:00
|
|
|
//AvPairsCount++; /* MsvAvRestrictions */
|
|
|
|
//AvPairsValueLength += 48;
|
|
|
|
|
|
|
|
if (!context->SuppressExtendedProtection)
|
|
|
|
{
|
|
|
|
AvPairsCount++; /* MsvChannelBindings */
|
|
|
|
AvPairsValueLength += 16;
|
|
|
|
|
|
|
|
if (context->ServicePrincipalName.Length > 0)
|
|
|
|
{
|
|
|
|
AvPairsCount++; /* MsvAvTargetName */
|
|
|
|
AvPairsValueLength += context->ServicePrincipalName.Length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
size = ntlm_av_pair_list_size(AvPairsCount, AvPairsValueLength);
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (context->NTLMv2)
|
|
|
|
size += 8; /* unknown 8-byte padding */
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
sspi_SecBufferAlloc(&context->AuthenticateTargetInfo, size);
|
|
|
|
AuthenticateTargetInfo = (NTLM_AV_PAIR*) context->AuthenticateTargetInfo.pvBuffer;
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
ntlm_av_pair_list_init(AuthenticateTargetInfo);
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (AvNbDomainName != NULL)
|
|
|
|
ntlm_av_pair_add_copy(AuthenticateTargetInfo, AvNbDomainName);
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (AvNbComputerName != NULL)
|
|
|
|
ntlm_av_pair_add_copy(AuthenticateTargetInfo, AvNbComputerName);
|
|
|
|
|
|
|
|
if (AvDnsDomainName != NULL)
|
|
|
|
ntlm_av_pair_add_copy(AuthenticateTargetInfo, AvDnsDomainName);
|
2012-03-19 04:30:20 +04:00
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (AvDnsComputerName != NULL)
|
|
|
|
ntlm_av_pair_add_copy(AuthenticateTargetInfo, AvDnsComputerName);
|
|
|
|
|
|
|
|
if (AvDnsTreeName != NULL)
|
|
|
|
ntlm_av_pair_add_copy(AuthenticateTargetInfo, AvDnsTreeName);
|
|
|
|
|
|
|
|
if (AvTimestamp != NULL)
|
|
|
|
ntlm_av_pair_add_copy(AuthenticateTargetInfo, AvTimestamp);
|
|
|
|
|
|
|
|
if (context->UseMIC)
|
2012-03-19 04:30:20 +04:00
|
|
|
{
|
2012-07-02 05:40:33 +04:00
|
|
|
UINT32 flags = MSV_AV_FLAGS_MESSAGE_INTEGRITY_CHECK;
|
|
|
|
ntlm_av_pair_add(AuthenticateTargetInfo, MsvAvFlags, (PBYTE) &flags, 4);
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|
|
|
|
|
2012-07-02 06:13:02 +04:00
|
|
|
if (!context->SuppressExtendedProtection)
|
|
|
|
{
|
|
|
|
BYTE ChannelBindingToken[16];
|
|
|
|
|
|
|
|
ZeroMemory(ChannelBindingToken, 16);
|
|
|
|
|
|
|
|
ntlm_av_pair_add(AuthenticateTargetInfo, MsvChannelBindings,
|
|
|
|
ChannelBindingToken, sizeof(ChannelBindingToken));
|
|
|
|
|
|
|
|
if (context->ServicePrincipalName.Length > 0)
|
|
|
|
{
|
|
|
|
ntlm_av_pair_add(AuthenticateTargetInfo, MsvAvTargetName,
|
|
|
|
(PBYTE) context->ServicePrincipalName.Buffer,
|
|
|
|
context->ServicePrincipalName.Length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:40:33 +04:00
|
|
|
if (context->NTLMv2)
|
|
|
|
{
|
|
|
|
NTLM_AV_PAIR* AvEOL;
|
|
|
|
|
|
|
|
AvEOL = ntlm_av_pair_get(ChallengeTargetInfo, MsvAvEOL);
|
2012-07-26 18:31:22 +04:00
|
|
|
ZeroMemory((void*) AvEOL, 4);
|
2012-07-02 05:40:33 +04:00
|
|
|
}
|
2012-03-19 04:30:20 +04:00
|
|
|
}
|