-
Notifications
You must be signed in to change notification settings - Fork 8
/
challenge_message.go
133 lines (106 loc) · 2.88 KB
/
challenge_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ntlmssp
import (
"bytes"
"encoding/binary"
"errors"
"unsafe"
)
var (
errInvalidChallengeMessage = errors.New("invalid NTLM challenge message")
)
type challengeMessageFields struct {
messageHeader
TargetNameFields payload
NegotiateFlags uint32
ServerChallenge [8]uint8
_ [8]uint8
TargetInfoFields payload
}
func (m *challengeMessageFields) IsValid() bool {
return m.messageHeader.IsValid() && m.MessageType == ntLmChallenge
}
type challengeMessage struct {
challengeMessageFields
TargetName string
TargetInfo targetInfo
Version *Version
}
func (m *challengeMessage) Marshal() ([]byte, error) {
offset := int(unsafe.Sizeof(m.challengeMessageFields) + unsafe.Sizeof(*m.Version))
m.NegotiateFlags = ntlmsspRequestTarget.Unset(m.NegotiateFlags)
if m.TargetName != "" {
m.NegotiateFlags = ntlmsspRequestTarget.Set(m.NegotiateFlags)
}
m.NegotiateFlags = ntlmsspNegotiateTargetInfo.Unset(m.NegotiateFlags)
if m.TargetInfo.Len() > 0 {
m.NegotiateFlags = ntlmsspNegotiateTargetInfo.Set(m.NegotiateFlags)
}
var targetName []byte
if ntlmsspNegotiateUnicode.IsSet(m.NegotiateFlags) {
var err error
targetName, err = utf16FromString(m.TargetName)
if err != nil {
return nil, err
}
} else {
targetName = []byte(m.TargetName)
}
m.TargetNameFields = newPayload(len(targetName), &offset)
targetInfo, err := m.TargetInfo.Marshal()
if err != nil {
return nil, err
}
m.TargetInfoFields = newPayload(len(targetInfo), &offset)
var version = &Version{}
m.NegotiateFlags = ntlmsspNegotiateVersion.Unset(m.NegotiateFlags)
if m.Version != nil {
m.NegotiateFlags = ntlmsspNegotiateVersion.Set(m.NegotiateFlags)
version = m.Version
}
b := bytes.Buffer{}
if err := binary.Write(&b, binary.LittleEndian, &m.challengeMessageFields); err != nil {
return nil, err
}
v, err := version.marshal()
if err != nil {
return nil, err
}
b.Write(v)
b.Write(targetName)
b.Write(targetInfo)
return b.Bytes(), nil
}
func (m *challengeMessage) Unmarshal(b []byte) error {
reader := bytes.NewReader(b)
err := binary.Read(reader, binary.LittleEndian, &m.challengeMessageFields)
if err != nil {
return err
}
if !m.challengeMessageFields.IsValid() {
return errInvalidChallengeMessage
}
if ntlmsspRequestTarget.IsSet(m.NegotiateFlags) && m.TargetNameFields.Len > 0 {
m.TargetName, err = m.TargetNameFields.ReadString(reader, ntlmsspNegotiateUnicode.IsSet(m.NegotiateFlags))
if err != nil {
return err
}
}
m.TargetInfo = newTargetInfo()
if ntlmsspNegotiateTargetInfo.IsSet(m.NegotiateFlags) && m.TargetInfoFields.Len > 0 {
v, err := m.TargetInfoFields.ReadValue(reader)
if err != nil {
return err
}
if err := m.TargetInfo.Unmarshal(v); err != nil {
return err
}
}
if ntlmsspNegotiateVersion.IsSet(m.NegotiateFlags) {
v := Version{}
if err := v.unmarshal(reader); err != nil {
return err
}
m.Version = &v
}
return nil
}