2 Unix SMB/CIFS implementation.
4 test suite for netlogon SamLogon operations
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Tim Potter 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "librpc/gen_ndr/ndr_netlogon.h"
27 #include "auth/auth.h"
28 #include "lib/crypto/crypto.h"
30 #define TEST_MACHINE_NAME "samlogontest"
41 struct samlogon_state {
43 const char *account_name;
44 const char *account_domain;
46 struct dcerpc_pipe *p;
48 struct netr_LogonSamLogon r;
49 struct netr_LogonSamLogonEx r_ex;
50 struct netr_LogonSamLogonWithFlags r_flags;
51 struct netr_Authenticator auth, auth2;
52 struct creds_CredentialState *creds;
58 Authenticate a user with a challenge/response, checking session key
59 and valid authentication types
61 static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state,
62 enum ntlm_break break_which,
64 DATA_BLOB *lm_response,
65 DATA_BLOB *nt_response,
67 uint8_t user_session_key[16],
71 struct netr_LogonSamLogon *r = &samlogon_state->r;
72 struct netr_LogonSamLogonEx *r_ex = &samlogon_state->r_ex;
73 struct netr_LogonSamLogonWithFlags *r_flags = &samlogon_state->r_flags;
74 struct netr_NetworkInfo ninfo;
75 struct netr_SamBaseInfo *base = NULL;
76 uint16 validation_level = 0;
78 samlogon_state->r.in.logon.network = &ninfo;
79 samlogon_state->r_ex.in.logon.network = &ninfo;
80 samlogon_state->r_flags.in.logon.network = &ninfo;
82 ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
83 ninfo.identity_info.parameter_control = 0;
84 ninfo.identity_info.logon_id_low = 0;
85 ninfo.identity_info.logon_id_high = 0;
86 ninfo.identity_info.account_name.string = samlogon_state->account_name;
87 ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
89 memcpy(ninfo.challenge, chall->data, 8);
91 switch (break_which) {
95 if (lm_response && lm_response->data) {
96 lm_response->data[0]++;
100 if (nt_response && nt_response->data) {
101 nt_response->data[0]++;
105 if (lm_response && lm_response->data) {
106 lm_response->data[0]++;
108 if (nt_response && nt_response->data) {
109 nt_response->data[0]++;
113 data_blob_free(lm_response);
116 data_blob_free(nt_response);
121 ninfo.nt.data = nt_response->data;
122 ninfo.nt.length = nt_response->length;
124 ninfo.nt.data = NULL;
129 ninfo.lm.data = lm_response->data;
130 ninfo.lm.length = lm_response->length;
132 ninfo.lm.data = NULL;
136 switch (samlogon_state->function_level) {
137 case DCERPC_NETR_LOGONSAMLOGON:
138 ZERO_STRUCT(samlogon_state->auth2);
139 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
141 r->out.return_authenticator = NULL;
142 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
143 if (!r->out.return_authenticator ||
144 !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
145 printf("Credential chaining failed\n");
147 if (!NT_STATUS_IS_OK(status)) {
149 *error_string = strdup(nt_errstr(status));
153 validation_level = r->in.validation_level;
154 switch (validation_level) {
156 base = &r->out.validation.sam2->base;
159 base = &r->out.validation.sam3->base;
162 base = &r->out.validation.sam6->base;
166 case DCERPC_NETR_LOGONSAMLOGONEX:
167 status = dcerpc_netr_LogonSamLogonEx(samlogon_state->p, samlogon_state->mem_ctx, r_ex);
168 if (!NT_STATUS_IS_OK(status)) {
170 *error_string = strdup(nt_errstr(status));
174 validation_level = r_ex->in.validation_level;
175 switch (validation_level) {
177 base = &r_ex->out.validation.sam2->base;
180 base = &r_ex->out.validation.sam3->base;
183 base = &r_ex->out.validation.sam6->base;
187 case DCERPC_NETR_LOGONSAMLOGONWITHFLAGS:
188 ZERO_STRUCT(samlogon_state->auth2);
189 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
191 r_flags->out.return_authenticator = NULL;
192 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
193 if (!r_flags->out.return_authenticator ||
194 !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
195 printf("Credential chaining failed\n");
197 if (!NT_STATUS_IS_OK(status)) {
199 *error_string = strdup(nt_errstr(status));
203 validation_level = r_flags->in.validation_level;
204 switch (validation_level) {
206 base = &r_flags->out.validation.sam2->base;
209 base = &r_flags->out.validation.sam3->base;
212 base = &r_flags->out.validation.sam6->base;
219 if (!NT_STATUS_IS_OK(status)) {
220 /* we cannot check the session key, if the logon failed... */
225 printf("No user info returned from 'successful' SamLogon*() call!\n");
226 return NT_STATUS_INVALID_PARAMETER;
229 /* find and decyrpt the session keys, return in parameters above */
230 if (validation_level == 6) {
231 /* they aren't encrypted! */
232 if (user_session_key) {
233 memcpy(user_session_key, base->key.key, 16);
236 memcpy(lm_key, base->LMSessKey.key, 8);
238 } else if (samlogon_state->creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
239 static const char zeros[16];
241 if (memcmp(base->key.key, zeros,
242 sizeof(base->key.key)) != 0) {
243 creds_arcfour_crypt(samlogon_state->creds,
245 sizeof(base->key.key));
248 if (user_session_key) {
249 memcpy(user_session_key, base->key.key, 16);
252 if (memcmp(base->LMSessKey.key, zeros,
253 sizeof(base->LMSessKey.key)) != 0) {
254 creds_arcfour_crypt(samlogon_state->creds,
256 sizeof(base->LMSessKey.key));
260 memcpy(lm_key, base->LMSessKey.key, 8);
263 static const char zeros[16];
265 if (user_session_key) {
266 memcpy(user_session_key, base->key.key, 16);
269 if (memcmp(base->LMSessKey.key, zeros,
270 sizeof(base->LMSessKey.key)) != 0) {
271 creds_des_decrypt_LMKey(samlogon_state->creds,
276 memcpy(lm_key, base->LMSessKey.key, 8);
285 * Test the normal 'LM and NTLM' combination
288 static BOOL test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
293 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
294 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
295 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
298 uint8_t user_session_key[16];
303 ZERO_STRUCT(user_session_key);
305 lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
307 ZERO_STRUCT(lm_hash);
309 E_deshash(samlogon_state->password, lm_hash);
312 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
314 E_md4hash(samlogon_state->password, nt_hash);
315 SMBsesskeygen_ntv1(nt_hash, session_key.data);
317 nt_status = check_samlogon(samlogon_state,
319 &samlogon_state->chall,
326 data_blob_free(&lm_response);
328 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
329 /* for 'long' passwords, the LM password is invalid */
330 if (break_which == NO_NT && !lm_good) {
333 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
336 if (!NT_STATUS_IS_OK(nt_status)) {
340 if (break_which == NO_NT && !lm_good) {
341 printf("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
345 if (memcmp(lm_hash, lm_key,
346 sizeof(lm_key)) != 0) {
347 printf("LM Key does not match expectations!\n");
349 dump_data(1, lm_key, 8);
350 printf("expected:\n");
351 dump_data(1, lm_hash, 8);
355 switch (break_which) {
358 uint8_t lm_key_expected[16];
359 memcpy(lm_key_expected, lm_hash, 8);
360 memset(lm_key_expected+8, '\0', 8);
361 if (memcmp(lm_key_expected, user_session_key,
363 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
364 printf("user_session_key:\n");
365 dump_data(1, user_session_key, sizeof(user_session_key));
366 printf("expected:\n");
367 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
373 if (memcmp(session_key.data, user_session_key,
374 sizeof(user_session_key)) != 0) {
375 printf("NT Session Key does not match expectations!\n");
376 printf("user_session_key:\n");
377 dump_data(1, user_session_key, 16);
378 printf("expected:\n");
379 dump_data(1, session_key.data, session_key.length);
387 * Test LM authentication, no NT response supplied
390 static BOOL test_lm(struct samlogon_state *samlogon_state, char **error_string)
393 return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
397 * Test the NTLM response only, no LM.
400 static BOOL test_ntlm(struct samlogon_state *samlogon_state, char **error_string)
402 return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
406 * Test the NTLM response only, but in the LM field.
409 static BOOL test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string)
413 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
417 uint8_t user_session_key[16];
419 ZERO_STRUCT(user_session_key);
421 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
423 E_deshash(samlogon_state->password, lm_hash);
425 nt_status = check_samlogon(samlogon_state,
427 &samlogon_state->chall,
434 if (!NT_STATUS_IS_OK(nt_status)) {
438 if (memcmp(lm_hash, lm_key,
439 sizeof(lm_key)) != 0) {
440 printf("LM Key does not match expectations!\n");
442 dump_data(1, lm_key, 8);
443 printf("expected:\n");
444 dump_data(1, lm_hash, 8);
447 if (memcmp(lm_hash, user_session_key, 8) != 0) {
448 uint8_t lm_key_expected[16];
449 memcpy(lm_key_expected, lm_hash, 8);
450 memset(lm_key_expected+8, '\0', 8);
451 if (memcmp(lm_key_expected, user_session_key,
453 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
454 printf("user_session_key:\n");
455 dump_data(1, user_session_key, sizeof(user_session_key));
456 printf("expected:\n");
457 dump_data(1, lm_key_expected, sizeof(lm_key_expected));
465 * Test the NTLM response only, but in the both the NT and LM fields.
468 static BOOL test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string)
473 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
474 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
478 uint8_t user_session_key[16];
482 ZERO_STRUCT(user_session_key);
484 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
486 E_md4hash(samlogon_state->password, nt_hash);
487 SMBsesskeygen_ntv1(nt_hash,
490 lm_good = E_deshash(samlogon_state->password, lm_hash);
492 ZERO_STRUCT(lm_hash);
495 nt_status = check_samlogon(samlogon_state,
497 &samlogon_state->chall,
504 if (!NT_STATUS_IS_OK(nt_status)) {
508 if (memcmp(lm_hash, lm_key,
509 sizeof(lm_key)) != 0) {
510 printf("LM Key does not match expectations!\n");
512 dump_data(1, lm_key, 8);
513 printf("expected:\n");
514 dump_data(1, lm_hash, 8);
517 if (memcmp(session_key.data, user_session_key,
518 sizeof(user_session_key)) != 0) {
519 printf("NT Session Key does not match expectations!\n");
520 printf("user_session_key:\n");
521 dump_data(1, user_session_key, 16);
522 printf("expected:\n");
523 dump_data(1, session_key.data, session_key.length);
532 * Test the NTLMv2 and LMv2 responses
535 static BOOL test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
539 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
540 DATA_BLOB lmv2_response = data_blob(NULL, 0);
541 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
542 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
543 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
545 uint8_t lm_session_key[8];
546 uint8_t user_session_key[16];
548 ZERO_STRUCT(lm_session_key);
549 ZERO_STRUCT(user_session_key);
551 /* TODO - test with various domain cases, and without domain */
552 if (!SMBNTLMv2encrypt(samlogon_state->account_name, samlogon_state->account_domain,
553 samlogon_state->password, &samlogon_state->chall,
555 &lmv2_response, &ntlmv2_response,
556 &lmv2_session_key, &ntlmv2_session_key)) {
557 data_blob_free(&names_blob);
560 data_blob_free(&names_blob);
562 nt_status = check_samlogon(samlogon_state,
564 &samlogon_state->chall,
571 data_blob_free(&lmv2_response);
572 data_blob_free(&ntlmv2_response);
575 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
576 return break_which == BREAK_BOTH;
579 if (!NT_STATUS_IS_OK(nt_status)) {
583 switch (break_which) {
585 if (memcmp(lmv2_session_key.data, user_session_key,
586 sizeof(user_session_key)) != 0) {
587 printf("USER (LMv2) Session Key does not match expectations!\n");
588 printf("user_session_key:\n");
589 dump_data(1, user_session_key, 16);
590 printf("expected:\n");
591 dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
594 if (memcmp(lmv2_session_key.data, lm_session_key,
595 sizeof(lm_session_key)) != 0) {
596 printf("LM (LMv2) Session Key does not match expectations!\n");
597 printf("lm_session_key:\n");
598 dump_data(1, lm_session_key, 8);
599 printf("expected:\n");
600 dump_data(1, lmv2_session_key.data, 8);
605 if (memcmp(ntlmv2_session_key.data, user_session_key,
606 sizeof(user_session_key)) != 0) {
607 printf("USER (NTLMv2) Session Key does not match expectations!\n");
608 printf("user_session_key:\n");
609 dump_data(1, user_session_key, 16);
610 printf("expected:\n");
611 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
614 if (memcmp(ntlmv2_session_key.data, lm_session_key,
615 sizeof(lm_session_key)) != 0) {
616 printf("LM (NTLMv2) Session Key does not match expectations!\n");
617 printf("lm_session_key:\n");
618 dump_data(1, lm_session_key, 8);
619 printf("expected:\n");
620 dump_data(1, ntlmv2_session_key.data, 8);
629 * Test the NTLMv2 and LMv2 responses
632 static BOOL test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
634 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, error_string);
638 * Test the LMv2 response only
641 static BOOL test_lmv2(struct samlogon_state *samlogon_state, char **error_string)
643 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, error_string);
647 * Test the NTLMv2 response only
650 static BOOL test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
652 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, error_string);
655 static BOOL test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string)
657 return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
660 static BOOL test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string)
662 return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
665 static BOOL test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string)
667 return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
670 static BOOL test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
672 return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
674 static BOOL test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
676 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, error_string);
679 static BOOL test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
681 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, error_string);
684 static BOOL test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string)
686 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, error_string);
690 * Test the NTLM2 response (extra challenge in LM feild)
692 * This test is the same as the 'break LM' test, but checks that the
693 * server implements NTLM2 session security in the right place
694 * (NETLOGON is the wrong place).
697 static BOOL test_ntlm2(struct samlogon_state *samlogon_state, char **error_string)
701 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
702 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
708 uint8_t user_session_key[16];
709 uint8_t expected_user_session_key[16];
710 uint8_t session_nonce_hash[16];
711 uint8_t client_chall[8];
713 struct MD5Context md5_session_nonce_ctx;
714 HMACMD5Context hmac_ctx;
716 ZERO_STRUCT(user_session_key);
718 generate_random_buffer(client_chall, 8);
720 MD5Init(&md5_session_nonce_ctx);
721 MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
722 MD5Update(&md5_session_nonce_ctx, client_chall, 8);
723 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
725 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
726 E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
727 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
730 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
732 memcpy(lm_response.data, session_nonce_hash, 8);
733 memset(lm_response.data + 8, 0, 16);
735 hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
736 hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
737 hmac_md5_update(client_chall, 8, &hmac_ctx);
738 hmac_md5_final(expected_user_session_key, &hmac_ctx);
740 nt_status = check_samlogon(samlogon_state,
742 &samlogon_state->chall,
749 if (!NT_STATUS_IS_OK(nt_status)) {
753 if (memcmp(lm_hash, lm_key,
754 sizeof(lm_key)) != 0) {
755 printf("LM Key does not match expectations!\n");
757 dump_data(1, lm_key, 8);
758 printf("expected:\n");
759 dump_data(1, lm_hash, 8);
762 if (memcmp(nt_key, user_session_key, 16) != 0) {
763 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
764 printf("user_session_key:\n");
765 dump_data(1, user_session_key, sizeof(user_session_key));
766 printf("expected:\n");
767 dump_data(1, nt_key, sizeof(nt_key));
773 static BOOL test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
776 DATA_BLOB nt_response = data_blob(NULL, 0);
777 DATA_BLOB lm_response = data_blob(NULL, 0);
782 uint8_t user_session_key[16];
784 static const uint8_t zeros[8];
785 DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
787 ZERO_STRUCT(user_session_key);
789 if ((push_ucs2_talloc(samlogon_state->mem_ctx, &unicodepw,
790 samlogon_state->password)) == -1) {
791 DEBUG(0, ("push_ucs2_allocate failed!\n"));
795 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
797 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
799 if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX,
802 (void**)&dospw)) == -1) {
803 DEBUG(0, ("convert_string_talloc failed!\n"));
807 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
809 nt_status = check_samlogon(samlogon_state,
818 if (!NT_STATUS_IS_OK(nt_status)) {
819 return break_which == BREAK_NT;
825 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state,
826 char **error_string) {
827 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
830 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
831 char **error_string) {
832 return test_plaintext(samlogon_state, BREAK_LM, error_string);
835 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
836 char **error_string) {
837 return test_plaintext(samlogon_state, BREAK_NT, error_string);
840 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state,
841 char **error_string) {
842 return test_plaintext(samlogon_state, NO_LM, error_string);
845 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state,
846 char **error_string) {
847 return test_plaintext(samlogon_state, NO_NT, error_string);
861 - plaintext tests (in challenge-response fields)
863 check we get the correct session key in each case
864 check what values we get for the LM session key
868 static const struct ntlm_tests {
869 BOOL (*fn)(struct samlogon_state *, char **);
873 {test_lm, "LM", False},
874 {test_lm_ntlm, "LM and NTLM", False},
875 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
876 {test_ntlm, "NTLM", False},
877 {test_ntlm_in_lm, "NTLM in LM", False},
878 {test_ntlm_in_both, "NTLM in both", False},
879 {test_ntlmv2, "NTLMv2", False},
880 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
881 {test_lmv2, "LMv2", False},
882 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
883 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
884 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
885 {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
886 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
887 {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
888 {test_plaintext_none_broken, "Plaintext", True},
889 {test_plaintext_lm_broken, "Plaintext LM broken", True},
890 {test_plaintext_nt_broken, "Plaintext NT broken", True},
891 {test_plaintext_nt_only, "Plaintext NT only", True},
892 {test_plaintext_lm_only, "Plaintext LM only", True},
897 try a netlogon SamLogon
899 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
900 struct creds_CredentialState *creds)
904 int validation_levels[] = {2,3,6};
905 int logon_levels[] = { 2, 6 };
906 int function_levels[] = {
907 DCERPC_NETR_LOGONSAMLOGON,
908 DCERPC_NETR_LOGONSAMLOGONEX,
909 DCERPC_NETR_LOGONSAMLOGONWITHFLAGS };
910 struct samlogon_state samlogon_state;
912 printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
914 samlogon_state.mem_ctx = mem_ctx;
915 samlogon_state.account_name = lp_parm_string(-1, "torture", "username");
916 samlogon_state.account_domain = lp_parm_string(-1, "torture", "userdomain");
917 samlogon_state.password = lp_parm_string(-1, "torture", "password");
918 samlogon_state.p = p;
919 samlogon_state.creds = creds;
921 samlogon_state.chall = data_blob_talloc(mem_ctx, NULL, 8);
923 generate_random_buffer(samlogon_state.chall.data, 8);
924 samlogon_state.r_flags.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
925 samlogon_state.r_flags.in.workstation = TEST_MACHINE_NAME;
926 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
927 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
928 samlogon_state.r_flags.in.flags = 0;
930 samlogon_state.r_ex.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
931 samlogon_state.r_ex.in.workstation = TEST_MACHINE_NAME;
932 samlogon_state.r_ex.in.flags = 0;
934 samlogon_state.r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
935 samlogon_state.r.in.workstation = TEST_MACHINE_NAME;
936 samlogon_state.r.in.credential = &samlogon_state.auth;
937 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
939 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
940 for (i=0; test_table[i].fn; i++) {
941 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
942 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
943 char *error_string = NULL;
944 samlogon_state.function_level = function_levels[f];
945 samlogon_state.r.in.validation_level = validation_levels[v];
946 samlogon_state.r.in.logon_level = logon_levels[l];
947 samlogon_state.r_ex.in.validation_level = validation_levels[v];
948 samlogon_state.r_ex.in.logon_level = logon_levels[l];
949 samlogon_state.r_flags.in.validation_level = validation_levels[v];
950 samlogon_state.r_flags.in.logon_level = logon_levels[l];
951 if (!test_table[i].fn(&samlogon_state, &error_string)) {
952 printf("Testing '%s' at validation level %d, logon level %d, function %d: \n",
953 test_table[i].name, validation_levels[v],
954 logon_levels[l], function_levels[f]);
956 if (test_table[i].expect_fail) {
957 printf(" failed (expected, test incomplete): %s\n", error_string);
959 printf(" failed: %s\n", error_string);
962 SAFE_FREE(error_string);
973 test an ADS style interactive domain logon
975 static BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
976 struct creds_CredentialState *creds)
979 struct netr_LogonSamLogonWithFlags r;
980 struct netr_Authenticator a, ra;
981 struct netr_PasswordInfo pinfo;
982 const char *plain_pass;
988 creds_client_authenticator(creds, &a);
990 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
991 r.in.workstation = TEST_MACHINE_NAME;
992 r.in.credential = &a;
993 r.in.return_authenticator = &ra;
994 r.in.logon_level = 5;
995 r.in.logon.password = &pinfo;
996 r.in.validation_level = 6;
999 pinfo.identity_info.domain_name.string = lp_parm_string(-1, "torture", "userdomain");
1000 pinfo.identity_info.parameter_control = 0;
1001 pinfo.identity_info.logon_id_low = 0;
1002 pinfo.identity_info.logon_id_high = 0;
1003 pinfo.identity_info.account_name.string = lp_parm_string(-1, "torture", "username");
1004 pinfo.identity_info.workstation.string = TEST_MACHINE_NAME;
1006 plain_pass = lp_parm_string(-1, "torture", "password");
1008 E_deshash(plain_pass, pinfo.lmpassword.hash);
1009 E_md4hash(plain_pass, pinfo.ntpassword.hash);
1011 if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1012 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1013 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1015 creds_des_encrypt(creds, &pinfo.lmpassword);
1016 creds_des_encrypt(creds, &pinfo.ntpassword);
1019 printf("Testing netr_LogonSamLogonWithFlags (Interactive Logon)\n");
1021 status = dcerpc_netr_LogonSamLogonWithFlags(p, mem_ctx, &r);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 printf("netr_LogonSamLogonWithFlags - %s\n", nt_errstr(status));
1027 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
1028 printf("Credential chaining failed\n");
1037 BOOL torture_rpc_samlogon(void)
1040 struct dcerpc_pipe *p;
1041 struct dcerpc_binding b;
1042 TALLOC_CTX *mem_ctx;
1045 const char *machine_password;
1046 const char *binding = lp_parm_string(-1, "torture", "binding");
1049 unsigned int credential_flags[] = {
1050 NETLOGON_NEG_AUTH2_FLAGS,
1051 NETLOGON_NEG_ARCFOUR,
1052 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1053 NETLOGON_NEG_AUTH2_ADS_FLAGS,
1054 0 /* yes, this is a valid flag, causes the use of DES */
1057 struct creds_CredentialState *creds;
1059 mem_ctx = talloc_init("torture_rpc_netlogon");
1061 join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST,
1064 printf("Failed to join as BDC\n");
1068 status = dcerpc_parse_binding(mem_ctx, binding, &b);
1069 if (!NT_STATUS_IS_OK(status)) {
1070 printf("Bad binding string %s\n", binding);
1075 /* We have to use schannel, otherwise the SamLogonEx fails
1076 * with INTERNAL_ERROR */
1078 b.flags &= ~DCERPC_AUTH_OPTIONS;
1079 b.flags |= DCERPC_SCHANNEL_BDC | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1081 status = dcerpc_pipe_connect_b(&p, &b,
1082 DCERPC_NETLOGON_UUID,
1083 DCERPC_NETLOGON_VERSION,
1088 if (!NT_STATUS_IS_OK(status)) {
1093 status = dcerpc_schannel_creds(p->security_state.generic_state, mem_ctx, &creds);
1094 if (!NT_STATUS_IS_OK(status)) {
1099 if (!test_InteractiveLogon(p, mem_ctx, creds)) {
1103 if (!test_SamLogon(p, mem_ctx, creds)) {
1107 for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1109 if (!test_SetupCredentials2(p, mem_ctx, credential_flags[i],
1110 TEST_MACHINE_NAME, machine_password, creds)) {
1114 if (!test_InteractiveLogon(p, mem_ctx, creds)) {
1118 if (!test_SamLogon(p, mem_ctx, creds)) {
1123 for (i=0; i < 32; i++) {
1124 if (!test_SetupCredentials2(p, mem_ctx, 1 << i,
1125 TEST_MACHINE_NAME, machine_password, creds)) {
1129 if (!test_InteractiveLogon(p, mem_ctx, creds)) {
1133 if (!test_SamLogon(p, mem_ctx, creds)) {
1139 talloc_destroy(mem_ctx);
1141 torture_rpc_close(p);
1143 torture_leave_domain(join_ctx);