2012-06-03 02:21:04 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* NTLM Hashing Tool
|
|
|
|
*
|
|
|
|
* Copyright 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2017-11-14 18:10:52 +03:00
|
|
|
#include <errno.h>
|
2012-06-03 02:21:04 +04:00
|
|
|
|
|
|
|
#include <winpr/ntlm.h>
|
2019-01-16 14:05:47 +03:00
|
|
|
#include <winpr/ssl.h>
|
2012-06-03 02:21:04 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Define NTOWFv1(Password, User, Domain) as
|
|
|
|
* MD4(UNICODE(Password))
|
|
|
|
* EndDefine
|
|
|
|
*
|
|
|
|
* Define LMOWFv1(Password, User, Domain) as
|
|
|
|
* ConcatenationOf(DES(UpperCase(Password)[0..6], "KGS!@#$%"),
|
|
|
|
* DES(UpperCase(Password)[7..13], "KGS!@#$%"))
|
|
|
|
* EndDefine
|
|
|
|
*
|
|
|
|
* Define NTOWFv2(Password, User, Domain) as
|
|
|
|
* HMAC_MD5(MD4(UNICODE(Password)),
|
|
|
|
* UNICODE(ConcatenationOf(UpperCase(User), Domain)))
|
|
|
|
* EndDefine
|
|
|
|
*
|
|
|
|
* Define LMOWFv2(Password, User, Domain) as
|
|
|
|
* NTOWFv2(Password, User, Domain)
|
|
|
|
* EndDefine
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-01-11 17:28:18 +03:00
|
|
|
void usage_and_exit()
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
printf("winpr-hash: NTLM hashing tool\n");
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("Usage: winpr-hash -u <username> -p <password> [-d <domain>] [-f <_default_,sam>] [-v "
|
|
|
|
"<_1_,2>]\n");
|
2017-11-14 18:10:52 +03:00
|
|
|
exit(1);
|
2017-01-11 17:28:18 +03:00
|
|
|
}
|
|
|
|
|
2012-06-03 02:21:04 +04:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int index = 1;
|
2014-06-19 00:02:13 +04:00
|
|
|
int format = 0;
|
2017-11-14 18:10:52 +03:00
|
|
|
unsigned long version = 1;
|
2012-06-03 02:21:04 +04:00
|
|
|
BYTE NtHash[16];
|
|
|
|
char* User = NULL;
|
|
|
|
UINT32 UserLength;
|
|
|
|
char* Domain = NULL;
|
|
|
|
UINT32 DomainLength;
|
|
|
|
char* Password = NULL;
|
|
|
|
UINT32 PasswordLength;
|
2017-11-14 18:10:52 +03:00
|
|
|
errno = 0;
|
2012-06-03 02:21:04 +04:00
|
|
|
|
|
|
|
while (index < argc)
|
|
|
|
{
|
|
|
|
if (strcmp("-d", argv[index]) == 0)
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (index == argc)
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
printf("missing domain\n\n");
|
|
|
|
usage_and_exit();
|
2012-06-03 02:21:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Domain = argv[index];
|
|
|
|
}
|
|
|
|
else if (strcmp("-u", argv[index]) == 0)
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (index == argc)
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
printf("missing username\n\n");
|
|
|
|
usage_and_exit();
|
2012-06-03 02:21:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
User = argv[index];
|
|
|
|
}
|
|
|
|
else if (strcmp("-p", argv[index]) == 0)
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (index == argc)
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
printf("missing password\n\n");
|
|
|
|
usage_and_exit();
|
2012-06-03 02:21:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Password = argv[index];
|
|
|
|
}
|
2014-06-19 00:02:13 +04:00
|
|
|
else if (strcmp("-v", argv[index]) == 0)
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (index == argc)
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
printf("missing version parameter\n\n");
|
|
|
|
usage_and_exit();
|
2014-06-19 00:02:13 +04:00
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
version = strtoul(argv[index], NULL, 0);
|
2014-06-19 00:02:13 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
if (((version != 1) && (version != 2)) || (errno != 0))
|
2017-01-11 17:28:18 +03:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
printf("unknown version %lu \n\n", version);
|
2017-01-11 17:28:18 +03:00
|
|
|
usage_and_exit();
|
|
|
|
}
|
2014-06-19 00:02:13 +04:00
|
|
|
}
|
|
|
|
else if (strcmp("-f", argv[index]) == 0)
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (index == argc)
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
printf("missing format\n\n");
|
|
|
|
usage_and_exit();
|
2014-06-19 00:02:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp("default", argv[index]) == 0)
|
|
|
|
format = 0;
|
|
|
|
else if (strcmp("sam", argv[index]) == 0)
|
|
|
|
format = 1;
|
|
|
|
}
|
2012-06-03 02:21:04 +04:00
|
|
|
else if (strcmp("-h", argv[index]) == 0)
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
usage_and_exit();
|
2012-06-03 02:21:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!User) || (!Password))
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
printf("missing username or password\n\n");
|
|
|
|
usage_and_exit();
|
2012-06-03 02:21:04 +04:00
|
|
|
}
|
2019-01-16 14:05:47 +03:00
|
|
|
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
2012-06-03 02:21:04 +04:00
|
|
|
|
|
|
|
UserLength = strlen(User);
|
|
|
|
PasswordLength = strlen(Password);
|
|
|
|
DomainLength = (Domain) ? strlen(Domain) : 0;
|
|
|
|
|
2014-06-19 00:02:13 +04:00
|
|
|
if (version == 2)
|
|
|
|
{
|
|
|
|
if (!Domain)
|
|
|
|
{
|
2017-01-11 17:28:18 +03:00
|
|
|
printf("missing domain (version 2 requires a domain to specified)\n\n");
|
|
|
|
usage_and_exit();
|
2014-06-19 00:02:13 +04:00
|
|
|
}
|
2012-06-03 02:21:04 +04:00
|
|
|
|
2019-01-16 14:05:47 +03:00
|
|
|
if (!NTOWFv2A(Password, PasswordLength, User, UserLength, Domain, DomainLength, NtHash))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Hash creation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-19 00:02:13 +04:00
|
|
|
}
|
2012-07-23 07:23:23 +04:00
|
|
|
else
|
2014-06-19 00:02:13 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!NTOWFv1A(Password, PasswordLength, NtHash))
|
2019-01-16 14:05:47 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Hash creation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-19 00:02:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format == 0)
|
|
|
|
{
|
|
|
|
for (index = 0; index < 16; index++)
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("%02" PRIx8 "", NtHash[index]);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-06-19 00:02:13 +04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
else if (format == 1)
|
|
|
|
{
|
|
|
|
printf("%s:", User);
|
|
|
|
|
|
|
|
if (DomainLength > 0)
|
|
|
|
printf("%s:", Domain);
|
|
|
|
else
|
|
|
|
printf(":");
|
2012-06-04 03:59:35 +04:00
|
|
|
|
2014-06-19 00:02:13 +04:00
|
|
|
printf(":");
|
2012-07-23 07:23:23 +04:00
|
|
|
|
2014-06-19 00:02:13 +04:00
|
|
|
for (index = 0; index < 16; index++)
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("%02" PRIx8 "", NtHash[index]);
|
2012-07-23 07:23:23 +04:00
|
|
|
|
2014-06-19 00:02:13 +04:00
|
|
|
printf(":::");
|
|
|
|
printf("\n");
|
|
|
|
}
|
2012-06-03 02:21:04 +04:00
|
|
|
|
2018-04-03 13:55:17 +03:00
|
|
|
return 0;
|
2012-06-03 02:21:04 +04:00
|
|
|
}
|