-
Notifications
You must be signed in to change notification settings - Fork 9
/
database.go
502 lines (447 loc) · 9.86 KB
/
database.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package appfinger
import (
"errors"
"fmt"
"github.com/lcvvvv/appfinger/httpfinger"
"io"
"os"
"reflect"
"regexp"
"strconv"
"strings"
)
var database []*fingerPrint
func add(productName string, expression string) error {
httpFinger, err := parseFingerPrint(productName, expression)
if err != nil {
return err
}
database = append(database, httpFinger)
return nil
}
func InitDatabase(path string) (n int, lastErr error) {
fs, err := os.Open(path)
if err != nil {
return 0, err
}
return InitDatabaseFS(fs)
}
func InitDatabaseFS(fs io.Reader) (n int, lastErr error) {
sourceBuf, err := io.ReadAll(fs)
if err != nil {
return 0, err
}
source := strings.Split(string(sourceBuf), "\n")
for _, line := range source {
line = strings.TrimSpace(line)
r := strings.SplitAfterN(line, "\t", 2)
if len(r) != 2 {
lastErr = errors.New(line + "invalid")
continue
}
err := add(r[0], r[1])
if err != nil {
lastErr = err
}
}
return len(source), lastErr
}
func Clear() {
database = []*fingerPrint{}
}
func search(banner *httpfinger.Banner) []string {
var products []string
for _, fingerPrint := range database {
if productName := fingerPrint.Match(banner); productName != "" {
products = append(products, productName)
}
}
return removeDuplicate(products)
}
type Expression struct {
//表达式数组
paramSlice []*Param
//表达式原文
value string
//表达式逻辑字符串
//value = (body="test" || header="tt") && response="aaaa"
//expr = (${1} || ${2}) && ${3}
expr string
}
func parseExpression(expr string) (*Expression, error) {
e := &Expression{}
e.value = expr
//去除表达式尾部空格
expr = strings.TrimSpace(expr)
//修饰expr
expr = strings.ReplaceAll(expr, `\"`, `[quota]`)
//字符合法性校验
if err := exprCharVerification(expr); err != nil {
return nil, err
}
//提取param数组
var paramSlice []*Param
paramRawSlice := paramRegx.FindAllStringSubmatch(expr, -1)
//对param进行解析
for index, value := range paramRawSlice {
expr = strings.Replace(expr, value[0], "${"+strconv.Itoa(index+1)+"}", 1)
param, err := parseParam(value[0])
if err != nil {
return nil, err
}
paramSlice = append(paramSlice, param)
}
//语义合法性校验
if err := exprSyntaxVerification(expr); err != nil {
return nil, err
}
e.expr = expr
e.paramSlice = paramSlice
return e, nil
}
func (e *Expression) match(banner *httpfinger.Banner) bool {
expr := e.makeBoolExpression(banner)
b, _ := parseBoolFromString(expr)
return b
}
func (e *Expression) makeBoolExpression(banner *httpfinger.Banner) string {
var expr = e.expr
for index, param := range e.paramSlice {
b := param.match(banner)
expr = strings.Replace(expr, "${"+strconv.Itoa(index+1)+"}", strconv.FormatBool(b), 1)
}
return expr
}
func (e *Expression) Split() []string {
r := recursiveSplitExpression(e.expr)
for i, v := range r {
r[i] = e.Reduction(v)
}
return r
}
func (e *Expression) Reduction(s string) string {
for i, v := range e.paramSlice {
param := fmt.Sprintf("${%d}", i+1)
s = strings.ReplaceAll(s, param, v.String())
}
return s
}
func exprCharVerification(expr string) error {
//把所有param替换为空
str := paramRegx.ReplaceAllString(expr, "")
//把所有逻辑字符替换为空
str = regexp.MustCompile(`[&| ()]`).ReplaceAllString(str, "")
//检测是否存在其他字符
if str != "" {
str = strings.ReplaceAll(str, `[quota]`, `\"`)
return errors.New(strconv.Quote(str) + " is unknown")
}
//检测语法合法性
return nil
}
var regxSyntaxVerification = regexp.MustCompile(`\${\d+}`)
func exprSyntaxVerification(expr string) error {
expr = regxSyntaxVerification.ReplaceAllString(expr, "true")
_, err := parseBoolFromString(expr)
if err != nil {
return errors.New(expr + ":" + err.Error())
}
return nil
}
type Param struct {
keyword string
value string
operator Operator
}
type Operator string
const (
unequal Operator = "!=" // !=
equal = "=" // =
regxEqual = "~=" // ~=
superEqual = "==" // ==
)
var keywordSlice = []string{
"Title",
"Header",
"Body",
"Response",
"Protocol",
"Cert",
"Port",
"Hash",
"Icon",
}
var paramRegx = regexp.MustCompile(`([a-zA-Z0-9]+) *(!=|=|~=|==) *"([^"\n]+)"`)
var keywordRegx = regexp.MustCompile("^" + strings.Join(keywordSlice, "|") + "$")
func parseParam(expr string) (*Param, error) {
p := paramRegx.FindStringSubmatch(expr)
keyword := p[1]
valueRaw := p[3]
keyword = strings.ToUpper(keyword[:1]) + keyword[1:]
if keywordRegx.MatchString(keyword) == false {
return nil, errors.New(keyword + " keyword is unknown")
}
operator := convOperator(p[2])
valueRaw = strings.ReplaceAll(valueRaw, `[quota]`, `\"`)
value, err := strconv.Unquote("\"" + valueRaw + "\"")
if err != nil {
return nil, err
}
if operator == regxEqual {
_, err = regexp.Compile(value)
if err != nil {
return nil, err
}
}
return &Param{
keyword: keyword,
value: value,
operator: operator,
}, nil
}
func (p *Param) match(banner *httpfinger.Banner) bool {
subStr := p.value
keyword := p.keyword
v := reflect.ValueOf(*banner)
str := v.FieldByName(keyword).String()
switch p.operator {
case unequal:
return !strings.Contains(str, subStr)
case equal:
return strings.Contains(str, subStr)
case regxEqual:
return regexp.MustCompile(subStr).MatchString(str)
case superEqual:
return str == subStr
default:
return false
}
}
func (p *Param) String() string {
return fmt.Sprintf("%s%s%s", p.keyword, p.operator, strconv.Quote(p.value))
}
const (
and = iota // &&
or // ||
)
func convOperator(expr string) Operator {
switch expr {
case "!=":
return unequal
case "=":
return equal
case "~=":
return regxEqual
case "==":
return superEqual
default:
panic(expr)
}
}
func parseBoolFromString(expr string) (bool, error) {
//去除空格
expr = strings.ReplaceAll(expr, " ", "")
//如果存在其他异常字符,则报错
s := regexp.MustCompile(`true|false|&|\||\(|\)`).ReplaceAllString(expr, "")
if s != "" {
return false, errors.New(s + "is known")
}
return stringParse(expr)
}
func stringParse(expr string) (bool, error) {
first := true
operator := and
if expr == "true" {
return true, nil
}
if expr == "false" {
return false, nil
}
for i := 0; i < len(expr); i++ {
char := expr[i : i+1]
if char == "t" {
first = parseCoupleBool(first, true, operator)
i += 3
}
if char == "f" {
first = parseCoupleBool(first, false, operator)
i += 4
}
if char == "&" {
operator = and
i += 1
}
if char == "|" {
operator = or
i += 1
}
if char == "(" {
length, err := findCoupleBracketIndex(expr[i:])
if err != nil {
return false, err
}
next, err := stringParse(expr[i+1 : i+length])
if err != nil {
return false, err
}
first = parseCoupleBool(first, next, operator)
i += length
}
}
return first, nil
}
func parseCoupleBool(first bool, next bool, operator int) bool {
if operator == or {
return first || next
}
if operator == and {
return first && next
}
return false
}
func findCoupleBracketIndex(expr string) (int, error) {
var leftIndex []int
var rightIndex []int
for index, value := range expr {
if value == '(' {
leftIndex = append(leftIndex, index)
}
if value == ')' {
rightIndex = append(rightIndex, index)
}
}
if len(leftIndex) != len(rightIndex) {
return 0, errors.New("bracket is not couple")
}
for i, index := range rightIndex {
countLeft := strings.Count(expr[:index], "(")
if countLeft == i+1 {
return index, nil
}
}
return 0, errors.New("bracket is not couple")
}
func trimParentheses(s string) string {
if s[0:1] != "(" {
return s
}
length := len(s)
if length == 0 {
return s
}
if s[length-1:length] != ")" {
return s
}
index, err := findCoupleBracketIndex(s)
if err != nil {
return s
}
if index+1 == length {
return s[1 : length-1]
}
return s
}
func splitExpression(s string) []string {
var data []string
for i := 0; i < len(s); i++ {
char := s[i : i+1]
if char == "(" {
length, err := findCoupleBracketIndex(s[i:])
if err != nil {
panic(err)
}
data = append(data, s[i+1:i+length])
i += length
}
if char == "&" {
data = append(data, "&&")
i += 1
}
if char == "|" {
data = append(data, "||")
i += 1
}
if char == "$" {
var j = 0
for j = 0; j < len(s)-i; j++ {
index := i + j
if s[index:index+1] == "}" {
break
}
}
data = append(data, s[i:i+j+1])
i += j
}
}
var r []string
for i := 0; i < len(data); i++ {
var s = data[i]
if s == "||" {
r = append(r, data[i+1])
i += 1
continue
}
if s == "&&" {
if len(r) == 0 {
panic("expression invalid")
}
for j := 1; j < len(r); j++ {
r[0] = r[0] + " || " + r[j]
}
r[0] = r[0] + " && " + data[i+1]
r = r[:1]
i += 1
continue
}
r = append(r, data[i])
}
return r
}
func recursiveSplitExpression(s string) []string {
var result []string
s = trimParentheses(s)
for _, str := range splitExpression(s) {
sr := splitExpression(s)
if len(sr) == 1 {
result = append(result, sr[0])
continue
}
result = append(result, recursiveSplitExpression(str)...)
}
return result
}
type fingerPrint struct {
//指纹适用范围
//Protocol string
//Path string
//指纹适配产品
ProductName string
//指纹识别规则
Keyword *Expression
}
func parseFingerPrint(productName string, expressionString string) (*fingerPrint, error) {
//校验合法,序列化表达式
expression, err := parseExpression(expressionString)
if err != nil {
return nil, err
}
return &fingerPrint{
//指纹适用范围
//Protocol: "",
//Path: "",
//指纹适配产品
ProductName: productName,
//指纹识别规则
//Icon: "",
//Hash: "",
Keyword: expression,
}, nil
}
func (f *fingerPrint) Match(banner *httpfinger.Banner) string {
if f.Keyword != nil {
if f.Keyword.match(banner) {
return f.ProductName
}
}
return ""
}