00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _LIBNTLM_H
00023 # define _LIBNTLM_H
00024
00025 #ifdef WIN32
00026 #ifdef LIBNTLM_EXPORTS
00027 #define LIBEXTERN extern __declspec(dllexport)
00028 #else
00029 #define LIBEXTERN extern __declspec(dllimport)
00030 #endif
00031 #else
00032 #define LIBEXTERN extern
00033 #endif
00034
00035 # ifdef __cplusplus
00036 extern "C"
00037 {
00038 # endif
00039
00040
00041 #include <stdio.h>
00042
00043 typedef unsigned short uint16;
00044 typedef unsigned int uint32;
00045 typedef unsigned char uint8;
00046
00047 #define NTLM_VERSION "0.4.2"
00048
00049
00050
00051
00052 #define NTLMSSP_NEGOTIATE_UNICODE 0x00000001
00053 #define NTLM_NEGOTIATE_OEM 0x00000002
00054 #define NTLMSSP_REQUEST_TARGET 0x00000004
00055 #define NTLMSSP_RESERVED_9 0x00000008
00056 #define NTLMSSP_NEGOTIATE_SIGN 0x00000010
00057 #define NTLMSSP_NEGOTIATE_SEAL 0x00000020
00058 #define NTLMSSP_NEGOTIATE_DATAGRAM 0x00000040
00059 #define NTLMSSP_NEGOTIATE_LM_KEY 0x00000080
00060 #define NTLMSSP_RESERVED_8 0x00000100
00061 #define NTLMSSP_NEGOTIATE_NTLM 0x00000200
00062 #define NTLMSSP_NEGOTIATE_NT_ONLY 0x00000400
00063 #define NTLMSSP_RESERVED_7 0x00000800
00064 #define NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED 0x00001000
00065 #define NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED 0x00002000
00066 #define NTLMSSP_RESERVED_6 0x00004000
00067 #define NTLMSSP_NEGOTIATE_ALWAYS_SIGN 0x00008000
00068 #define NTLMSSP_TARGET_TYPE_DOMAIN 0x00010000
00069 #define NTLMSSP_TARGET_TYPE_SERVER 0x00020000
00070 #define NTLMSSP_TARGET_TYPE_SHARE 0x00040000
00071 #define NTLMSSP_NEGOTIATE_NTLM2 0x00080000
00072 #define NTLMSSP_NEGOTIATE_IDENTIFY 0x00100000
00073 #define NTLMSSP_RESERVED_5 0x00200000
00074 #define NTLMSSP_REQUEST_NON_NT_SESSION_KEY 0x00400000
00075 #define NTLMSSP_NEGOTIATE_TARGET_INFO 0x00800000
00076 #define NTLMSSP_RESERVED_4 0x01000000
00077 #define NTLMSSP_NEGOTIATE_VERSION 0x02000000
00078 #define NTLMSSP_RESERVED_3 0x04000000
00079 #define NTLMSSP_RESERVED_2 0x08000000
00080 #define NTLMSSP_RESERVED_1 0x10000000
00081 #define NTLMSSP_NEGOTIATE_128 0x20000000
00082 #define NTLMSSP_NEGOTIATE_KEY_EXCH 0x40000000
00083 #define NTLMSSP_NEGOTIATE_56 0x80000000
00084
00085 typedef struct
00086 {
00087 uint32 NEGOTIATE_UNICODE : 1;
00088 uint32 NEGOTIATE_OEM : 1;
00089 uint32 REQUEST_TARGET : 1;
00090 uint32 RESERVED_9 : 1;
00091 uint32 NEGOTIATE_SIGN : 1;
00092 uint32 NEGOTIATE_SEAL : 1;
00093 uint32 NEGOTIATE_DATAGRAM : 1;
00094 uint32 NEGOTIATE_LM_KEY : 1;
00095 uint32 RESERVED_8 : 1;
00096 uint32 NEGOTIATE_NTLM : 1;
00097 uint32 NEGOTIATE_NT_ONLY : 1;
00098 uint32 RESERVED_7 : 1;
00099 uint32 NEGOTIATE_OEM_DOMAIN_SUPPLIED : 1;
00100 uint32 NEGOTIATE_OEM_WORKSTATION_SUPPLIED : 1;
00101 uint32 RESERVED_6 : 1;
00102 uint32 NEGOTIATE_ALWAYS_SIGN : 1;
00103 uint32 TARGET_TYPE_DOMAIN : 1;
00104 uint32 TARGET_TYPE_SERVER : 1;
00105 uint32 TARGET_TYPE_SHARE : 1;
00106 uint32 NEGOTIATE_NTLM2 : 1;
00107 uint32 NEGOTIATE_IDENTIFY : 1;
00108 uint32 RESERVED_5 : 1;
00109 uint32 REQUEST_NON_NT_SESSION_KEY : 1;
00110 uint32 NEGOTIATE_TARGET_INFO : 1;
00111 uint32 RESERVED_4 : 1;
00112 uint32 NEGOTIATE_VERSION : 1;
00113 uint32 RESERVED_3 : 1;
00114 uint32 RESERVED_2 : 1;
00115 uint32 RESERVED_1 : 1;
00116 uint32 NEGOTIATE_128 : 1;
00117 uint32 NEGOTIATE_KEY_EXCH : 1;
00118 uint32 NEGOTIATE_56 : 1;
00119
00120 } tSmbNtlmFlagBits;
00121
00122 #define NTLM_BUF_SIZE 1024
00123
00124 #define NTLM_VER(ptr) (((ptr)->v1.flagBits.NEGOTIATE_VERSION) ? 2 : 1)
00125
00126 #define SmbLength(ptr) (NTLM_VER(ptr) == 2 ? \
00127 (((ptr)->v2.buffer - (uint8*)(ptr)) + (ptr)->v2.bufIndex) : \
00128 (((ptr)->v1.buffer - (uint8*)(ptr)) + (ptr)->v1.bufIndex))
00129
00130
00131
00132
00133
00134
00135 typedef struct
00136 {
00137 uint16 len;
00138 uint16 maxlen;
00139 uint32 offset;
00140 } tSmbStrHeader;
00141
00142 typedef struct
00143 {
00144 uint8 major;
00145 uint8 minor;
00146 uint16 buildNumber;
00147 uint8 reserved[3];
00148 uint8 ntlmRevisionCurrent;
00149 } tSmbOsVersion;
00150
00151 typedef struct
00152 {
00153 char ident[8];
00154 uint32 msgType;
00155 union {
00156 uint32 flags;
00157 tSmbNtlmFlagBits flagBits;
00158 };
00159 tSmbStrHeader domainName;
00160 tSmbStrHeader workStation;
00161 uint8 buffer[NTLM_BUF_SIZE];
00162 uint32 bufIndex;
00163 } NtlmAuthNegotiate1;
00164
00165 typedef struct
00166 {
00167 char ident[8];
00168 uint32 msgType;
00169 union {
00170 uint32 flags;
00171 tSmbNtlmFlagBits flagBits;
00172 };
00173 tSmbStrHeader domainName;
00174 tSmbStrHeader workStation;
00175 tSmbOsVersion version;
00176 uint8 buffer[NTLM_BUF_SIZE];
00177 uint32 bufIndex;
00178 } NtlmAuthNegotiate2;
00179
00180 typedef union
00181 {
00182 NtlmAuthNegotiate1 v1;
00183 NtlmAuthNegotiate2 v2;
00184 } tSmbNtlmAuthNegotiate;
00185
00186 typedef struct
00187 {
00188 char ident[8];
00189 uint32 msgType;
00190 tSmbStrHeader targetName;
00191 union {
00192 uint32 flags;
00193 tSmbNtlmFlagBits flagBits;
00194 };
00195 uint8 challengeData[8];
00196 uint8 reserved[8];
00197 tSmbStrHeader targetInfo;
00198 uint8 buffer[NTLM_BUF_SIZE];
00199 uint32 bufIndex;
00200 } NtlmAuthChallenge1;
00201
00202 typedef struct
00203 {
00204 char ident[8];
00205 uint32 msgType;
00206 tSmbStrHeader targetName;
00207 union {
00208 uint32 flags;
00209 tSmbNtlmFlagBits flagBits;
00210 };
00211 uint8 challengeData[8];
00212 uint8 reserved[8];
00213 tSmbStrHeader targetInfo;
00214 tSmbOsVersion version;
00215 uint8 buffer[NTLM_BUF_SIZE];
00216 uint32 bufIndex;
00217 } NtlmAuthChallenge2;
00218
00219 typedef union
00220 {
00221 NtlmAuthChallenge1 v1;
00222 NtlmAuthChallenge2 v2;
00223 } tSmbNtlmAuthChallenge;
00224
00225 typedef struct
00226 {
00227 char ident[8];
00228 uint32 msgType;
00229 tSmbStrHeader lmResponse;
00230 tSmbStrHeader ntResponse;
00231 tSmbStrHeader domainName;
00232 tSmbStrHeader user;
00233 tSmbStrHeader workStation;
00234 tSmbStrHeader sessionKey;
00235 union {
00236 uint32 flags;
00237 tSmbNtlmFlagBits flagBits;
00238 };
00239 uint8 buffer[NTLM_BUF_SIZE];
00240 uint32 bufIndex;
00241 } NtlmAuthResponse1;
00242
00243 typedef struct
00244 {
00245 char ident[8];
00246 uint32 msgType;
00247 tSmbStrHeader lmResponse;
00248 tSmbStrHeader ntResponse;
00249 tSmbStrHeader domainName;
00250 tSmbStrHeader user;
00251 tSmbStrHeader workStation;
00252 tSmbStrHeader sessionKey;
00253 union {
00254 uint32 flags;
00255 tSmbNtlmFlagBits flagBits;
00256 };
00257 tSmbOsVersion version;
00258
00259 uint8 buffer[NTLM_BUF_SIZE];
00260 uint32 bufIndex;
00261 } NtlmAuthResponse2;
00262
00263 typedef union
00264 {
00265 NtlmAuthResponse1 v1;
00266 NtlmAuthResponse2 v2;
00267 } tSmbNtlmAuthResponse;
00268
00269
00270 LIBEXTERN void
00271 dumpSmbNtlmAuthNegotiate(FILE * fp, tSmbNtlmAuthNegotiate * Negotiate);
00272
00273 LIBEXTERN void
00274 dumpSmbNtlmAuthChallenge(FILE * fp, tSmbNtlmAuthChallenge * challenge);
00275
00276 LIBEXTERN void
00277 dumpSmbNtlmAuthResponse(FILE * fp, tSmbNtlmAuthResponse * response);
00278
00279 LIBEXTERN void
00280 buildSmbNtlmAuthNegotiate( tSmbNtlmAuthNegotiate *negotiate,
00281 const char *user,
00282 const char *domain);
00283
00284
00285
00286 LIBEXTERN void
00287 buildSmbNtlmAuthNegotiate_noatsplit( tSmbNtlmAuthNegotiate *negotiate,
00288 const char *user,
00289 const char *domain);
00290
00291 LIBEXTERN void
00292 buildSmbNtlmAuthResponse(tSmbNtlmAuthChallenge * challenge,
00293 tSmbNtlmAuthResponse * response,
00294 const char *user,
00295 const char *workstation,
00296 const char *domain,
00297 const char *password,
00298 const uint8 time[8]);
00299
00300 LIBEXTERN void
00301 ntlm_smb_encrypt (const char *passwd,
00302 const uint8 * challenge,
00303 uint8 * answer);
00304 LIBEXTERN void
00305 ntlm_smb_nt_encrypt (const char *passwd,
00306 const uint8 * challenge,
00307 uint8 * answer);
00308
00309 LIBEXTERN const char *ntlm_check_version (const char *req_version);
00310
00311 LIBEXTERN char *getString(void *ptr, tSmbStrHeader *hdr, char *output);
00312
00313 LIBEXTERN void
00314 hmac_md5
00315 (
00316 unsigned char *text,
00317 int text_len,
00318 unsigned char *key,
00319 int key_len,
00320 unsigned char *digest
00321 );
00322
00323 LIBEXTERN int
00324 GenerateRandom(uint8 *ptr, int len);
00325
00326 # ifdef __cplusplus
00327 }
00328 # endif
00329
00330 #endif