summaryrefslogtreecommitdiffstats
path: root/winpr/tools/hash-cli
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:24:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:24:41 +0000
commita9bcc81f821d7c66f623779fa5147e728eb3c388 (patch)
tree98676963bcdd537ae5908a067a8eb110b93486a6 /winpr/tools/hash-cli
parentInitial commit. (diff)
downloadfreerdp3-a9bcc81f821d7c66f623779fa5147e728eb3c388.tar.xz
freerdp3-a9bcc81f821d7c66f623779fa5147e728eb3c388.zip
Adding upstream version 3.3.0+dfsg1.upstream/3.3.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'winpr/tools/hash-cli')
-rw-r--r--winpr/tools/hash-cli/CMakeLists.txt59
-rw-r--r--winpr/tools/hash-cli/hash.c216
-rw-r--r--winpr/tools/hash-cli/winpr-hash.1.in42
3 files changed, 317 insertions, 0 deletions
diff --git a/winpr/tools/hash-cli/CMakeLists.txt b/winpr/tools/hash-cli/CMakeLists.txt
new file mode 100644
index 0000000..8f583d3
--- /dev/null
+++ b/winpr/tools/hash-cli/CMakeLists.txt
@@ -0,0 +1,59 @@
+# WinPR: Windows Portable Runtime
+# winpr-hash cmake build script
+#
+# 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.
+
+set(MODULE_NAME "winpr-hash")
+set(MODULE_PREFIX "WINPR_TOOLS_HASH")
+
+set(${MODULE_PREFIX}_SRCS
+ hash.c)
+
+# On windows create dll version information.
+# Vendor, product and year are already set in top level CMakeLists.txt
+if (WIN32)
+ set(RC_VERSION_MAJOR ${WINPR_VERSION_MAJOR})
+ set(RC_VERSION_MINOR ${WINPR_VERSION_MINOR})
+ set(RC_VERSION_BUILD ${WINPR_VERSION_REVISION})
+ set(RC_VERSION_FILE "${MODULE_NAME}${CMAKE_EXECUTABLE_SUFFIX}")
+
+ configure_file(
+ ${PROJECT_SOURCE_DIR}/cmake/WindowsDLLVersion.rc.in
+ ${CMAKE_CURRENT_BINARY_DIR}/version.rc
+ @ONLY)
+
+ set(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
+endif()
+
+add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
+
+set(${MODULE_PREFIX}_LIBS winpr)
+
+set(MANPAGE_NAME "${MODULE_NAME}")
+if (WITH_BINARY_VERSIONING)
+ set_target_properties(${MODULE_NAME} PROPERTIES OUTPUT_NAME "${MODULE_NAME}${WINPR_API_VERSION}")
+ set(MANPAGE_NAME "${MODULE_NAME}${WINPR_API_VERSION}")
+endif()
+target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS})
+
+install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tools EXPORT WinPRTargets)
+
+if (WITH_DEBUG_SYMBOLS AND MSVC)
+ install(FILES ${PROJECT_BINARY_DIR}/${MODULE_NAME}.pdb DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT symbols)
+endif()
+
+set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Tools")
+configure_file(winpr-hash.1.in ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE_NAME}.1)
+install_freerdp_man(${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE_NAME}.1 1)
diff --git a/winpr/tools/hash-cli/hash.c b/winpr/tools/hash-cli/hash.c
new file mode 100644
index 0000000..b98f8e9
--- /dev/null
+++ b/winpr/tools/hash-cli/hash.c
@@ -0,0 +1,216 @@
+/**
+ * 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>
+#include <errno.h>
+
+#include <winpr/ntlm.h>
+#include <winpr/ssl.h>
+#include <winpr/assert.h>
+
+/**
+ * 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
+ *
+ */
+
+static WINPR_NORETURN(void usage_and_exit(void))
+{
+ printf("winpr-hash: NTLM hashing tool\n");
+ printf("Usage: winpr-hash -u <username> -p <password> [-d <domain>] [-f <_default_,sam>] [-v "
+ "<_1_,2>]\n");
+ exit(1);
+}
+
+int main(int argc, char* argv[])
+{
+ int index = 1;
+ int format = 0;
+ unsigned long version = 1;
+ BYTE NtHash[16];
+ char* User = NULL;
+ size_t UserLength = 0;
+ char* Domain = NULL;
+ size_t DomainLength = 0;
+ char* Password = NULL;
+ size_t PasswordLength = 0;
+ errno = 0;
+
+ while (index < argc)
+ {
+ if (strcmp("-d", argv[index]) == 0)
+ {
+ index++;
+
+ if (index == argc)
+ {
+ printf("missing domain\n\n");
+ usage_and_exit();
+ }
+
+ Domain = argv[index];
+ }
+ else if (strcmp("-u", argv[index]) == 0)
+ {
+ index++;
+
+ if (index == argc)
+ {
+ printf("missing username\n\n");
+ usage_and_exit();
+ }
+
+ User = argv[index];
+ }
+ else if (strcmp("-p", argv[index]) == 0)
+ {
+ index++;
+
+ if (index == argc)
+ {
+ printf("missing password\n\n");
+ usage_and_exit();
+ }
+
+ Password = argv[index];
+ }
+ else if (strcmp("-v", argv[index]) == 0)
+ {
+ index++;
+
+ if (index == argc)
+ {
+ printf("missing version parameter\n\n");
+ usage_and_exit();
+ }
+
+ version = strtoul(argv[index], NULL, 0);
+
+ if (((version != 1) && (version != 2)) || (errno != 0))
+ {
+ printf("unknown version %lu \n\n", version);
+ usage_and_exit();
+ }
+ }
+ else if (strcmp("-f", argv[index]) == 0)
+ {
+ index++;
+
+ if (index == argc)
+ {
+ printf("missing format\n\n");
+ usage_and_exit();
+ }
+
+ if (strcmp("default", argv[index]) == 0)
+ format = 0;
+ else if (strcmp("sam", argv[index]) == 0)
+ format = 1;
+ }
+ else if (strcmp("-h", argv[index]) == 0)
+ {
+ usage_and_exit();
+ }
+
+ index++;
+ }
+
+ if ((!User) || (!Password))
+ {
+ printf("missing username or password\n\n");
+ usage_and_exit();
+ }
+ winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
+
+ UserLength = strlen(User);
+ PasswordLength = strlen(Password);
+ DomainLength = (Domain) ? strlen(Domain) : 0;
+
+ WINPR_ASSERT(UserLength <= UINT32_MAX);
+ WINPR_ASSERT(PasswordLength <= UINT32_MAX);
+ WINPR_ASSERT(DomainLength <= UINT32_MAX);
+
+ if (version == 2)
+ {
+ if (!Domain)
+ {
+ printf("missing domain (version 2 requires a domain to specified)\n\n");
+ usage_and_exit();
+ }
+
+ if (!NTOWFv2A(Password, (UINT32)PasswordLength, User, (UINT32)UserLength, Domain,
+ (UINT32)DomainLength, NtHash))
+ {
+ fprintf(stderr, "Hash creation failed\n");
+ return 1;
+ }
+ }
+ else
+ {
+ if (!NTOWFv1A(Password, (UINT32)PasswordLength, NtHash))
+ {
+ fprintf(stderr, "Hash creation failed\n");
+ return 1;
+ }
+ }
+
+ if (format == 0)
+ {
+ for (int index = 0; index < 16; index++)
+ printf("%02" PRIx8 "", NtHash[index]);
+
+ printf("\n");
+ }
+ else if (format == 1)
+ {
+ printf("%s:", User);
+
+ if (DomainLength > 0)
+ printf("%s:", Domain);
+ else
+ printf(":");
+
+ printf(":");
+
+ for (int index = 0; index < 16; index++)
+ printf("%02" PRIx8 "", NtHash[index]);
+
+ printf(":::");
+ printf("\n");
+ }
+
+ return 0;
+}
diff --git a/winpr/tools/hash-cli/winpr-hash.1.in b/winpr/tools/hash-cli/winpr-hash.1.in
new file mode 100644
index 0000000..0b1f36a
--- /dev/null
+++ b/winpr/tools/hash-cli/winpr-hash.1.in
@@ -0,0 +1,42 @@
+.TH @MANPAGE_NAME@ 1 2017-01-11 "@WINPR_VERSION_FULL@" "FreeRDP"
+.SH NAME
+@MANPAGE_NAME@ \- NTLM hashing tool
+.SH SYNOPSIS
+.B @MANPAGE_NAME@
+\fB-u\fP username
+\fB-p\fP password
+[\fB-d\fP domain]
+[\fB-f\fP { \fIdefault\fP | sam }]
+[\fB-v\fP { \fI1\fP | 2 }]
+.SH DESCRIPTION
+.B @MANPAGE_NAME@
+is a small utility that can be used to create a NTLM hash from a username and password pair. The created hash can be outputed as plain hash or in SAM format.
+.SH OPTIONS
+.IP "-u username"
+The username to use.
+.IP "-p password"
+Password to use.
+.IP "-d domain"
+A optional parameter to specify the domain of the user.
+.IP "-f format"
+Specify the output format. The \fIdefault\fP outputs only the plain NTLM
+hash. The second output format available is \fIsam\fP which outputs the
+created hash in a format that it can be used in SAM file:
+
+user:domain::hash:::
+.IP "-v version"
+Version allows it to specify the NTLM version to use. The default is to use version 1. In case
+version 2 is used a domain needs to be specified.
+.SH EXAMPLES
+@MANPAGE_NAME@ -u \fIuser\fP -p \fIpassword\fP -d \fIdomain\fP -f \fIsam\fP -v \fI2\fP
+
+Create a version \fI2\fP NTLM hash for \fIuser\fP with \fIdomain\fP and \fIpassword\fP and output it in \fIsam\fP format.
+.SH EXIT STATUS
+.TP
+.B 0
+Successful program execution.
+.TP
+.B 1
+Missing or invalid arguments.
+.SH AUTHOR
+FreeRDP <team@freerdp.com>