OpenVAS Scanner  7.0.0~git
nasl_crypto.c
Go to the documentation of this file.
1 /* Based on work Copyright (C) 2002 - 2004 Tenable Network Security
2  *
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
24 /* MODIFICATION: added definitions for implementing NTLMSSP features */
25 
26 #include "nasl_crypto.h"
27 
28 #include "exec.h"
29 #include "hmacmd5.h"
30 #include "nasl_debug.h"
31 #include "nasl_func.h"
32 #include "nasl_global_ctxt.h"
33 #include "nasl_lex_ctxt.h"
34 #include "nasl_tree.h"
35 #include "nasl_var.h"
36 #include "ntlmssp.h"
37 #include "smb.h"
38 #include "smb_crypt.h"
39 #include "smb_signing.h"
40 
41 #include <assert.h>
42 #include <ctype.h>
43 #include <gcrypt.h>
44 #include <glib.h>
45 #include <gvm/base/logging.h>
46 #include <stdlib.h>
47 
48 #ifndef uchar
49 #define uchar unsigned char
50 #endif
51 
52 #ifndef uint8
53 #define uint8 uint8_t
54 #endif
55 
56 #ifndef uint32
57 #define uint32 uint32_t
58 #endif
59 
60 #undef G_LOG_DOMAIN
61 
64 #define G_LOG_DOMAIN "lib nasl"
65 
66 /*-------------------[ Std. HASH ]-------------------------------------*/
67 static tree_cell *
68 nasl_gcrypt_hash (lex_ctxt *lexic, int algorithm, void *data, size_t datalen,
69  void *key, size_t keylen)
70 {
71  gcry_md_hd_t hd;
72  gcry_error_t err;
73  tree_cell *retc;
74  int dlen = gcry_md_get_algo_dlen (algorithm);
75 
76  if (data == NULL)
77  return NULL;
78 
79  err = gcry_md_open (&hd, algorithm, key ? GCRY_MD_FLAG_HMAC : 0);
80  if (err)
81  {
82  nasl_perror (lexic, "nasl_gcrypt_hash(): gcry_md_open failed: %s/%s\n",
83  gcry_strsource (err), gcry_strerror (err));
84  return NULL;
85  }
86 
87  if (key)
88  {
89  err = gcry_md_setkey (hd, key, keylen);
90  if (err)
91  {
92  nasl_perror (lexic,
93  "nasl_gcrypt_hash():"
94  " gcry_md_setkey failed: %s/%s\n",
95  gcry_strsource (err), gcry_strerror (err));
96  return NULL;
97  }
98  }
99 
100  gcry_md_write (hd, data, datalen);
101 
102  retc = alloc_typed_cell (CONST_DATA);
103  retc->x.str_val = g_memdup (gcry_md_read (hd, algorithm), dlen + 1);
104  retc->size = dlen;
105 
106  gcry_md_close (hd);
107 
108  return retc;
109 }
110 
111 static tree_cell *
112 nasl_hash (lex_ctxt *lexic, int algorithm)
113 {
114  char *data = get_str_var_by_num (lexic, 0);
115  int len = get_var_size_by_num (lexic, 0);
116 
117  return nasl_gcrypt_hash (lexic, algorithm, data, len, NULL, 0);
118 }
119 
120 tree_cell *
122 {
123  return nasl_hash (lexic, GCRY_MD_MD2);
124 }
125 
126 tree_cell *
128 {
129  return nasl_hash (lexic, GCRY_MD_MD4);
130 }
131 
132 tree_cell *
134 {
135  return nasl_hash (lexic, GCRY_MD_MD5);
136 }
137 
138 tree_cell *
140 {
141  return nasl_hash (lexic, GCRY_MD_SHA1);
142 }
143 
144 tree_cell *
146 {
147  return nasl_hash (lexic, GCRY_MD_SHA256);
148 }
149 
150 tree_cell *
152 {
153  return nasl_hash (lexic, GCRY_MD_RMD160);
154 }
155 
156 static tree_cell *
157 nasl_cipher (int algorithm, void *data, size_t dlen, void *key, size_t klen)
158 {
159  gcry_cipher_hd_t hd;
160  gcry_error_t error;
161  tree_cell *retc;
162  char *result;
163 
164  if ((error = gcry_cipher_open (&hd, algorithm, GCRY_CIPHER_MODE_ECB, 0)))
165  {
166  g_message ("gcry_cipher_open: %s", gcry_strerror (error));
167  return NULL;
168  }
169  if ((error = gcry_cipher_setkey (hd, key, klen)))
170  {
171  g_message ("gcry_cipher_setkey: %s", gcry_strerror (error));
172  return NULL;
173  }
174  result = g_malloc0 (dlen);
175  if ((error = gcry_cipher_encrypt (hd, result, dlen, data, dlen)))
176  {
177  g_message ("gcry_cipher_encrypt: %s", gcry_strerror (error));
178  return NULL;
179  }
180  gcry_cipher_close (hd);
181 
182  retc = alloc_typed_cell (CONST_DATA);
183  retc->x.str_val = result;
184  retc->size = dlen;
185  return retc;
186 }
187 
188 tree_cell *
190 {
191  char *data, *key;
192  size_t dlen, klen;
193 
194  data = get_str_var_by_num (lexic, 0);
195  dlen = get_var_size_by_num (lexic, 0);
196  key = get_str_var_by_num (lexic, 1);
197  klen = get_var_size_by_num (lexic, 1);
198  return nasl_cipher (GCRY_CIPHER_DES, data, dlen, key, klen);
199 }
200 
201 /*-------------------[ HMAC ]-------------------------------------*/
202 
203 static tree_cell *
204 nasl_hmac (lex_ctxt *lexic, int algorithm)
205 {
206  char *data = get_str_var_by_name (lexic, "data");
207  char *key = get_str_var_by_name (lexic, "key");
208  int data_len = get_var_size_by_name (lexic, "data");
209  int key_len = get_var_size_by_name (lexic, "key");
210 
211  return nasl_gcrypt_hash (lexic, algorithm, data, data_len, key, key_len);
212 }
213 
214 tree_cell *
216 {
217  return nasl_hmac (lexic, GCRY_MD_MD2);
218 }
219 
220 tree_cell *
222 {
223  return nasl_hmac (lexic, GCRY_MD_MD5);
224 }
225 
226 tree_cell *
228 {
229  return nasl_hmac (lexic, GCRY_MD_SHA1);
230 }
231 
232 tree_cell *
234 {
235  return nasl_hmac (lexic, GCRY_MD_SHA384);
236 }
237 
238 tree_cell *
240 {
241  return nasl_hmac (lexic, GCRY_MD_RMD160);
242 }
243 
244 /*-------------------[ Windows ]-------------------------------------*/
245 tree_cell *
247 {
248  char *mac_key = (char *) get_str_var_by_name (lexic, "key");
249  uint8_t *buf = (uint8_t *) get_str_var_by_name (lexic, "buf");
250  int buflen = get_int_var_by_name (lexic, "buflen", -1);
251  int seq_num = get_int_var_by_name (lexic, "seq_number", -1);
252  if (mac_key == NULL || buf == NULL || buflen == -1 || seq_num <= -1)
253  {
254  nasl_perror (lexic, "Syntax : get_signature(key:<k>, buf:<b>, "
255  "buflen:<bl>, seq_number:<s>)\n");
256  return NULL;
257  }
258  uint8_t calc_md5_mac[16];
259  simple_packet_signature_ntlmssp ((uint8_t *) mac_key, buf, seq_num,
260  calc_md5_mac);
261  memcpy (buf + 18, calc_md5_mac, 8);
262  char *ret = g_malloc0 (buflen);
263  memcpy (ret, buf, buflen);
264  tree_cell *retc;
265  retc = alloc_typed_cell (CONST_DATA);
266  retc->size = buflen;
267  retc->x.str_val = (char *) ret;
268  return retc;
269 }
270 
271 static void *
272 hmac_md5_for_prf (const void *key, int keylen, const void *buf, int buflen)
273 {
274  void *signature = g_malloc0 (16);
275  gsize signlen = 16;
276  GHmac *hmac;
277 
278  hmac = g_hmac_new (G_CHECKSUM_MD5, key, keylen);
279  g_hmac_update (hmac, buf, buflen);
280  g_hmac_get_digest (hmac, signature, &signlen);
281  g_hmac_unref (hmac);
282  return signature;
283 }
284 
285 static void *
286 hmac_sha1 (const void *key, int keylen, const void *buf, int buflen)
287 {
288  void *signature = g_malloc0 (20);
289  gsize signlen = 20;
290  GHmac *hmac;
291 
292  hmac = g_hmac_new (G_CHECKSUM_SHA1, key, keylen);
293  g_hmac_update (hmac, buf, buflen);
294  g_hmac_get_digest (hmac, signature, &signlen);
295  g_hmac_unref (hmac);
296  return signature;
297 }
298 
299 static void *
300 hmac_sha256 (const void *key, int keylen, const void *buf, int buflen)
301 {
302  void *signature = g_malloc0 (32);
303  gsize signlen = 32;
304  GHmac *hmac;
305 
306  hmac = g_hmac_new (G_CHECKSUM_SHA256, key, keylen);
307  g_hmac_update (hmac, buf, buflen);
308  g_hmac_get_digest (hmac, signature, &signlen);
309  g_hmac_unref (hmac);
310  return signature;
311 }
312 
313 static void *
314 hmac_sha384 (const void *key, int keylen, const void *buf, int buflen)
315 {
316  gcry_md_hd_t hd;
317  gcry_error_t err;
318  void *ret;
319 
320  if (!buf || buflen <= 0)
321  return NULL;
322 
323  err = gcry_md_open (&hd, GCRY_MD_SHA384, key ? GCRY_MD_FLAG_HMAC : 0);
324  if (err)
325  {
326  g_message ("nasl_gcrypt_hash(): gcry_md_open failed: %s/%s",
327  gcry_strsource (err), gcry_strerror (err));
328  return NULL;
329  }
330 
331  if (key)
332  {
333  err = gcry_md_setkey (hd, key, keylen);
334  if (err)
335  {
336  g_message ("nasl_gcrypt_hash(): gcry_md_setkey failed: %s/%s",
337  gcry_strsource (err), gcry_strerror (err));
338  return NULL;
339  }
340  }
341 
342  gcry_md_write (hd, buf, buflen);
343  ret = g_memdup (gcry_md_read (hd, 0), 48);
344  gcry_md_close (hd);
345  return ret;
346 }
347 
348 tree_cell *
350 {
351  void *key, *data, *signature;
352  int keylen, datalen;
353  tree_cell *retc;
354 
355  key = get_str_var_by_name (lexic, "key");
356  data = get_str_var_by_name (lexic, "data");
357  datalen = get_var_size_by_name (lexic, "data");
358  keylen = get_var_size_by_name (lexic, "key");
359  if (!key || !data || keylen <= 0 || datalen <= 0)
360  {
361  nasl_perror (lexic, "Syntax : hmac_sha256(data:<b>, key:<k>)\n");
362  return NULL;
363  }
364  signature = hmac_sha256 (key, keylen, data, datalen);
365 
366  retc = alloc_typed_cell (CONST_DATA);
367  retc->size = 32;
368  retc->x.str_val = (char *) signature;
369  return retc;
370 }
371 
372 /* @brief PRF function from RFC 2246 chapter 5.
373  *
374  * @param hmac 0 for SHA256, 1 for SHA384, 2 for MD5, 3 for SHA1.
375  *
376  * */
377 static void *
378 tls_prf (const void *secret, size_t secret_len, const void *seed,
379  size_t seed_len, const void *label, size_t outlen, int hmac)
380 {
381  char *result = NULL;
382  size_t pos = 0, lslen, hmac_size;
383  void *Ai;
384  void *lseed;
385  void *(*hmac_func) (const void *, int, const void *, int);
386 
387  if (hmac == 0)
388  {
389  hmac_size = 32;
390  hmac_func = hmac_sha256;
391  }
392  else if (hmac == 1)
393  {
394  hmac_size = 48;
395  hmac_func = hmac_sha384;
396  }
397  else if (hmac == 2)
398  {
399  hmac_size = 16;
400  hmac_func = hmac_md5_for_prf;
401  }
402  else
403  {
404  hmac_size = 20;
405  hmac_func = hmac_sha1;
406  }
407 
408  /*
409  * lseed = label + seed
410  * A0 = lseed (new seed)
411  * Ai = HMAC(secret, A(i - 1))
412  */
413  lslen = strlen (label) + seed_len;
414  lseed = g_malloc0 (lslen);
415  memcpy (lseed, label, strlen (label));
416  memcpy ((char *) lseed + strlen (label), seed, seed_len);
417 
418  Ai = hmac_func (secret, secret_len, lseed, lslen);
419  if (!Ai)
420  return NULL;
421 
422  result = g_malloc0 (outlen);
423  while (pos < outlen)
424  {
425  void *tmp, *tmp2;
426  size_t clen;
427 
428  /* HMAC_hash(secret, Ai + lseed) */
429  tmp = g_malloc0 (hmac_size + lslen);
430  memcpy (tmp, Ai, hmac_size);
431  memcpy ((char *) tmp + hmac_size, lseed, lslen);
432  tmp2 = hmac_func (secret, secret_len, tmp, hmac_size + lslen);
433  g_free (tmp);
434  /* concat to result */
435  clen = outlen - pos;
436  if (clen > hmac_size)
437  clen = hmac_size;
438  memcpy (result + pos, tmp2, clen);
439  pos += clen;
440  g_free (tmp2);
441 
442  /* A(i+1) */
443  tmp = hmac_func (secret, secret_len, Ai, hmac_size);
444  g_free (Ai);
445  Ai = tmp;
446  }
447 
448  g_free (Ai);
449  g_free (lseed);
450  return result;
451 }
452 
453 /* @brief PRF function from RFC 4346 chapter 5. TLS v1.1
454  *
455  * Legacy function in which P_MD5 and PSHA1 are combined.
456  *
457  * Legacy function has been replaced with prf_sha256 and prf_sha348
458  * in TLS v1.2, as it can be read in chapter 1.2
459  * */
460 static void *
461 tls1_prf (const void *secret, size_t secret_len, const void *seed,
462  size_t seed_len, const void *label, size_t outlen)
463 {
464  void *result, *secret1 = NULL, *secret2 = NULL;
465  unsigned int half_slen, odd = 0, i;
466  char *resultmd5 = NULL, *resultsha1 = NULL, *aux_res = NULL;
467 
468  if (secret_len % 2 == 0)
469  half_slen = secret_len / 2;
470  else
471  {
472  half_slen = (secret_len + 1) / 2;
473  odd = 1;
474  }
475 
476  secret1 = g_malloc0 (half_slen);
477  memcpy (secret1, secret, half_slen);
478  resultmd5 = tls_prf (secret1, half_slen, seed, seed_len, label, outlen, 2);
479  if (!resultmd5)
480  {
481  g_free (secret1);
482  return NULL;
483  }
484 
485  secret2 = g_malloc0 (half_slen);
486  memcpy (secret2, (char *) secret + (half_slen - odd), half_slen);
487  resultsha1 = tls_prf (secret2, half_slen, seed, seed_len, label, outlen, 3);
488  if (!resultsha1)
489  {
490  g_free (resultmd5);
491  g_free (secret1);
492  g_free (secret2);
493  return NULL;
494  }
495 
496  aux_res = g_malloc0 (outlen);
497  for (i = 0; i < outlen; i++)
498  aux_res[i] = resultmd5[i] ^ resultsha1[i];
499 
500  result = g_malloc (outlen);
501  memcpy (result, aux_res, outlen);
502 
503  g_free (resultmd5);
504  g_free (resultsha1);
505  g_free (secret1);
506  g_free (secret2);
507  g_free (aux_res);
508 
509  return result;
510 }
511 
512 static tree_cell *
513 nasl_prf (lex_ctxt *lexic, int hmac)
514 {
515  void *secret, *seed, *label, *result;
516  int secret_len, seed_len, label_len, outlen;
517  tree_cell *retc;
518 
519  secret = get_str_var_by_name (lexic, "secret");
520  seed = get_str_var_by_name (lexic, "seed");
521  label = get_str_var_by_name (lexic, "label");
522  outlen = get_int_var_by_name (lexic, "outlen", -1);
523  seed_len = get_var_size_by_name (lexic, "seed");
524  secret_len = get_var_size_by_name (lexic, "secret");
525  label_len = get_var_size_by_name (lexic, "label");
526  if (!secret || !seed || secret_len <= 0 || seed_len <= 0 || !label
527  || label_len <= 0 || outlen <= 0)
528  {
529  nasl_perror (lexic, "Syntax : prf(secret, seed, label, outlen)\n");
530  return NULL;
531  }
532  if (hmac != 2)
533  result = tls_prf (secret, secret_len, seed, seed_len, label, outlen, hmac);
534  else
535  result = tls1_prf (secret, secret_len, seed, seed_len, label, outlen);
536 
537  if (!result)
538  return NULL;
539 
540  retc = alloc_typed_cell (CONST_DATA);
541  retc->size = outlen;
542  retc->x.str_val = (char *) result;
543  return retc;
544 }
545 
546 tree_cell *
548 {
549  return nasl_prf (lexic, 0);
550 }
551 
552 tree_cell *
554 {
555  return nasl_prf (lexic, 1);
556 }
557 
558 tree_cell *
560 {
561  return nasl_prf (lexic, 2);
562 }
563 
564 tree_cell *
566 {
567  return nasl_hmac (lexic, GCRY_MD_SHA512);
568 }
569 
570 tree_cell *
572 {
573  void *key, *buf, *signature, *ret;
574  int keylen, buflen;
575  tree_cell *retc;
576 
577  key = get_str_var_by_name (lexic, "key");
578  buf = get_str_var_by_name (lexic, "buf");
579  keylen = get_var_size_by_name (lexic, "key");
580  buflen = get_var_size_by_name (lexic, "buf");
581  if (!key || !buf || keylen <= 0)
582  {
583  nasl_perror (lexic, "Syntax : get_smb2_signature(buf:<b>, key:<k>)");
584  return NULL;
585  }
586  if (buflen < 64)
587  {
588  nasl_perror (lexic, "get_smb2_sign: Buffer length < 64");
589  return NULL;
590  }
591 
592  /* Zero the SMB2 signature field, then calculate signature */
593  memset ((char *) buf + 48, 0, 16);
594  signature = hmac_sha256 (key, keylen, buf, buflen);
595 
596  /* Return the header with signature included. */
597  ret = g_malloc0 (buflen);
598  memcpy (ret, buf, buflen);
599  memcpy ((char *) ret + 48, signature, 16);
600  g_free (signature);
601  retc = alloc_typed_cell (CONST_DATA);
602  retc->size = buflen;
603  retc->x.str_val = (char *) ret;
604  return retc;
605 }
606 
607 tree_cell *
609 {
610  char *cryptkey = (char *) get_str_var_by_name (lexic, "cryptkey");
611  char *user = (char *) get_str_var_by_name (lexic, "user");
612  char *domain = (char *) get_str_var_by_name (lexic, "domain");
613  unsigned char *ntlmv2_hash =
614  (unsigned char *) get_str_var_by_name (lexic, "ntlmv2_hash");
615  char *address_list = get_str_var_by_name (lexic, "address_list");
616  int address_list_len = get_int_var_by_name (lexic, "address_list_len", -1);
617 
618  if (cryptkey == NULL || user == NULL || domain == NULL || ntlmv2_hash == NULL
619  || address_list == NULL || address_list_len < 0)
620  {
621  nasl_perror (
622  lexic, "Syntax : ntlmv2_response(cryptkey:<c>, user:<u>, domain:<d>, "
623  "ntlmv2_hash:<n>, address_list:<a>, address_list_len:<len>)\n");
624  return NULL;
625  }
626  uint8_t lm_response[24];
627  uint8_t nt_response[16 + 28 + address_list_len];
628  uint8_t session_key[16];
629  bzero (lm_response, sizeof (lm_response));
630  bzero (nt_response, sizeof (nt_response));
631  bzero (session_key, sizeof (session_key));
632 
633  ntlmssp_genauth_ntlmv2 (user, domain, address_list, address_list_len,
634  cryptkey, lm_response, nt_response, session_key,
635  ntlmv2_hash);
636  tree_cell *retc;
637  int lm_response_len = 24;
638  int nt_response_len = 16 + 28 + address_list_len;
639  int len = lm_response_len + nt_response_len + sizeof (session_key);
640  char *ret = g_malloc0 (len);
641  memcpy (ret, lm_response, lm_response_len);
642  memcpy (ret + lm_response_len, session_key, sizeof (session_key));
643  memcpy (ret + lm_response_len + sizeof (session_key), nt_response,
644  nt_response_len);
645  retc = alloc_typed_cell (CONST_DATA);
646  retc->size = len;
647  retc->x.str_val = ret;
648  return retc;
649 }
650 
651 tree_cell *
653 {
654  char *cryptkey = (char *) get_str_var_by_name (lexic, "cryptkey");
655  char *password = get_str_var_by_name (lexic, "password");
656  uint8_t pass_len = get_var_size_by_name (lexic, "password");
657  void *nt_hash = get_str_var_by_name (lexic, "nt_hash");
658  int hash_len = get_var_size_by_name (lexic, "nt_hash");
659 
660  if (!cryptkey || !password || !nt_hash || hash_len < 16)
661  {
662  nasl_perror (lexic, "Syntax : ntlm2_response(cryptkey:<c>, password:<p>, "
663  "nt_hash:<n[16]>)\n");
664  return NULL;
665  }
666 
667  uint8_t lm_response[24];
668  uint8_t nt_response[24];
669  uint8_t session_key[16];
670 
671  tree_cell *retc;
672  ntlmssp_genauth_ntlm2 (password, pass_len, lm_response, nt_response,
673  session_key, cryptkey, nt_hash);
674  int len = sizeof (lm_response) + sizeof (nt_response) + sizeof (session_key);
675  char *ret = g_malloc0 (len);
676  memcpy (ret, lm_response, sizeof (lm_response));
677  memcpy (ret + sizeof (lm_response), nt_response, sizeof (nt_response));
678  memcpy (ret + sizeof (lm_response) + sizeof (nt_response), session_key,
679  sizeof (session_key));
680  retc = alloc_typed_cell (CONST_DATA);
681  retc->size = len;
682  retc->x.str_val = ret;
683  return retc;
684 }
685 
686 tree_cell *
688 {
689  char *cryptkey = (char *) get_str_var_by_name (lexic, "cryptkey");
690  char *password = get_str_var_by_name (lexic, "password");
691  uint8_t pass_len = (uint8_t) get_var_size_by_name (lexic, "password");
692  void *nt_hash = get_str_var_by_name (lexic, "nt_hash");
693  int hash_len = get_var_size_by_name (lexic, "nt_hash");
694  int neg_flags = get_int_var_by_name (lexic, "neg_flags", -1);
695 
696  if (!cryptkey || !password || !nt_hash || hash_len < 16 || neg_flags < 0)
697  {
698  nasl_perror (lexic, "Syntax : ntlm_response(cryptkey:<c>, password:<p>, "
699  "nt_hash:<n[16]>, neg_flags:<nf>)\n");
700  return NULL;
701  }
702 
703  uint8_t lm_response[24];
704  uint8_t nt_response[24];
705  uint8_t session_key[16];
706 
707  tree_cell *retc;
708 
709  ntlmssp_genauth_ntlm (password, pass_len, lm_response, nt_response,
710  session_key, cryptkey, nt_hash, neg_flags);
711 
712  int len = sizeof (lm_response) + sizeof (nt_response) + sizeof (session_key);
713  char *ret = g_malloc0 (len);
714  memcpy (ret, lm_response, sizeof (lm_response));
715  memcpy (ret + sizeof (lm_response), nt_response, sizeof (nt_response));
716  memcpy (ret + sizeof (lm_response) + sizeof (nt_response), session_key,
717  sizeof (session_key));
718  retc = alloc_typed_cell (CONST_DATA);
719  retc->size = len;
720  retc->x.str_val = ret;
721  return retc;
722 }
723 
724 tree_cell *
726 {
727  char *cryptkey = (char *) get_str_var_by_name (lexic, "cryptkey");
728  uint8_t *session_key = (uint8_t *) get_str_var_by_name (lexic, "session_key");
729  unsigned char *nt_hash =
730  (unsigned char *) get_str_var_by_name (lexic, "nt_hash");
731 
732  if (cryptkey == NULL || session_key == NULL || nt_hash == NULL)
733  {
734  nasl_perror (
735  lexic,
736  "Syntax : key_exchange(cryptkey:<c>, session_key:<s>, nt_hash:<n> )\n");
737  return NULL;
738  }
739  uint8_t new_sess_key[16];
740  tree_cell *retc;
741  uint8_t *encrypted_session_key = NULL;
742  encrypted_session_key = ntlmssp_genauth_keyexchg (
743  session_key, cryptkey, nt_hash, (uint8_t *) &new_sess_key);
744  int len = 16 + 16;
745  char *ret = g_malloc0 (len);
746  memcpy (ret, new_sess_key, 16);
747  memcpy (ret + 16, encrypted_session_key, 16);
748  retc = alloc_typed_cell (CONST_DATA);
749  retc->size = len;
750  retc->x.str_val = ret;
751  return retc;
752 }
753 
754 tree_cell *
756 {
757  const uchar *cryptkey = (uchar *) get_str_var_by_name (lexic, "cryptkey");
758  char *password = get_str_var_by_name (lexic, "passhash");
759  int pass_len = get_var_size_by_name (lexic, "passhash");
760  unsigned char p21[21];
761  tree_cell *retc;
762  uchar *ret;
763 
764  if (cryptkey == NULL || password == NULL)
765  {
766  nasl_perror (lexic, "Syntax : ntlmv1_hash(cryptkey:<c>, passhash:<p>)\n");
767  return NULL;
768  }
769 
770  if (pass_len < 16)
771  pass_len = 16;
772 
773  bzero (p21, sizeof (p21));
774  memcpy (p21, password, pass_len);
775 
776  ret = g_malloc0 (24);
777 
778  E_P24 (p21, cryptkey, ret);
779  retc = alloc_typed_cell (CONST_DATA);
780  retc->size = 24;
781  retc->x.str_val = (char *) ret;
782 
783  return retc;
784 }
785 
786 tree_cell *
788 {
789  char *pass = get_str_var_by_num (lexic, 0);
790  gunichar2 *upass;
791  glong upass_len;
792  tree_cell *ret;
793 
794  if (!pass)
795  {
796  nasl_perror (lexic, "Syntax : nt_owf_gen(<password>)\n");
797  return NULL;
798  }
799  upass = g_utf8_to_utf16 (pass, -1, NULL, &upass_len, NULL);
800  ret = nasl_gcrypt_hash (lexic, GCRY_MD_MD4, upass, upass_len * 2, NULL, 0);
801  g_free (upass);
802  return ret;
803 }
804 
805 tree_cell *
807 {
808  char *pass = get_str_var_by_num (lexic, 0);
809  int pass_len = get_var_size_by_num (lexic, 0);
810  tree_cell *retc;
811  uchar pwd[15];
812  uchar p16[16];
813  unsigned int i;
814 
815  if (pass_len < 0 || pass == NULL)
816  {
817  nasl_perror (lexic, "Syntax : nt_lm_gen(password:<p>)\n");
818  return NULL;
819  }
820 
821  bzero (pwd, sizeof (pwd));
822  strncpy ((char *) pwd, pass, sizeof (pwd) - 1);
823  for (i = 0; i < sizeof (pwd); i++)
824  pwd[i] = toupper (pwd[i]);
825 
826  E_P16 (pwd, p16);
827 
828  retc = alloc_typed_cell (CONST_DATA);
829  retc->size = 16;
830  retc->x.str_val = g_memdup (p16, 16);
831  return retc;
832 }
833 
834 tree_cell *
836 {
837  const uchar *in = (uchar *) get_str_var_by_name (lexic, "in");
838  int in_len = get_var_size_by_name (lexic, "in");
839  char *src;
840  smb_ucs2_t *out, *dst, val;
841  int i;
842  size_t byte_len;
843  tree_cell *retc;
844  if (in_len < 0 || in == NULL)
845  {
846  nasl_perror (lexic, "Syntax : insert_hexzeros(in:<i>)\n");
847  return NULL;
848  }
849 
850  byte_len = sizeof (smb_ucs2_t) * (strlen ((char *) in) + 1);
851  out = g_malloc0 (byte_len);
852  dst = out;
853  src = (char *) in;
854 
855  for (i = 0; i < in_len; i++)
856  {
857  val = *src;
858  *dst = val;
859  dst++;
860  src++;
861  if (val == 0)
862  break;
863  }
864 
865  /* We don't want null termination */
866  byte_len = byte_len - 2;
867 
868  retc = alloc_typed_cell (CONST_DATA);
869  retc->size = byte_len;
870  retc->x.str_val = (char *) out;
871  return retc;
872 }
873 
874 /* Does both the NTLMv2 owfs of a user's password */
875 tree_cell *
877 {
878  const uchar *owf_in = (uchar *) get_str_var_by_name (lexic, "owf");
879  int owf_in_len = get_var_size_by_name (lexic, "owf");
880  char *user_in = get_str_var_by_name (lexic, "login");
881  int user_in_len = get_var_size_by_name (lexic, "login");
882  char *domain_in = get_str_var_by_name (lexic, "domain");
883  int domain_len = get_var_size_by_name (lexic, "domain");
884  char *src_user, *src_domain;
885  smb_ucs2_t *user, *dst_user, val_user;
886  smb_ucs2_t *domain, *dst_domain, val_domain;
887  int i;
888  size_t user_byte_len;
889  size_t domain_byte_len;
890  tree_cell *retc;
891  uchar *kr_buf;
892  HMACMD5Context ctx;
893 
894  if (owf_in_len < 0 || owf_in == NULL || user_in_len < 0 || user_in == NULL
895  || domain_len < 0 || domain_in == NULL)
896  {
897  nasl_perror (lexic,
898  "Syntax : ntv2_owf_gen(owf:<o>, login:<l>, domain:<d>)\n");
899  return NULL;
900  }
901 
902  assert (owf_in_len == 16);
903 
904  user_byte_len = sizeof (smb_ucs2_t) * (strlen (user_in) + 1);
905  user = g_malloc0 (user_byte_len);
906  dst_user = user;
907  src_user = user_in;
908 
909  for (i = 0; i < user_in_len; i++)
910  {
911  val_user = *src_user;
912  *dst_user = val_user;
913  dst_user++;
914  src_user++;
915  if (val_user == 0)
916  break;
917  }
918 
919  domain_byte_len = sizeof (smb_ucs2_t) * (strlen (domain_in) + 1);
920  domain = g_malloc0 (domain_byte_len);
921  dst_domain = domain;
922  src_domain = domain_in;
923 
924  for (i = 0; i < domain_len; i++)
925  {
926  val_domain = *src_domain;
927  *dst_domain = val_domain;
928 
929  dst_domain++;
930  src_domain++;
931  if (val_domain == 0)
932  break;
933  }
934 
935  strupper_w (user);
936  strupper_w (domain);
937 
938  assert (user_byte_len >= 2);
939  assert (domain_byte_len >= 2);
940 
941  /* We don't want null termination */
942  user_byte_len = user_byte_len - 2;
943  domain_byte_len = domain_byte_len - 2;
944 
945  kr_buf = g_malloc0 (16);
946 
947  hmac_md5_init_limK_to_64 (owf_in, 16, &ctx);
948  hmac_md5_update ((const unsigned char *) user, user_byte_len, &ctx);
949  hmac_md5_update ((const unsigned char *) domain, domain_byte_len, &ctx);
950  hmac_md5_final (kr_buf, &ctx);
951 
952  g_free (user);
953  g_free (domain);
954 
955  retc = alloc_typed_cell (CONST_DATA);
956  retc->size = 16;
957  retc->x.str_val = (char *) kr_buf;
958 
959  return retc;
960 }
961 
962 tree_cell *
964 {
965  const uchar *server_chal = (uchar *) get_str_var_by_name (lexic, "cryptkey");
966  int sc_len = get_var_size_by_name (lexic, "cryptkey");
967  const uchar *ntlm_v2_hash = (uchar *) get_str_var_by_name (lexic, "passhash");
968  int hash_len = get_var_size_by_name (lexic, "passhash");
969  int client_chal_length = get_int_var_by_name (lexic, "length", -1);
970  tree_cell *retc;
971  unsigned char ntlmv2_response[16];
972  unsigned char *ntlmv2_client_data = NULL;
973  unsigned char *final_response;
974  int i;
975 
976  if (sc_len < 0 || server_chal == NULL || hash_len < 0 || ntlm_v2_hash == NULL
977  || client_chal_length < 0)
978  {
979  nasl_perror (
980  lexic,
981  "Syntax : ntlmv2_hash(cryptkey:<c>, passhash:<p>, length:<l>)\n");
982  return NULL;
983  }
984 
985  /* NTLMv2 */
986 
987  /* We also get to specify some random data */
988  ntlmv2_client_data = g_malloc0 (client_chal_length);
989  for (i = 0; i < client_chal_length; i++)
990  ntlmv2_client_data[i] = rand () % 256;
991 
992  assert (hash_len == 16);
993  /* Given that data, and the challenge from the server, generate a response */
994  SMBOWFencrypt_ntv2_ntlmssp (ntlm_v2_hash, server_chal, 8, ntlmv2_client_data,
995  client_chal_length, ntlmv2_response);
996 
997  /* put it into nt_response, for the code below to put into the packet */
998  final_response = g_malloc0 (client_chal_length + sizeof (ntlmv2_response));
999  memcpy (final_response, ntlmv2_response, sizeof (ntlmv2_response));
1000  /* after the first 16 bytes is the random data we generated above, so the
1001  * server can verify us with it */
1002  memcpy (final_response + sizeof (ntlmv2_response), ntlmv2_client_data,
1003  client_chal_length);
1004 
1005  g_free (ntlmv2_client_data);
1006 
1007  retc = alloc_typed_cell (CONST_DATA);
1008  retc->size = client_chal_length + sizeof (ntlmv2_response);
1009  retc->x.str_val = (char *) final_response;
1010 
1011  return retc;
1012 }
HMACMD5Context
Definition: hmacmd5.h:41
E_P24
void E_P24(const uchar *p21, const uchar *c8, uchar *p24)
Definition: smb_crypt.c:323
nasl_md5
tree_cell * nasl_md5(lex_ctxt *lexic)
Definition: nasl_crypto.c:133
nasl_ntlmv2_response
tree_cell * nasl_ntlmv2_response(lex_ctxt *lexic)
Definition: nasl_crypto.c:608
uchar
#define uchar
Definition: hmacmd5.h:35
CONST_DATA
@ CONST_DATA
Definition: nasl_tree.h:93
get_var_size_by_name
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1147
nasl_nt_owf_gen
tree_cell * nasl_nt_owf_gen(lex_ctxt *lexic)
Definition: nasl_crypto.c:787
TC::str_val
char * str_val
Definition: nasl_tree.h:112
nasl_ntlmv1_hash
tree_cell * nasl_ntlmv1_hash(lex_ctxt *lexic)
Definition: nasl_crypto.c:755
nasl_hmac_ripemd160
tree_cell * nasl_hmac_ripemd160(lex_ctxt *lexic)
Definition: nasl_crypto.c:239
tls1_prf
static void * tls1_prf(const void *secret, size_t secret_len, const void *seed, size_t seed_len, const void *label, size_t outlen)
Definition: nasl_crypto.c:461
nasl_cipher
static tree_cell * nasl_cipher(int algorithm, void *data, size_t dlen, void *key, size_t klen)
Definition: nasl_crypto.c:157
nasl_ntv2_owf_gen
tree_cell * nasl_ntv2_owf_gen(lex_ctxt *lexic)
Definition: nasl_crypto.c:876
ntlmssp_genauth_keyexchg
uint8_t * ntlmssp_genauth_keyexchg(uint8_t *session_key, char *challenge_data, unsigned char *nt_hash, uint8_t *new_sess_key)
Definition: ntlmssp.c:99
nasl_hmac_md2
tree_cell * nasl_hmac_md2(lex_ctxt *lexic)
Definition: nasl_crypto.c:215
TC::x
union TC::@2 x
E_P16
void E_P16(uchar *p14, uchar *p16)
Definition: smb_crypt.c:315
hmac_md5_for_prf
static void * hmac_md5_for_prf(const void *key, int keylen, const void *buf, int buflen)
Definition: nasl_crypto.c:272
get_str_var_by_name
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1127
nasl_insert_hexzeros
tree_cell * nasl_insert_hexzeros(lex_ctxt *lexic)
Definition: nasl_crypto.c:835
hmac_sha384
static void * hmac_sha384(const void *key, int keylen, const void *buf, int buflen)
Definition: nasl_crypto.c:314
exec.h
nasl_keyexchg
tree_cell * nasl_keyexchg(lex_ctxt *lexic)
Definition: nasl_crypto.c:725
smb.h
Unix SMB/CIFS implementation.
nasl_hmac_sha384
tree_cell * nasl_hmac_sha384(lex_ctxt *lexic)
Definition: nasl_crypto.c:233
hmac_md5_init_limK_to_64
void hmac_md5_init_limK_to_64(const uchar *key, int key_len, HMACMD5Context *ctx)
The microsoft version of hmac_md5 initialisation.
Definition: hmacmd5.c:37
nasl_hmac_sha1
tree_cell * nasl_hmac_sha1(lex_ctxt *lexic)
Definition: nasl_crypto.c:227
nasl_prf_sha384
tree_cell * nasl_prf_sha384(lex_ctxt *lexic)
Definition: nasl_crypto.c:553
nasl_debug.h
nasl_hmac_sha512
tree_cell * nasl_hmac_sha512(lex_ctxt *lexic)
Definition: nasl_crypto.c:565
nasl_md4
tree_cell * nasl_md4(lex_ctxt *lexic)
Definition: nasl_crypto.c:127
nasl_perror
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
smb_signing.h
Unix SMB/CIFS implementation. SMB Signing Code.
tls_prf
static void * tls_prf(const void *secret, size_t secret_len, const void *seed, size_t seed_len, const void *label, size_t outlen, int hmac)
Definition: nasl_crypto.c:378
nasl_cipher_des
tree_cell * nasl_cipher_des(lex_ctxt *lexic)
Definition: nasl_crypto.c:189
SMBOWFencrypt_ntv2_ntlmssp
void SMBOWFencrypt_ntv2_ntlmssp(const uchar kr[16], const uint8_t *srv_chal, int srv_chal_len, const uint8_t *cli_chal, int cli_chal_len, uchar resp_buf[16])
nasl_md2
tree_cell * nasl_md2(lex_ctxt *lexic)
Definition: nasl_crypto.c:121
TC::size
int size
Definition: nasl_tree.h:109
nasl_hash
static tree_cell * nasl_hash(lex_ctxt *lexic, int algorithm)
Definition: nasl_crypto.c:112
nasl_lex_ctxt.h
strupper_w
int strupper_w(smb_ucs2_t *s)
Definition: smb_crypt2.c:48
nasl_lm_owf_gen
tree_cell * nasl_lm_owf_gen(lex_ctxt *lexic)
Definition: nasl_crypto.c:806
hmac_md5_update
void hmac_md5_update(const uchar *text, int text_len, HMACMD5Context *ctx)
Update hmac_md5 "inner" buffer.
Definition: hmacmd5.c:68
get_int_var_by_name
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1113
ntlmssp.h
Functions to support Authentication(type3 message) for NTLMSSP (NTLMv2, NTLM2, NTLM,...
smb_ucs2_t
uint16 smb_ucs2_t
Definition: hmacmd5.h:65
nasl_func.h
get_str_var_by_num
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1120
nasl_prf_sha256
tree_cell * nasl_prf_sha256(lex_ctxt *lexic)
Definition: nasl_crypto.c:547
TC
Definition: nasl_tree.h:104
nasl_ntlm_response
tree_cell * nasl_ntlm_response(lex_ctxt *lexic)
Definition: nasl_crypto.c:687
struct_lex_ctxt
Definition: nasl_lex_ctxt.h:33
ntlmssp_genauth_ntlm2
void ntlmssp_genauth_ntlm2(char *password, uint8_t pass_len, uint8_t *lm_response, uint8_t *nt_response, uint8_t *session_key, char *challenge_data, unsigned char *nt_hash)
Definition: ntlmssp.c:44
nasl_gcrypt_hash
static tree_cell * nasl_gcrypt_hash(lex_ctxt *lexic, int algorithm, void *data, size_t datalen, void *key, size_t keylen)
Definition: nasl_crypto.c:68
nasl_var.h
hmacmd5.h
Unix SMB/CIFS implementation. HMAC MD5 code for use in NTLMv2.
ntlmssp_genauth_ntlm
void ntlmssp_genauth_ntlm(char *password, uint8_t pass_len, uint8_t *lm_response, uint8_t *nt_response, uint8_t *session_key, char *challenge_data, unsigned char *nt_hash, int neg_flags)
Definition: ntlmssp.c:75
nasl_ripemd160
tree_cell * nasl_ripemd160(lex_ctxt *lexic)
Definition: nasl_crypto.c:151
nasl_sha1
tree_cell * nasl_sha1(lex_ctxt *lexic)
Definition: nasl_crypto.c:139
nasl_hmac_md5
tree_cell * nasl_hmac_md5(lex_ctxt *lexic)
Definition: nasl_crypto.c:221
nasl_global_ctxt.h
hmac_md5_final
void hmac_md5_final(uchar *digest, HMACMD5Context *ctx)
Finish off hmac_md5 "inner" buffer and generate outer one.
Definition: hmacmd5.c:77
nasl_tls1_prf
tree_cell * nasl_tls1_prf(lex_ctxt *lexic)
Definition: nasl_crypto.c:559
get_var_size_by_num
int get_var_size_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1154
val
const char * val
Definition: nasl_init.c:378
simple_packet_signature_ntlmssp
void simple_packet_signature_ntlmssp(uint8_t *mac_key, const uchar *buf, uint32 seq_number, unsigned char *calc_md5_mac)
Definition: smb_signing.c:36
nasl_hmac_sha256
tree_cell * nasl_hmac_sha256(lex_ctxt *lexic)
Definition: nasl_crypto.c:349
nasl_get_smb2_sign
tree_cell * nasl_get_smb2_sign(lex_ctxt *lexic)
Definition: nasl_crypto.c:571
hmac_sha256
static void * hmac_sha256(const void *key, int keylen, const void *buf, int buflen)
Definition: nasl_crypto.c:300
nasl_prf
static tree_cell * nasl_prf(lex_ctxt *lexic, int hmac)
Definition: nasl_crypto.c:513
nasl_hmac
static tree_cell * nasl_hmac(lex_ctxt *lexic, int algorithm)
Definition: nasl_crypto.c:204
hmac_sha1
static void * hmac_sha1(const void *key, int keylen, const void *buf, int buflen)
Definition: nasl_crypto.c:286
nasl_ntlmv2_hash
tree_cell * nasl_ntlmv2_hash(lex_ctxt *lexic)
Definition: nasl_crypto.c:963
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:40
nasl_ntlm2_response
tree_cell * nasl_ntlm2_response(lex_ctxt *lexic)
Definition: nasl_crypto.c:652
smb_crypt.h
Unix SMB/Netbios implementation. Version 1.9.
nasl_tree.h
nasl_get_sign
tree_cell * nasl_get_sign(lex_ctxt *lexic)
Definition: nasl_crypto.c:246
nasl_crypto.h
nasl_sha256
tree_cell * nasl_sha256(lex_ctxt *lexic)
Definition: nasl_crypto.c:145
ntlmssp_genauth_ntlmv2
void ntlmssp_genauth_ntlmv2(char *user, char *domain, char *address_list, int address_list_len, char *challenge_data, uint8_t *lm_response, uint8_t *nt_response, uint8_t *session_key, unsigned char *ntlmv2_hash)
Definition: ntlmssp.c:33