Skip to content

Commit

Permalink
fix: fixes #217 correct ntlm hash calculation on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
aegoroff committed Nov 30, 2024
1 parent 3df306b commit 7579b3d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/hc/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "encoding.h"
#include "intl.h"

#ifndef _MSC_VER
#include <uchar.h>
#endif

static apr_pool_t* builtin_pool = NULL;
static hash_definition_t* builtin_hash = NULL;

Expand Down Expand Up @@ -60,8 +64,13 @@ apr_byte_t* builtin_hash_from_string(const char* string) {

// some hashes like NTLM required unicode string so convert multi byte string to unicode one
if(builtin_hash->use_wide_string_) {
#ifdef _MSC_VER
wchar_t* str = enc_from_ansi_to_unicode(string, builtin_pool);
builtin_hash->pfn_digest_(digest, str, wcslen(str) * sizeof(wchar_t));
#else
char16_t* str = enc_from_ansi_to_wide_chars(string, builtin_pool);
builtin_hash->pfn_digest_(digest, str, strlen(string) * sizeof(char16_t));
#endif
} else {
builtin_hash->pfn_digest_(digest, string, strlen(string));
}
Expand Down
6 changes: 6 additions & 0 deletions src/srclib/bf.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ void bf_crack_hash(const char *dict, const char *hash, const uint32_t passmin, u
const char *t = "123";
const size_t max_time_msg_size = 63;
if (use_wide_pass) {
#ifdef _MSC_VER
wchar_t *s = enc_from_ansi_to_unicode(t, pool);
pfn_digest_function(digest, s, wcslen(s) * sizeof(wchar_t));
#else
char16_t* s = enc_from_ansi_to_wide_chars(t, pool);
pfn_digest_function(digest, s, strlen(t) * sizeof(char16_t));
#endif
} else {
pfn_digest_function(digest, t, strlen(t));
}
Expand Down Expand Up @@ -299,6 +304,7 @@ char *bf_brute_force(const uint32_t passmin, const uint32_t passmax, const char

if (thd_ctx[i]->use_wide_pass_) {
if (thd_ctx[i]->wide_pass_ != NULL) {
// TODO: make correct implementation on Linux
pass = (unsigned char *)enc_from_unicode_to_ansi(thd_ctx[i]->wide_pass_, pool);
}
} else {
Expand Down
29 changes: 29 additions & 0 deletions src/srclib/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ wchar_t *enc_from_ansi_to_unicode(const char *from, apr_pool_t *pool) {
return enc_from_code_page_to_unicode(from, CP_ACP, pool);
}

#ifndef _MSC_VER
/*!
* IMPORTANT: Memory allocated for result must be freed up by caller
*/
char16_t *enc_from_ansi_to_wide_chars(const char *from, apr_pool_t *pool) {
char16_t pc16 = 0;
mbstate_t state = { 0 };

size_t len = strlen(from);
char16_t* wide = (char16_t *)apr_pcalloc(pool, (len + 1) * sizeof(char16_t));

for (size_t i = 0; i < len; i++)
{
size_t rc = mbrtoc16(&pc16, &from[i], len - i + 1, &state);
if (rc == (size_t)-3)
continue;
else if (rc == (size_t) - 2)
break;
else if (rc == (size_t) - 1)
break;
else
{
wide[i] = pc16;
}
}
return wide;
}
#endif

/*!
* IMPORTANT: Memory allocated for result must be freed up by caller
*/
Expand Down
10 changes: 10 additions & 0 deletions src/srclib/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ typedef enum { bom_unknown = 0, bom_utf8 = 1, bom_utf16le = 2, bom_utf16be = 3,
#define _UINT
typedef unsigned int UINT;
#endif

#include <uchar.h>

#endif

/*!
Expand All @@ -52,6 +55,13 @@ char *enc_from_ansi_to_utf8(const char *from, apr_pool_t *pool);
*/
wchar_t *enc_from_ansi_to_unicode(const char *from, apr_pool_t *pool);

#ifndef _MSC_VER
/*!
* IMPORTANT: Memory allocated for result must be freed up by caller
*/
char16_t *enc_from_ansi_to_wide_chars(const char *from, apr_pool_t *pool);
#endif

/*!
* IMPORTANT: Memory allocated for result must be freed up by caller
*/
Expand Down

0 comments on commit 7579b3d

Please sign in to comment.