/*
 * NETLMv2_fmt.c -- LMv2 Challenge/Response
 *
 * Written by JoMo-Kun <jmk at foofus.net> in 2008
 * and placed in the public domain.
 *
 * Performance fixes, OMP and utf-8 support by magnum 2010-2011
 * no rights reserved.
 *
 * This algorithm is designed for performing brute-force cracking of the LMv2
 * challenge/response sets exchanged during network-based authentication
 * attempts [1]. The captured challenge/response set from these attempts
 * should be stored using the following format:
 *
 * USERNAME::DOMAIN:SERVER CHALLENGE:LMv2 RESPONSE:CLIENT CHALLENGE
 *
 * For example:
 * Administrator::WORKGROUP:1122334455667788:6759A5A7EFB25452911DE7DE8296A0D8:F503236B200A5B3A
 *
 * It should be noted that a LMv2 authentication response is not same as a LM
 * password hash, which can be extracted using tools such as FgDump [2]. In
 * fact, a NTLM hash and not a LM hash is used within the LMv2 algorithm. LMv2
 * challenge/response authentication typically takes place when the GPO
 * "Network Security: LAN Manager authentication level" is configured to a setting
 * that enforces the use of NTLMv2, such as "Send NTLMv2 response only\refuse
 * LM & NTLM."
 *
 * LMv2 responses can be gathered via normal network capture or via tools which
 * perform layer 2 attacks, such as Ettercap [3] and Cain [4]. The responses can
 * also be harvested using a modified Samba service [5] in conjunction with
 * some trickery to convince the user to connect to it. I leave what that
 * trickery may actually be as an exercise for the reader (HINT: Karma, NMB
 * broadcasts, IE, Outlook, social engineering, ...).
 *
 * [1] http://davenport.sourceforge.net/ntlm.html#theLmv2Response
 * [2] http://www.foofus.net/fizzgig/fgdump/
 * [3] http://ettercap.sourceforge.net/
 * [4] http://www.oxid.it/cain.html
 * [5] http://www.foofus.net/jmk/smbchallenge.html
 *
 */

#include <string.h>
#ifdef _OPENMP
#include <omp.h>
#endif

#include "misc.h"
#include "common.h"
#include "formats.h"
#include "options.h"
#include "unicode.h"

#if 0
#warning INFO: We are using OpenSSL HMAC_MD5
#include <openssl/hmac.h>
static void hmac_md5(const unsigned char *key, const unsigned char *data, int datalen, unsigned char *digest)
{
	HMAC(EVP_md5(), key, 16, data, datalen, digest, NULL);
}
#else
#include "md5.h"
#include "hmacmd5.h"
// Move a hmac_md5_init() from crypt_all to set_key(),
// and store &ctx in saved_key.
//
// Pro: saves another key setup using same key
// Con: larger saved_key (288 bytes vs 16)
//
// This boosted performance with about 8%
#define SPLIT_HMACMD5
#endif

#ifndef uchar
#define uchar unsigned char
#endif

#define FORMAT_LABEL         "netlmv2"
#define FORMAT_NAME          "LMv2 C/R MD4 HMAC-MD5"
#define ALGORITHM_NAME       "netlmv2"
#define BENCHMARK_COMMENT    ""
#define BENCHMARK_LENGTH     0
#define PLAINTEXT_LENGTH     54 /* lmcons.h - PWLEN (256) ? 127 ? */
#define UTF8_PLAINTEXT_LEN   125
#define USERNAME_LENGTH      20 /* lmcons.h - UNLEN (256) / LM20_UNLEN (20) */
#define DOMAIN_LENGTH        15 /* lmcons.h - CNLEN / DNLEN */
#define BINARY_SIZE          16
#define CHALLENGE_LENGTH     32
#define SALT_SIZE            16 + 1 + 2 * (USERNAME_LENGTH + DOMAIN_LENGTH) + 1
#define CIPHERTEXT_LENGTH    32
#define TOTAL_LENGTH         12 + USERNAME_LENGTH + DOMAIN_LENGTH + CHALLENGE_LENGTH + CIPHERTEXT_LENGTH

