📄 ntlm_check.c
字号:
/* Unix SMB/CIFS implementation. Password and authentication handling Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2004 Copyright (C) Gerald Carter 2003 Copyright (C) Luke Kenneth Casson Leighton 1996-2000 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 "lib/crypto/crypto.h"#include "librpc/gen_ndr/netlogon.h"#include "libcli/auth/libcli_auth.h"#include "param/param.h"#include "auth/ntlm/ntlm_check.h"/**************************************************************************** Core of smb password checking routine.****************************************************************************/static bool smb_pwd_check_ntlmv1(TALLOC_CTX *mem_ctx, const DATA_BLOB *nt_response, const uint8_t *part_passwd, const DATA_BLOB *sec_blob, DATA_BLOB *user_sess_key){ /* Finish the encryption of part_passwd. */ uint8_t p24[24]; if (part_passwd == NULL) { DEBUG(10,("No password set - DISALLOWING access\n")); /* No password set - always false ! */ return false; } if (sec_blob->length != 8) { DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%lu)\n", (unsigned long)sec_blob->length)); return false; } if (nt_response->length != 24) { DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n", (unsigned long)nt_response->length)); return false; } SMBOWFencrypt(part_passwd, sec_blob->data, p24); #if DEBUG_PASSWORD DEBUG(100,("Part password (P16) was |\n")); dump_data(100, part_passwd, 16); DEBUGADD(100,("Password from client was |\n")); dump_data(100, nt_response->data, nt_response->length); DEBUGADD(100,("Given challenge was |\n")); dump_data(100, sec_blob->data, sec_blob->length); DEBUGADD(100,("Value from encryption was |\n")); dump_data(100, p24, 24);#endif if (memcmp(p24, nt_response->data, 24) == 0) { if (user_sess_key != NULL) { *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16); SMBsesskeygen_ntv1(part_passwd, user_sess_key->data); } return true; } return false;}/**************************************************************************** Core of smb password checking routine. (NTLMv2, LMv2) Note: The same code works with both NTLMv2 and LMv2.****************************************************************************/static bool smb_pwd_check_ntlmv2(TALLOC_CTX *mem_ctx, const DATA_BLOB *ntv2_response, const uint8_t *part_passwd, const DATA_BLOB *sec_blob, const char *user, const char *domain, bool upper_case_domain, /* should the domain be transformed into upper case? */ DATA_BLOB *user_sess_key){ /* Finish the encryption of part_passwd. */ uint8_t kr[16]; uint8_t value_from_encryption[16]; DATA_BLOB client_key_data; if (part_passwd == NULL) { DEBUG(10,("No password set - DISALLOWING access\n")); /* No password set - always false */ return false; } if (sec_blob->length != 8) { DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n", (unsigned long)sec_blob->length)); return false; } if (ntv2_response->length < 24) { /* We MUST have more than 16 bytes, or the stuff below will go crazy. No known implementation sends less than the 24 bytes for LMv2, let alone NTLMv2. */ DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n", (unsigned long)ntv2_response->length)); return false; } client_key_data = data_blob_talloc(mem_ctx, ntv2_response->data+16, ntv2_response->length-16); /* todo: should we be checking this for anything? We can't for LMv2, but for NTLMv2 it is meant to contain the current time etc. */ if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) { return false; } SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);#if DEBUG_PASSWORD DEBUG(100,("Part password (P16) was |\n")); dump_data(100, part_passwd, 16); DEBUGADD(100,("Password from client was |\n")); dump_data(100, ntv2_response->data, ntv2_response->length); DEBUGADD(100,("Variable data from client was |\n")); dump_data(100, client_key_data.data, client_key_data.length); DEBUGADD(100,("Given challenge was |\n")); dump_data(100, sec_blob->data, sec_blob->length); DEBUGADD(100,("Value from encryption was |\n")); dump_data(100, value_from_encryption, 16);#endif data_blob_clear_free(&client_key_data); if (memcmp(value_from_encryption, ntv2_response->data, 16) == 0) { if (user_sess_key != NULL) { *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16); SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data); } return true; } return false;}/**************************************************************************** Core of smb password checking routine. (NTLMv2, LMv2) Note: The same code works with both NTLMv2 and LMv2.****************************************************************************/static bool smb_sess_key_ntlmv2(TALLOC_CTX *mem_ctx, const DATA_BLOB *ntv2_response, const uint8_t *part_passwd, const DATA_BLOB *sec_blob, const char *user, const char *domain, bool upper_case_domain, /* should the domain be transformed into upper case? */ DATA_BLOB *user_sess_key){ /* Finish the encryption of part_passwd. */ uint8_t kr[16]; uint8_t value_from_encryption[16]; DATA_BLOB client_key_data; if (part_passwd == NULL) { DEBUG(10,("No password set - DISALLOWING access\n")); /* No password set - always false */ return false; } if (sec_blob->length != 8) { DEBUG(0, ("smb_sess_key_ntlmv2: incorrect challenge size (%lu)\n", (unsigned long)sec_blob->length)); return false; } if (ntv2_response->length < 24) { /* We MUST have more than 16 bytes, or the stuff below will go crazy. No known implementation sends less than the 24 bytes for LMv2, let alone NTLMv2. */ DEBUG(0, ("smb_sess_key_ntlmv2: incorrect password length (%lu)\n", (unsigned long)ntv2_response->length)); return false; } client_key_data = data_blob_talloc(mem_ctx, ntv2_response->data+16, ntv2_response->length-16); if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) { return false; } SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption); *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16); SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data); return true;}/** * Compare password hashes against those from the SAM * * @param mem_ctx talloc context * @param client_lanman LANMAN password hash, as supplied by the client * @param client_nt NT (MD4) password hash, as supplied by the client * @param username internal Samba username, for log messages * @param client_username username the client used * @param client_domain domain name the client used (may be mapped) * @param stored_lanman LANMAN password hash, as stored on the SAM * @param stored_nt NT (MD4) password hash, as stored on the SAM * @param user_sess_key User session key * @param lm_sess_key LM session key (first 8 bytes of the LM hash) */NTSTATUS hash_password_check(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, const struct samr_Password *client_lanman, const struct samr_Password *client_nt, const char *username, const struct samr_Password *stored_lanman, const struct samr_Password *stored_nt){ if (stored_nt == NULL) { DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n", username)); } if (client_nt && stored_nt) { if (memcmp(client_nt->hash, stored_nt->hash, sizeof(stored_nt->hash)) == 0) { return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } else if (client_lanman && stored_lanman) { if (!lp_lanman_auth(lp_ctx)) { DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n", username)); return NT_STATUS_WRONG_PASSWORD; } if (strchr_m(username, '@')) { return NT_STATUS_NOT_FOUND; } if (memcmp(client_lanman->hash, stored_lanman->hash, sizeof(stored_lanman->hash)) == 0) { return NT_STATUS_OK; } else { DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n", username)); return NT_STATUS_WRONG_PASSWORD; } } if (strchr_m(username, '@')) { return NT_STATUS_NOT_FOUND; } return NT_STATUS_WRONG_PASSWORD;}/** * Check a challenge-response password against the value of the NT or * LM password hash. * * @param mem_ctx talloc context * @param challenge 8-byte challenge. If all zero, forces plaintext comparison * @param nt_response 'unicode' NT response to the challenge, or unicode password * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page * @param username internal Samba username, for log messages * @param client_username username the client used * @param client_domain domain name the client used (may be mapped) * @param stored_lanman LANMAN ASCII password from our passdb or similar * @param stored_nt MD4 unicode password from our passdb or similar * @param user_sess_key User session key * @param lm_sess_key LM session key (first 8 bytes of the LM hash) */NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, uint32_t logon_parameters, const DATA_BLOB *challenge, const DATA_BLOB *lm_response, const DATA_BLOB *nt_response, const char *username, const char *client_username, const char *client_domain, const struct samr_Password *stored_lanman, const struct samr_Password *stored_nt, DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key){ const static uint8_t zeros[8]; DATA_BLOB tmp_sess_key; if (stored_nt == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -