/
connection.go
213 lines (185 loc) · 6.05 KB
/
connection.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
/*
* 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 encoder
import (
"flag"
"strconv"
"sync"
"time"
"github.com/dreadl0ck/netcap/types"
"github.com/dreadl0ck/netcap/utils"
"github.com/golang/protobuf/proto"
"github.com/google/gopacket"
)
// AtomicConnMap contains all connections and provides synchronized access
type AtomicConnMap struct {
Items map[string]*types.Connection
sync.Mutex
}
// Size returns the number of elements in the Items map
func (a *AtomicConnMap) Size() int {
a.Lock()
defer a.Unlock()
return len(a.Items)
}
var (
// Connections hold all connections
Connections = &AtomicConnMap{
Items: make(map[string]*types.Connection),
}
connEncoderInstance *CustomEncoder
conns int64
// flags for flushing intervals
flagConnFlushInterval = flag.Int("conn-flush-interval", 10000, "flush connections every X flows")
flagConnTimeOut = flag.Int("conn-timeout", 60, "close connections older than X seconds")
)
var (
connFlushInterval = int64(*flagConnFlushInterval)
connTimeOut = time.Second * time.Duration(*flagConnTimeOut)
)
// ConnectionID is a bidirectional connection
// between two devices over the network
// that includes the Link, Network and TransportLayer
type ConnectionID struct {
LinkFlowID uint64
NetworkFlowID uint64
TransportFlowID uint64
}
func (c ConnectionID) String() string {
return strconv.FormatUint(c.LinkFlowID, 10) + strconv.FormatUint(c.NetworkFlowID, 10) + strconv.FormatUint(c.TransportFlowID, 10)
}
var connectionEncoder = CreateCustomEncoder(types.Type_NC_Connection, "Connection", func(d *CustomEncoder) error {
connEncoderInstance = d
return nil
}, func(p gopacket.Packet) proto.Message {
// assemble connectionID
c := ConnectionID{}
if ll := p.LinkLayer(); ll != nil {
c.LinkFlowID = ll.LinkFlow().FastHash()
}
if nl := p.NetworkLayer(); nl != nil {
c.NetworkFlowID = nl.NetworkFlow().FastHash()
}
if tl := p.TransportLayer(); tl != nil {
c.TransportFlowID = tl.TransportFlow().FastHash()
}
// lookup flow
Connections.Lock()
if conn, ok := Connections.Items[c.String()]; ok {
// conn exists. update fields
calcDuration := false
// check if received packet from the same flow
// was captured BEFORE the flows first seen timestamp
if !utils.StringToTime(conn.TimestampFirst).Before(p.Metadata().Timestamp) {
calcDuration = true
// rewrite timestamp
conn.TimestampFirst = utils.TimeToString(p.Metadata().Timestamp)
// rewrite source and destination parameters
// since the first packet decides about the flow direction
if ll := p.LinkLayer(); ll != nil {
conn.SrcMAC = ll.LinkFlow().Src().String()
conn.DstMAC = ll.LinkFlow().Dst().String()
}
if nl := p.NetworkLayer(); nl != nil {
conn.SrcIP = nl.NetworkFlow().Src().String()
conn.DstIP = nl.NetworkFlow().Dst().String()
}
if tl := p.TransportLayer(); tl != nil {
conn.SrcPort = tl.TransportFlow().Src().String()
conn.DstPort = tl.TransportFlow().Dst().String()
}
}
// check if last timestamp was before the current packet
if utils.StringToTime(conn.TimestampLast).Before(p.Metadata().Timestamp) {
// current packet is newer
// update last seen timestamp
conn.TimestampLast = utils.TimeToString(p.Metadata().Timestamp)
calcDuration = true
} // else: do nothing, timestamp is still the oldest one
conn.NumPackets++
conn.Size += int32(len(p.Data()))
// only calculate duration when timetamps have changed
if calcDuration {
conn.Duration = utils.StringToTime(conn.TimestampLast).Sub(utils.StringToTime(conn.TimestampFirst)).Nanoseconds()
}
} else {
// create a new Connection
conn := &types.Connection{}
conn.UID = calcMd5(c.String())
conn.TimestampFirst = utils.TimeToString(p.Metadata().Timestamp)
if ll := p.LinkLayer(); ll != nil {
conn.LinkProto = ll.LayerType().String()
conn.SrcMAC = ll.LinkFlow().Src().String()
conn.DstMAC = ll.LinkFlow().Dst().String()
}
if nl := p.NetworkLayer(); nl != nil {
conn.NetworkProto = nl.LayerType().String()
conn.SrcIP = nl.NetworkFlow().Src().String()
conn.DstIP = nl.NetworkFlow().Dst().String()
}
if tl := p.TransportLayer(); tl != nil {
conn.TransportProto = tl.LayerType().String()
conn.SrcPort = tl.TransportFlow().Src().String()
conn.DstPort = tl.TransportFlow().Dst().String()
}
if al := p.ApplicationLayer(); al != nil {
conn.ApplicationProto = al.LayerType().String()
conn.AppPayloadSize = int32(len(al.Payload()))
}
Connections.Items[c.String()] = conn
conns++
// flush
if conns%connFlushInterval == 0 {
var selectConns []*types.Connection
for id, c := range Connections.Items {
// flush entries whose last timestamp is connTimeOut older than current packet
if p.Metadata().Timestamp.Sub(utils.StringToTime(c.TimestampLast)) > connTimeOut {
selectConns = append(selectConns, c)
// cleanup
delete(Connections.Items, id)
}
}
// flush selection in background
go func() {
for _, c := range selectConns {
writeConn(c)
}
}()
}
}
Connections.Unlock()
return nil
}, func(d *CustomEncoder) error {
if d.cWriter == nil {
for _, c := range Connections.Items {
writeConn(c)
}
}
return nil
})
// writeConn writes the connection
func writeConn(c *types.Connection) {
if connEncoderInstance.csv {
// write as csv
_, err := connEncoderInstance.csvWriter.WriteRecord(c)
if err != nil {
errorMap.Inc(err.Error())
}
} else {
// write protobuf
err := connEncoderInstance.aWriter.PutProto(c)
if err != nil {
panic(err)
}
}
}