This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline_mode.go
173 lines (156 loc) · 4.28 KB
/
offline_mode.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
package stanislav
import (
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var (
AnalysisCsvFlow = make(map[string][][]string)
PeriodicCsvFLows = make(map[string][][]string)
ChronologicalOrderCsvFlows = make(map[string][][]string) //they key is actually the epoch
)
func OfflineMode() {
opts = GetOptions()
logger = opts.Logger
dirs := WalkAllDirs(FlowPath) //gather all nprobe output files
ReadFlowFiles(dirs, InspectFlow)
}
func CleanInternalData() {
AnalysisCsvFlow = make(map[string][][]string)
PeriodicCsvFLows = make(map[string][][]string)
ChronologicalOrderCsvFlows = make(map[string][][]string)
analisi = make(AllFlows)
PeriodiFlows = make(PeriodicFlows)
PossibleThreat = make(map[string][]string)
}
func ReadFlowFiles(dirs []string, inspect func(v RawFlow) bool) {
for _, fPath := range dirs {
file, err := os.OpenFile(fPath, os.O_RDONLY, 0777)
if err != nil {
log.Println(err)
continue
}
r := csv.NewReader(file)
r.Comma = '|'
for {
csvField, err := r.Read()
if err != nil {
break
}
//Parse csv fields
Ipv4SrcAddr := csvField[0]
Ipv4DstAddr := csvField[1]
FirstSwitched, err := ConvStringToDate(csvField[7])
if err != nil {
continue
}
PortDst, err := strconv.Atoi(csvField[10])
if err != nil {
continue
}
InPkts, _ := strconv.Atoi(csvField[5])
InBytes, _ := strconv.Atoi(csvField[6])
LastSwitched, err := ConvStringToDate(csvField[8])
if err != nil {
continue
}
PortSrc, err := strconv.Atoi(csvField[9])
if err != nil {
continue
}
biflow, err := strconv.Atoi(csvField[20])
if err != nil {
continue
}
end := 6
switch csvField[18] {
case "reserved":
end = 0
case "idle_timeout":
end = 1
case "active_timeout":
end = 2
case "end_of_flow_detected":
end = 3
case "forced_end":
end = 4
case "lack_of_resources":
end = 5
case "unassigned":
end = 6
default:
continue
}
riskName := csvField[21]
riskRaw, err := strconv.Atoi(csvField[22])
if err != nil {
continue
}
rawFlow := RawFlow{
Ipv4SrcAddr: Ipv4SrcAddr,
Ipv4DstAddr: Ipv4DstAddr,
FirstSwitched: FirstSwitched,
PortDst: uint16(PortDst),
PortSrc: uint16(PortSrc),
InPkts: uint32(InPkts),
InBytes: uint32(InBytes),
LastSwitched: LastSwitched,
EndReason: uint8(end),
BiFlowDirection: uint(biflow),
}
key := fmt.Sprintf("%s/%s/%d", rawFlow.Ipv4SrcAddr, rawFlow.Ipv4DstAddr, rawFlow.PortDst)
isPeriodic := inspect(rawFlow)
if val, ok := AnalysisCsvFlow[key]; ok {
val = append(val, csvField)
AnalysisCsvFlow[key] = val
} else {
AnalysisCsvFlow[key] = [][]string{csvField}
}
if isPeriodic {
if val, ok := AnalysisCsvFlow[key]; ok {
if pFlows, pfOk := PeriodicCsvFLows[key]; pfOk {
lastPeriodic := strings.Join(pFlows[len(pFlows)-1], "|")
elm := [][]string{val[len(val)-2], val[len(val)-1]}
if strings.Compare(strings.Join(elm[0], "|"), lastPeriodic) == 0 {
//put only last elm
pFlows = append(pFlows, elm[1])
PeriodicCsvFLows[key] = pFlows
addChronologicalFlow(elm[1])
} else { // put last 2 elem
pFlows = append(pFlows, elm...)
PeriodicCsvFLows[key] = pFlows
addChronologicalFlow(elm...)
}
} else { //case that i've seen first periodic flow
elm := [][]string{val[len(val)-3], val[len(val)-2], val[len(val)-1]}
PeriodicCsvFLows[key] = elm
addChronologicalFlow(elm...)
}
}
}
if !(Ipv4DstAddr != "" && Ipv4SrcAddr != "0.0.0.0" && Ipv4DstAddr != "0.0.0.0" &&
!ExcludeMultiAndBroadcast(Ipv4SrcAddr) && !ExcludeMultiAndBroadcast(Ipv4DstAddr)) {
continue
}
risk := riskRaw
if risk > 0 && risk != 9 && risk != 8 && risk != 7 && risk != 128 && risk != 384 && risk != 640 && riskName != "" {
AddPossibleThreat(Ipv4SrcAddr+"/"+Ipv4DstAddr, riskName)
}
}
file.Close()
}
}
func addChronologicalFlow(v ...[]string) {
for _, record := range v {
firstSwitched := record[7]
if flow, ok := ChronologicalOrderCsvFlows[firstSwitched]; ok {
flow = append(flow, record)
ChronologicalOrderCsvFlows[firstSwitched] = flow
} else {
ChronologicalOrderCsvFlows[firstSwitched] = [][]string{record}
}
}
}