📄 smbencrypt.c
字号:
{ uchar p24[24]; uchar partial_lm_hash[16]; memcpy(partial_lm_hash, lm_hash, 8); memset(partial_lm_hash + 8, 0xbd, 8); SMBOWFencrypt(partial_lm_hash, lm_resp, p24); memcpy(sess_key, p24, 16);#ifdef DEBUG_PASSWORD DEBUG(100, ("SMBsesskeygen_lmv1_jerry:\n")); dump_data(100, (const char *)sess_key, 16);#endif}DATA_BLOB NTLMv2_generate_names_blob(const char *hostname, const char *domain){ DATA_BLOB names_blob = data_blob(NULL, 0); msrpc_gen(&names_blob, "aaa", NTLMSSP_NAME_TYPE_DOMAIN, domain, NTLMSSP_NAME_TYPE_SERVER, hostname, 0, ""); return names_blob;}static DATA_BLOB NTLMv2_generate_client_data(const DATA_BLOB *names_blob) { uchar client_chal[8]; DATA_BLOB response = data_blob(NULL, 0); char long_date[8]; generate_random_buffer(client_chal, sizeof(client_chal)); put_long_date(long_date, time(NULL)); /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */ msrpc_gen(&response, "ddbbdb", 0x00000101, /* Header */ 0, /* 'Reserved' */ long_date, 8, /* Timestamp */ client_chal, 8, /* client challenge */ 0, /* Unknown */ names_blob->data, names_blob->length); /* End of name list */ return response;}static DATA_BLOB NTLMv2_generate_response(const uchar ntlm_v2_hash[16], const DATA_BLOB *server_chal, const DATA_BLOB *names_blob){ uchar ntlmv2_response[16]; DATA_BLOB ntlmv2_client_data; DATA_BLOB final_response; /* NTLMv2 */ /* generate some data to pass into the response function - including the hostname and domain name of the server */ ntlmv2_client_data = NTLMv2_generate_client_data(names_blob); /* Given that data, and the challenge from the server, generate a response */ SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response); final_response = data_blob(NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length); memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response)); memcpy(final_response.data+sizeof(ntlmv2_response), ntlmv2_client_data.data, ntlmv2_client_data.length); data_blob_free(&ntlmv2_client_data); return final_response;}static DATA_BLOB LMv2_generate_response(const uchar ntlm_v2_hash[16], const DATA_BLOB *server_chal){ uchar lmv2_response[16]; DATA_BLOB lmv2_client_data = data_blob(NULL, 8); DATA_BLOB final_response = data_blob(NULL, 24); /* LMv2 */ /* client-supplied random data */ generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length); /* Given that data, and the challenge from the server, generate a response */ SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response); memcpy(final_response.data, lmv2_response, sizeof(lmv2_response)); /* after the first 16 bytes is the random data we generated above, so the server can verify us with it */ memcpy(final_response.data+sizeof(lmv2_response), lmv2_client_data.data, lmv2_client_data.length); data_blob_free(&lmv2_client_data); return final_response;}BOOL SMBNTLMv2encrypt(const char *user, const char *domain, const char *password, const DATA_BLOB *server_chal, const DATA_BLOB *names_blob, DATA_BLOB *lm_response, DATA_BLOB *nt_response, DATA_BLOB *user_session_key) { uchar nt_hash[16]; uchar ntlm_v2_hash[16]; E_md4hash(password, nt_hash); /* We don't use the NT# directly. Instead we use it mashed up with the username and domain. This prevents username swapping during the auth exchange */ if (!ntv2_owf_gen(nt_hash, user, domain, True, ntlm_v2_hash)) { return False; } if (nt_response) { *nt_response = NTLMv2_generate_response(ntlm_v2_hash, server_chal, names_blob); if (user_session_key) { *user_session_key = data_blob(NULL, 16); /* The NTLMv2 calculations also provide a session key, for signing etc later */ /* use only the first 16 bytes of nt_response for session key */ SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data); } } /* LMv2 */ if (lm_response) { *lm_response = LMv2_generate_response(ntlm_v2_hash, server_chal); } return True;}/*********************************************************** encode a password buffer with a unicode password. The buffer is filled with random data to make it harder to attack.************************************************************/BOOL encode_pw_buffer(uint8 buffer[516], const char *password, int string_flags){ uchar new_pw[512]; size_t new_pw_len; new_pw_len = push_string(NULL, new_pw, password, sizeof(new_pw), string_flags); memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len); generate_random_buffer(buffer, 512 - new_pw_len); /* * The length of the new password is in the last 4 bytes of * the data buffer. */ SIVAL(buffer, 512, new_pw_len); ZERO_STRUCT(new_pw); return True;}/*********************************************************** decode a password buffer *new_pw_len is the length in bytes of the possibly mulitbyte returned password including termination.************************************************************/BOOL decode_pw_buffer(uint8 in_buffer[516], char *new_pwrd, int new_pwrd_size, uint32 *new_pw_len, int string_flags){ int byte_len=0; /* Warning !!! : This function is called from some rpc call. The password IN the buffer may be a UNICODE string. The password IN new_pwrd is an ASCII string If you reuse that code somewhere else check first. */ /* The length of the new password is in the last 4 bytes of the data buffer. */ byte_len = IVAL(in_buffer, 512);#ifdef DEBUG_PASSWORD dump_data(100, (const char *)in_buffer, 516);#endif /* Password cannot be longer than the size of the password buffer */ if ( (byte_len < 0) || (byte_len > 512)) { DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len)); DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n")); return False; } /* decode into the return buffer. Buffer length supplied */ *new_pw_len = pull_string(NULL, new_pwrd, &in_buffer[512 - byte_len], new_pwrd_size, byte_len, string_flags);#ifdef DEBUG_PASSWORD DEBUG(100,("decode_pw_buffer: new_pwrd: ")); dump_data(100, (const char *)new_pwrd, *new_pw_len); DEBUG(100,("multibyte len:%d\n", *new_pw_len)); DEBUG(100,("original char len:%d\n", byte_len/2));#endif return True;}/*********************************************************** Encrypt/Decrypt used for LSA secrets and trusted domain passwords.************************************************************/void sess_crypt_blob(DATA_BLOB *out, const DATA_BLOB *in, const DATA_BLOB *session_key, int forward){ int i, k; for (i=0,k=0; i<in->length; i += 8, k += 7) { uint8 bin[8], bout[8], key[7]; memset(bin, 0, 8); memcpy(bin, &in->data[i], MIN(8, in->length-i)); if (k + 7 > session_key->length) { k = (session_key->length - k); } memcpy(key, &session_key->data[k], 7); des_crypt56(bout, bin, key, forward?1:0); memcpy(&out->data[i], bout, MIN(8, in->length-i)); }}/* Decrypts password-blob with session-key * @param pass password for session-key * @param data_in DATA_BLOB encrypted password * * Returns cleartext password in CH_UNIX * Caller must free the returned string */char *decrypt_trustdom_secret(const char *pass, DATA_BLOB *data_in){ DATA_BLOB data_out, sess_key; uchar nt_hash[16]; uint32_t length; uint32_t version; fstring cleartextpwd; if (!data_in || !pass) return NULL; /* generate md4 password-hash derived from the NT UNICODE password */ E_md4hash(pass, nt_hash); /* hashed twice with md4 */ mdfour(nt_hash, nt_hash, 16); /* 16-Byte session-key */ sess_key = data_blob(nt_hash, 16); if (sess_key.data == NULL) return NULL; data_out = data_blob(NULL, data_in->length); if (data_out.data == NULL) return NULL; /* decrypt with des3 */ sess_crypt_blob(&data_out, data_in, &sess_key, 0); /* 4 Byte length, 4 Byte version */ length = IVAL(data_out.data, 0); version = IVAL(data_out.data, 4); if (length > data_in->length - 8) { DEBUG(0,("decrypt_trustdom_secret: invalid length (%d)\n", length)); return NULL; } if (version != 1) { DEBUG(0,("decrypt_trustdom_secret: unknown version number (%d)\n", version)); return NULL; } rpcstr_pull(cleartextpwd, data_out.data + 8, sizeof(fstring), length, 0 );#ifdef DEBUG_PASSWORD DEBUG(100,("decrypt_trustdom_secret: length is: %d, version is: %d, password is: %s\n", length, version, cleartextpwd));#endif data_blob_free(&data_out); data_blob_free(&sess_key); return SMB_STRDUP(cleartextpwd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -