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;
76 struct netr_SamBaseInfo *base;
78 uint16 validation_level;
80 samlogon_state->r.in.logon.network = &ninfo;
81 samlogon_state->r_ex.in.logon.network = &ninfo;
82 samlogon_state->r_flags.in.logon.network = &ninfo;
84 ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
85 ninfo.identity_info.parameter_control = 0;
86 ninfo.identity_info.logon_id_low = 0;
87 ninfo.identity_info.logon_id_high = 0;
88 ninfo.identity_info.account_name.string = samlogon_state->account_name;
89 ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
91 memcpy(ninfo.challenge, chall->data, 8);
93 switch (break_which) {
97 if (lm_response && lm_response->data) {
98 lm_response->data[0]++;
102 if (nt_response && nt_response->data) {
103 nt_response->data[0]++;
107 if (lm_response && lm_response->data) {
108 lm_response->data[0]++;
110 if (nt_response && nt_response->data) {
111 nt_response->data[0]++;
115 data_blob_free(lm_response);
118 data_blob_free(nt_response);
123 ninfo.nt.data = nt_response->data;
124 ninfo.nt.length = nt_response->length;
126 ninfo.nt.data = NULL;
131 ninfo.lm.data = lm_response->data;
132 ninfo.lm.length = lm_response->length;
134 ninfo.lm.data = NULL;
138 switch (samlogon_state->function_level) {
139 case DCERPC_NETR_LOGONSAMLOGON:
140 ZERO_STRUCT(samlogon_state->auth2);
141 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
143 r->out.return_authenticator = NULL;
144 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
145 if (!r->out.return_authenticator ||
146 !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
147 printf("Credential chaining failed\n");
149 if (!NT_STATUS_IS_OK(status)) {
151 *error_string = strdup(nt_errstr(status));
155 validation_level = r->in.validation_level;
156 switch (validation_level) {
158 base = &r->out.validation.sam2->base;
161 base = &r->out.validation.sam3->base;
164 base = &r->out.validation.sam6->base;
168 case DCERPC_NETR_LOGONSAMLOGONEX:
169 status = dcerpc_netr_LogonSamLogonEx(samlogon_state->p, samlogon_state->mem_ctx, r_ex);
170 if (!NT_STATUS_IS_OK(status)) {
172 *error_string = strdup(nt_errstr(status));
176 validation_level = r_ex->in.validation_level;
177 switch (validation_level) {
179 base = &r_ex->out.validation.sam2->base;
182 base = &r_ex->out.validation.sam3->base;
185 base = &r_ex->out.validation.sam6->base;
189 case DCERPC_NETR_LOGONSAMLOGONWITHFLAGS:
190 ZERO_STRUCT(samlogon_state->auth2);
191 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
193 r_flags->out.return_authenticator = NULL;
194 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
195 if (!r_flags->out.return_authenticator ||
196 !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
197 printf("Credential chaining failed\n");
199 if (!NT_STATUS_IS_OK(status)) {
201 *error_string = strdup(nt_errstr(status));
205 validation_level = r_flags->in.validation_level;
206 switch (validation_level) {
208 base = &r_flags->out.validation.sam2->base;
211 base = &r_flags->out.validation.sam3->base;
214 base = &r_flags->out.validation.sam6->base;
221 if (!NT_STATUS_IS_OK(status)) {
222 /* we cannot check the session key, if the logon failed... */
227 /* find and decyrpt the session keys, return in parameters above */
228 if (validation_level == 6) {
229 /* they aren't encrypted! */
230 if (user_session_key) {
231 memcpy(user_session_key, base->key.key, 16);
234 memcpy(lm_key, base->LMSessKey.key, 8);
236 } else if (samlogon_state->creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
237 static const char zeros[16];
239 if (memcmp(base->key.key, zeros,
240 sizeof(base->key.key)) != 0) {
241 creds_arcfour_crypt(samlogon_state->creds,
243 sizeof(base->key.key));
246 if (user_session_key) {
247 memcpy(user_session_key, base->key.key, 16);
250 if (memcmp(base->LMSessKey.key, zeros,
251 sizeof(base->LMSessKey.key)) != 0) {
252 creds_arcfour_crypt(samlogon_state->creds,
254 sizeof(base->LMSessKey.key));
258 memcpy(lm_key, base->LMSessKey.key, 8);
261 static const char zeros[16];
263 if (user_session_key) {
264 memcpy(user_session_key, base->key.key, 16);
267 if (memcmp(base->LMSessKey.key, zeros,
268 sizeof(base->LMSessKey.key)) != 0) {
269 creds_des_decrypt_LMKey(samlogon_state->creds,
274 memcpy(lm_key, base->LMSessKey.key, 8);
283 * Test the normal 'LM and NTLM' combination
286 static BOOL test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
291 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
292 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
293 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
296 uint8_t user_session_key[16];
301 ZERO_STRUCT(user_session_key);
303 lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
305 ZERO_STRUCT(lm_hash);
307 E_deshash(samlogon_state->password, lm_hash);
310 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
312 E_md4hash(samlogon_state->password, nt_hash);
313 SMBsesskeygen_ntv1(nt_hash, session_key.data);
315 nt_status = check_samlogon(samlogon_state,
317 &samlogon_state->chall,
324 data_blob_free(&lm_response);
326 if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
327 /* for 'long' passwords, the LM password is invalid */
328 if (break_which == NO_NT && !lm_good) {
331 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
334 if (!NT_STATUS_IS_OK(nt_status)) {
338 if (break_which == NO_NT && !lm_good) {
339 printf("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
343 if (memcmp(lm_hash, lm_key,
344 sizeof(lm_key)) != 0) {
345 printf("LM Key does not match expectations!\n");
347 dump_data(1, (const char *)lm_key, 8);
348 printf("expected:\n");
349 dump_data(1, (const char *)lm_hash, 8);
353 switch (break_which) {
356 char lm_key_expected[16];
357 memcpy(lm_key_expected, lm_hash, 8);
358 memset(lm_key_expected+8, '\0', 8);
359 if (memcmp(lm_key_expected, user_session_key,
361 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
362 printf("user_session_key:\n");
363 dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
364 printf("expected:\n");
365 dump_data(1, (const char *)lm_key_expected, sizeof(lm_key_expected));
371 if (memcmp(session_key.data, user_session_key,
372 sizeof(user_session_key)) != 0) {
373 printf("NT Session Key does not match expectations!\n");
374 printf("user_session_key:\n");
375 dump_data(1, (const char *)user_session_key, 16);
376 printf("expected:\n");
377 dump_data(1, (const char *)session_key.data, session_key.length);
385 * Test LM authentication, no NT response supplied
388 static BOOL test_lm(struct samlogon_state *samlogon_state, char **error_string)
391 return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
395 * Test the NTLM response only, no LM.
398 static BOOL test_ntlm(struct samlogon_state *samlogon_state, char **error_string)
400 return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
404 * Test the NTLM response only, but in the LM field.
407 static BOOL test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string)
411 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
415 uint8_t user_session_key[16];
417 ZERO_STRUCT(user_session_key);
419 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
421 E_deshash(samlogon_state->password, lm_hash);
423 nt_status = check_samlogon(samlogon_state,
425 &samlogon_state->chall,
432 if (!NT_STATUS_IS_OK(nt_status)) {
436 if (memcmp(lm_hash, lm_key,
437 sizeof(lm_key)) != 0) {
438 printf("LM Key does not match expectations!\n");
440 dump_data(1, (const char *)lm_key, 8);
441 printf("expected:\n");
442 dump_data(1, (const char *)lm_hash, 8);
445 if (memcmp(lm_hash, user_session_key, 8) != 0) {
446 char lm_key_expected[16];
447 memcpy(lm_key_expected, lm_hash, 8);
448 memset(lm_key_expected+8, '\0', 8);
449 if (memcmp(lm_key_expected, user_session_key,
451 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
452 printf("user_session_key:\n");
453 dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
454 printf("expected:\n");
455 dump_data(1, (const char *)lm_key_expected, sizeof(lm_key_expected));
463 * Test the NTLM response only, but in the both the NT and LM fields.
466 static BOOL test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string)
471 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
472 DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
476 char user_session_key[16];
480 ZERO_STRUCT(user_session_key);
482 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data,
484 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
485 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
488 lm_good = E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
490 ZERO_STRUCT(lm_hash);
493 nt_status = check_samlogon(samlogon_state,
495 &samlogon_state->chall,
502 if (!NT_STATUS_IS_OK(nt_status)) {
506 if (memcmp(lm_hash, lm_key,
507 sizeof(lm_key)) != 0) {
508 printf("LM Key does not match expectations!\n");
510 dump_data(1, lm_key, 8);
511 printf("expected:\n");
512 dump_data(1, lm_hash, 8);
515 if (memcmp(session_key.data, user_session_key,
516 sizeof(user_session_key)) != 0) {
517 printf("NT Session Key does not match expectations!\n");
518 printf("user_session_key:\n");
519 dump_data(1, user_session_key, 16);
520 printf("expected:\n");
521 dump_data(1, (const char *)session_key.data, session_key.length);
530 * Test the NTLMv2 and LMv2 responses
533 static BOOL test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
537 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
538 DATA_BLOB lmv2_response = data_blob(NULL, 0);
539 DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
540 DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
541 DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
543 uint8_t lm_session_key[8];
544 uint8_t user_session_key[16];
546 ZERO_STRUCT(lm_session_key);
547 ZERO_STRUCT(user_session_key);
549 /* TODO - test with various domain cases, and without domain */
550 if (!SMBNTLMv2encrypt(samlogon_state->account_name, samlogon_state->account_domain,
551 samlogon_state->password, &samlogon_state->chall,
553 &lmv2_response, &ntlmv2_response,
554 &lmv2_session_key, &ntlmv2_session_key)) {
555 data_blob_free(&names_blob);
558 data_blob_free(&names_blob);
560 nt_status = check_samlogon(samlogon_state,
562 &samlogon_state->chall,
569 data_blob_free(&lmv2_response);
570 data_blob_free(&ntlmv2_response);
573 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
574 return break_which == BREAK_BOTH;
577 if (!NT_STATUS_IS_OK(nt_status)) {
581 switch (break_which) {
583 if (memcmp(lmv2_session_key.data, user_session_key,
584 sizeof(user_session_key)) != 0) {
585 printf("USER (LMv2) Session Key does not match expectations!\n");
586 printf("user_session_key:\n");
587 dump_data(1, (const char *)user_session_key, 16);
588 printf("expected:\n");
589 dump_data(1, (const char *)lmv2_session_key.data, ntlmv2_session_key.length);
592 if (memcmp(lmv2_session_key.data, lm_session_key,
593 sizeof(lm_session_key)) != 0) {
594 printf("LM (LMv2) Session Key does not match expectations!\n");
595 printf("lm_session_key:\n");
596 dump_data(1, (const char *)lm_session_key, 8);
597 printf("expected:\n");
598 dump_data(1, (const char *)lmv2_session_key.data, 8);
603 if (memcmp(ntlmv2_session_key.data, user_session_key,
604 sizeof(user_session_key)) != 0) {
605 printf("USER (NTLMv2) Session Key does not match expectations!\n");
606 printf("user_session_key:\n");
607 dump_data(1, (const char *)user_session_key, 16);
608 printf("expected:\n");
609 dump_data(1, (const char *)ntlmv2_session_key.data, ntlmv2_session_key.length);
612 if (memcmp(ntlmv2_session_key.data, lm_session_key,
613 sizeof(lm_session_key)) != 0) {
614 printf("LM (NTLMv2) Session Key does not match expectations!\n");
615 printf("lm_session_key:\n");
616 dump_data(1, (const char *)lm_session_key, 8);
617 printf("expected:\n");
618 dump_data(1, (const char *)ntlmv2_session_key.data, 8);
627 * Test the NTLMv2 and LMv2 responses
630 static BOOL test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
632 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, error_string);
636 * Test the LMv2 response only
639 static BOOL test_lmv2(struct samlogon_state *samlogon_state, char **error_string)
641 return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, error_string);
645 * Test the NTLMv2 response only
648 static BOOL test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string)
650 return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, error_string);
653 static BOOL test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string)
655 return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
658 static BOOL test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string)
660 return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
663 static BOOL test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string)
665 return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
668 static BOOL test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string)
670 return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
672 static BOOL test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
674 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, error_string);
677 static BOOL test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string)
679 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, error_string);
682 static BOOL test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string)
684 return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, error_string);
688 * Test the NTLM2 response (extra challenge in LM feild)
690 * This test is the same as the 'break LM' test, but checks that the
691 * server implements NTLM2 session security in the right place
692 * (NETLOGON is the wrong place).
695 static BOOL test_ntlm2(struct samlogon_state *samlogon_state, char **error_string)
699 DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
700 DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
706 uint8_t user_session_key[16];
707 uint8_t expected_user_session_key[16];
708 uint8_t session_nonce_hash[16];
709 uint8_t client_chall[8];
711 struct MD5Context md5_session_nonce_ctx;
712 HMACMD5Context hmac_ctx;
714 ZERO_STRUCT(user_session_key);
716 generate_random_buffer(client_chall, 8);
718 MD5Init(&md5_session_nonce_ctx);
719 MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
720 MD5Update(&md5_session_nonce_ctx, client_chall, 8);
721 MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
723 E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
724 E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
725 SMBsesskeygen_ntv1((const uint8_t *)nt_hash,
728 SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
730 memcpy(lm_response.data, session_nonce_hash, 8);
731 memset(lm_response.data + 8, 0, 16);
733 hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
734 hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
735 hmac_md5_update(client_chall, 8, &hmac_ctx);
736 hmac_md5_final(expected_user_session_key, &hmac_ctx);
738 nt_status = check_samlogon(samlogon_state,
740 &samlogon_state->chall,
747 if (!NT_STATUS_IS_OK(nt_status)) {
751 if (memcmp(lm_hash, lm_key,
752 sizeof(lm_key)) != 0) {
753 printf("LM Key does not match expectations!\n");
755 dump_data(1, (const char *)lm_key, 8);
756 printf("expected:\n");
757 dump_data(1, (const char *)lm_hash, 8);
760 if (memcmp(nt_key, user_session_key, 16) != 0) {
761 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
762 printf("user_session_key:\n");
763 dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
764 printf("expected:\n");
765 dump_data(1, (const char *)nt_key, sizeof(nt_key));
771 static BOOL test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
774 DATA_BLOB nt_response = data_blob(NULL, 0);
775 DATA_BLOB lm_response = data_blob(NULL, 0);
780 uint8_t user_session_key[16];
782 static const uint8_t zeros[8];
783 DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
785 ZERO_STRUCT(user_session_key);
787 if ((push_ucs2_talloc(samlogon_state->mem_ctx, &unicodepw,
788 samlogon_state->password)) == -1) {
789 DEBUG(0, ("push_ucs2_allocate failed!\n"));
793 nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
795 password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
797 if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX,
800 (void**)&dospw)) == -1) {
801 DEBUG(0, ("convert_string_talloc failed!\n"));
805 lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
807 nt_status = check_samlogon(samlogon_state,
816 if (!NT_STATUS_IS_OK(nt_status)) {
817 return break_which == BREAK_NT;
823 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state,
824 char **error_string) {
825 return test_plaintext(samlogon_state, BREAK_NONE, error_string);
828 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state,
829 char **error_string) {
830 return test_plaintext(samlogon_state, BREAK_LM, error_string);
833 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state,
834 char **error_string) {
835 return test_plaintext(samlogon_state, BREAK_NT, error_string);
838 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state,
839 char **error_string) {
840 return test_plaintext(samlogon_state, NO_LM, error_string);
843 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state,
844 char **error_string) {
845 return test_plaintext(samlogon_state, NO_NT, error_string);
859 - plaintext tests (in challenge-response fields)
861 check we get the correct session key in each case
862 check what values we get for the LM session key
866 static const struct ntlm_tests {
867 BOOL (*fn)(struct samlogon_state *, char **);
871 {test_lm, "LM", False},
872 {test_lm_ntlm, "LM and NTLM", False},
873 {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
874 {test_ntlm, "NTLM", False},
875 {test_ntlm_in_lm, "NTLM in LM", False},
876 {test_ntlm_in_both, "NTLM in both", False},
877 {test_ntlmv2, "NTLMv2", False},
878 {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
879 {test_lmv2, "LMv2", False},
880 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
881 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
882 {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
883 {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
884 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
885 {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
886 {test_plaintext_none_broken, "Plaintext", True},
887 {test_plaintext_lm_broken, "Plaintext LM broken", True},
888 {test_plaintext_nt_broken, "Plaintext NT broken", True},
889 {test_plaintext_nt_only, "Plaintext NT only", True},
890 {test_plaintext_lm_only, "Plaintext LM only", True},
895 try a netlogon SamLogon
897 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
898 struct creds_CredentialState *creds)
902 int validation_levels[] = {2,3,6};
903 int logon_levels[] = { 2, 6 };
904 int function_levels[] = {
905 DCERPC_NETR_LOGONSAMLOGON,
906 DCERPC_NETR_LOGONSAMLOGONEX,
907 DCERPC_NETR_LOGONSAMLOGONWITHFLAGS };
908 struct samlogon_state samlogon_state;
910 printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
912 samlogon_state.mem_ctx = mem_ctx;
913 samlogon_state.account_name = lp_parm_string(-1, "torture", "username");
914 samlogon_state.account_domain = lp_parm_string(-1, "torture", "userdomain");
915 samlogon_state.password = lp_parm_string(-1, "torture", "password");
916 samlogon_state.p = p;
917 samlogon_state.creds = creds;
919 samlogon_state.chall = data_blob_talloc(mem_ctx, NULL, 8);
921 generate_random_buffer(samlogon_state.chall.data, 8);
922 samlogon_state.r_flags.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
923 samlogon_state.r_flags.in.workstation = TEST_MACHINE_NAME;
924 samlogon_state.r_flags.in.credential = &samlogon_state.auth;
925 samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
926 samlogon_state.r_flags.in.flags = 0;
928 samlogon_state.r_ex.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
929 samlogon_state.r_ex.in.workstation = TEST_MACHINE_NAME;
930 samlogon_state.r_ex.in.flags = 0;
932 samlogon_state.r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
933 samlogon_state.r.in.workstation = TEST_MACHINE_NAME;
934 samlogon_state.r.in.credential = &samlogon_state.auth;
935 samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
937 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
938 for (i=0; test_table[i].fn; i++) {
939 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
940 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
941 char *error_string = NULL;
942 samlogon_state.function_level = function_levels[f];
943 samlogon_state.r.in.validation_level = validation_levels[v];
944 samlogon_state.r.in.logon_level = logon_levels[l];
945 samlogon_state.r_ex.in.validation_level = validation_levels[v];
946 samlogon_state.r_ex.in.logon_level = logon_levels[l];
947 samlogon_state.r_flags.in.validation_level = validation_levels[v];
948 samlogon_state.r_flags.in.logon_level = logon_levels[l];
949 if (!test_table[i].fn(&samlogon_state, &error_string)) {
950 printf("Testing '%s' at validation level %d, logon level %d, function %d: \n",
951 test_table[i].name, validation_levels[v],
952 logon_levels[l], function_levels[f]);
954 if (test_table[i].expect_fail) {
955 printf(" failed (expected, test incomplete): %s\n", error_string);
957 printf(" failed: %s\n", error_string);
960 SAFE_FREE(error_string);
971 test an ADS style interactive domain logon
973 static BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
974 struct creds_CredentialState *creds)
977 struct netr_LogonSamLogonWithFlags r;
978 struct netr_Authenticator a, ra;
979 struct netr_PasswordInfo pinfo;
980 const char *plain_pass;
986 creds_client_authenticator(creds, &a);
988 r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
989 r.in.workstation = TEST_MACHINE_NAME;
990 r.in.credential = &a;
991 r.in.return_authenticator = &ra;
992 r.in.logon_level = 5;
993 r.in.logon.password = &pinfo;
994 r.in.validation_level = 6;
997 pinfo.identity_info.domain_name.string = lp_parm_string(-1, "torture", "userdomain");
998 pinfo.identity_info.parameter_control = 0;
999 pinfo.identity_info.logon_id_low = 0;
1000 pinfo.identity_info.logon_id_high = 0;
1001 pinfo.identity_info.account_name.string = lp_parm_string(-1, "torture", "username");
1002 pinfo.identity_info.workstation.string = TEST_MACHINE_NAME;
1004 plain_pass = lp_parm_string(-1, "torture", "password");
1006 E_deshash(plain_pass, pinfo.lmpassword.hash);
1007 E_md4hash(plain_pass, pinfo.ntpassword.hash);
1009 if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1010 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1011 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1013 creds_des_encrypt(creds, &pinfo.lmpassword);
1014 creds_des_encrypt(creds, &pinfo.ntpassword);
1017 printf("Testing netr_LogonSamLogonWithFlags (Interactive Logon)\n");
1019 status = dcerpc_netr_LogonSamLogonWithFlags(p, mem_ctx, &r);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 printf("netr_LogonSamLogonWithFlags - %s\n", nt_errstr(status));
1025 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
1026 printf("Credential chaining failed\n");
1035 BOOL torture_rpc_samlogon(void)
1038 struct dcerpc_pipe *p;
1039 struct dcerpc_binding b;
1040 TALLOC_CTX *mem_ctx;
1043 const char *machine_password;
1044 const char *binding = lp_parm_string(-1, "torture", "binding");
1047 unsigned int credential_flags[] = {
1049 NETLOGON_NEG_AUTH2_FLAGS,
1050 NETLOGON_NEG_ARCFOUR,
1051 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1052 NETLOGON_NEG_AUTH2_ADS_FLAGS};
1054 struct creds_CredentialState *creds;
1056 mem_ctx = talloc_init("torture_rpc_netlogon");
1058 join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST,
1061 printf("Failed to join as BDC\n");
1065 status = dcerpc_parse_binding(mem_ctx, binding, &b);
1066 if (!NT_STATUS_IS_OK(status)) {
1067 printf("Bad binding string %s\n", binding);
1072 /* We have to use schannel, otherwise the SamLogonEx fails
1073 * with INTERNAL_ERROR */
1075 b.flags &= ~DCERPC_AUTH_OPTIONS;
1076 b.flags |= DCERPC_SCHANNEL_BDC | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1078 status = dcerpc_pipe_connect_b(&p, &b,
1079 DCERPC_NETLOGON_UUID,
1080 DCERPC_NETLOGON_VERSION,
1085 if (!NT_STATUS_IS_OK(status)) {
1090 status = dcerpc_schannel_creds(p->security_state.generic_state, mem_ctx, &creds);
1091 if (!NT_STATUS_IS_OK(status)) {
1096 if (!test_InteractiveLogon(p, mem_ctx, creds)) {
1100 if (!test_SamLogon(p, mem_ctx, creds)) {
1104 for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1106 if (!test_SetupCredentials2(p, mem_ctx, credential_flags[i],
1107 TEST_MACHINE_NAME, machine_password, creds)) {
1111 if (!test_InteractiveLogon(p, mem_ctx, creds)) {
1115 if (!test_SamLogon(p, mem_ctx, creds)) {
1120 for (i=0; i < 32; i++) {
1121 if (!test_SetupCredentials2(p, mem_ctx, 1 << i,
1122 TEST_MACHINE_NAME, machine_password, creds)) {
1126 if (!test_InteractiveLogon(p, mem_ctx, creds)) {
1130 if (!test_SamLogon(p, mem_ctx, creds)) {
1136 talloc_destroy(mem_ctx);
1138 torture_rpc_close(p);
1140 torture_leave_domain(join_ctx);