smb_ntlm.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_buffer.h"
  21. #include "bdsm/smb_defs.h"
  22. #define SMB_LM2_BLOB_SIZE 8
  23. #define SMB_NTLM_HASH_SIZE 16
  24. typedef uint8_t smb_ntlmh[SMB_NTLM_HASH_SIZE];
  25. typedef struct
  26. {
  27. uint32_t header;
  28. uint32_t reserved;
  29. uint64_t timestamp;
  30. uint64_t challenge;
  31. uint32_t unknown;
  32. uint8_t target[];
  33. } __attribute__((packed)) smb_ntlm_blob;
  34. #define SMB_NTLMSSP_CMD_NEGO 0x01
  35. #define SMB_NTLMSSP_CMD_AUTH 0x03
  36. #define _NTLMSSP_COMMON \
  37. char id[8]; \
  38. uint32_t type;
  39. #define _NTLMSSP_FIELD(FIELD) \
  40. uint16_t FIELD ## _len; \
  41. uint16_t FIELD ## _maxlen; \
  42. uint32_t FIELD ## _offset;
  43. typedef struct
  44. {
  45. _NTLMSSP_COMMON
  46. uint32_t flags;
  47. _NTLMSSP_FIELD(domain)
  48. _NTLMSSP_FIELD(host)
  49. uint8_t names[];
  50. } __attribute__((packed)) smb_ntlmssp_nego;
  51. typedef struct
  52. {
  53. _NTLMSSP_COMMON
  54. _NTLMSSP_FIELD(name)
  55. uint32_t flags;
  56. uint64_t challenge;
  57. uint64_t reserved;
  58. _NTLMSSP_FIELD(tgt) // Target Info
  59. uint8_t data[];
  60. } __attribute__((packed)) smb_ntlmssp_challenge;
  61. typedef struct
  62. {
  63. _NTLMSSP_COMMON
  64. _NTLMSSP_FIELD(lm)
  65. _NTLMSSP_FIELD(ntlm)
  66. _NTLMSSP_FIELD(domain)
  67. _NTLMSSP_FIELD(user)
  68. _NTLMSSP_FIELD(host)
  69. _NTLMSSP_FIELD(session_key)
  70. uint32_t flags;
  71. uint8_t data[];
  72. } __attribute__((packed)) smb_ntlmssp_auth;
  73. uint64_t smb_ntlm_generate_challenge();
  74. void smb_ntlm_generate_xkey(smb_ntlmh *cli_session_key);
  75. void smb_ntlm_hash(const char *password, smb_ntlmh *hash);
  76. void smb_ntlm2_hash(const char *username, const char *password,
  77. const char *destination, smb_ntlmh *hash);
  78. // Precompute the blob that will be HMAC'ed to produce NTLM2 Response
  79. // You have to free() the blob after usage
  80. size_t smb_ntlm_make_blob(smb_ntlm_blob **blob, uint64_t ts,
  81. uint64_t user_challenge, smb_buffer *target);
  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. smb_buffer *blob);
  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. smb_buffer *token);
  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. smb_buffer *target, smb_buffer *token);
  97. #endif