-
Notifications
You must be signed in to change notification settings - Fork 4
/
packet.go
150 lines (116 loc) · 3.61 KB
/
packet.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
package simba
import (
"encoding/binary"
"log"
)
type PacketCodec []byte
func (p PacketCodec) IsInvalid() bool {
// MS-SMB2, MUST be set to 64
if len(p) < 64 {
log.Println("PacketCodec IsInvalid: len(p) < 64")
return true
}
// check if the packet is a valid SMB2 packet
// MS-SMB2, MUST be set to (0xFF ~ FC), 'S', 'M', 'B'
// MS-SMB2 v20230920 - page 386 / 488
if p[1] != 0x53 || p[2] != 0x4d || p[3] != 0x42 {
log.Println("PacketCodec IsInvalid: p[1] != 0x53 || p[2] != 0x4d || p[3] != 0x42")
return true
}
return false
}
func (p PacketCodec) ProtocolId() []byte {
return p[:4]
}
func (p PacketCodec) SetProtocolId() {
copy(p[:4], []byte{0xfe, 0x53, 0x4d, 0x42})
}
func (p PacketCodec) StructureSize() uint16 {
return binary.LittleEndian.Uint16(p[4:6])
}
func (p PacketCodec) SetStructureSize() {
binary.LittleEndian.PutUint16(p[4:6], 64)
}
func (p PacketCodec) CreditCharge() uint16 {
return binary.LittleEndian.Uint16(p[6:8])
}
func (p PacketCodec) SetCreditCharge(v uint16) {
binary.LittleEndian.PutUint16(p[6:8], v)
}
// In SMB 3.x, this field is ChannelSequence field followed by Reserved field
func (p PacketCodec) ChannelSequence() uint16 {
return binary.LittleEndian.Uint16(p[8:10])
}
func (p PacketCodec) SetChannelSequence(v uint16) {
binary.LittleEndian.PutUint16(p[8:10], v)
}
func (p PacketCodec) Reserved() uint16 {
return binary.LittleEndian.Uint16(p[10:12])
}
func (p PacketCodec) SetReserved(v uint16) {
binary.LittleEndian.PutUint16(p[10:12], v)
}
// In SMB 2.0.2 and SMB 2.1 dialects, this field is Status field
func (p PacketCodec) Status() uint32 {
return binary.LittleEndian.Uint32(p[8:12])
}
func (p PacketCodec) SetStatus(v uint32) {
binary.LittleEndian.PutUint32(p[8:12], v)
}
func (p PacketCodec) Command() Command {
return Command(binary.LittleEndian.Uint16(p[12:14]))
}
func (p PacketCodec) SetCommand(v Command) {
binary.LittleEndian.PutUint16(p[12:14], uint16(v))
}
func (p PacketCodec) CreditRequestResponse() uint16 {
return binary.LittleEndian.Uint16(p[14:16])
}
func (p PacketCodec) SetCreditRequestResponse(v uint16) {
binary.LittleEndian.PutUint16(p[14:16], v)
}
func (p PacketCodec) Flags() FLAGS {
return FLAGS(binary.LittleEndian.Uint32(p[16:20]))
}
func (p PacketCodec) SetFlags(v FLAGS) {
binary.LittleEndian.PutUint32(p[16:20], uint32(v))
}
func (p PacketCodec) NextCommand() uint32 {
return binary.LittleEndian.Uint32(p[20:24])
}
func (p PacketCodec) SetNextCommand(v uint32) {
binary.LittleEndian.PutUint32(p[20:24], v)
}
func (p PacketCodec) MessageId() uint64 {
return binary.LittleEndian.Uint64(p[24:32])
}
func (p PacketCodec) SetMessageId(v uint64) {
binary.LittleEndian.PutUint64(p[24:32], v)
}
// If the SMB2_FLAGS_ASYNC_COMMAND flag is set in the Flags field, this field will be AsyncId
func (p PacketCodec) AsyncId() uint64 {
return binary.LittleEndian.Uint64(p[32:40])
}
// If SMB2_FLAGS_ASYNC_COMMAND set
func (p PacketCodec) SetAsyncId(v uint64) {
binary.LittleEndian.PutUint64(p[32:40], v)
}
// If SMB2_FLAGS_ASYNC_COMMAND not set
func (p PacketCodec) TreeId() uint32 {
return binary.LittleEndian.Uint32(p[36:40])
}
func (p PacketCodec) SetTreeId(v uint32) {
binary.LittleEndian.PutUint32(p[36:40], v)
}
func (p PacketCodec) SessionId() uint64 {
return binary.LittleEndian.Uint64(p[40:48])
}
func (p PacketCodec) SetSessionId(v uint64) {
binary.LittleEndian.PutUint64(p[40:48], v)
}
func (p PacketCodec) Signature() []byte {
return p[48:64]
}
func (p PacketCodec) SetSignature(v []byte) {
copy(p[48:64], v)
}