-
Notifications
You must be signed in to change notification settings - Fork 158
/
tcp.go
264 lines (241 loc) · 8.04 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
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
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017-2020 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"
"time"
"github.com/dreadl0ck/netcap/encoder"
"github.com/prometheus/client_golang/prometheus"
)
const (
fieldSeqNum = "SeqNum"
fieldAckNum = "AckNum"
fieldDataOffset = "DataOffset"
fieldFIN = "FIN"
fieldSYN = "SYN"
fieldRST = "RST"
fieldPSH = "PSH"
fieldACK = "ACK"
fieldURG = "URG"
fieldECE = "ECE"
fieldCWR = "CWR"
fieldNS = "NS"
fieldWindow = "Window"
fieldUrgent = "Urgent"
)
var fieldsTCP = []string{
fieldTimestamp,
fieldSrcPort,
fieldDstPort,
fieldSeqNum,
fieldAckNum,
fieldDataOffset,
fieldFIN,
fieldSYN,
fieldRST,
fieldPSH,
fieldACK,
fieldURG,
fieldECE,
fieldCWR,
fieldNS,
fieldWindow,
fieldChecksum,
fieldUrgent,
//fieldOptions,
fieldPayloadEntropy,
fieldPayloadSize,
fieldSrcIP,
fieldDstIP,
}
// CSVHeader returns the CSV header for the audit record.
func (t *TCP) CSVHeader() []string {
return filter(fieldsTCP)
}
// CSVRecord returns the CSV record for the audit record.
func (t *TCP) CSVRecord() []string {
return filter([]string{
formatTimestamp(t.Timestamp), // int64
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
t.getOptionString(), // []*TCPOption
strconv.FormatFloat(t.PayloadEntropy, 'f', 8, 64), // float64
formatInt32(t.PayloadSize), // int32
t.SrcIP,
t.DstIP,
})
}
func (t *TCP) getOptionString() string {
var b strings.Builder
for _, o := range t.Options {
b.WriteString(StructureBegin)
b.WriteString(strconv.Itoa(int(o.OptionType)))
b.WriteString(FieldSeparator)
b.WriteString(strconv.Itoa(int(o.OptionLength)))
b.WriteString(FieldSeparator)
b.WriteString(hex.EncodeToString(o.OptionData))
b.WriteString(StructureEnd)
}
return b.String()
}
// Time returns the timestamp associated with the audit record.
func (t *TCP) Time() int64 {
return t.Timestamp
}
// JSON returns the JSON representation of the audit record.
func (t *TCP) JSON() (string, error) {
// convert unix timestamp from nano to millisecond precision for elastic
t.Timestamp /= int64(time.Millisecond)
return jsonMarshaler.MarshalToString(t)
}
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{
fieldSrcPort,
fieldDstPort,
// fieldSeqNum,
// fieldAckNum,
fieldDataOffset,
fieldFIN,
fieldSYN,
fieldRST,
fieldPSH,
fieldACK,
fieldURG,
fieldECE,
fieldCWR,
fieldNS,
// fieldWindow,
fieldUrgent,
// fieldPadding,
// fieldOptions,
}
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
}
}
// Inc increments the metrics for the audit record.
func (t *TCP) Inc() {
tcpMetric.WithLabelValues(t.metricValues()...).Inc()
tcpPayloadEntropy.WithLabelValues().Observe(t.PayloadEntropy)
tcpPayloadSize.WithLabelValues().Observe(float64(t.PayloadSize))
}
// SetPacketContext sets the associated packet context for the audit record.
func (t *TCP) SetPacketContext(ctx *PacketContext) {
t.SrcIP = ctx.SrcIP
t.DstIP = ctx.DstIP
}
// Src returns the source address of the audit record.
func (t *TCP) Src() string {
return t.SrcIP
}
// Dst returns the destination address of the audit record.
func (t *TCP) Dst() string {
return t.DstIP
}
var tcpEncoder = encoder.NewValueEncoder()
// Encode will encode categorical values and normalize according to configuration
func (t *TCP) Encode() []string {
return filter([]string{
tcpEncoder.Int64(fieldTimestamp, t.Timestamp), // int64
tcpEncoder.Int32(fieldSrcPort, t.SrcPort), // int32
tcpEncoder.Int32(fieldDstPort, t.DstPort), // int32
tcpEncoder.Uint32(fieldSeqNum, t.SeqNum), // uint32
tcpEncoder.Uint32(fieldAckNum, t.AckNum), // uint32
tcpEncoder.Int32(fieldDataOffset, t.DataOffset), // int32
tcpEncoder.Bool(t.FIN), // bool
tcpEncoder.Bool(t.SYN), // bool
tcpEncoder.Bool(t.RST), // bool
tcpEncoder.Bool(t.PSH), // bool
tcpEncoder.Bool(t.ACK), // bool
tcpEncoder.Bool(t.URG), // bool
tcpEncoder.Bool(t.ECE), // bool
tcpEncoder.Bool(t.CWR), // bool
tcpEncoder.Bool(t.NS), // bool
tcpEncoder.Int32(fieldWindow, t.Window), // int32
tcpEncoder.Int32(fieldChecksum, t.Checksum), // int32
tcpEncoder.Int32(fieldUrgent, t.Urgent), // int32
//tcpEncoder.String(fieldOptions, t.getOptionString()), // []*TCPOption
tcpEncoder.Float64(fieldPayloadEntropy, t.PayloadEntropy), // float64
tcpEncoder.Int32(fieldPayloadSize, t.PayloadSize), // int32
tcpEncoder.Int64(fieldSrcIP, ipToInt64(t.SrcIP)),
tcpEncoder.Int64(fieldDstIP, ipToInt64(t.DstIP)),
})
}
// Analyze will invoke the configured analyzer for the audit record and return a score.
func (t *TCP) Analyze() {
}
// NetcapType returns the type of the current audit record
func (t *TCP) NetcapType() Type {
return Type_NC_TCP
}