/
keyvalue.go
523 lines (446 loc) · 13.4 KB
/
keyvalue.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
Copyright © 2020 GUILLAUME FOURNIER
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package keyvalue
import (
"encoding/binary"
"fmt"
"math/rand"
"net"
"unsafe"
"github.com/pkg/errors"
"github.com/Gui774ume/network-security-probe/pkg/model/kernel"
"github.com/Gui774ume/network-security-probe/pkg/utils"
)
// Cookie - Unique cookie used to identify a profile in the kernel
type Cookie uint32
// CookieValue - Cookie structure used as value in hashmaps
type CookieValue struct {
Cookie Cookie
}
// NewCookie - Returns a new cookie randomly generated
func NewCookie() Cookie {
return Cookie(rand.Uint32())
}
// CookieListContains - Checks if a cookie is in a list of cookies.
// The functions returns the first id in the map if there is a match.
func CookieListContains(cookies []Cookie, cookie Cookie) int {
for index, c := range cookies {
if c == cookie {
return index
}
}
return -1
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (cv *CookieValue) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
valueB, err := utils.InterfaceToBytes(cv, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&valueB[0]), nil
}
// NewRandomMapName - Returns a new map name randomly generated
func NewRandomMapName() string {
allowedCharacters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
allowedCharactersLen := len(allowedCharacters)
nameLength := 15
b := make([]rune, nameLength)
for i := range b {
b[i] = allowedCharacters[rand.Intn(allowedCharactersLen)]
}
return string(b)
}
// SecurityProfileActionValue - Security profile action used for value in hashmaps
type SecurityProfileActionValue struct {
Action kernel.SecurityProfileAction
}
// NetworkAttack - Network attack
type NetworkAttack uint32
const (
// EmptyNetworkAttack - Used to specify that no attacks was selected. This is the default.
EmptyNetworkAttack NetworkAttack = 0
// ArpSpoofing - ARP spoofing network attack
ArpSpoofing NetworkAttack = 1 << 0
)
func (na NetworkAttack) String() string {
switch na {
case EmptyNetworkAttack:
return "EmptyNetworkAttack"
case ArpSpoofing:
return "ArpSpoofing"
default:
return "Unknown"
}
}
// KeyValue - Key value of a rule in a profile
type KeyValue struct {
Key Key
Value interface{}
}
func (kv *KeyValue) String() string {
return fmt.Sprintf("%v", *kv)
}
// GetMapSection - Returns the map section of the hashmap in which the key-value should live
func (kv *KeyValue) GetMapSection() string {
return kv.Key.GetMapSection()
}
// GetKey - Returns the unsafe pointer to the key
func (kv *KeyValue) GetKey(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
return kv.Key.GetUnsafePointer(byteOrder)
}
// GetValue - Returns the unsafe pointer to the value
func (kv *KeyValue) GetValue(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
valueB, err := utils.InterfaceToBytes(kv.Value, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&valueB[0]), nil
}
// Key - Key interface
type Key interface {
GetMapSection() string
GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error)
String() string
}
// NetworkAttacksKey - Network attacks key structure
type NetworkAttacksKey struct {
Cookie Cookie
}
func (nak *NetworkAttacksKey) String() string {
return fmt.Sprintf("%v", *nak)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (nak *NetworkAttacksKey) GetMapSection() string {
return "network_attacks_rules"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (nak *NetworkAttacksKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(nak, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// NetworkAttacksValue - Network attack value
type NetworkAttacksValue struct {
Value NetworkAttack
}
// ActionKey - Action key structure
type ActionKey struct {
Cookie Cookie
}
func (ak *ActionKey) String() string {
return fmt.Sprintf("%v", *ak)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (ak *ActionKey) GetMapSection() string {
return "action_rules"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (ak *ActionKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(ak, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// ActionValue - Action value
type ActionValue struct {
Action kernel.SecurityProfileAction
}
// ProtocolKey - Protocol key structure
type ProtocolKey struct {
Cookie Cookie
Protocol uint16
TrafficType kernel.TrafficType
Layer uint8
}
func (pk *ProtocolKey) String() string {
return fmt.Sprintf("%v", *pk)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (pk *ProtocolKey) GetMapSection() string {
return "protocol_rules"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (pk *ProtocolKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(pk, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// ProtocolPortKey - ProtocolPortKey key structure
type ProtocolPortKey struct {
Cookie Cookie
Protocol uint16
Port uint16
TrafficType kernel.TrafficType
}
func (ppk *ProtocolPortKey) String() string {
return fmt.Sprintf("%v", *ppk)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (ppk *ProtocolPortKey) GetMapSection() string {
return "protocol_port_rules"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (ppk *ProtocolPortKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(ppk, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// DNSKey - DNS key structure
type DNSKey struct {
DNS [kernel.DNSMaxLength]byte
Cookie Cookie
TrafficType kernel.TrafficType
Layer uint8
}
func (k *DNSKey) String() string {
return fmt.Sprintf("%v", *k)
}
// NewDNSKey - Creates a new DNSKey and encodes the domain string appropriately
func NewDNSKey(tt kernel.TrafficType, cookie Cookie, layer uint8, dns string) (*DNSKey, error) {
encodedName, err := utils.EncodeDNS(dns)
if err != nil {
return nil, errors.Wrap(err, "couldn't encode DNS name")
}
rep := DNSKey{
TrafficType: tt,
Cookie: cookie,
Layer: layer,
DNS: encodedName,
}
return &rep, nil
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (k *DNSKey) GetMapSection() string {
return "dns_rules"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (k *DNSKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(k, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// HTTPKey - Http key structure
type HTTPKey struct {
TrafficType kernel.TrafficType
Cookie Cookie
Method [kernel.HTTPMaxMethodLength]byte
URI [kernel.HTTPMaxURILength]byte
}
func (k *HTTPKey) String() string {
return fmt.Sprintf("%v", *k)
}
// NewHTTPKey - Creates a new HTTPKey and encodes the method and URI appropriately
func NewHTTPKey(tt kernel.TrafficType, cookie Cookie, method string, uri string) *HTTPKey {
rep := HTTPKey{
TrafficType: tt,
Cookie: cookie,
Method: [kernel.HTTPMaxMethodLength]byte{},
URI: [kernel.HTTPMaxURILength]byte{},
}
copy(rep.Method[:], method)
copy(rep.URI[:], uri)
return &rep
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (k HTTPKey) GetMapSection() string {
return "http_rules"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (k *HTTPKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(k, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// PIDnsKey - Process namespace key
type PIDnsKey struct {
NS uint64
}
func (pk *PIDnsKey) String() string {
return fmt.Sprintf("%v", *pk)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (pk PIDnsKey) GetMapSection() string {
return "pidns_profile_id"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (pk *PIDnsKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(pk, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// NETnsKey - Process namespace key
type NETnsKey struct {
NS uint64
}
func (nk *NETnsKey) String() string {
return fmt.Sprintf("%v", *nk)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (nk NETnsKey) GetMapSection() string {
return "netns_profile_id"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (nk *NETnsKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(nk, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// PIDKey - Process key
type PIDKey struct {
PID uint32
}
func (pk *PIDKey) String() string {
return fmt.Sprintf("%v", *pk)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (pk PIDKey) GetMapSection() string {
return "pid_binary_id"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (pk *PIDKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(pk, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// BinaryPathKey - Binary path key
type BinaryPathKey struct {
Cookie Cookie
Path [kernel.PathMax]byte
}
func (bpk *BinaryPathKey) String() string {
return fmt.Sprintf("%v", *bpk)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (bpk BinaryPathKey) GetMapSection() string {
return "path_binary_id"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (bpk *BinaryPathKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(bpk, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// MapOfMapsKeyValue - Key value of a rule in a profile
type MapOfMapsKeyValue struct {
MapOfMapsKey *KeyValue
MapSectionToClone string
Keys []*KeyValue
}
func (momkv *MapOfMapsKeyValue) String() string {
return fmt.Sprintf("%v", *momkv)
}
// CIDRKey - CIDR key
type CIDRKey struct {
Prefix uint32
Data [16]uint8
}
func (k *CIDRKey) String() string {
return fmt.Sprintf("%v", *k)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (k *CIDRKey) GetMapSection() string {
return "cidr_ranges"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (k *CIDRKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(k, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// CIDRRouterKey - CIDR router key
type CIDRRouterKey struct {
Cookie Cookie
IPVersion kernel.NetworkProtocol
TrafficType kernel.TrafficType
}
func (k *CIDRRouterKey) String() string {
return fmt.Sprintf("%v", *k)
}
// GetMapSection - Returns the kernel map section of the hasmap for this key
func (k *CIDRRouterKey) GetMapSection() string {
return "cidr_rules"
}
// GetUnsafePointer - Returns an unsafe Pointer to the data
func (k *CIDRRouterKey) GetUnsafePointer(byteOrder binary.ByteOrder) (unsafe.Pointer, error) {
keyB, err := utils.InterfaceToBytes(k, byteOrder)
if err != nil {
return nil, err
}
return unsafe.Pointer(&keyB[0]), nil
}
// NewCIDRMapOfMapsKeyValue - Creates a new MapOfMaps key-value for the provided cidrs
func NewCIDRMapOfMapsKeyValue(cidrs []string, cookie Cookie, tt kernel.TrafficType, ipVersion kernel.NetworkProtocol, action interface{}) (*MapOfMapsKeyValue, error) {
cidrKv := MapOfMapsKeyValue{
MapOfMapsKey: &KeyValue{
Key: &CIDRRouterKey{
TrafficType: tt,
Cookie: cookie,
IPVersion: ipVersion,
},
Value: nil,
},
MapSectionToClone: "cidr_ranges",
Keys: []*KeyValue{},
}
for _, cidr := range cidrs {
ip, net, err := net.ParseCIDR(cidr)
if err != nil {
return nil, errors.Wrapf(err, "couldn't parse CIDR %v", cidr)
}
prefix, _ := net.Mask.Size()
cidrk := CIDRKey{
Prefix: uint32(prefix),
}
switch ipVersion {
case kernel.EthPIP:
ip4 := ip.To4()
if ip4 == nil {
return nil, errors.New("invalid IPv4 addr")
}
copy(cidrk.Data[:], ip4)
case kernel.EthPIPV6:
ip6 := ip.To16()
if ip6 == nil {
return nil, errors.New("invalid IPv6 addr")
}
copy(cidrk.Data[:], ip6)
}
cidrkv := KeyValue{
Key: &cidrk,
Value: action,
}
cidrKv.Keys = append(cidrKv.Keys, &cidrkv)
}
return &cidrKv, nil
}