-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggressivescanners.go
174 lines (158 loc) · 4.1 KB
/
aggressivescanners.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
package greynetanalysis
import (
"encoding/json"
"log"
"net/netip"
"os"
"sort"
"github.com/google/gopacket"
"gonum.org/v1/gonum/stat"
)
type ScannerProfile struct {
Dest map[netip.Addr]uint8
Port map[string]uint8
PckCount uint32
}
type ScannerMap struct {
Scanner map[netip.Addr]*ScannerProfile
ScannerChan chan gopacket.Packet
}
func CreateScannerProfile() ScannerMap {
return ScannerMap{Scanner: make(map[netip.Addr]*ScannerProfile), ScannerChan: make(chan gopacket.Packet, 150)}
}
func ReadScannerProfile(fname string) ScannerMap {
var m ScannerMap
infile, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
defer infile.Close()
err = json.NewDecoder(infile).Decode(&m)
if err != nil {
log.Fatal(err)
}
return m
}
func (m ScannerMap) AddScannerProfile() {
for p := range m.ScannerChan {
dstip, dok := netip.AddrFromSlice(p.NetworkLayer().NetworkFlow().Dst().Raw())
srcip, sok := netip.AddrFromSlice(p.NetworkLayer().NetworkFlow().Src().Raw())
if dok && sok {
if p.TransportLayer() != nil {
dstepoint := p.TransportLayer().TransportFlow().EndpointType().String() + ":" + p.TransportLayer().TransportFlow().Dst().String()
if _, exist := m.Scanner[srcip]; !exist {
m.Scanner[srcip] = &ScannerProfile{Dest: make(map[netip.Addr]uint8), Port: make(map[string]uint8), PckCount: 1}
m.Scanner[srcip].Dest[dstip] = 1
m.Scanner[srcip].Port[dstepoint] = 1
} else {
m.Scanner[srcip].PckCount++
if _, sexist := m.Scanner[srcip].Dest[dstip]; !sexist {
m.Scanner[srcip].Dest[dstip] = 1
}
if _, pexist := m.Scanner[srcip].Port[dstepoint]; !pexist {
m.Scanner[srcip].Port[dstepoint] = 1
}
}
} else {
if _, exist := m.Scanner[srcip]; !exist {
m.Scanner[srcip] = &ScannerProfile{Dest: make(map[netip.Addr]uint8), Port: make(map[string]uint8), PckCount: 1}
m.Scanner[srcip].Dest[dstip] = 1
} else {
m.Scanner[srcip].PckCount++
if _, sexist := m.Scanner[srcip].Dest[dstip]; !sexist {
m.Scanner[srcip].Dest[dstip] = 1
}
}
}
}
}
}
func (m ScannerMap) MergeScannerMap(mnew ScannerMap) {
//merge mnew into m
for k, v := range mnew.Scanner {
if _, exist := m.Scanner[k]; !exist {
m.Scanner[k] = v
} else {
m.Scanner[k].PckCount += v.PckCount
for dk, dv := range v.Dest {
if _, dexist := m.Scanner[k].Dest[dk]; !dexist {
m.Scanner[k].Dest[dk] = dv
}
}
for pk, pv := range v.Port {
if _, pexist := m.Scanner[k].Port[pk]; !pexist {
m.Scanner[k].Port[pk] = pv
}
}
}
}
}
func (m ScannerMap) OutputJSON(fname string) {
outfile, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer outfile.Close()
j, err := json.Marshal(m)
if err != nil {
log.Println(err)
} else {
outfile.Write(j)
log.Println("Output JSON file: ", fname)
}
}
func (m ScannerMap) OutputStat(fname string) {
outfile, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer outfile.Close()
for k, v := range m.Scanner {
outfile.WriteString(k.String() + "," + string(len(v.Dest)) + "," + string(len(v.Port)) + "\n")
}
}
func (m ScannerMap) GetAggressiveScannersAD() []netip.Addr {
//Address dispersion
var ad []netip.Addr
theshold := 11000000 * 0.1 / 24 //one hour
for k, v := range m.Scanner {
if len(v.Dest) > int(theshold) {
ad = append(ad, k)
}
}
return ad
}
func (m ScannerMap) GetAggressiveScannersPV() []netip.Addr {
//Packet Volume
var pv []netip.Addr
var vol []float64
theshold := 1 - 0.0001
for _, v := range m.Scanner {
vol = append(vol, float64(v.PckCount))
}
sort.Float64s(vol)
th := stat.CDF(theshold, stat.Empirical, vol, nil)
for k, v := range m.Scanner {
if float64(v.PckCount) >= th {
pv = append(pv, k)
}
}
return pv
}
func (m ScannerMap) GetAggressiveScannersDP() []netip.Addr {
//Distinct dest ports
var dp []netip.Addr
var dport []float64
theshold := 1 - 0.0001
for _, v := range m.Scanner {
dport = append(dport, float64(len(v.Port)))
}
sort.Float64s(dport)
th := stat.CDF(theshold, stat.Empirical, dport, nil)
for k, v := range m.Scanner {
if float64(len(v.Port)) >= th {
dp = append(dp, k)
}
}
return dp
}