// these may be altered in init() if running OMP
// and that formula is subject to change
#define MIN_KEYS_PER_CRYPT	    1
#define THREAD_RATIO            64
#ifdef _OPENMP
#define MAX_KEYS_PER_CRYPT	    0x10000
#else
#define MAX_KEYS_PER_CRYPT	    THREAD_RATIO
#endif

static struct fmt_tests tests[] = {
  {"$NETLMv2$ADMINISTRATORFOODOM$1122334455667788$6F64C5C1E35F68DD80388C0F00F34406$F0F3FF27037AA69F", "1337adminPASS"},
  {"$NETLMv2$USER1$1122334455667788$B1D163EA5881504F3963DC50FCDC26C1$EB4D9E8138149E20", "foobar"},
  {"$NETLMv2$ATEST$1122334455667788$83B59F1536D3321DBF1FAEC14ADB1675$A1E7281FE8C10E53", "SomeFancyP4$$w0rdHere"},
  {"", "1337adminPASS",         {"FOODOM\\Administrator", "", "",       "1122334455667788", "6F64C5C1E35F68DD80388C0F00F34406", "F0F3FF27037AA69F"} },
  {"", "1337adminPASS",         {"administrator",         "", "FOODOM", "1122334455667788", "6F64C5C1E35F68DD80388C0F00F34406", "F0F3FF27037AA69F"} },
  {"", "foobar",                {"user1",                 "", "",       "1122334455667788", "B1D163EA5881504F3963DC50FCDC26C1", "EB4D9E8138149E20"} },
  {"", "SomeFancyP4$$w0rdHere", {"aTest",                 "", "",       "1122334455667788", "83B59F1536D3321DBF1FAEC14ADB1675", "A1E7281FE8C10E53"} },
  {NULL}
};

static unsigned char saved_plain[MAX_KEYS_PER_CRYPT][UTF8_PLAINTEXT_LEN+1];
static unsigned char *challenge;
static ARCH_WORD_32 output[MAX_KEYS_PER_CRYPT][BINARY_SIZE/sizeof(ARCH_WORD_32)];

#ifdef SPLIT_HMACMD5
static HMACMD5Context saved_key[MAX_KEYS_PER_CRYPT];
#else
static unsigned char saved_key[MAX_KEYS_PER_CRYPT][16];
#endif

#if !defined(uint16) && !defined(HAVE_UINT16_FROM_RPC_RPC_H)
#if (SIZEOF_SHORT == 4)
#define uint16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
#else /* SIZEOF_SHORT != 4 */
#define uint16 unsigned short
#endif /* SIZEOF_SHORT != 4 */
#endif

#if !defined(int16) && !defined(HAVE_INT16_FROM_RPC_RPC_H)
#if (SIZEOF_SHORT == 4)
#define int16 __ERROR___CANNOT_DETERMINE_TYPE_FOR_INT16;
#else /* SIZEOF_SHORT != 4 */
#define int16 short
#endif /* SIZEOF_SHORT != 4 */
#endif

#include "byteorder.h"

extern struct fmt_main fmt_NETLMv2;
static void init(struct fmt_main *pFmt)
{
#ifdef _OPENMP
	int n = MIN_KEYS_PER_CRYPT * omp_get_max_threads();
	if (n < MIN_KEYS_PER_CRYPT)
		n = MIN_KEYS_PER_CRYPT;
	if (n > MAX_KEYS_PER_CRYPT)
		n = MAX_KEYS_PER_CRYPT;
	fmt_NETLMv2.params.min_keys_per_crypt = n;
	n = n * n * ((n >> 1) + 1) * THREAD_RATIO;
	if (n > MAX_KEYS_PER_CRYPT)
		n = MAX_KEYS_PER_CRYPT;
	fmt_NETLMv2.params.max_keys_per_crypt = n;
#endif
	if (options.utf8) {
		// in utf-8, up to four bytes can compose one character
		fmt_NETLMv2.params.plaintext_length = UTF8_PLAINTEXT_LEN;
	}
}

