-
Notifications
You must be signed in to change notification settings - Fork 158
/
tcp.go
218 lines (200 loc) · 5.68 KB
/
tcp.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
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package types
import (
"encoding/hex"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
var fieldsTCP = []string{
"Timestamp",
"SrcPort",
"DstPort",
"SeqNum",
"AckNum",
"DataOffset",
"FIN",
"SYN",
"RST",
"PSH",
"ACK",
"URG",
"ECE",
"CWR",
"NS",
"Window",
"Checksum",
"Urgent",
"Padding",
"Options",
"PayloadEntropy",
"PayloadSize",
"Payload",
"SrcIP",
"DstIP",
}
func (t TCP) CSVHeader() []string {
return filter(fieldsTCP)
}
func (t TCP) CSVRecord() []string {
// prevent accessing nil pointer
if t.Context == nil {
t.Context = &PacketContext{}
}
return filter([]string{
formatTimestamp(t.Timestamp), // string
formatInt32(t.SrcPort), // int32
formatInt32(t.DstPort), // int32
strconv.FormatUint(uint64(t.SeqNum), 10), // uint32
strconv.FormatUint(uint64(t.AckNum), 10), // uint32
formatInt32(t.DataOffset), // int32
strconv.FormatBool(t.FIN), // bool
strconv.FormatBool(t.SYN), // bool
strconv.FormatBool(t.RST), // bool
strconv.FormatBool(t.PSH), // bool
strconv.FormatBool(t.ACK), // bool
strconv.FormatBool(t.URG), // bool
strconv.FormatBool(t.ECE), // bool
strconv.FormatBool(t.CWR), // bool
strconv.FormatBool(t.NS), // bool
formatInt32(t.Window), // int32
formatInt32(t.Checksum), // int32
formatInt32(t.Urgent), // int32
string(t.Padding), // []byte
t.GetOptionString(), // []*TCPOption
strconv.FormatFloat(t.PayloadEntropy, 'f', 8, 64), // float64
formatInt32(t.PayloadSize), // int32
hex.EncodeToString(t.Payload),
t.Context.SrcIP,
t.Context.DstIP,
})
}
func (t TCP) GetOptionString() string {
var b strings.Builder
for _, o := range t.Options {
b.WriteString(Begin)
b.WriteString(strconv.Itoa(int(o.OptionType)))
b.WriteString(Separator)
b.WriteString(strconv.Itoa(int(o.OptionLength)))
b.WriteString(Separator)
b.WriteString(hex.EncodeToString(o.OptionData))
b.WriteString(End)
}
return b.String()
}
func (f TCP) Time() string {
return f.Timestamp
}
func (u TCP) JSON() (string, error) {
return jsonMarshaler.MarshalToString(&u)
}
var (
tcpMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: strings.ToLower(Type_NC_TCP.String()),
Help: Type_NC_TCP.String() + " audit records",
},
fieldsTCPMetrics,
)
tcpPayloadEntropy = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: strings.ToLower(Type_NC_TCP.String()) + "_entropy",
Help: Type_NC_TCP.String() + " payload entropy",
Buckets: prometheus.LinearBuckets(20, 5, 5),
},
// []string{"SrcPort", "DstPort"},
[]string{},
)
tcpPayloadSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: strings.ToLower(Type_NC_TCP.String()) + "_size",
Help: Type_NC_TCP.String() + " payload sizes",
Buckets: prometheus.LinearBuckets(20, 5, 5),
},
// []string{"SrcPort", "DstPort"},
[]string{},
)
)
var fieldsTCPMetrics = []string{
"SrcPort",
"DstPort",
// "SeqNum",
// "AckNum",
"DataOffset",
"FIN",
"SYN",
"RST",
"PSH",
"ACK",
"URG",
"ECE",
"CWR",
"NS",
// "Window",
"Urgent",
// "Padding",
// "Options",
}
func (t TCP) metricValues() []string {
return []string{
formatInt32(t.SrcPort), // int32
formatInt32(t.DstPort), // int32
// strconv.FormatUint(uint64(t.SeqNum), 10), // uint32
// strconv.FormatUint(uint64(t.AckNum), 10), // uint32
formatInt32(t.DataOffset), // int32
strconv.FormatBool(t.FIN), // bool
strconv.FormatBool(t.SYN), // bool
strconv.FormatBool(t.RST), // bool
strconv.FormatBool(t.PSH), // bool
strconv.FormatBool(t.ACK), // bool
strconv.FormatBool(t.URG), // bool
strconv.FormatBool(t.ECE), // bool
strconv.FormatBool(t.CWR), // bool
strconv.FormatBool(t.NS), // bool
// formatInt32(t.Window), // int32
formatInt32(t.Urgent), // int32
// string(t.Padding), // []byte
// t.GetOptionString(), // []*TCPOption
}
}
func init() {
prometheus.MustRegister(tcpMetric)
prometheus.MustRegister(tcpPayloadEntropy)
prometheus.MustRegister(tcpPayloadSize)
}
func (a TCP) Inc() {
tcpMetric.WithLabelValues(a.metricValues()...).Inc()
tcpPayloadEntropy.WithLabelValues().Observe(a.PayloadEntropy)
tcpPayloadSize.WithLabelValues().Observe(float64(a.PayloadSize))
}
func (a *TCP) SetPacketContext(ctx *PacketContext) {
// create new context and only add information that is
// not yet present on the audit record type
a.Context = &PacketContext{
SrcIP: ctx.SrcIP,
DstIP: ctx.DstIP,
}
}
func (a TCP) Src() string {
if a.Context != nil {
return a.Context.SrcIP
}
return ""
}
func (a TCP) Dst() string {
if a.Context != nil {
return a.Context.DstIP
}
return ""
}