1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
/*
Unix SMB/CIFS implementation.
User credentials handling
Copyright (C) Andrew Tridgell 2001
Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
Copyright (C) Stefan Metzmacher 2005
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 "librpc/gen_ndr/samr.h" /* for struct samrPassword */
#include "../lib/crypto/crypto.h"
#include "libcli/auth/libcli_auth.h"
#include "auth/credentials/credentials.h"
#include "auth/credentials/credentials_internal.h"
#include "lib/crypto/gnutls_helpers.h"
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_AUTH
_PUBLIC_ NTSTATUS cli_credentials_get_ntlm_response(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
int *flags,
DATA_BLOB challenge,
const NTTIME *server_timestamp,
DATA_BLOB target_info,
DATA_BLOB *_lm_response, DATA_BLOB *_nt_response,
DATA_BLOB *_lm_session_key, DATA_BLOB *_session_key)
{
TALLOC_CTX *frame = talloc_stackframe();
const char *user = NULL;
const char *domain = NULL;
DATA_BLOB lm_response = data_blob_null;
DATA_BLOB nt_response = data_blob_null;
DATA_BLOB lm_session_key = data_blob_null;
DATA_BLOB session_key = data_blob_null;
const struct samr_Password *nt_hash = NULL;
int rc;
if (cred->kerberos_state == CRED_USE_KERBEROS_REQUIRED) {
TALLOC_FREE(frame);
return NT_STATUS_INVALID_PARAMETER_MIX;
}
/* We may already have an NTLM response we prepared earlier.
* This is used for NTLM pass-though authentication */
if (cred->nt_response.data || cred->lm_response.data) {
if (cred->nt_response.length != 0) {
nt_response = data_blob_dup_talloc(frame,
cred->nt_response);
if (nt_response.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
}
if (cred->nt_session_key.length != 0) {
session_key = data_blob_dup_talloc(frame,
cred->nt_session_key);
if (session_key.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
}
if (cred->lm_response.length != 0) {
lm_response = data_blob_dup_talloc(frame,
cred->lm_response);
if (lm_response.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
}
if (cred->lm_session_key.length != 0) {
lm_session_key = data_blob_dup_talloc(frame,
cred->lm_session_key);
if (lm_session_key.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
}
if (cred->lm_response.data == NULL) {
*flags = *flags & ~CLI_CRED_LANMAN_AUTH;
}
goto done;
}
nt_hash = cli_credentials_get_nt_hash(cred, frame);
cli_credentials_get_ntlm_username_domain(cred, frame, &user, &domain);
if (user == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
if (domain == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
/* If we are sending a username@realm login (see function
* above), then we will not send LM, it will not be
* accepted */
if (cred->principal_obtained > cred->username_obtained) {
*flags = *flags & ~CLI_CRED_LANMAN_AUTH;
}
/* Likewise if we are a machine account (avoid protocol downgrade attacks) */
if (cred->machine_account) {
*flags = *flags & ~CLI_CRED_LANMAN_AUTH;
}
if (!nt_hash) {
/* do nothing - blobs are zero length */
/* session key is all zeros */
session_key = data_blob_talloc_zero(frame, 16);
if (session_key.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
lm_session_key = data_blob_talloc_zero(frame, 16);
if (lm_session_key.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
/* not doing NTLM2 without a password */
*flags &= ~CLI_CRED_NTLM2;
} else if (*flags & CLI_CRED_NTLMv2_AUTH) {
if (!target_info.length) {
/* be lazy, match win2k - we can't do NTLMv2 without it */
DEBUG(1, ("Server did not provide 'target information', required for NTLMv2\n"));
TALLOC_FREE(frame);
return NT_STATUS_INVALID_PARAMETER;
}
/* TODO: if the remote server is standalone, then we should replace 'domain'
with the server name as supplied above */
if (!SMBNTLMv2encrypt_hash(frame,
user,
domain,
nt_hash->hash, &challenge,
server_timestamp, &target_info,
&lm_response, &nt_response,
NULL, &session_key)) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
/* LM Key is incompatible... */
*flags &= ~CLI_CRED_LANMAN_AUTH;
if (lm_response.length != 0) {
/*
* We should not expose the lm key.
*/
memset(lm_response.data, 0, lm_response.length);
}
} else if (*flags & CLI_CRED_NTLM2) {
uint8_t session_nonce[16];
uint8_t session_nonce_hash[16];
uint8_t user_session_key[16];
lm_response = data_blob_talloc_zero(frame, 24);
if (lm_response.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
generate_random_buffer(lm_response.data, 8);
memcpy(session_nonce, challenge.data, 8);
memcpy(&session_nonce[8], lm_response.data, 8);
rc = gnutls_hash_fast(GNUTLS_DIG_MD5,
session_nonce,
sizeof(session_nonce),
session_nonce_hash);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_NTLM_BLOCKED);
}
DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
DEBUG(5, ("challenge is: \n"));
dump_data(5, session_nonce_hash, 8);
nt_response = data_blob_talloc_zero(frame, 24);
if (nt_response.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
rc = SMBOWFencrypt(nt_hash->hash,
session_nonce_hash,
nt_response.data);
if (rc != 0) {
TALLOC_FREE(frame);
return gnutls_error_to_ntstatus(rc, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER);
}
ZERO_ARRAY(session_nonce_hash);
session_key = data_blob_talloc_zero(frame, 16);
if (session_key.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
SMBsesskeygen_ntv1(nt_hash->hash, user_session_key);
rc = gnutls_hmac_fast(GNUTLS_MAC_MD5,
user_session_key,
sizeof(user_session_key),
session_nonce,
sizeof(session_nonce),
session_key.data);
if (rc < 0) {
return gnutls_error_to_ntstatus(rc, NT_STATUS_NTLM_BLOCKED);
}
ZERO_ARRAY(user_session_key);
dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
/* LM Key is incompatible... */
*flags &= ~CLI_CRED_LANMAN_AUTH;
} else {
const char *password = cli_credentials_get_password(cred);
uint8_t lm_hash[16];
bool do_lm = false;
nt_response = data_blob_talloc_zero(frame, 24);
if (nt_response.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
rc = SMBOWFencrypt(nt_hash->hash, challenge.data,
nt_response.data);
if (rc != 0) {
TALLOC_FREE(frame);
return gnutls_error_to_ntstatus(rc, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER);
}
session_key = data_blob_talloc_zero(frame, 16);
if (session_key.data == NULL) {
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
SMBsesskeygen_ntv1(nt_hash->hash, session_key.data);
dump_data_pw("NT session key:\n", session_key.data, session_key.length);
/* lanman auth is insecure, it may be disabled.
We may also not have a password */
if (password != NULL) {
do_lm = E_deshash(password, lm_hash);
}
if (*flags & CLI_CRED_LANMAN_AUTH && do_lm) {
lm_response = data_blob_talloc_zero(frame, 24);
if (lm_response.data == NULL) {
ZERO_STRUCT(lm_hash);
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
rc = SMBencrypt_hash(lm_hash,
challenge.data,
lm_response.data);
if (rc != 0) {
ZERO_STRUCT(lm_hash);
TALLOC_FREE(frame);
return gnutls_error_to_ntstatus(rc, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER);
}
} else {
/* just copy the nt_response */
lm_response = data_blob_dup_talloc(frame, nt_response);
if (lm_response.data == NULL) {
ZERO_STRUCT(lm_hash);
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
}
if (do_lm) {
lm_session_key = data_blob_talloc_zero(frame, 16);
if (lm_session_key.data == NULL) {
ZERO_STRUCT(lm_hash);
TALLOC_FREE(frame);
return NT_STATUS_NO_MEMORY;
}
memcpy(lm_session_key.data, lm_hash, 8);
if (!(*flags & CLI_CRED_NTLM_AUTH)) {
memcpy(session_key.data, lm_session_key.data, 16);
}
ZERO_STRUCT(lm_hash);
}
}
done:
if (_lm_response != NULL) {
talloc_steal(mem_ctx, lm_response.data);
*_lm_response = lm_response;
} else {
data_blob_clear(&lm_response);
}
if (_nt_response != NULL) {
talloc_steal(mem_ctx, nt_response.data);
*_nt_response = nt_response;
} else {
data_blob_clear(&nt_response);
}
if (_lm_session_key != NULL) {
talloc_steal(mem_ctx, lm_session_key.data);
*_lm_session_key = lm_session_key;
} else {
data_blob_clear(&lm_session_key);
}
if (_session_key != NULL) {
talloc_steal(mem_ctx, session_key.data);
*_session_key = session_key;
} else {
data_blob_clear(&session_key);
}
TALLOC_FREE(frame);
return NT_STATUS_OK;
}
/*
* Set a utf16 password on the credentials context, including an indication
* of 'how' the password was obtained
*
* This is required because the nt_hash is calculated over the raw utf16 blob,
* which might not be completely valid utf16, which means the conversion
* from CH_UTF16MUNGED to CH_UTF8 might lose information.
*/
_PUBLIC_ bool cli_credentials_set_utf16_password(struct cli_credentials *cred,
const DATA_BLOB *password_utf16,
enum credentials_obtained obtained)
{
cred->password_will_be_nt_hash = false;
if (password_utf16 == NULL) {
return cli_credentials_set_password(cred, NULL, obtained);
}
if (obtained >= cred->password_obtained) {
struct samr_Password *nt_hash = NULL;
char *password_talloc = NULL;
size_t password_len = 0;
bool ok;
nt_hash = talloc(cred, struct samr_Password);
if (nt_hash == NULL) {
return false;
}
ok = convert_string_talloc(cred,
CH_UTF16MUNGED, CH_UTF8,
password_utf16->data,
password_utf16->length,
&password_talloc,
&password_len);
if (!ok) {
TALLOC_FREE(nt_hash);
return false;
}
ok = cli_credentials_set_password(cred, password_talloc, obtained);
TALLOC_FREE(password_talloc);
if (!ok) {
TALLOC_FREE(nt_hash);
return false;
}
mdfour(nt_hash->hash, password_utf16->data, password_utf16->length);
cred->nt_hash = nt_hash;
return true;
}
return false;
}
/*
* Set a old utf16 password on the credentials context.
*
* This is required because the nt_hash is calculated over the raw utf16 blob,
* which might not be completely valid utf16, which means the conversion
* from CH_UTF16MUNGED to CH_UTF8 might lose information.
*/
_PUBLIC_ bool cli_credentials_set_old_utf16_password(struct cli_credentials *cred,
const DATA_BLOB *password_utf16)
{
struct samr_Password *nt_hash = NULL;
char *password_talloc = NULL;
size_t password_len = 0;
bool ok;
if (password_utf16 == NULL) {
return cli_credentials_set_old_password(cred, NULL, CRED_SPECIFIED);
}
nt_hash = talloc(cred, struct samr_Password);
if (nt_hash == NULL) {
return false;
}
ok = convert_string_talloc(cred,
CH_UTF16MUNGED, CH_UTF8,
password_utf16->data,
password_utf16->length,
&password_talloc,
&password_len);
if (!ok) {
TALLOC_FREE(nt_hash);
return false;
}
ok = cli_credentials_set_old_password(cred, password_talloc, CRED_SPECIFIED);
TALLOC_FREE(password_talloc);
if (!ok) {
TALLOC_FREE(nt_hash);
return false;
}
mdfour(nt_hash->hash, password_utf16->data, password_utf16->length);
cred->old_nt_hash = nt_hash;
return true;
}
_PUBLIC_ void cli_credentials_set_password_will_be_nt_hash(struct cli_credentials *cred,
bool val)
{
/*
* We set this here and the next cli_credentials_set_password()
* that resets the password or password callback
* will pick this up.
*
* cli_credentials_set_nt_hash() and
* cli_credentials_set_utf16_password() will reset this
* to false.
*/
cred->password_will_be_nt_hash = val;
}
_PUBLIC_ bool cli_credentials_is_password_nt_hash(struct cli_credentials *cred)
{
return cred->password_will_be_nt_hash;
}
_PUBLIC_ bool cli_credentials_set_nt_hash(struct cli_credentials *cred,
const struct samr_Password *nt_hash,
enum credentials_obtained obtained)
{
cred->password_will_be_nt_hash = false;
if (obtained >= cred->password_obtained) {
cli_credentials_set_password(cred, NULL, obtained);
if (nt_hash) {
cred->nt_hash = talloc(cred, struct samr_Password);
if (cred->nt_hash == NULL) {
return false;
}
*cred->nt_hash = *nt_hash;
} else {
cred->nt_hash = NULL;
}
return true;
}
return false;
}
_PUBLIC_ bool cli_credentials_set_old_nt_hash(struct cli_credentials *cred,
const struct samr_Password *nt_hash)
{
cli_credentials_set_old_password(cred, NULL, CRED_SPECIFIED);
if (nt_hash) {
cred->old_nt_hash = talloc(cred, struct samr_Password);
if (cred->old_nt_hash == NULL) {
return false;
}
*cred->old_nt_hash = *nt_hash;
} else {
cred->old_nt_hash = NULL;
}
return true;
}
_PUBLIC_ bool cli_credentials_set_ntlm_response(struct cli_credentials *cred,
const DATA_BLOB *lm_response,
const DATA_BLOB *lm_session_key,
const DATA_BLOB *nt_response,
const DATA_BLOB *nt_session_key,
enum credentials_obtained obtained)
{
if (obtained >= cred->password_obtained) {
cli_credentials_set_password(cred, NULL, obtained);
data_blob_clear_free(&cred->lm_response);
data_blob_clear_free(&cred->lm_session_key);
data_blob_clear_free(&cred->nt_response);
data_blob_clear_free(&cred->nt_session_key);
if (lm_response != NULL && lm_response->length != 0) {
cred->lm_response = data_blob_talloc(cred,
lm_response->data,
lm_response->length);
if (cred->lm_response.data == NULL) {
return false;
}
}
if (lm_session_key != NULL && lm_session_key->length != 0) {
cred->lm_session_key = data_blob_talloc(cred,
lm_session_key->data,
lm_session_key->length);
if (cred->lm_session_key.data == NULL) {
return false;
}
}
if (nt_response != NULL && nt_response->length != 0) {
cred->nt_response = data_blob_talloc(cred,
nt_response->data,
nt_response->length);
if (cred->nt_response.data == NULL) {
return false;
}
}
if (nt_session_key != NULL && nt_session_key->length != 0) {
cred->nt_session_key = data_blob_talloc(cred,
nt_session_key->data,
nt_session_key->length);
if (cred->nt_session_key.data == NULL) {
return false;
}
}
return true;
}
return false;
}
|