static int netlmv2_valid(char *ciphertext, struct fmt_main *pFmt)
{
  char *pos, *pos2;

  if (ciphertext == NULL) return 0;
  else if (strncmp(ciphertext, "$NETLMv2$", 9)!=0) return 0;

  pos = &ciphertext[9];

  /* Validate Username and Domain Length */
  for (pos2 = pos; strncmp(pos2, "$", 1) != 0; pos2++)
    if ( (*pos2 < 0x20) || (*pos2 > 0x7E) )
      return 0;

  if ( !(*pos2 && (pos2 - pos <= USERNAME_LENGTH + DOMAIN_LENGTH)) )
    return 0;

  /* Validate Server Challenge Length */
  pos2++; pos = pos2;
  for (; strncmp(pos2, "$", 1) != 0; pos2++)
    if (atoi16[ARCH_INDEX(*pos2)] == 0x7F)
      return 0;

  if ( !(*pos2 && (pos2 - pos == CHALLENGE_LENGTH / 2)) )
    return 0;

  /* Validate LMv2 Response Length */
  pos2++; pos = pos2;
  for (; strncmp(pos2, "$", 1) != 0; pos2++)
    if (atoi16[ARCH_INDEX(*pos2)] == 0x7F)
      return 0;

  if ( !(*pos2 && (pos2 - pos == CIPHERTEXT_LENGTH)) )
    return 0;

  /* Validate Client Challenge Length */
  pos2++; pos = pos2;
  for (; atoi16[ARCH_INDEX(*pos2)] != 0x7F; pos2++);
  if (pos2 - pos != CHALLENGE_LENGTH / 2)
    return 0;

  return 1;
}

