-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaly.go
319 lines (291 loc) · 7.5 KB
/
anomaly.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"crypto/md5"
"strings"
"time"
go_iforest "github.com/codegaudi/go-iforest"
"github.com/twsnmp/golof/lof"
"github.com/twsnmp/tfidf"
"github.com/twsnmp/tfidf/seg"
)
var oscmdKeys = []string{
"rm%20", "cat%20", "wget%20",
"curl%20", "sudo%20", "ssh%20",
"usermod%20", "useradd%20", "grep%20", "ls%20",
";", "|", "&",
"/bin", "/dev", "/home", "/lib", "/misc", "/opt",
"/root", "/tftpboot", "/usr", "/boot", "/etc", "/initrd",
"/lost+found", "/mnt", "/proc", "/sbin", "/tmp", "/var",
}
var dirTraversalKeys = []string{
"../", "..\\", ":\\",
"/bin", "/dev", "/home", "/lib", "/misc", "/opt",
"/root", "/tftpboot", "/usr", "/boot", "/etc/", "/initrd",
"/lost+found", "/mnt", "/proc", "/sbin", "/tmp", "/var",
}
var sqlKeys = []string{
"'", "*", ";", "%20", "--",
"select", "delete", "create", "drop", "alter",
"insert", "update", "set", "from", "where",
"union", "all", "like",
"and", "&", "or", "|",
"user", "username", "passwd", "id", "admin", "information_schema",
}
func (b *App) setAnomalyScore(algo, vmode string, sr *SearchResult) {
OutLog("start setAnomalyScore")
if sr == nil || len(sr.Logs) < 1 {
return
}
st := time.Now()
var vectors [][]float64
switch vmode {
case "walu":
OutLog("start make vector form Walu log")
for _, l := range sr.Logs {
v := getWaluVector(&l.All)
if len(v) > 20 {
vectors = append(vectors, v)
} else {
OutLog("v=%v %s", v, l.All)
}
}
case "sql":
for _, l := range sr.Logs {
v := getKeywordsVector(&l.All, &sqlKeys)
if len(v) == len(sqlKeys) {
vectors = append(vectors, v)
}
}
case "oscmd":
for _, l := range sr.Logs {
v := getKeywordsVector(&l.All, &oscmdKeys)
if len(v) == len(oscmdKeys) {
vectors = append(vectors, v)
}
}
case "dirt":
for _, l := range sr.Logs {
v := getKeywordsVector(&l.All, &dirTraversalKeys)
if len(v) == len(dirTraversalKeys) {
vectors = append(vectors, v)
}
}
case "tfidf":
OutLog("start tfidf")
lines := []string{}
for _, l := range sr.Logs {
lines = append(lines, l.All)
}
f := tfidf.NewTokenizer(seg.NewLogTokenizer(true))
f.AddDocs(lines...)
vectors = f.GetTFIDF(100, lines...)
OutLog("tfidf lines=%d docs=%d words=%d", len(lines), f.GetDocumentCount(), len(f.GetAllTerms()))
default:
OutLog("start make vector form number fields")
numKeys := []string{}
strKeys := []string{}
addStr := vmode == "all" || vmode == "alltime"
for k, v := range sr.Logs[0].KeyValue {
if _, ok := v.(float64); ok {
numKeys = append(numKeys, string(k))
} else if addStr {
if _, ok := v.(string); ok {
strKeys = append(strKeys, string(k))
}
}
}
addTime := vmode == "time" || vmode == "alltime"
for _, l := range sr.Logs {
vector := []float64{}
for _, key := range numKeys {
if f, ok := l.KeyValue[key].(float64); ok {
vector = append(vector, f)
} else {
vector = append(vector, 0.0)
}
}
for _, key := range strKeys {
if s, ok := l.KeyValue[key].(string); ok {
vector = append(vector, strToFloat(s))
} else {
vector = append(vector, 0.0)
}
}
if addTime {
ts := time.Unix(0, l.Time).Local()
vector = append(vector, float64(ts.Day()))
vector = append(vector, float64(ts.Weekday()))
vector = append(vector, float64(ts.Hour()))
}
vectors = append(vectors, vector)
}
}
switch algo {
case "iforest":
OutLog("start IForest")
iforest, err := go_iforest.NewIForest(vectors, 1000, 256)
if err != nil {
OutLog("NewIForest err=%v", err)
return
}
OutLog("IForest Calculate AnomalyScore")
for i, v := range vectors {
sr.Logs[i].KeyValue["anomalyScore"] = iforest.CalculateAnomalyScore(v)
}
case "lof":
OutLog("start LOF")
samples := lof.GetSamplesFromFloat64s(vectors)
lofGetter := lof.NewLOF(5)
OutLog("LOF Train")
if err := lofGetter.Train(samples); err != nil {
OutLog("LOF err=%v", err)
return
}
OutLog("LOF Calculate AnomalyScore")
for i, s := range samples {
sr.Logs[i].KeyValue["anomalyScore"] = lofGetter.GetLOF(s, "fast")
}
case "sum":
OutLog("start sum")
for i, v := range vectors {
sum := 0.0
for _, e := range v {
sum += e
}
sr.Logs[i].KeyValue["anomalyScore"] = sum
}
default:
OutLog("Other set vector")
for i, v := range vectors {
sr.Logs[i].KeyValue["vector"] = v
}
}
sr.AnomalyDur = time.Now().UnixMilli() - st.UnixMilli()
OutLog("end setAnomalyScore dur=%v", time.Since(st))
}
// strToFloat : 文字列の識別するための数値を取得する、MD5の上位8バイト
func strToFloat(s string) float64 {
var r int64
h := md5.Sum([]byte(s))
for i := 0; i < 8 && i < len(h); i++ {
r *= 256
r += int64(h[i])
}
return float64(r)
}
// getKeywordsVector : キーワードのりストから特徴ベクターを作成する
func getKeywordsVector(s *string, keys *[]string) []float64 {
vector := []float64{}
for _, k := range *keys {
vector = append(vector, float64(strings.Count(*s, k)))
}
return vector
}
// https://github.com/Kanatoko/Walu
func getWaluVector(s *string) []float64 {
vector := []float64{}
a := strings.Split(*s, "\"")
if len(a) < 2 {
return vector
}
query := ""
path := ""
f := strings.Fields(a[1])
if len(f) > 1 {
ua := strings.SplitN(f[1], "?", 2)
if len(ua) > 1 {
path = ua[0]
query = ua[1]
}
}
ca := getCharCount(a[1])
//findex_%
vector = append(vector, float64(strings.Index(a[1], "%")))
//findex_:
vector = append(vector, float64(strings.Index(a[1], ":")))
// countedCharArray
for _, c := range []rune{':', '(', ';', '%', '/', '\'', '<', '?', '.', '#'} {
vector = append(vector, float64(ca[c]))
}
//encoded =
vector = append(vector, float64(strings.Count(a[1], "%3D")+strings.Count(a[1], "%3d")))
//encoded /
vector = append(vector, float64(strings.Count(a[1], "%2F")+strings.Count(a[1], "%2f")))
//encoded \
vector = append(vector, float64(strings.Count(a[1], "%5C")+strings.Count(a[1], "%5c")))
//encoded %
vector = append(vector, float64(strings.Count(a[1], "%25")))
//%20
vector = append(vector, float64(strings.Count(a[1], "%20")))
//POST
if strings.HasPrefix(a[1], "POST") {
vector = append(vector, 1)
} else {
vector = append(vector, 0)
}
//path_nonalnum_count
vector = append(vector, float64(len(path)-getAlphaNumCount(path)))
//pvalue_nonalnum_avg
vector = append(vector, float64(len(query)-getAlphaNumCount(query)))
//non_alnum_len(max_len)
vector = append(vector, float64(getMaxNonAlnumLength(a[1])))
//non_alnum_count
vector = append(vector, float64(getNonAlnumCount(a[1])))
for _, p := range []string{"/%", "//", "/.", "..", "=/", "./", "/?"} {
vector = append(vector, float64(strings.Count(a[1], p)))
}
return vector
}
func getCharCount(s string) []int {
ret := []int{}
for i := 0; i < 96; i++ {
ret = append(ret, 0)
}
for _, c := range s {
if 33 <= c && c <= 95 {
ret[c] += 1
}
}
return ret
}
func getAlphaNumCount(s string) int {
ret := 0
for _, c := range s {
if 65 <= c && c <= 90 {
ret++
} else if 97 <= c && c <= 122 {
ret++
} else if 48 <= c && c <= 57 {
ret++
}
}
return ret
}
func getMaxNonAlnumLength(s string) int {
max := 0
length := 0
for _, c := range s {
if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') {
if length > max {
max = length
}
length = 0
} else {
length++
}
}
if max < length {
max = length
}
return max
}
func getNonAlnumCount(s string) int {
ret := 0
for _, c := range s {
if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') {
} else {
ret++
}
}
return ret
}