Debian Codegraph @

ntlm/message_challenge.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 ChallengeMessage struct {
14 // sig - 8 bytes
15 Signature []byte
16 // message type - 4 bytes
17 MessageType uint32
18 // targetname - 12 bytes
19 TargetName *PayloadStruct
20 // negotiate flags - 4bytes
21 NegotiateFlags uint32
22 // server challenge - 8 bytes
23 ServerChallenge []byte
24
25 // MS-NLMP and Davenport disagree a little on the next few fields and how optional they are
26 // This is what Davenport has to say:
27 // As with the Type 1 message, there are a few versions of the Type 2 that have been observed:
28 //
29 // Version 1 -- The Context, Target Information, and OS Version structure are all omitted. The data block
30 // (containing only the contents of the Target Name security buffer) begins at offset 32. This form
31 // is seen in older Win9x-based systems, and is roughly documented in the Open Group's ActiveX reference
32 // documentation (Section 11.2.3).
33 //
34 // Version 2 -- The Context and Target Information fields are present, but the OS Version structure is not.
35 // The data block begins after the Target Information header, at offset 48. This form is seen in most out-of-box
36 // shipping versions of Windows.
37 //
38 // Version 3 -- The Context, Target Information, and OS Version structure are all present. The data block begins
39 // after the OS Version structure, at offset 56. Again, the buffers may be empty (yielding a zero-length data block).
40 // This form was introduced in a relatively recent Service Pack, and is seen on currently-patched versions of Windows 2000,
41 // Windows XP, and Windows 2003.
42
43 // reserved - 8 bytes (set to 0). This field is also known as 'context' in the davenport documentation
44 Reserved []byte
45
46 // targetinfo - 12 bytes
47 TargetInfoPayloadStruct *PayloadStruct
48 TargetInfo *AvPairs
49
50 // version - 8 bytes
51 Version *VersionStruct
52 // payload - variable
53 Payload []byte
54 }
55
56 func ParseChallengeMessage(body []byte) (*ChallengeMessage, error) {
57 challenge := new(ChallengeMessage)
58
59 challenge.Signature = body[0:8]
60 if !bytes.Equal(challenge.Signature, []byte("NTLMSSP\x00")) {
61 return challenge, errors.New("Invalid NTLM message signature")
62 }
63
64 challenge.MessageType = binary.LittleEndian.Uint32(body[8:12])
65 if challenge.MessageType != 2 {
66 return challenge, errors.New("Invalid NTLM message type should be 0x00000002 for challenge message")
67 }
68
69 var err error
70
71 challenge.TargetName, err = ReadStringPayload(12, body)
72 if err != nil {
73 return nil, err
74 }
75
76 challenge.NegotiateFlags = binary.LittleEndian.Uint32(body[20:24])
77
78 challenge.ServerChallenge = body[24:32]
79
80 challenge.Reserved = body[32:40]
81
82 challenge.TargetInfoPayloadStruct, err = ReadBytePayload(40, body)
83 if err != nil {
84 return nil, err
85 }
86
87 challenge.TargetInfo = ReadAvPairs(challenge.TargetInfoPayloadStruct.Payload)
88
89 offset := 48
90
91 if NTLMSSP_NEGOTIATE_VERSION.IsSet(challenge.NegotiateFlags) {
92 challenge.Version, err = ReadVersionStruct(body[offset : offset+8])
93 if err != nil {
94 return nil, err
95 }
96 offset = offset + 8
97 }
98
99 challenge.Payload = body[offset:]
100
101 return challenge, nil
102 }
103
104 func (c *ChallengeMessage) Bytes() []byte {
105 payloadLen := int(c.TargetName.Len + c.TargetInfoPayloadStruct.Len)
106 messageLen := 8 + 4 + 8 + 4 + 8 + 8 + 8 + 8
107 payloadOffset := uint32(messageLen)
108
109 messageBytes := make([]byte, 0, messageLen+payloadLen)
110 buffer := bytes.NewBuffer(messageBytes)
111
112 buffer.Write(c.Signature)
113 binary.Write(buffer, binary.LittleEndian, c.MessageType)
114
115 c.TargetName.Offset = payloadOffset
116 buffer.Write(c.TargetName.Bytes())
117 payloadOffset += uint32(c.TargetName.Len)
118
119 binary.Write(buffer, binary.LittleEndian, c.NegotiateFlags)
120 buffer.Write(c.ServerChallenge)
121 buffer.Write(make([]byte, 8))
122
123 c.TargetInfoPayloadStruct.Offset = payloadOffset
124 buffer.Write(c.TargetInfoPayloadStruct.Bytes())
125 payloadOffset += uint32(c.TargetInfoPayloadStruct.Len)
126
127 // if(c.Version != nil) {
128 buffer.Write(c.Version.Bytes())
129 // } else {
130 // buffer.Write(make([]byte, 8))
131 //}
132
133 // Write out the payloads
134 buffer.Write(c.TargetName.Payload)
135 buffer.Write(c.TargetInfoPayloadStruct.Payload)
136
137 return buffer.Bytes()
138 }
139
140 func (c *ChallengeMessage) getLowestPayloadOffset() int {
141 payloadStructs := [...]*PayloadStruct{c.TargetName, c.TargetInfoPayloadStruct}
142
143 // Find the lowest offset value
144 lowest := 9999
145 for i := range payloadStructs {
146 p := payloadStructs[i]
147 if p != nil && p.Offset > 0 && int(p.Offset) < lowest {
148 lowest = int(p.Offset)
149 }
150 }
151
152 return lowest
153 }
154
155 func (c *ChallengeMessage) String() string {
156 var buffer bytes.Buffer
157
158 buffer.WriteString("Challenge NTLM Message")
159 buffer.WriteString(fmt.Sprintf("\nPayload Offset: %d Length: %d", c.getLowestPayloadOffset(), len(c.Payload)))
160 buffer.WriteString(fmt.Sprintf("\nTargetName: %s", c.TargetName.String()))
161 buffer.WriteString(fmt.Sprintf("\nServerChallenge: %s", hex.EncodeToString(c.ServerChallenge)))
162 if c.Version != nil {
163 buffer.WriteString(fmt.Sprintf("\nVersion: %s\n", c.Version.String()))
164 }
165 buffer.WriteString("\nTargetInfo")
166 buffer.WriteString(c.TargetInfo.String())
167 buffer.WriteString(fmt.Sprintf("\nFlags %d\n", c.NegotiateFlags))
168 buffer.WriteString(FlagsToString(c.NegotiateFlags))
169
170 return buffer.String()
171 }