static char *netlmv2_prepare(char *split_fields[10], struct fmt_main *pFmt)
{
//	else if (options.format && ((strncmp(options.format, "netlmv2", 7)==0) ||
//			(strncmp(options.format, "netntlmv2", 9)==0))) {
//		char *srv_challenge = ldr_get_field(&line, db_options->field_sep_char);
//		char *nethashv2 = ldr_get_field(&line, db_options->field_sep_char);
//		char *cli_challenge = ldr_get_field(&line, db_options->field_sep_char);
//		char *identity = NULL;
//		int i;
//
//	/* DOMAIN\USER: -or- USER::DOMAIN: */
//	if ((tmp = strstr(*login, "\\")) != NULL) {
//		identity = (char *) mem_alloc(strlen(*login));
//	  	strcpy(identity, tmp + 1);
//
//		/* Upper-Case Username - Not Domain */
//		for(i=0; i<strlen(identity); i++)
//		 if ((identity[i] >= 'a') && (identity[i] <= 'z')) identity[i] ^= 0x20;
//
//		  strncat(identity, *login, tmp - *login);
//	}
//	else {
//		identity = (char *) mem_alloc(strlen(*login) + strlen(uid) + 1);
//		strcpy(identity, *login);
//
//			for(i=0; i<strlen(identity); i++)
//		if ((identity[i] >= 'a') && (identity[i] <= 'z')) identity[i] ^= 0x20;
//
//		strcat(identity, uid);
//	}
//	if (strncmp(options.format, "netlmv2", 7)==0) {
//		  tmp = (char *) mem_alloc_tiny(9 + strlen(identity) + 1 + strlen(srv_challenge) + 1 + strlen(nethashv2) + 1 + strlen(cli_challenge) + 1, MEM_ALIGN_NONE);
//		  sprintf(tmp, "$NETLMv2$%s$%s$%s$%s", identity, srv_challenge, nethashv2, cli_challenge);
//		*ciphertext = tmp;
//	}
//	else {
//		  tmp = (char *) mem_alloc_tiny(9 + strlen(identity) + 1 + strlen(srv_challenge) + 1 + strlen(nethashv2) + 1 + strlen(cli_challenge) + 1, MEM_ALIGN_NONE);
//		  sprintf(tmp, "$NETNTLMv2$%s$%s$%s$%s", identity, srv_challenge, nethashv2, cli_challenge);
//		  *ciphertext = tmp;
//	}
//
//		MEM_FREE(identity);
//	}

	char *srv_challenge = split_fields[3];
	char *nethashv2     = split_fields[4];
	char *cli_challenge = split_fields[5];
	char *login = split_fields[0];
	char *uid = split_fields[2];
	char *identity = NULL, *tmp;
	int i;

	if (!strncmp(split_fields[1], "$NETLMv2$", 9))
		return split_fields[1];
	/* DOMAIN\USER: -or- USER::DOMAIN: */
	if ((tmp = strstr(login, "\\")) != NULL) {
		identity = (char *) mem_alloc(strlen(login));
	  	strcpy(identity, tmp + 1);

		/* Upper-Case Username - Not Domain */
		for(i=0; i<strlen(identity); i++)
		 if ((identity[i] >= 'a') && (identity[i] <= 'z')) identity[i] ^= 0x20;

		  strncat(identity, login, tmp - login);
	}
	else {
		identity = (char *) mem_alloc(strlen(login) + strlen(uid) + 1);
		strcpy(identity, login);

			for(i=0; i<strlen(identity); i++)
		if ((identity[i] >= 'a') && (identity[i] <= 'z')) identity[i] ^= 0x20;
		strcat(identity, uid);
	}
	tmp = (char *) mem_alloc(9 + strlen(identity) + 1 + strlen(srv_challenge) + 1 + strlen(nethashv2) + 1 + strlen(cli_challenge) + 1);
	sprintf(tmp, "$NETLMv2$%s$%s$%s$%s", identity, srv_challenge, nethashv2, cli_challenge);
	MEM_FREE(identity);

	if (netlmv2_valid(tmp, pFmt)) {
		char *cp = str_alloc_copy(tmp);
		MEM_FREE(tmp);
		return cp;
	}
	MEM_FREE(tmp);
	return split_fields[1];
}


static char *netlmv2_split(char *ciphertext, int index)
{
  static char out[TOTAL_LENGTH + 1];
  char *pos = NULL;
  int identity_length = 0;

  /* Calculate identity length */
  for (pos = ciphertext + 9; strncmp(pos, "$", 1) != 0; pos++);
  identity_length = pos - (ciphertext + 9);

  memset(out, 0, TOTAL_LENGTH + 1);
  memcpy(&out, ciphertext, strlen(ciphertext));
  strlwr(&out[10 + identity_length]); /* Exclude: $NETLMv2$USERDOMAIN$ */

  return out;
}

static void *netlmv2_get_binary(char *ciphertext)
{
  static uchar *binary;
  char *pos = NULL;
  int i, identity_length;

  if (!binary) binary = mem_alloc_tiny(BINARY_SIZE, MEM_ALIGN_WORD);

  for (pos = ciphertext + 9; strncmp(pos, "$", 1) != 0; pos++);
  identity_length = pos - (ciphertext + 9);

  ciphertext += 9 + identity_length + 1 + CHALLENGE_LENGTH / 2 + 1;
  for (i=0; i<BINARY_SIZE; i++)
  {
    binary[i] = (atoi16[ARCH_INDEX(ciphertext[i*2])])<<4;
    binary[i] |= (atoi16[ARCH_INDEX(ciphertext[i*2+1])]);
  }

  return binary;
}

