Debian Codegraph @

ntlm/message_authenticate.go

raw symbols SCIP
1 //Copyright 2013 Thomson Reuters Global Resources. BSD License please see License file for more information
2
3 package ntlm
4
5 import (
6 "bytes"
7 "encoding/binary"
8 "encoding/hex"
9 "errors"
10 "fmt"
11 )
12
13 type AuthenticateMessage struct {
14 // sig - 8 bytes
15 Signature []byte
16 // message type - 4 bytes
17 MessageType uint32
18
19 // The LmChallenge Response can be v1 or v2
20 LmChallengeResponse *PayloadStruct // 8 bytes
21 LmV1Response *LmV1Response
22 LmV2Response *LmV2Response
23
24 // The NtChallengeResponse can be v1 or v2
25 NtChallengeResponseFields *PayloadStruct // 8 bytes
26 NtlmV1Response *NtlmV1Response
27 NtlmV2Response *NtlmV2Response
28
29 DomainName *PayloadStruct // 8 bytes
30 UserName *PayloadStruct // 8 bytes
31 Workstation *PayloadStruct // 8 bytes
32
33 // If the NTLMSSP_NEGOTIATE_KEY_EXCH flag is set in the neogitate flags then this will point to the offset in the payload
34 // with the key, otherwise it will have Len = 0. According to Davenport these bytes are optional (see Type3 message).
35 // The MS-NLMP docs do not mention this.
36 EncryptedRandomSessionKey *PayloadStruct // 8 bytes
37
38 /// MS-NLMP 2.2.1.3 - In connectionless mode, a NEGOTIATE structure that contains a set of bit flags (section 2.2.2.5) and represents the
39 // conclusion of negotiation—the choices the client has made from the options the server offered in the CHALLENGE_MESSAGE.
40 // In connection-oriented mode, a NEGOTIATE structure that contains the set of bit flags (section 2.2.2.5) negotiated in
41 // the previous
42 NegotiateFlags uint32 // 4 bytes
43
44 // Version (8 bytes): A VERSION structure (section 2.2.2.10) that is present only when the NTLMSSP_NEGOTIATE_VERSION
45 // flag is set in the NegotiateFlags field. This structure is used for debugging purposes only. In normal protocol
46 // messages, it is ignored and does not affect the NTLM message processing.<9>
47 Version *VersionStruct
48
49 // The message integrity for the NTLM NEGOTIATE_MESSAGE, CHALLENGE_MESSAGE, and AUTHENTICATE_MESSAGE.<10>
50 Mic []byte // 16 bytes
51
52 // payload - variable
53 Payload []byte
54 }
55
56 func ParseAuthenticateMessage(body []byte, ntlmVersion int) (*AuthenticateMessage, error) {
57 am := new(AuthenticateMessage)
58
59 am.Signature = body[0:8]
60 if !bytes.Equal(am.Signature, []byte("NTLMSSP\x00")) {
61 return nil, errors.New("Invalid NTLM message signature")
62 }
63
64 am.MessageType = binary.LittleEndian.Uint32(body[8:12])
65 if am.MessageType != 3 {
66 return nil, errors.New("Invalid NTLM message type should be 0x00000003 for authenticate message")
67 }
68
69 var err error
70
71 am.LmChallengeResponse, err = ReadBytePayload(12, body)
72 if err != nil {
73 return nil, err
74 }
75
76 if ntlmVersion == 2 {
77 am.LmV2Response = ReadLmV2Response(am.LmChallengeResponse.Payload)
78 } else {
79 am.LmV1Response = ReadLmV1Response(am.LmChallengeResponse.Payload)
80 }
81
82 am.NtChallengeResponseFields, err = ReadBytePayload(20, body)
83 if err != nil {
84 return nil, err
85 }
86
87 // Check to see if this is a v1 or v2 response
88 if ntlmVersion == 2 {
89 am.NtlmV2Response, err = ReadNtlmV2Response(am.NtChallengeResponseFields.Payload)
90 } else {
91 am.NtlmV1Response, err = ReadNtlmV1Response(am.NtChallengeResponseFields.Payload)
92 }
93
94 if err != nil {
95 return nil, err
96 }
97
98 am.DomainName, err = ReadStringPayload(28, body)
99 if err != nil {
100 return nil, err
101 }
102
103 am.UserName, err = ReadStringPayload(36, body)
104 if err != nil {
105 return nil, err
106 }
107
108 am.Workstation, err = ReadStringPayload(44, body)
109 if err != nil {
110 return nil, err
111 }
112
113 lowestOffset := am.getLowestPayloadOffset()
114 offset := 52
115
116 // If the lowest payload offset is 52 then:
117 // The Session Key, flags, and OS Version structure are omitted. The data (payload) block in this case starts after the Workstation Name
118 // security buffer header, at offset 52. This form is seen in older Win9x-based systems. This is from the davenport notes about Type 3
119 // messages and this information does not seem to be present in the MS-NLMP document
120 if lowestOffset > 52 {
121 am.EncryptedRandomSessionKey, err = ReadBytePayload(offset, body)
122 if err != nil {
123 return nil, err
124 }
125 offset = offset + 8
126
127 am.NegotiateFlags = binary.LittleEndian.Uint32(body[offset : offset+4])
128 offset = offset + 4
129
130 // Version (8 bytes): A VERSION structure (section 2.2.2.10) that is present only when the NTLMSSP_NEGOTIATE_VERSION flag is set in the NegotiateFlags field. This structure is used for debugging purposes only. In normal protocol messages, it is ignored and does not affect the NTLM message processing.<9>
131 if NTLMSSP_NEGOTIATE_VERSION.IsSet(am.NegotiateFlags) {
132 am.Version, err = ReadVersionStruct(body[offset : offset+8])
133 if err != nil {
134 return nil, err
135 }
136 offset = offset + 8
137 }
138
139 // The MS-NLMP has this to say about the MIC
140 // "An AUTHENTICATE_MESSAGE indicates the presence of a MIC field if the TargetInfo field has an AV_PAIR structure whose two fields are:
141 // AvId == MsvAvFlags Value bit 0x2 == 1"
142 // However there is no TargetInfo structure in the Authenticate Message! There is one in the Challenge Message though. So I'm using
143 // a hack to check to see if there is a MIC. I look to see if there is room for the MIC before the payload starts. If so I assume
144 // there is a MIC and read it out.
145 var lowestOffset = am.getLowestPayloadOffset()
146 if lowestOffset > offset {
147 // MIC - 16 bytes
148 am.Mic = body[offset : offset+16]
149 offset = offset + 16
150 }
151 }
152
153 am.Payload = body[offset:]
154
155 return am, nil
156 }
157
158 func (a *AuthenticateMessage) ClientChallenge() (response []byte) {
159 if a.NtlmV2Response != nil {
160 response = a.NtlmV2Response.NtlmV2ClientChallenge.ChallengeFromClient
161 } else if a.NtlmV1Response != nil && NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY.IsSet(a.NegotiateFlags) {
162 response = a.LmV1Response.Response[0:8]
163 }
164
165 return response
166 }
167
168 func (a *AuthenticateMessage) getLowestPayloadOffset() int {
169 payloadStructs := [...]*PayloadStruct{a.LmChallengeResponse, a.NtChallengeResponseFields, a.DomainName, a.UserName, a.Workstation, a.EncryptedRandomSessionKey}
170
171 // Find the lowest offset value
172 lowest := 9999
173 for i := range payloadStructs {
174 p := payloadStructs[i]
175 if p != nil && p.Offset > 0 && int(p.Offset) < lowest {
176 lowest = int(p.Offset)
177 }
178 }
179
180 return lowest
181 }
182
183 func (a *AuthenticateMessage) Bytes() []byte {
184 payloadLen := int(a.LmChallengeResponse.Len + a.NtChallengeResponseFields.Len + a.DomainName.Len + a.UserName.Len + a.Workstation.Len + a.EncryptedRandomSessionKey.Len)
185 messageLen := 8 + 4 + 6*8 + 4 + 8 + 16
186 payloadOffset := uint32(messageLen)
187
188 messageBytes := make([]byte, 0, messageLen+payloadLen)
189 buffer := bytes.NewBuffer(messageBytes)
190
191 buffer.Write(a.Signature)
192
193 binary.Write(buffer, binary.LittleEndian, a.MessageType)
194
195 a.LmChallengeResponse.Offset = payloadOffset
196 payloadOffset += uint32(a.LmChallengeResponse.Len)
197 buffer.Write(a.LmChallengeResponse.Bytes())
198
199 a.NtChallengeResponseFields.Offset = payloadOffset
200 payloadOffset += uint32(a.NtChallengeResponseFields.Len)
201 buffer.Write(a.NtChallengeResponseFields.Bytes())
202
203 a.DomainName.Offset = payloadOffset
204 payloadOffset += uint32(a.DomainName.Len)
205 buffer.Write(a.DomainName.Bytes())
206
207 a.UserName.Offset = payloadOffset
208 payloadOffset += uint32(a.UserName.Len)
209 buffer.Write(a.UserName.Bytes())
210
211 a.Workstation.Offset = payloadOffset
212 payloadOffset += uint32(a.Workstation.Len)
213 buffer.Write(a.Workstation.Bytes())
214
215 a.EncryptedRandomSessionKey.Offset = payloadOffset
216 payloadOffset += uint32(a.EncryptedRandomSessionKey.Len)
217 buffer.Write(a.EncryptedRandomSessionKey.Bytes())
218
219 buffer.Write(uint32ToBytes(a.NegotiateFlags))
220
221 if a.Version != nil {
222 buffer.Write(a.Version.Bytes())
223 } else {
224 buffer.Write(make([]byte, 8))
225 }
226
227 if a.Mic != nil {
228 buffer.Write(a.Mic)
229 } else {
230 buffer.Write(make([]byte, 16))
231 }
232
233 // Write out the payloads
234 buffer.Write(a.LmChallengeResponse.Payload)
235 buffer.Write(a.NtChallengeResponseFields.Payload)
236 buffer.Write(a.DomainName.Payload)
237 buffer.Write(a.UserName.Payload)
238 buffer.Write(a.Workstation.Payload)
239 buffer.Write(a.EncryptedRandomSessionKey.Payload)
240
241 return buffer.Bytes()
242 }
243
244 func (a *AuthenticateMessage) String() string {
245 var buffer bytes.Buffer
246
247 buffer.WriteString("Authenticate NTLM Message\n")
248 buffer.WriteString(fmt.Sprintf("Payload Offset: %d Length: %d\n", a.getLowestPayloadOffset(), len(a.Payload)))
249
250 if a.LmV2Response != nil {
251 buffer.WriteString(a.LmV2Response.String())
252 buffer.WriteString("\n")
253 }
254
255 if a.LmV1Response != nil {
256 buffer.WriteString(a.LmV1Response.String())
257 buffer.WriteString("\n")
258 }
259
260 if a.NtlmV2Response != nil {
261 buffer.WriteString(a.NtlmV2Response.String())
262 buffer.WriteString("\n")
263 }
264
265 if a.NtlmV1Response != nil {
266 buffer.WriteString(fmt.Sprintf("NtlmResponse Length: %d\n", a.NtChallengeResponseFields.Len))
267 buffer.WriteString(a.NtlmV1Response.String())
268 buffer.WriteString("\n")
269 }
270
271 buffer.WriteString(fmt.Sprintf("UserName: %s\n", a.UserName.String()))
272 buffer.WriteString(fmt.Sprintf("DomainName: %s\n", a.DomainName.String()))
273 buffer.WriteString(fmt.Sprintf("Workstation: %s\n", a.Workstation.String()))
274
275 if a.EncryptedRandomSessionKey != nil {
276 buffer.WriteString(fmt.Sprintf("EncryptedRandomSessionKey: %s\n", a.EncryptedRandomSessionKey.String()))
277 }
278
279 if a.Version != nil {
280 buffer.WriteString(fmt.Sprintf("Version: %s\n", a.Version.String()))
281 }
282
283 if a.Mic != nil {
284 buffer.WriteString(fmt.Sprintf("MIC: %s\n", hex.EncodeToString(a.Mic)))
285 }
286
287 buffer.WriteString(fmt.Sprintf("Flags %d\n", a.NegotiateFlags))
288 buffer.WriteString(FlagsToString(a.NegotiateFlags))
289
290 return buffer.String()
291 }