⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smbencrypt.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    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 2 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, write to the Free Software   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "includes.h"#include "byteorder.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 uchar *c8, uchar p24[24]){	BOOL ret;	uchar 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, (const char *)p21, 16);	dump_data(100, (const char *)c8, 8);	dump_data(100, (const char *)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 */ void E_md4hash(const char *passwd, uchar p16[16]){	int len;	smb_ucs2_t wpwd[129];		/* Password must be converted to NT unicode - null terminated. */	push_ucs2(NULL, wpwd, (const char *)passwd, 256, STR_UNICODE|STR_NOALIGN|STR_TERMINATE);	/* Calculate length in bytes */	len = strlen_w(wpwd) * sizeof(int16);	mdfour(p16, (unsigned char *)wpwd, len);	ZERO_STRUCT(wpwd);	}/** * Creates the MD5 Hash of a combination of 16 byte salt and 16 byte NT hash. * @param 16 byte salt. * @param 16 byte NT hash. * @param 16 byte return hashed with md5, caller allocated 16 byte buffer */void E_md5hash(const uchar salt[16], const uchar nthash[16], uchar hash_out[16]){	struct MD5Context tctx;	uchar array[32];		memset(hash_out, '\0', 16);	memcpy(array, salt, 16);	memcpy(&array[16], nthash, 16);	MD5Init(&tctx);	MD5Update(&tctx, array, 32);	MD5Final(hash_out, &tctx);}/** * 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, uchar p16[16]){	BOOL ret = True;	fstring dospwd; 	ZERO_STRUCT(dospwd);		/* Password must be converted to DOS charset - null terminated, uppercase. */	push_ascii(dospwd, passwd, sizeof(dospwd), STR_UPPER|STR_TERMINATE);       	/* Only the fisrt 14 chars are considered, password need not be null terminated. */	E_P16((const unsigned char *)dospwd, p16);	if (strlen(dospwd) > 14) {		ret = False;	}	ZERO_STRUCT(dospwd);		return ret;}/** * Creates the MD4 and DES (LM) Hash of the users password.   * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password. * @param passwd password in 'unix' charset. * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer * @param p16 return password hashed with des, caller allocated 16 byte buffer */ /* Does both the NT and LM owfs of a user's password */void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar p16[16]){	/* Calculate the MD4 hash (NT compatible) of the password */	memset(nt_p16, '\0', 16);	E_md4hash(pwd, nt_p16);#ifdef DEBUG_PASSWORD	DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));	dump_data(120, pwd, strlen(pwd));	dump_data(100, (char *)nt_p16, 16);#endif	E_deshash(pwd, (uchar *)p16);#ifdef DEBUG_PASSWORD	DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));	dump_data(120, pwd, strlen(pwd));	dump_data(100, (char *)p16, 16);#endif}/* Does both the NTLMv2 owfs of a user's password */BOOL ntv2_owf_gen(const uchar owf[16],		  const char *user_in, const char *domain_in,		  BOOL upper_case_domain, /* Transform the domain into UPPER case */		  uchar kr_buf[16]){	smb_ucs2_t *user;	smb_ucs2_t *domain;		size_t user_byte_len;	size_t domain_byte_len;	HMACMD5Context ctx;	user_byte_len = push_ucs2_allocate(&user, user_in);	if (user_byte_len == (size_t)-1) {		DEBUG(0, ("push_uss2_allocate() for user returned -1 (probably malloc() failure)\n"));		return False;	}	domain_byte_len = push_ucs2_allocate(&domain, domain_in);	if (domain_byte_len == (size_t)-1) {		DEBUG(0, ("push_uss2_allocate() for domain returned -1 (probably malloc() failure)\n"));		return False;	}	strupper_w(user);	if (upper_case_domain)		strupper_w(domain);	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((const unsigned char *)user, user_byte_len, &ctx);	hmac_md5_update((const unsigned char *)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, (const char *)user, user_byte_len);	dump_data(100, (const char *)domain, domain_byte_len);	dump_data(100, (const char *)owf, 16);	dump_data(100, (const char *)kr_buf, 16);#endif	SAFE_FREE(user);	SAFE_FREE(domain);	return True;}/* Does the des encryption from the NT or LM MD4 hash. */void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24]){	uchar p21[21];	ZERO_STRUCT(p21); 	memcpy(p21, passwd, 16);    	E_P24(p21, c8, p24);}/* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24]){	uchar p21[21]; 	memset(p21,'\0',21);	memcpy(p21, passwd, 8);    	memset(p21 + 8, 0xbd, 8);    	E_P24(p21, ntlmchalresp, p24);#ifdef DEBUG_PASSWORD	DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));	dump_data(100, (char *)p21, 21);	dump_data(100, (const char *)ntlmchalresp, 8);	dump_data(100, (char *)p24, 24);#endif}/* Does the NT MD4 hash then des encryption. */ void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24){	uchar 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, (char *)p21, 16);	dump_data(100, (char *)c8, 8);	dump_data(100, (char *)p24, 24);#endif}/* Does the md5 encryption from the Key Response for NTLMv2. */void SMBOWFencrypt_ntv2(const uchar kr[16],			const DATA_BLOB *srv_chal,			const DATA_BLOB *cli_chal,			uchar 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(cli_chal->data, cli_chal->length, &ctx);	hmac_md5_final(resp_buf, &ctx);#ifdef DEBUG_PASSWORD	DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));	dump_data(100, (const char *)srv_chal->data, srv_chal->length);	dump_data(100, (const char *)cli_chal->data, cli_chal->length);	dump_data(100, (const char *)resp_buf, 16);#endif}void SMBsesskeygen_ntv2(const uchar kr[16],			const uchar * nt_resp, uint8 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((unsigned char *)sess_key, &ctx);#ifdef DEBUG_PASSWORD	DEBUG(100, ("SMBsesskeygen_ntv2:\n"));	dump_data(100, (const char *)sess_key, 16);#endif}void SMBsesskeygen_ntv1(const uchar kr[16],			const uchar * nt_resp, uint8 sess_key[16]){	/* yes, this session key does not change - yes, this 	   is a problem - but it is 128 bits */		mdfour((unsigned char *)sess_key, kr, 16);#ifdef DEBUG_PASSWORD	DEBUG(100, ("SMBsesskeygen_ntv1:\n"));	dump_data(100, (const char *)sess_key, 16);#endif}void SMBsesskeygen_lm_sess_key(const uchar lm_hash[16],			const uchar lm_resp[24], /* only uses 8 */ 			uint8 sess_key[16])

⌨️ 快捷键说明

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