/* Calculate the LMv2 response for the given challenge, using the
   specified authentication identity (username and domain), password
   and client nonce.
*/
static void netlmv2_crypt_all(int count)
{
	unsigned char ntlm_v2_hash[16];
	int i;

#ifdef _OPENMP
#pragma omp parallel for default(none) private(i, ntlm_v2_hash) shared(count, saved_key, challenge, output)
#endif
	for(i=0; i<count; i++) {
		/* Generate 16-byte NTLMv2 Hash */
		/* HMAC-MD5(Username + Domain, NTLM Hash) */
#ifdef SPLIT_HMACMD5
		HMACMD5Context ctx; // can't be moved above the OMP pragma
		memcpy(&ctx, &saved_key[i], sizeof(ctx));
		hmac_md5_update(&challenge[17], (int)challenge[16], &ctx);
		hmac_md5_final(ntlm_v2_hash, &ctx);
#else
		hmac_md5(saved_key[i], &challenge[17], (int)challenge[16], ntlm_v2_hash);
#endif

		/* Generate 16-byte non-client nonce portion of LMv2 Response */
		/* HMAC-MD5(Challenge + Nonce, NTLMv2 Hash) + Nonce */
		hmac_md5(ntlm_v2_hash, challenge, 16, (unsigned char*)output[i]);
	}
}

static int netlmv2_cmp_all(void *binary, int count)
{
	int index;
	for(index=0; index<count; index++)
		if (!memcmp(output[index], binary, BINARY_SIZE))
			return 1;
	return 0;
}

static int netlmv2_cmp_one(void *binary, int index)
{
	return !memcmp(output[index], binary, BINARY_SIZE);
}

static int netlmv2_cmp_exact(char *source, int index)
{
	return !memcmp(output[index], netlmv2_get_binary(source), BINARY_SIZE);
}

/* We're essentially using three salts, but we're going to pack it into a single blob for now.
   |Client Challenge (8 Bytes)|Server Challenge (8 Bytes)|Unicode(Username (<=20).Domain (<=15))
*/
static void *netlmv2_get_salt(char *ciphertext)
{
  static unsigned char *binary_salt;
  unsigned char identity[USERNAME_LENGTH + DOMAIN_LENGTH + 1];
  UTF16 identity_usc[USERNAME_LENGTH + DOMAIN_LENGTH + 1];
  int i, identity_length;
  int identity_usc_length;
  char *pos = NULL;

  if (!binary_salt) binary_salt = mem_alloc_tiny(SALT_SIZE, MEM_ALIGN_WORD);

  /* Calculate identity length */
  for (pos = ciphertext + 9; strncmp(pos, "$", 1) != 0; pos++);
  identity_length = pos - (ciphertext + 9);

  /* Convert identity (username + domain) string to NT unicode */
  strnzcpy((char *)identity, ciphertext + 9, sizeof(identity));
  identity_usc_length = plaintowcs((UTF16 *)identity_usc, USERNAME_LENGTH + DOMAIN_LENGTH, (UTF8 *)identity, identity_length) * sizeof(int16);

  if (identity_usc_length < 0) // Truncated at Unicode conversion.
	  identity_usc_length = strlen16((UTF16 *)identity_usc) * sizeof(int16);

  binary_salt[16] = (unsigned char)identity_usc_length;
  memcpy(&binary_salt[17], (char *)identity_usc, identity_usc_length);

  /* Set server challenge */
  ciphertext += 10 + identity_length;

  for (i = 0; i < 8; i++)
    binary_salt[i] = (atoi16[ARCH_INDEX(ciphertext[i*2])] << 4) + atoi16[ARCH_INDEX(ciphertext[i*2+1])];

  /* Set client challenge */
  ciphertext += 2 + CHALLENGE_LENGTH / 2 + CIPHERTEXT_LENGTH;

  for (i = 0; i < 8; ++i)
    binary_salt[i + 8] = (atoi16[ARCH_INDEX(ciphertext[i*2])] << 4) + atoi16[ARCH_INDEX(ciphertext[i*2+1])];

  /* Return a concatenation of the server and client challenges and the identity value */
  return (void*)binary_salt;
}

