-
Notifications
You must be signed in to change notification settings - Fork 160
/
linkFlow.go
130 lines (109 loc) · 3.71 KB
/
linkFlow.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
/*
* 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 label
import (
"io"
"os"
"path/filepath"
"strings"
"sync"
"github.com/dreadl0ck/netcap"
"github.com/dreadl0ck/netcap/types"
"github.com/dreadl0ck/netcap/utils"
"github.com/gogo/protobuf/proto"
pb "gopkg.in/cheggaaa/pb.v1"
)
func LinkFlow(wg *sync.WaitGroup, file string, alerts []*SuricataAlert, outDir, separator, selection string) *pb.ProgressBar {
// @TODO:
// LinkFlows currently cannot be labeled
// since suricata currently does not provide the MAC address of an alert...
// as discussed here: https://redmine.openinfosecfoundation.org/issues/962
// there might an option to enable it for the eve log in the future
return nil
var (
fname = filepath.Join(outDir, file)
total = netcap.Count(fname)
labelsTotal = 0
progress = pb.New(int(total)).Prefix(utils.Pad(utils.TrimFileExtension(file), 25))
outFileName = filepath.Join(outDir, "LinkFlow_labeled.csv")
)
go func() {
r, err := netcap.Open(fname)
if err != nil {
panic(err)
}
// read netcap header
header := r.ReadHeader()
if header.Type != types.Type_NC_LinkFlow {
panic("file does not contain LinkFlow records: " + header.Type.String())
}
// outfile handle
f, err := os.Create(outFileName)
if err != nil {
panic(err)
}
var (
flow = new(types.LinkFlow)
co types.CSV
pm proto.Message
ok bool
)
pm = flow
types.Select(flow, selection)
if co, ok = pm.(types.CSV); !ok {
panic("type does not implement CSV interface")
}
// write header
_, err = f.WriteString(strings.Join(co.CSVHeader(), separator) + separator + "result" + "\n")
if err != nil {
panic(err)
}
read:
for {
err := r.Next(flow)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
} else if err != nil {
panic(err)
}
if UseProgressBars {
progress.Increment()
}
// check if flow has a source or destination adress matching an alert
// if not label it as normal
for _, a := range alerts {
// transport protocol must match
if a.Proto == flow.Proto { // &&
// // AND conn source ip must either be source or destination of alert
// (flow.SrcIP == a.SrcIP || flow.SrcIP == a.DstIP) &&
// // AND conn destination ip must either be source or destination of alert
// (flow.DstIP == a.SrcIP || flow.DstIP == a.DstIP) &&
// // AND conn source port must either be source or destination of alert
// (flow.SrcPort == strconv.Itoa(a.SrcPort) || flow.SrcPort == strconv.Itoa(a.DstPort)) &&
// // AND conn destination port must either be source or destination of alert
// (flow.DstPort == strconv.Itoa(a.SrcPort) || flow.DstPort == strconv.Itoa(a.DstPort)) {
// ((a.DstIP == conn.DstIP && a.SrcIP == conn.SrcIP) ||
// (a.DstIP == conn.SrcIP && a.SrcIP == conn.DstIP)) &&
// add label
f.WriteString(strings.Join(flow.CSVRecord(), separator) + separator + a.Classification + "\n")
labelsTotal++
goto read
}
}
// label as normal
f.WriteString(strings.Join(flow.CSVRecord(), separator) + separator + "normal\n")
}
finish(wg, r, f, labelsTotal, outFileName, progress)
}()
return nil
}