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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
package ntlm
import (
"crypto/hmac"
"crypto/md5"
"crypto/rc4"
"encoding/binary"
"hash"
"hash/crc32"
"golang.org/x/crypto/md4" //nolint:staticcheck // md4 may be deprecated, but SMB still uses it
)
var zero [16]byte
var version = []byte{
0: WINDOWS_MAJOR_VERSION_10,
1: WINDOWS_MINOR_VERSION_0,
7: NTLMSSP_REVISION_W2K3,
}
const defaultFlags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_NEGOTIATE_KEY_EXCH | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY | NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_VERSION
var le = binary.LittleEndian
const (
NtLmNegotiate = 0x00000001
NtLmChallenge = 0x00000002
NtLmAuthenticate = 0x00000003
)
const (
NTLMSSP_NEGOTIATE_UNICODE = 1 << iota
NTLM_NEGOTIATE_OEM
NTLMSSP_REQUEST_TARGET
_
NTLMSSP_NEGOTIATE_SIGN
NTLMSSP_NEGOTIATE_SEAL
NTLMSSP_NEGOTIATE_DATAGRAM
NTLMSSP_NEGOTIATE_LM_KEY
_
NTLMSSP_NEGOTIATE_NTLM
_
NTLMSSP_ANONYMOUS
NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED
NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED
_
NTLMSSP_NEGOTIATE_ALWAYS_SIGN
NTLMSSP_TARGET_TYPE_DOMAIN
NTLMSSP_TARGET_TYPE_SERVER
_
NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
NTLMSSP_NEGOTIATE_IDENTIFY
_
NTLMSSP_REQUEST_NON_NT_SESSION_KEY
NTLMSSP_NEGOTIATE_TARGET_INFO
_
NTLMSSP_NEGOTIATE_VERSION
_
_
_
NTLMSSP_NEGOTIATE_128
NTLMSSP_NEGOTIATE_KEY_EXCH
NTLMSSP_NEGOTIATE_56
)
const (
MsvAvEOL = iota
MsvAvNbComputerName
MsvAvNbDomainName
MsvAvDnsComputerName
MsvAvDnsDomainName
MsvAvDnsTreeName
MsvAvFlags
MsvAvTimestamp
MsvAvSingleHost
MsvAvTargetName
MsvAvChannelBindings
)
//nolint:unused
type addr struct {
typ uint32
val []byte
}
// channelBindings represents gss_channel_bindings_struct
//
//nolint:unused
type channelBindings struct {
InitiatorAddress addr
AcceptorAddress addr
AppData []byte
}
var signature = []byte("NTLMSSP\x00")
// Version
// 0-1: ProductMajorVersion
// 1-2: ProductMinorVersion
// 2-4: ProductBuild
// 4-7: Reserved
// 7-8: NTLMRevisionCurrent
const (
WINDOWS_MAJOR_VERSION_5 = 0x05
WINDOWS_MAJOR_VERSION_6 = 0x06
WINDOWS_MAJOR_VERSION_10 = 0x0a
)
const (
WINDOWS_MINOR_VERSION_0 = 0x00
WINDOWS_MINOR_VERSION_1 = 0x01
WINDOWS_MINOR_VERSION_2 = 0x02
WINDOWS_MINOR_VERSION_3 = 0x03
)
const (
NTLMSSP_REVISION_W2K3 = 0x0f
)
func ntowfv2(USER, password, domain []byte) []byte {
h := md4.New()
h.Write(password)
hash := h.Sum(nil)
return ntowfv2Hash(USER, hash, domain)
}
func ntowfv2Hash(USER, hash, domain []byte) []byte {
hm := hmac.New(md5.New, hash)
hm.Write(USER)
hm.Write(domain)
return hm.Sum(nil)
}
func encodeNtlmv2Response(dst []byte, h hash.Hash, serverChallenge, clientChallenge, timeStamp []byte, targetInfo encoder) {
// NTLMv2Response
// 0-16: Response
// 16-: NTLMv2ClientChallenge
ntlmv2ClientChallenge := dst[16:]
// NTLMv2ClientChallenge
// 0-1: RespType
// 1-2: HiRespType
// 2-4: _
// 4-8: _
// 8-16: TimeStamp
// 16-24: ChallengeFromClient
// 24-28: _
// 28-: AvPairs
ntlmv2ClientChallenge[0] = 1
ntlmv2ClientChallenge[1] = 1
copy(ntlmv2ClientChallenge[8:16], timeStamp)
copy(ntlmv2ClientChallenge[16:24], clientChallenge)
targetInfo.encode(ntlmv2ClientChallenge[28:])
h.Write(serverChallenge)
h.Write(ntlmv2ClientChallenge)
h.Sum(dst[:0]) // ntChallengeResponse.Response
}
type encoder interface {
size() int
encode(bs []byte)
}
type bytesEncoder []byte
func (b bytesEncoder) size() int {
return len(b)
}
func (b bytesEncoder) encode(bs []byte) {
copy(bs, b)
}
type targetInfoEncoder struct {
Info []byte
SPN []byte
InfoMap map[uint16][]byte
}
func newTargetInfoEncoder(info, spn []byte) *targetInfoEncoder {
infoMap, ok := parseAvPairs(info)
if !ok {
return nil
}
return &targetInfoEncoder{
Info: info,
SPN: spn,
InfoMap: infoMap,
}
}
func (i *targetInfoEncoder) size() int {
size := len(i.Info)
if _, ok := i.InfoMap[MsvAvFlags]; !ok {
size += 8
}
size += 20
if len(i.SPN) != 0 {
size += 4 + len(i.SPN)
}
return size
}
func (i *targetInfoEncoder) encode(dst []byte) {
var off int
if flags, ok := i.InfoMap[MsvAvFlags]; ok {
le.PutUint32(flags, le.Uint32(flags)|0x02)
off = copy(dst, i.Info[:len(i.Info)-4])
} else {
off = copy(dst, i.Info[:len(i.Info)-4])
le.PutUint16(dst[off:off+2], MsvAvFlags)
le.PutUint16(dst[off+2:off+4], 4)
le.PutUint32(dst[off+4:off+8], 0x02)
off += 8
}
le.PutUint16(dst[off:off+2], MsvAvChannelBindings)
le.PutUint16(dst[off+2:off+4], 16)
off += 20
if len(i.SPN) != 0 {
le.PutUint16(dst[off:off+2], MsvAvTargetName)
le.PutUint16(dst[off+2:off+4], uint16(len(i.SPN)))
copy(dst[off+4:], i.SPN)
off += 4 + len(i.SPN)
}
le.PutUint16(dst[off:off+2], MsvAvEOL)
le.PutUint16(dst[off+2:off+4], 0)
//nolint:ineffassign // we know that this does nothing, it will be removed in a later cleanup
off += 4
}
func mac(dst []byte, negotiateFlags uint32, handle *rc4.Cipher, signingKey []byte, seqNum uint32, msg []byte) ([]byte, uint32) {
ret, tag := sliceForAppend(dst, 16)
if negotiateFlags&NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY == 0 {
// NtlmsspMessageSignature
// 0-4: Version
// 4-8: RandomPad
// 8-12: Checksum
// 12-16: SeqNum
le.PutUint32(tag[:4], 0x00000001)
le.PutUint32(tag[8:12], crc32.ChecksumIEEE(msg))
handle.XORKeyStream(tag[4:8], tag[4:8])
handle.XORKeyStream(tag[8:12], tag[8:12])
handle.XORKeyStream(tag[12:16], tag[12:16])
tag[12] ^= byte(seqNum)
tag[13] ^= byte(seqNum >> 8)
tag[14] ^= byte(seqNum >> 16)
tag[15] ^= byte(seqNum >> 24)
if negotiateFlags&NTLMSSP_NEGOTIATE_DATAGRAM == 0 {
seqNum++
}
tag[4] = 0
tag[5] = 0
tag[6] = 0
tag[7] = 0
} else {
// NtlmsspMessageSignatureExt
// 0-4: Version
// 4-12: Checksum
// 12-16: SeqNum
le.PutUint32(tag[:4], 0x00000001)
le.PutUint32(tag[12:16], seqNum)
h := hmac.New(md5.New, signingKey)
h.Write(tag[12:16])
h.Write(msg)
copy(tag[4:12], h.Sum(nil))
if negotiateFlags&NTLMSSP_NEGOTIATE_KEY_EXCH != 0 {
handle.XORKeyStream(tag[4:12], tag[4:12])
}
seqNum++
}
return ret, seqNum
}
func signKey(negotiateFlags uint32, randomSessionKey []byte, fromClient bool) []byte {
if negotiateFlags&NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY != 0 {
h := md5.New()
h.Write(randomSessionKey)
if fromClient {
h.Write([]byte("session key to client-to-server signing key magic constant\x00"))
} else {
h.Write([]byte("session key to server-to-client signing key magic constant\x00"))
}
return h.Sum(nil)
}
return nil
}
func sealKey(negotiateFlags uint32, randomSessionKey []byte, fromClient bool) []byte {
if negotiateFlags&NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY != 0 {
h := md5.New()
switch {
case negotiateFlags&NTLMSSP_NEGOTIATE_128 != 0:
h.Write(randomSessionKey)
case negotiateFlags&NTLMSSP_NEGOTIATE_56 != 0:
h.Write(randomSessionKey[:7])
default:
h.Write(randomSessionKey[:5])
}
if fromClient {
h.Write([]byte("session key to client-to-server sealing key magic constant\x00"))
} else {
h.Write([]byte("session key to server-to-client sealing key magic constant\x00"))
}
return h.Sum(nil)
}
if negotiateFlags&NTLMSSP_NEGOTIATE_LM_KEY != 0 {
sealingKey := make([]byte, 8)
if negotiateFlags&NTLMSSP_NEGOTIATE_56 != 0 {
copy(sealingKey, randomSessionKey[:7])
sealingKey[7] = 0xa0
} else {
copy(sealingKey, randomSessionKey[:5])
sealingKey[5] = 0xe5
sealingKey[6] = 0x38
sealingKey[7] = 0xb0
}
return sealingKey
}
return randomSessionKey
}
func parseAvPairs(bs []byte) (pairs map[uint16][]byte, ok bool) {
// AvPair
// 0-2: AvId
// 2-4: AvLen
// 4-: Value
if len(bs) < 4 {
return nil, false
}
// check MsvAvEOL
for _, c := range bs[len(bs)-4:] {
if c != 0x00 {
return nil, false
}
}
pairs = make(map[uint16][]byte)
for len(bs) > 0 {
if len(bs) < 4 {
return nil, false
}
id := le.Uint16(bs[:2])
// if _, dup := pairs[id]; dup {
// return nil, false
// }
n := int(le.Uint16(bs[2:4]))
if len(bs) < 4+n {
return nil, false
}
pairs[id] = bs[4 : 4+n]
bs = bs[4+n:]
}
return pairs, true
}
func sliceForAppend(in []byte, n int) (head, tail []byte) {
if total := len(in) + n; cap(in) >= total {
head = in[:total]
} else {
head = make([]byte, total)
copy(head, in)
}
tail = head[len(in):]
return
}
|