static void netlmv2_set_salt(void *salt)
{
	challenge = salt;
}

static void netlmv2_set_key(char *key, int index)
{
	int len;
#ifdef SPLIT_HMACMD5
	unsigned char ntlm[16];
#endif

	len = strlen(key);
	memcpy((char *)saved_plain[index], key, len + 1);

#ifdef SPLIT_HMACMD5
	/* Generate 16-byte NTLM hash */
	len = E_md4hash(saved_plain[index], len, ntlm);

	// We do key setup of the next HMAC_MD5 here. rest in crypt_all()
	// &saved_key is now &ctx
	hmac_md5_init_K16(ntlm, &saved_key[index]);
#else
	/* Generate 16-byte NTLM hash */
	len = E_md4hash(saved_plain[index], len, saved_key[index]);
#endif

	if (len <= 0)
		saved_plain[index][-len] = 0; // match truncation
}

static char *netlmv2_get_key(int index)
{
  return (char *)saved_plain[index];
}

static int salt_hash(void *salt)
{
	// Hash the client challenge (in case server salt was spoofed)
	return (*(ARCH_WORD_32 *)salt+8) & (SALT_HASH_SIZE - 1);
}

static int binary_hash_0(void *binary)
{
	return *(ARCH_WORD_32 *)binary & 0xF;
}

static int binary_hash_1(void *binary)
{
	return *(ARCH_WORD_32 *)binary & 0xFF;
}

static int binary_hash_2(void *binary)
{
	return *(ARCH_WORD_32 *)binary & 0xFFF;
}

static int binary_hash_3(void *binary)
{
	return *(ARCH_WORD_32 *)binary & 0xFFFF;
}

static int binary_hash_4(void *binary)
{
	return *(ARCH_WORD_32 *)binary & 0xFFFFF;
}

static int get_hash_0(int index)
{
	return *(ARCH_WORD_32 *)output[index] & 0xF;
}

static int get_hash_1(int index)
{
	return *(ARCH_WORD_32 *)output[index] & 0xFF;
}

static int get_hash_2(int index)
{
	return *(ARCH_WORD_32 *)output[index] & 0xFFF;
}

static int get_hash_3(int index)
{
	return *(ARCH_WORD_32 *)output[index] & 0xFFFF;
}

static int get_hash_4(int index)
{
	return *(ARCH_WORD_32 *)output[index] & 0xFFFFF;
}

struct fmt_main fmt_NETLMv2 = {
  {
    FORMAT_LABEL,
    FORMAT_NAME,
    ALGORITHM_NAME,
    BENCHMARK_COMMENT,
    BENCHMARK_LENGTH,
    PLAINTEXT_LENGTH,
    BINARY_SIZE,
    SALT_SIZE,
    MIN_KEYS_PER_CRYPT,
    MAX_KEYS_PER_CRYPT,
    FMT_CASE | FMT_8_BIT | FMT_SPLIT_UNIFIES_CASE | FMT_OMP | FMT_UNICODE | FMT_UTF8,
    tests
  }, {
    init,
	netlmv2_prepare,
    netlmv2_valid,
    netlmv2_split,
    netlmv2_get_binary,
    netlmv2_get_salt,
    {
	    binary_hash_0,
	    binary_hash_1,
	    binary_hash_2,
	    binary_hash_3,
	    binary_hash_4
    },
    salt_hash,
    netlmv2_set_salt,
    netlmv2_set_key,
    netlmv2_get_key,
    fmt_default_clear_keys,
    netlmv2_crypt_all,
    {
	    get_hash_0,
	    get_hash_1,
	    get_hash_2,
	    get_hash_3,
	    get_hash_4
    },
    netlmv2_cmp_all,
    netlmv2_cmp_one,
    netlmv2_cmp_exact
  }
};
