/
net_packet_helpers.go
170 lines (138 loc) · 3.64 KB
/
net_packet_helpers.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
package derive
import (
"net"
"strings"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/nextlinux/tracee/pkg/errfmt"
"github.com/nextlinux/tracee/pkg/events"
"github.com/nextlinux/tracee/types/trace"
)
// helpers for all supported protocol derivations
// Event return value (retval) encodes network event related information, such
// as the L3 layer protocol (impossible to know without a L2 header, since
// cgroup_skb programs are L3 only). It might also encode some other
// information, such as L7 flow direction (impossible to know without deriving
// the network event. knowing in advance helps to derive correct event and not
// all possible ones).
const (
familyIpv4 int = 1 << iota
familyIpv6
protoHttpRequest
protoHttpResponse
)
func boolToUint8(b bool) uint8 {
if b {
return 1
}
return 0
}
func convertArrayOfBytes(given [][]byte) []string {
var res []string
for _, i := range given {
res = append(res, string(i))
}
return res
}
func strToLower(given string) string {
return strings.ToLower(given)
}
//
// Helper Functions
//
type netPair struct {
srcIP net.IP
dstIP net.IP
srcPort uint16
dstPort uint16
proto uint8
length uint32
}
const (
IPPROTO_TCP uint8 = 6
IPPROTO_UDP uint8 = 17
)
// convertNetPairToPktMeta converts the local netPair type, used by this code,
// to PktMeta type, expected by the old dns events, which, for now, we want the
// new network packet simple dns events to be compatible with.
func convertNetPairToPktMeta(n *netPair) *trace.PktMeta {
return &trace.PktMeta{
SrcIP: n.srcIP.String(),
DstIP: n.dstIP.String(),
SrcPort: n.srcPort,
DstPort: n.dstPort,
Protocol: n.proto,
PacketLen: n.length,
Iface: "any", // TODO: pick iface index from the kernel ?
}
}
func parseUntilLayer7(event *trace.Event, pair *netPair) (gopacket.ApplicationLayer, error) {
var ok bool
var payload []byte
var layerType gopacket.LayerType
// sanity checks
payloadArg := events.GetArg(event, "payload")
if payloadArg == nil {
return nil, noPayloadError()
}
if payload, ok = payloadArg.Value.([]byte); !ok {
return nil, nonByteArgError()
}
payloadSize := len(payload)
if payloadSize < 1 {
return nil, emptyPayloadError()
}
// event retval encodes layer 3 protocol
if event.ReturnValue&familyIpv4 == familyIpv4 {
layerType = layers.LayerTypeIPv4
} else if event.ReturnValue&familyIpv6 == familyIpv6 {
layerType = layers.LayerTypeIPv6
} else {
return nil, errfmt.Errorf("base layer type not supported: %d", event.ReturnValue)
}
// parse packet with gopacket
packet := gopacket.NewPacket(
payload,
layerType,
gopacket.Default,
)
if packet == nil {
return nil, parsePacketError()
}
// network layer
layer3 := packet.NetworkLayer()
switch v := layer3.(type) {
case (*layers.IPv4):
pair.srcIP = v.SrcIP
pair.dstIP = v.DstIP
pair.length = uint32(v.Length)
case (*layers.IPv6):
pair.srcIP = v.SrcIP
pair.dstIP = v.DstIP
pair.length = uint32(v.Length)
default:
return nil, errfmt.Errorf("layer 3 not supported: %v", layer3)
}
// transport layer
layer4 := packet.TransportLayer()
switch v := layer4.(type) {
case (*layers.TCP):
pair.srcPort = uint16(v.SrcPort)
pair.dstPort = uint16(v.DstPort)
pair.proto = IPPROTO_TCP
case (*layers.UDP):
pair.srcPort = uint16(v.SrcPort)
pair.dstPort = uint16(v.DstPort)
pair.proto = IPPROTO_UDP
default:
return nil, errfmt.Errorf("layer 4 not supported: %v", layer4)
}
// check partial packet decoding
errorLayer := packet.ErrorLayer()
if errorLayer != nil {
return nil, errorLayer.Error()
}
// application layer
layer7 := packet.ApplicationLayer()
return layer7, nil
}