smbencrypt.c

来自「samba最新软件」· C语言 代码 · 共 541 行 · 第 1/2 页

C
541
字号
/*    Unix SMB/CIFS implementation.   SMB parameters and setup   Copyright (C) Andrew Tridgell 1992-1998   Modified by Jeremy Allison 1995.   Copyright (C) Jeremy Allison 1995-2000.   Copyright (C) Luke Kennethc Casson Leighton 1996-2000.   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003      This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 3 of the License, or   (at your option) any later version.      This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program.  If not, see <http://www.gnu.org/licenses/>.*/#include "includes.h"#include "system/time.h"#include "auth/ntlmssp/ntlmssp.h"#include "auth/ntlmssp/msrpc_parse.h"#include "lib/crypto/crypto.h"#include "libcli/auth/libcli_auth.h"#include "pstring.h"#include "param/param.h"/*   This implements the X/Open SMB password encryption   It takes a password ('unix' string), a 8 byte "crypt key"    and puts 24 bytes of encrypted password into p24    Returns false if password must have been truncated to create LM hash*/bool SMBencrypt(const char *passwd, const uint8_t *c8, uint8_t p24[24]){	bool ret;	uint8_t p21[21];	memset(p21,'\0',21);	ret = E_deshash(passwd, p21); 	SMBOWFencrypt(p21, c8, p24);#ifdef DEBUG_PASSWORD	DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));	dump_data(100, p21, 16);	dump_data(100, c8, 8);	dump_data(100, p24, 24);#endif	return ret;}/** * Creates the MD4 Hash of the users password in NT UNICODE. * @param passwd password in 'unix' charset. * @param p16 return password hashed with md4, caller allocated 16 byte buffer */ bool E_md4hash(const char *passwd, uint8_t p16[16]){	int len;	void *wpwd;	len = push_ucs2_talloc(NULL, lp_iconv_convenience(global_loadparm), &wpwd, passwd);	if (len < 2) {		/* We don't want to return fixed data, as most callers		 * don't check */		mdfour(p16, (const uint8_t *)passwd, strlen(passwd));		return false;	}		len -= 2;	mdfour(p16, wpwd, len);	talloc_free(wpwd);	return true;}/** * Creates the DES forward-only Hash of the users password in DOS ASCII charset * @param passwd password in 'unix' charset. * @param p16 return password hashed with DES, caller allocated 16 byte buffer * @return false if password was > 14 characters, and therefore may be incorrect, otherwise true * @note p16 is filled in regardless */ bool E_deshash(const char *passwd, uint8_t p16[16]){	bool ret = true;	fstring dospwd; 	ZERO_STRUCT(dospwd);	/* Password must be converted to DOS charset - null terminated, uppercase. */	push_string(lp_iconv_convenience(global_loadparm), dospwd, passwd, sizeof(dospwd), STR_ASCII|STR_UPPER|STR_TERMINATE);	/* Only the first 14 chars are considered, password need not be null terminated. */	E_P16((const uint8_t *)dospwd, p16);	if (strlen(dospwd) > 14) {		ret = false;	}	ZERO_STRUCT(dospwd);		return ret;}/* Does both the NTLMv2 owfs of a user's password */bool ntv2_owf_gen(const uint8_t owf[16],		  const char *user_in, const char *domain_in,		  bool upper_case_domain, /* Transform the domain into UPPER case */		  uint8_t kr_buf[16]){	void *user;	void *domain;		size_t user_byte_len;	size_t domain_byte_len;	HMACMD5Context ctx;	TALLOC_CTX *mem_ctx = talloc_init("ntv2_owf_gen for %s\\%s", domain_in, user_in); 	struct smb_iconv_convenience *iconv_convenience = lp_iconv_convenience(global_loadparm);	if (!mem_ctx) {		return false;	}	if (!user_in) {		user_in = "";	}	if (!domain_in) {		domain_in = "";	}	user_in = strupper_talloc(mem_ctx, user_in);	if (user_in == NULL) {		talloc_free(mem_ctx);		return false;	}	if (upper_case_domain) {		domain_in = strupper_talloc(mem_ctx, domain_in);		if (domain_in == NULL) {			talloc_free(mem_ctx);			return false;		}	}	user_byte_len = push_ucs2_talloc(mem_ctx, iconv_convenience, &user, user_in);	if (user_byte_len == (ssize_t)-1) {		DEBUG(0, ("push_uss2_talloc() for user returned -1 (probably talloc() failure)\n"));		talloc_free(mem_ctx);		return false;	}	domain_byte_len = push_ucs2_talloc(mem_ctx, iconv_convenience, &domain, domain_in);	if (domain_byte_len == (ssize_t)-1) {		DEBUG(0, ("push_ucs2_talloc() for domain returned -1 (probably talloc() failure)\n"));		talloc_free(mem_ctx);		return false;	}	SMB_ASSERT(user_byte_len >= 2);	SMB_ASSERT(domain_byte_len >= 2);	/* We don't want null termination */	user_byte_len = user_byte_len - 2;	domain_byte_len = domain_byte_len - 2;		hmac_md5_init_limK_to_64(owf, 16, &ctx);	hmac_md5_update(user, user_byte_len, &ctx);	hmac_md5_update(domain, domain_byte_len, &ctx);	hmac_md5_final(kr_buf, &ctx);#ifdef DEBUG_PASSWORD	DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));	dump_data(100, user, user_byte_len);	dump_data(100, domain, domain_byte_len);	dump_data(100, owf, 16);	dump_data(100, kr_buf, 16);#endif	talloc_free(mem_ctx);	return true;}/* Does the des encryption from the NT or LM MD4 hash. */void SMBOWFencrypt(const uint8_t passwd[16], const uint8_t *c8, uint8_t p24[24]){	uint8_t p21[21];	ZERO_STRUCT(p21); 	memcpy(p21, passwd, 16);    	E_P24(p21, c8, p24);}/* Does the NT MD4 hash then des encryption. */ void SMBNTencrypt(const char *passwd, uint8_t *c8, uint8_t *p24){	uint8_t p21[21]; 	memset(p21,'\0',21); 	E_md4hash(passwd, p21);    	SMBOWFencrypt(p21, c8, p24);#ifdef DEBUG_PASSWORD	DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));	dump_data(100, p21, 16);	dump_data(100, c8, 8);	dump_data(100, p24, 24);#endif}/* Does the md5 encryption from the Key Response for NTLMv2. */void SMBOWFencrypt_ntv2(const uint8_t kr[16],			const DATA_BLOB *srv_chal,			const DATA_BLOB *smbcli_chal,			uint8_t resp_buf[16]){	HMACMD5Context ctx;	hmac_md5_init_limK_to_64(kr, 16, &ctx);	hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);	hmac_md5_update(smbcli_chal->data, smbcli_chal->length, &ctx);	hmac_md5_final(resp_buf, &ctx);#ifdef DEBUG_PASSWORD	DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, smbcli_chal, resp_buf\n"));	dump_data(100, srv_chal->data, srv_chal->length);	dump_data(100, smbcli_chal->data, smbcli_chal->length);	dump_data(100, resp_buf, 16);#endif}void SMBsesskeygen_ntv2(const uint8_t kr[16],			const uint8_t * nt_resp, uint8_t sess_key[16]){	/* a very nice, 128 bit, variable session key */		HMACMD5Context ctx;	hmac_md5_init_limK_to_64(kr, 16, &ctx);	hmac_md5_update(nt_resp, 16, &ctx);	hmac_md5_final((uint8_t *)sess_key, &ctx);#ifdef DEBUG_PASSWORD	DEBUG(100, ("SMBsesskeygen_ntv2:\n"));	dump_data(100, sess_key, 16);#endif}void SMBsesskeygen_ntv1(const uint8_t kr[16], uint8_t sess_key[16]){	/* yes, this session key does not change - yes, this 	   is a problem - but it is 128 bits */		mdfour((uint8_t *)sess_key, kr, 16);#ifdef DEBUG_PASSWORD	DEBUG(100, ("SMBsesskeygen_ntv1:\n"));	dump_data(100, sess_key, 16);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?