-
Notifications
You must be signed in to change notification settings - Fork 12
/
type1.go
177 lines (142 loc) · 5.21 KB
/
type1.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ntlmssp
import (
"fmt"
"math/bits"
"unsafe"
)
const NegotiateMsgPayloadOffset = 32
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/
type NegotiateMsg struct {
Signature [8]byte
MessageType uint32
NegotiateFlags uint32
DomainNameLen uint16
DomainNameMaxLen uint16
DomainNameBufferOffset uint32
WorkstationLen uint16
WorkstationMaxLen uint16
WorkstationBufferOffset uint32
// Version is variable, saved in Payload field
// Version [8]byte
Payload []byte
offset uint32
}
func (nm NegotiateMsg) Display() {
fmt.Println("Negotiate Message (type1)")
fmt.Printf("Signature: %v (%s)\n", nm.Signature[:], nm.Signature[:])
fmt.Printf("MessageType: %x\n", nm.MessageType)
fmt.Printf("NegotiateFlags: %x\n", nm.NegotiateFlags)
fmt.Println("NegotiateFlags Details:")
DisplayNegotiateFlags(nm.NegotiateFlags)
fmt.Printf("DomainName: %s\n", nm.DomainName())
fmt.Printf(" (Len: %d Offset: %d)\n", nm.DomainNameLen, nm.DomainNameBufferOffset)
fmt.Printf("Workstation: %s\n", nm.Workstation())
fmt.Printf(" (Len: %d Offset: %d)\n\n", nm.WorkstationLen, nm.WorkstationBufferOffset)
}
func (nm NegotiateMsg) Marshal(endian byte) []byte {
bs := []byte{}
// NTLMSSP is little endian
if endian == '>' {
nm.MessageType = bits.ReverseBytes32(nm.MessageType)
nm.NegotiateFlags = bits.ReverseBytes32(nm.NegotiateFlags)
nm.DomainNameLen = bits.ReverseBytes16(nm.DomainNameLen)
nm.DomainNameMaxLen = bits.ReverseBytes16(nm.DomainNameMaxLen)
nm.DomainNameBufferOffset = bits.ReverseBytes32(nm.DomainNameBufferOffset)
nm.WorkstationLen = bits.ReverseBytes16(nm.WorkstationLen)
nm.WorkstationMaxLen = bits.ReverseBytes16(nm.WorkstationMaxLen)
nm.WorkstationBufferOffset = bits.ReverseBytes32(nm.WorkstationBufferOffset)
}
bs = append(bs, nm.Signature[:]...)
bs = append(bs, (*(*[4]byte)(unsafe.Pointer(&nm.MessageType)))[:]...)
bs = append(bs, (*(*[4]byte)(unsafe.Pointer(&nm.NegotiateFlags)))[:]...)
bs = append(bs, (*(*[2]byte)(unsafe.Pointer(&nm.DomainNameLen)))[:]...)
bs = append(bs, (*(*[2]byte)(unsafe.Pointer(&nm.DomainNameMaxLen)))[:]...)
bs = append(bs, (*(*[4]byte)(unsafe.Pointer(&nm.DomainNameBufferOffset)))[:]...)
bs = append(bs, (*(*[2]byte)(unsafe.Pointer(&nm.WorkstationLen)))[:]...)
bs = append(bs, (*(*[2]byte)(unsafe.Pointer(&nm.WorkstationMaxLen)))[:]...)
bs = append(bs, (*(*[4]byte)(unsafe.Pointer(&nm.WorkstationBufferOffset)))[:]...)
bs = append(bs, nm.Payload...)
return bs
}
func (nm *NegotiateMsg) UnMarshal(bs []byte) {
copy(nm.Signature[:], bs[:8])
nm.MessageType = uint32(bytes2Uint(bs[8:12], '<'))
nm.NegotiateFlags = uint32(bytes2Uint(bs[12:16], '<'))
nm.DomainNameLen = uint16(bytes2Uint(bs[16:18], '<'))
nm.DomainNameMaxLen = uint16(bytes2Uint(bs[18:20], '<'))
nm.DomainNameBufferOffset = uint32(bytes2Uint(bs[20:24], '<'))
nm.WorkstationLen = uint16(bytes2Uint(bs[24:26], '<'))
nm.WorkstationMaxLen = uint16(bytes2Uint(bs[26:28], '<'))
nm.WorkstationBufferOffset = uint32(bytes2Uint(bs[28:32], '<'))
nm.offset = NegotiateMsgPayloadOffset
plen := 0
if nm.DomainNameBufferOffset != 0 && nm.DomainNameLen != 0 {
plen += int(nm.DomainNameLen)
}
if nm.WorkstationBufferOffset != 0 && nm.WorkstationLen != 0 {
plen += int(nm.WorkstationLen)
}
if nm.NegotiateFlags&NEGOTIATE_VERSION != 0 {
plen += 8
}
nm.Payload = make([]byte, plen)
copy(nm.Payload, bs[NegotiateMsgPayloadOffset:NegotiateMsgPayloadOffset+uint32(plen)])
}
func NewNegotiateMsg(bs []byte) *NegotiateMsg {
nm := NegotiateMsg{}
if bs == nil {
nm.Signature = [8]byte{'N', 'T', 'L', 'M', 'S', 'S', 'P', 0}
nm.MessageType = 0x01
nm.offset = NegotiateMsgPayloadOffset
} else {
nm.UnMarshal(bs)
}
return &nm
}
// Must be OEM charset
func (nm NegotiateMsg) DomainName() string {
if nm.DomainNameLen == 0 {
return ""
}
return string(nm.Payload[nm.DomainNameBufferOffset-NegotiateMsgPayloadOffset : nm.DomainNameBufferOffset-NegotiateMsgPayloadOffset+uint32(nm.DomainNameLen)])
}
// Must be OEM charset
func (nm NegotiateMsg) Workstation() string {
if nm.WorkstationLen == 0 {
return ""
}
return string(nm.Payload[nm.WorkstationBufferOffset-NegotiateMsgPayloadOffset : nm.WorkstationBufferOffset-NegotiateMsgPayloadOffset+uint32(nm.WorkstationLen)])
}
func (nm *NegotiateMsg) SetDomainName(dname []byte) {
if nm.DomainNameLen != 0 {
panic("Can't set DomainName field repeatedly")
}
nm.NegotiateFlags |= NEGOTIATE_OEM_DOMAIN_SUPPLIED
nm.DomainNameLen = uint16(len(dname))
nm.DomainNameMaxLen = nm.DomainNameLen
nm.DomainNameBufferOffset = nm.offset
nm.Payload = append(nm.Payload, dname...)
nm.offset += uint32(nm.DomainNameLen)
}
func (nm *NegotiateMsg) SetWorkstation(ws []byte) {
if nm.WorkstationLen != 0 {
panic("Can't set Workstation field repeatedly")
}
nm.NegotiateFlags |= NEGOTIATE_OEM_WORKSTATION_SUPPLIED
nm.WorkstationLen = uint16(len(ws))
nm.WorkstationMaxLen = nm.WorkstationLen
nm.WorkstationBufferOffset = nm.offset
nm.Payload = append(nm.Payload, ws...)
nm.offset += uint32(nm.WorkstationLen)
}
func (nm NegotiateMsg) Version() []byte {
if nm.NegotiateFlags&NEGOTIATE_VERSION != 0 {
return nm.Payload[:8]
} else {
return nil
}
}
func (nm *NegotiateMsg) Reset() {
nm.Payload = nil
nm.offset = NegotiateMsgPayloadOffset
}