forked from hirochachacha/go-smb2
/
session.go
162 lines (134 loc) · 4.25 KB
/
session.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
package ntlm
import (
"bytes"
"crypto/rc4"
"errors"
"github.com/microo8/go-smb2/internal/utf16le"
)
type Session struct {
isClientSide bool
user string
negotiateFlags uint32
exportedSessionKey []byte
clientSigningKey []byte
serverSigningKey []byte
clientHandle *rc4.Cipher
serverHandle *rc4.Cipher
infoMap map[uint16][]byte
}
func (s *Session) User() string {
return s.user
}
func (s *Session) SessionKey() []byte {
return s.exportedSessionKey
}
type InfoMap struct {
NbComputerName string
NbDomainName string
DnsComputerName string
DnsDomainName string
DnsTreeName string
// Flags uint32
// Timestamp time.Time
// SingleHost
// TargetName string
// ChannelBindings
}
// TODO export to somewhere
func (s *Session) InfoMap() *InfoMap {
return &InfoMap{
NbComputerName: utf16le.DecodeToString(s.infoMap[MsvAvNbComputerName]),
NbDomainName: utf16le.DecodeToString(s.infoMap[MsvAvNbDomainName]),
DnsComputerName: utf16le.DecodeToString(s.infoMap[MsvAvDnsComputerName]),
DnsDomainName: utf16le.DecodeToString(s.infoMap[MsvAvDnsDomainName]),
DnsTreeName: utf16le.DecodeToString(s.infoMap[MsvAvDnsTreeName]),
// Flags: le.Uint32(s.infoMap[MsvAvFlags]),
}
}
func (s *Session) Overhead() int {
return 16
}
func (s *Session) Sum(plaintext []byte, seqNum uint32) ([]byte, uint32) {
if s.negotiateFlags&NTLMSSP_NEGOTIATE_SIGN == 0 {
return nil, 0
}
if s.isClientSide {
return mac(nil, s.negotiateFlags, s.clientHandle, s.clientSigningKey, seqNum, plaintext)
}
return mac(nil, s.negotiateFlags, s.serverHandle, s.serverSigningKey, seqNum, plaintext)
}
func (s *Session) CheckSum(sum, plaintext []byte, seqNum uint32) (bool, uint32) {
if s.negotiateFlags&NTLMSSP_NEGOTIATE_SIGN == 0 {
if sum == nil {
return true, 0
}
return false, 0
}
if s.isClientSide {
ret, seqNum := mac(nil, s.negotiateFlags, s.serverHandle, s.serverSigningKey, seqNum, plaintext)
if !bytes.Equal(sum, ret) {
return false, 0
}
return true, seqNum
}
ret, seqNum := mac(nil, s.negotiateFlags, s.clientHandle, s.clientSigningKey, seqNum, plaintext)
if !bytes.Equal(sum, ret) {
return false, 0
}
return true, seqNum
}
func (s *Session) Seal(dst, plaintext []byte, seqNum uint32) ([]byte, uint32) {
ret, ciphertext := sliceForAppend(dst, len(plaintext)+16)
switch {
case s.negotiateFlags&NTLMSSP_NEGOTIATE_SEAL != 0:
s.clientHandle.XORKeyStream(ciphertext[16:], plaintext)
if s.isClientSide {
_, seqNum = mac(ciphertext[:0], s.negotiateFlags, s.clientHandle, s.clientSigningKey, seqNum, plaintext)
} else {
_, seqNum = mac(ciphertext[:0], s.negotiateFlags, s.serverHandle, s.serverSigningKey, seqNum, plaintext)
}
case s.negotiateFlags&NTLMSSP_NEGOTIATE_SIGN != 0:
copy(ciphertext[16:], plaintext)
if s.isClientSide {
_, seqNum = mac(ciphertext[:0], s.negotiateFlags, s.clientHandle, s.clientSigningKey, seqNum, plaintext)
} else {
_, seqNum = mac(ciphertext[:0], s.negotiateFlags, s.serverHandle, s.serverSigningKey, seqNum, plaintext)
}
}
return ret, seqNum
}
func (s *Session) Unseal(dst, ciphertext []byte, seqNum uint32) ([]byte, uint32, error) {
ret, plaintext := sliceForAppend(dst, len(ciphertext)-16)
switch {
case s.negotiateFlags&NTLMSSP_NEGOTIATE_SEAL != 0:
s.serverHandle.XORKeyStream(plaintext, ciphertext[16:])
var sum []byte
if s.isClientSide {
sum, seqNum = mac(nil, s.negotiateFlags, s.serverHandle, s.serverSigningKey, seqNum, plaintext)
} else {
sum, seqNum = mac(nil, s.negotiateFlags, s.clientHandle, s.clientSigningKey, seqNum, plaintext)
}
if !bytes.Equal(ciphertext[:16], sum) {
return nil, 0, errors.New("signature mismatch")
}
case s.negotiateFlags&NTLMSSP_NEGOTIATE_SIGN != 0:
copy(plaintext, ciphertext[16:])
var sum []byte
if s.isClientSide {
sum, seqNum = mac(nil, s.negotiateFlags, s.serverHandle, s.serverSigningKey, seqNum, plaintext)
} else {
sum, seqNum = mac(nil, s.negotiateFlags, s.clientHandle, s.clientSigningKey, seqNum, plaintext)
}
if !bytes.Equal(ciphertext[:16], sum) {
return nil, 0, errors.New("signature mismatch")
}
default:
copy(plaintext, ciphertext[16:])
for _, s := range ciphertext[:16] {
if s != 0x0 {
return nil, 0, errors.New("signature mismatch")
}
}
}
return ret, seqNum, nil
}