smb_ntlm.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //---------------------------------------------------------------------------
  2. // __________________ _________ _____ _____ .__ ._.
  3. // \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. // | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. // | | \| ` \/ / Y \ / | | \ ___/ \|
  6. // |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. // \/ \/ \/ \/ )/ \/ \/ \/
  8. //
  9. // This file is part of libdsm. Copyright © 2014 VideoLabs SAS
  10. //
  11. // Author: Julien 'Lta' BALLET <contact@lta.io>
  12. //
  13. // This program is free software. It comes without any warranty, to the extent
  14. // permitted by applicable law. You can redistribute it and/or modify it under
  15. // the terms of the Do What The Fuck You Want To Public License, Version 2, as
  16. // published by Sam Hocevar. See the COPYING file for more details.
  17. //----------------------------------------------------------------------------
  18. #ifndef __BDSM_SMB_NTLM_H_
  19. #define __BDSM_SMB_NTLM_H_
  20. #include "bdsm/smb_defs.h"
  21. #define SMB_LM2_BLOB_SIZE 8
  22. #define SMB_NTLM_HASH_SIZE 16
  23. typedef uint8_t smb_ntlmh[SMB_NTLM_HASH_SIZE];
  24. typedef struct
  25. {
  26. uint32_t header;
  27. uint32_t reserved;
  28. uint64_t timestamp;
  29. uint64_t challenge;
  30. uint32_t unknown;
  31. uint8_t target[];
  32. } __attribute__((packed)) smb_ntlm_blob;
  33. #define SMB_NTLMSSP_CMD_NEGO 0x01
  34. #define SMB_NTLMSSP_CMD_AUTH 0x03
  35. #define _NTLMSSP_COMMON \
  36. char id[8]; \
  37. uint32_t type;
  38. #define _NTLMSSP_FIELD(FIELD) \
  39. uint16_t FIELD ## _len; \
  40. uint16_t FIELD ## _maxlen; \
  41. uint32_t FIELD ## _offset;
  42. typedef struct
  43. {
  44. _NTLMSSP_COMMON
  45. uint32_t flags;
  46. _NTLMSSP_FIELD(domain)
  47. _NTLMSSP_FIELD(host)
  48. uint8_t names[];
  49. } __attribute__((packed)) smb_ntlmssp_nego;
  50. typedef struct
  51. {
  52. _NTLMSSP_COMMON
  53. _NTLMSSP_FIELD(name)
  54. uint32_t flags;
  55. uint64_t challenge;
  56. uint64_t reserved;
  57. _NTLMSSP_FIELD(tgt) // Target Info
  58. uint8_t data[];
  59. } __attribute__((packed)) smb_ntlmssp_challenge;
  60. typedef struct
  61. {
  62. _NTLMSSP_COMMON
  63. _NTLMSSP_FIELD(lm)
  64. _NTLMSSP_FIELD(ntlm)
  65. _NTLMSSP_FIELD(domain)
  66. _NTLMSSP_FIELD(user)
  67. _NTLMSSP_FIELD(host)
  68. _NTLMSSP_FIELD(session_key)
  69. uint32_t flags;
  70. uint8_t data[];
  71. } __attribute__((packed)) smb_ntlmssp_auth;
  72. uint64_t smb_ntlm_generate_challenge();
  73. void smb_ntlm_generate_xkey(smb_ntlmh *cli_session_key);
  74. void smb_ntlm_hash(const char *password, smb_ntlmh *hash);
  75. void smb_ntlm2_hash(const char *username, const char *password,
  76. const char *destination, smb_ntlmh *hash);
  77. // Precompute the blob that will be HMAC'ed to produce NTLM2 Response
  78. // You have to free() the blob after usage
  79. size_t smb_ntlm_make_blob(smb_ntlm_blob **blob, uint64_t ts,
  80. uint64_t user_challenge, void *tgt,
  81. size_t tgt_sz);
  82. // Returned response is blob_size + 16 long. You'll have to free it
  83. uint8_t *smb_ntlm2_response(smb_ntlmh *hash_v2, uint64_t srv_challenge,
  84. uint8_t *blob, size_t blob_size);
  85. // Returned response is 24 bytes long. You'll have to free it.
  86. uint8_t *smb_lm2_response(smb_ntlmh *hash_v2, uint64_t srv_challenge,
  87. uint64_t user_challenge);
  88. // You have to allocate session key
  89. void smb_ntlm2_session_key(smb_ntlmh *hash_v2, void *ntlm2,
  90. smb_ntlmh *xkey, smb_ntlmh *enc_xkey);
  91. void smb_ntlmssp_negotiate(const char *host, const char *domain,
  92. void **token, size_t *token_sz);
  93. void smb_ntlmssp_response(uint64_t srv_challenge, uint64_t srv_ts,
  94. const char *host, const char *domain,
  95. const char *user, const char *password,
  96. void *tgt, size_t tgt_sz,
  97. void **token, size_t *token_sz);
  98. #endif