-
Notifications
You must be signed in to change notification settings - Fork 9
/
packet.go
259 lines (246 loc) · 6.59 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
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
package nsca
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/rand"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
)
const (
STATE_OK = iota
STATE_WARNING
STATE_CRITICAL
STATE_UNKNOWN
)
const (
ENCRYPT_NONE = iota /* no encryption */
ENCRYPT_XOR /* not really encrypted, just obfuscated */
ENCRYPT_DES /* DES */
ENCRYPT_3DES /* 3DES or Triple DES */
ENCRYPT_CAST128 /* CAST-128 */ /* UNUSED */
ENCRYPT_CAST256 /* CAST-256 */ /* UNUSED */
ENCRYPT_XTEA /* xTEA */ /* UNUSED */
ENCRYPT_3WAY /* 3-WAY */ /* UNUSED */
ENCRYPT_BLOWFISH /* SKIPJACK */ /* UNUSED */
ENCRYPT_TWOFISH /* TWOFISH */ /* UNUSED */
ENCRYPT_LOKI97 /* LOKI97 */ /* UNUSED */
ENCRYPT_RC2 /* RC2 */ /* UNUSED */
ENCRYPT_ARCFOUR /* RC4 */ /* UNUSED */
ENCRYPT_RC6 /* RC6 */ /* UNUSED */
ENCRYPT_RIJNDAEL128 /* RIJNDAEL-128 */ /* AES-128 */
ENCRYPT_RIJNDAEL192 /* RIJNDAEL-192 */ /* AES-192 */
ENCRYPT_RIJNDAEL256 /* RIJNDAEL-256 */ /* AES-256 */
ENCRYPT_MARS /* MARS */ /* UNUSED */
ENCRYPT_PANAMA /* PANAMA */ /* UNUSED */
ENCRYPT_WAKE /* WAKE */ /* UNUSED */
ENCRYPT_SERPENT /* SERPENT */ /* UNUSED */
ENCRYPT_IDEA /* IDEA */ /* UNUSED */
ENCRYPT_ENIGMA /* ENIGMA (Unix crypt) */ /* UNUSED */
ENCRYPT_GOST /* GOST */ /* UNUSED */
ENCRYPT_SAFER64 /* SAFER-sk64 */ /* UNUSED */
ENCRYPT_SAFER128 /* SAFER-sk128 */ /* UNUSED */
ENCRYPT_SAFERPLUS /* SAFER+ */ /* UNUSED */
)
type dataPacket struct {
packetVersion int16
crc32 uint32
timestamp uint32
returnCode int16
hostName string // 64 char max
serviceDescription string // 128 char max
pluginOutput string // 512 char max
}
type initializationPacket struct {
iv []byte // 128 bytes
timestamp uint32
}
type encryption struct {
method int
iv []byte
password []byte
}
func (e *encryption) encrypt(b []byte) error {
if e.method == ENCRYPT_NONE {
return nil
}
if len(e.password) == 0 {
return fmt.Errorf("Zero length password")
}
if e.method == ENCRYPT_XOR {
for i := range b {
b[i] = b[i] ^ e.iv[i%len(e.iv)] ^ e.password[i%len(e.password)]
}
return nil
}
var err error
var block cipher.Block
key := make([]byte, 128)
copy(key, e.password)
switch e.method {
case ENCRYPT_DES:
block, err = des.NewCipher(key[:des.BlockSize])
case ENCRYPT_3DES:
block, err = des.NewTripleDESCipher(key[:des.BlockSize*3])
case ENCRYPT_RIJNDAEL128:
block, err = aes.NewCipher(key[:16])
case ENCRYPT_RIJNDAEL192:
block, err = aes.NewCipher(key[:24])
case ENCRYPT_RIJNDAEL256:
block, err = aes.NewCipher(key[:32])
case ENCRYPT_CAST128:
fallthrough
case ENCRYPT_CAST256:
fallthrough
case ENCRYPT_XTEA:
fallthrough
case ENCRYPT_3WAY:
fallthrough
case ENCRYPT_BLOWFISH:
fallthrough
case ENCRYPT_TWOFISH:
fallthrough
case ENCRYPT_LOKI97:
fallthrough
case ENCRYPT_RC2:
fallthrough
case ENCRYPT_ARCFOUR:
fallthrough
case ENCRYPT_RC6:
fallthrough
case ENCRYPT_MARS:
fallthrough
case ENCRYPT_PANAMA:
fallthrough
case ENCRYPT_WAKE:
fallthrough
case ENCRYPT_SERPENT:
fallthrough
case ENCRYPT_IDEA:
fallthrough
case ENCRYPT_ENIGMA:
fallthrough
case ENCRYPT_GOST:
fallthrough
case ENCRYPT_SAFER64:
fallthrough
case ENCRYPT_SAFER128:
fallthrough
case ENCRYPT_SAFERPLUS:
err = fmt.Errorf("Unsupported encryption method")
default:
err = fmt.Errorf("Unrecognized encryption method")
}
if err != nil {
return err
}
enc := cipher.NewCFBEncrypter(block, e.iv[:block.BlockSize()])
enc.XORKeyStream(b, b)
return nil
}
func newEncryption(method int, iv []byte, password string) *encryption {
e := encryption{
method: method,
iv: make([]byte, len(iv)),
password: make([]byte, len(password)),
}
copy(e.iv, iv)
copy(e.password, password)
return &e
}
func readInitializationPacket(reader io.Reader) (*initializationPacket, error) {
p := initializationPacket{iv: make([]byte, 128)}
err := binary.Read(reader, binary.BigEndian, p.iv)
if err != nil {
return nil, err
}
err = binary.Read(reader, binary.BigEndian, &p.timestamp)
if err != nil {
return nil, err
}
return &p, nil
}
func makeBuffer(s string, length int) ([]byte, error) {
if length == 0 {
return make([]byte, 0), nil
}
b := make([]byte, length)
n, err := rand.Read(b)
if err != nil {
return nil, err
}
if n != len(b) {
return nil, fmt.Errorf("Unexpected result from rand.Read")
}
n = copy(b, s)
if n == len(b) {
b[len(b)-1] = 0
} else {
b[n] = 0
}
return b, nil
}
func newDataPacket(serverTimestamp uint32, returnCode int16, hostName, serviceDescription, pluginOutput string) *dataPacket {
d := dataPacket{
packetVersion: 3,
timestamp: serverTimestamp,
returnCode: returnCode,
hostName: hostName,
serviceDescription: serviceDescription,
pluginOutput: pluginOutput,
}
return &d
}
func (p *dataPacket) write(w io.Writer, e *encryption) error {
if p.packetVersion == 0 {
p.packetVersion = 3
}
p.crc32 = 0
hostName, err := makeBuffer(p.hostName, 64)
if err != nil {
return err
}
service, err := makeBuffer(p.serviceDescription, 128)
if err != nil {
return err
}
output, err := makeBuffer(p.pluginOutput, 512)
if err != nil {
return err
}
// 2 bytes for c struct padding
padding, err := makeBuffer("", 2)
if err != nil {
return err
}
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, p.packetVersion)
binary.Write(buf, binary.BigEndian, padding)
binary.Write(buf, binary.BigEndian, p.crc32)
binary.Write(buf, binary.BigEndian, p.timestamp)
binary.Write(buf, binary.BigEndian, p.returnCode)
binary.Write(buf, binary.BigEndian, hostName)
binary.Write(buf, binary.BigEndian, service)
binary.Write(buf, binary.BigEndian, output)
binary.Write(buf, binary.BigEndian, padding)
b := buf.Bytes()
p.crc32 = crc32.ChecksumIEEE(b)
crc := make([]byte, 4)
binary.BigEndian.PutUint32(crc, p.crc32)
copy(b[4:], crc)
err = e.encrypt(b)
if err != nil {
return err
}
n, err := w.Write(b)
if err != nil {
return err
}
if n != len(b) {
return fmt.Errorf("Wrong byte count returned from write: expected %d, got %d", len(b), n)
}
return nil
}