-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
executable file
·655 lines (605 loc) · 16.6 KB
/
parse.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package ddfinger
import (
"bytes"
"container/list"
"github.com/hanbufei/dddd/structs"
"github.com/hanbufei/dddd/utils"
"encoding/base64"
"fmt"
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/gologger"
"gopkg.in/yaml.v3"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
)
// 检测指纹
// 指纹支持如下格式:
// header="123" 返回头中包含123
// header!="123" 返回头中不包含123
// body="123" body中包含123
// body!="123" body中不包含123
// body=="123" body为123
// server="Sundray" 返回头server字段中包含Sundray
// server!="Sundray" 返回头server字段中不包含Sundray
// server=="Sundray" server字段为Sundray
// title="123" 标题包含123
// title!="123" 标题不包含123
// title=="123" 标题为123
// cert="123" 证书中包含123
// cert!="123" 证书中不包含123
// port="80" 服务端口为80
// port!="80" 服务端口不为80
// port>="80" 服务端口大于等于80
// port<="80" 服务端口小于等于80
// protocol="mysql" 协议为mysql
// protocol!="mysql" 协议不为mysql
// path="123/123.html" 爬虫结果中包含 123/123.html
// body_hash="619335048" 响应体mmh3 hash为619335048
// icon_hash="619335048" icon mmh3 hash
// status="200" 页面返回码为200
// status!="200" 页面返回码不为200
// content_type="text/html" content_type包含text/html
// content_type!="text/html" content_type不包含text/html
// banner="123"
// banner!="123"
// 永真
// type="service"
func stdBase64(braw []byte) []byte {
bckd := base64.StdEncoding.EncodeToString(braw)
var buffer bytes.Buffer
for i := 0; i < len(bckd); i++ {
ch := bckd[i]
buffer.WriteByte(ch)
if (i+1)%76 == 0 {
buffer.WriteByte('\n')
}
}
buffer.WriteByte('\n')
return buffer.Bytes()
}
// 判断优先级 非运算符返回0
func advance(ch int) int {
// !
if ch == 33 {
return 3
}
// &
if ch == 38 {
return 2
}
// |
if ch == 124 {
return 1
}
return 0
}
// 计算纯bool表达式,支持 ! && & || | ( )
func boolEval(expression string) bool {
// 左右括号相等
if strings.Count(expression, "(") != strings.Count(expression, ")") {
gologger.Fatal().Msg(fmt.Sprintf("[-] 纯布尔表达式 [%s] 左右括号不匹配", expression))
}
// 去除空格
for strings.Contains(expression, " ") {
expression = strings.ReplaceAll(expression, " ", "")
}
// 去除空表达式
for strings.Contains(expression, "()") {
expression = strings.ReplaceAll(expression, "()", "")
}
for strings.Contains(expression, "&&") {
expression = strings.ReplaceAll(expression, "&&", "&")
}
for strings.Contains(expression, "||") {
expression = strings.ReplaceAll(expression, "||", "|")
}
if !strings.Contains(expression, "T") && !strings.Contains(expression, "F") {
return false
// panic("纯布尔表达式错误,没有包含T/F")
}
expr := list.New()
operator_stack := list.New()
for _, ch := range expression {
// ch 为 T或者F
if ch == 84 || ch == 70 {
expr.PushBack(int(ch))
} else if advance(int(ch)) > 0 {
if operator_stack.Len() == 0 {
operator_stack.PushBack(int(ch))
continue
}
// 两个!抵消
if ch == 33 && operator_stack.Back().Value.(int) == 33 {
operator_stack.Remove(operator_stack.Back())
continue
}
for operator_stack.Len() != 0 && operator_stack.Back().Value.(int) != 40 && advance(operator_stack.Back().Value.(int)) >= advance(int(ch)) {
e := operator_stack.Back()
expr.PushBack(e.Value.(int))
operator_stack.Remove(e)
}
operator_stack.PushBack(int(ch))
} else if ch == 40 {
operator_stack.PushBack(int(ch))
} else if ch == 40 {
for operator_stack.Back().Value.(int) != 40 {
e := operator_stack.Back()
expr.PushBack(e.Value.(int))
operator_stack.Remove(e)
}
operator_stack.Remove(operator_stack.Back())
}
}
for operator_stack.Len() != 0 {
e := operator_stack.Back()
expr.PushBack(e.Value.(int))
operator_stack.Remove(e)
}
tf_stack := list.New()
for expr.Len() != 0 {
e := expr.Front()
ch := e.Value.(int)
expr.Remove(e)
if ch == 84 || ch == 70 {
tf_stack.PushBack(int(ch))
}
if ch == 38 { // &
em := tf_stack.Back()
a := em.Value.(int)
tf_stack.Remove(em)
em = tf_stack.Back()
b := em.Value.(int)
tf_stack.Remove(em)
if a == 84 && b == 84 {
tf_stack.PushBack(84)
} else {
tf_stack.PushBack(70)
}
}
if ch == 124 { // |
em := tf_stack.Back()
a := em.Value.(int)
tf_stack.Remove(em)
em = tf_stack.Back()
b := em.Value.(int)
tf_stack.Remove(em)
if a == 70 && b == 70 {
tf_stack.PushBack(70)
} else {
tf_stack.PushBack(84)
}
}
if ch == 33 { // !
em := tf_stack.Back()
a := em.Value.(int)
tf_stack.Remove(em)
if a == 70 {
tf_stack.PushBack(84)
} else if a == 84 {
tf_stack.PushBack(70)
}
}
}
if tf_stack.Front().Value.(int) == 84 {
return true
} else {
return false
}
}
func readFingerYaml() map[string]interface{} {
data, err := os.ReadFile("config/finger.yaml")
fps := make(map[string]interface{})
err = yaml.Unmarshal(data, &fps)
if err != nil {
gologger.Fatal().Msg(err.Error())
}
return fps
}
func getRuleData(rule string) structs.RuleData {
if !strings.Contains(rule, "=\"") {
return structs.RuleData{}
}
pos := strings.Index(rule, "=\"")
op := 0
if rule[pos-1] == 33 {
op = 1
} else if rule[pos-1] == 61 {
op = 2
} else if rule[pos-1] == 62 {
op = 3
} else if rule[pos-1] == 60 {
op = 4
} else if rule[pos-1] == 126 {
op = 5
}
start := 0
ti := 0
if op > 0 {
ti = 1
}
for i := pos - 1 - ti; i >= 0; i-- {
if rule[i] > 122 || rule[i] < 97 || rule[i] == 95 {
start = i + 1
break
}
}
key := rule[start : pos-ti]
end := pos + 2
for i := pos + 2; i < len(rule)-1; i++ {
if rule[i] != 92 && rule[i+1] == 34 {
end = i + 2
break
}
}
value := rule[pos+2 : end-1]
all := rule[start:end]
return structs.RuleData{Start: start, End: end, Op: int16(op), Key: key, Value: value, All: all}
}
func parseRule(rule string) []structs.RuleData {
var result []structs.RuleData
empty := structs.RuleData{}
for {
data := getRuleData(rule)
if data == empty {
break
}
result = append(result, data)
rule = rule[:data.Start] + "T" + rule[data.End:]
}
return result
}
func ParseFingerYaml() []structs.FingerPEntity {
var result []structs.FingerPEntity
fingerprintYaml := readFingerYaml()
for productName, rulesInterface := range fingerprintYaml {
for _, ruleInterface := range rulesInterface.([]interface{}) {
ruleL := ruleInterface.(string)
result = append(result, structs.FingerPEntity{ProductName: productName, Rule: parseRule(ruleL), AllString: ruleL})
}
}
return result
}
func regexMatch(pattern string, s string) (bool, error) {
matched, err := regexp.MatchString(pattern, s)
if err != nil {
return false, err
}
return matched, nil
}
// body="123" op=0 dataSource为http.body dataRule=123
func dataCheckString(op int16, dataSource string, dataRule string) bool {
dataSource = strings.ToLower(dataSource)
dataRule = strings.ToLower(dataRule)
dataRule = strings.ReplaceAll(dataRule, "\\\"", "\"")
if op == 0 {
if strings.Contains(dataSource, dataRule) {
return true
}
} else if op == 1 {
if !strings.Contains(dataSource, dataRule) {
return true
}
} else if op == 2 {
if dataSource == dataRule {
return true
}
} else if op == 5 {
rs, err := regexMatch(dataRule, dataSource)
if err == nil && rs {
return true
}
}
return false
}
func dataCheckInt(op int16, dataSource int, dataRule int) bool {
if op == 0 { // 数字相等
if dataSource == dataRule {
return true
}
} else if op == 1 { // 数字不相等
if dataSource != dataRule {
return true
}
} else if op == 3 { // 大于等于
if dataSource >= dataRule {
return true
}
} else if op == 4 {
if dataSource <= dataRule {
return true
}
}
return false
}
func checkPath(Path string,
webPath structs.UrlPathEntity,
Port int, // 所开放的端口
Protocol string, // 协议
Banner string, // 响应
Cert string, // TLS证书
) []string {
var fingerPrintResults []string
isWeb := Path != "no#web" && webPath.Hash != ""
hashString := webPath.Hash
body := ""
bodyBytes, ok := structs.GlobalHttpBodyHMap.Get(hashString)
if !ok {
body = ""
} else {
body = string(bodyBytes)
}
headerString := ""
headerBytes, ok := structs.GlobalHttpHeaderHMap.Get(webPath.HeaderHashString)
if !ok {
headerString = ""
} else {
headerString = string(headerBytes)
}
workers := 100
inputChan := make(chan structs.FingerPEntity, len(structs.FingerprintDB))
defer close(inputChan)
results := make(chan string, len(structs.FingerprintDB))
defer close(results)
var wg sync.WaitGroup
//接收结果
go func() {
for found := range results {
if found != "" {
fingerPrintResults = append(fingerPrintResults, found)
}
wg.Done()
}
}()
//多线程扫描
for i := 0; i < workers; i++ {
go func() {
for finger := range inputChan {
rules := finger.Rule
product := finger.ProductName
expr := finger.AllString
for _, singleRule := range rules {
singleRuleResult := false
if singleRule.Key == "header" {
if isWeb && dataCheckString(singleRule.Op, headerString, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "body" {
if isWeb && dataCheckString(singleRule.Op, body, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "server" {
if isWeb && dataCheckString(singleRule.Op, webPath.Server, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "title" {
if isWeb && dataCheckString(singleRule.Op, webPath.Title, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "cert" {
if dataCheckString(singleRule.Op, Cert, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "port" {
value, err := strconv.Atoi(singleRule.Value)
if err == nil && dataCheckInt(singleRule.Op, Port, value) {
singleRuleResult = true
}
} else if singleRule.Key == "protocol" {
if singleRule.Op == 0 {
if Protocol == singleRule.Value {
singleRuleResult = true
}
} else if singleRule.Op == 1 {
if Protocol != singleRule.Value {
singleRuleResult = true
}
}
} else if singleRule.Key == "path" {
if isWeb && dataCheckString(singleRule.Op, Path, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "body_hash" {
if isWeb && dataCheckString(singleRule.Op, webPath.Hash, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "icon_hash" {
value, err := strconv.Atoi(singleRule.Value)
hashIcon, errHash := strconv.Atoi(webPath.IconHash)
if isWeb && err == nil && errHash == nil && dataCheckInt(singleRule.Op, hashIcon, value) {
singleRuleResult = true
}
} else if singleRule.Key == "status" {
value, err := strconv.Atoi(singleRule.Value)
if isWeb && err == nil && dataCheckInt(singleRule.Op, webPath.StatusCode, value) {
singleRuleResult = true
}
} else if singleRule.Key == "content_type" {
if isWeb && dataCheckString(singleRule.Op, webPath.ContentType, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "banner" {
if dataCheckString(singleRule.Op, Banner, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "type" {
if singleRule.Value == "service" {
singleRuleResult = true
}
}
if singleRuleResult {
expr = expr[:singleRule.Start] + "T" + expr[singleRule.End:]
} else {
expr = expr[:singleRule.Start] + "F" + expr[singleRule.End:]
}
}
r := boolEval(expr)
if r {
results <- product
} else {
results <- ""
}
}
}()
}
//添加扫描目标
for _, input := range structs.FingerprintDB {
wg.Add(1)
inputChan <- input
}
wg.Wait()
return utils.RemoveDuplicateElement(fingerPrintResults)
}
func FingerprintIdentification() {
gologger.Info().Msg("指纹识别中")
// 先识别非Web
for hostPort, protocol := range structs.GlobalIPPortMap {
if protocol == "http" || protocol == "https" || protocol == "" {
continue
}
t := strings.Split(hostPort, ":")
if len(t) != 2 {
continue
}
// host := t[0]
port, err := strconv.Atoi(t[1])
if err != nil {
continue
}
banner := ""
bodyBytes, ok := structs.GlobalBannerHMap.Get(hostPort)
if !ok {
banner = ""
} else {
banner = string(bodyBytes)
}
results := checkPath("no#web", structs.UrlPathEntity{}, port, protocol, banner, "")
if len(results) > 0 {
Url := fmt.Sprintf("%s://%s", protocol, hostPort)
structs.GlobalResultMap[Url] = results
msg := "[Finger] " + Url + " ["
for _, r := range results {
msg += aurora.Cyan(r).String() + ","
}
msg = msg[:len(msg)-1] + "]"
gologger.Silent().Msg(msg)
}
}
for rootURL, urlEntity := range structs.GlobalURLMap {
banner := ""
if urlEntity.IP != "" {
hostPort := fmt.Sprintf("%s:%d", urlEntity.IP, urlEntity.Port)
bodyBytes, ok := structs.GlobalBannerHMap.Get(hostPort)
if !ok {
banner = ""
} else {
banner = string(bodyBytes)
}
}
URL, _ := url.Parse(rootURL)
for path, pathEntity := range urlEntity.WebPaths {
results := checkPath(path, pathEntity, urlEntity.Port, URL.Scheme, banner, urlEntity.Cert)
fullURL := rootURL + path
if len(results) > 0 {
structs.GlobalResultMap[fullURL] = results
msg := "[Finger] " + fullURL + " "
msg += fmt.Sprintf("[%d] [", pathEntity.StatusCode)
for _, r := range results {
msg += aurora.Cyan(r).String() + ","
}
msg = msg[:len(msg)-1] + "]"
if pathEntity.Title != "" {
msg += fmt.Sprintf(" [%s]", pathEntity.Title)
}
gologger.Silent().Msg(msg)
} else {
structs.GlobalResultMap[fullURL] = []string{}
}
}
}
}
func SingleCheck(finger structs.FingerPEntity, Protocol string, headerString string, body string,
Server string, Title string, Cert string, Port int, Path string, Hash string, IconHash string, StatusCode int,
ContentType string, Banner string) bool {
rules := finger.Rule
expr := finger.AllString
for _, singleRule := range rules {
singleRuleResult := false
if singleRule.Key == "header" {
if dataCheckString(singleRule.Op, headerString, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "body" {
if dataCheckString(singleRule.Op, body, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "server" {
if dataCheckString(singleRule.Op, Server, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "title" {
if dataCheckString(singleRule.Op, Title, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "cert" {
if dataCheckString(singleRule.Op, Cert, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "port" {
value, err := strconv.Atoi(singleRule.Value)
if err == nil && dataCheckInt(singleRule.Op, Port, value) {
singleRuleResult = true
}
} else if singleRule.Key == "protocol" {
if singleRule.Op == 0 {
if Protocol == singleRule.Value {
singleRuleResult = true
}
} else if singleRule.Op == 1 {
if Protocol != singleRule.Value {
singleRuleResult = true
}
}
} else if singleRule.Key == "path" {
if dataCheckString(singleRule.Op, Path, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "body_hash" {
if dataCheckString(singleRule.Op, Hash, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "icon_hash" {
value, err := strconv.Atoi(singleRule.Value)
hashIcon, errHash := strconv.Atoi(IconHash)
if err == nil && errHash == nil && dataCheckInt(singleRule.Op, hashIcon, value) {
singleRuleResult = true
}
} else if singleRule.Key == "status" {
value, err := strconv.Atoi(singleRule.Value)
if err == nil && dataCheckInt(singleRule.Op, StatusCode, value) {
singleRuleResult = true
}
} else if singleRule.Key == "content_type" {
if dataCheckString(singleRule.Op, ContentType, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "banner" {
if dataCheckString(singleRule.Op, Banner, singleRule.Value) {
singleRuleResult = true
}
} else if singleRule.Key == "type" {
if singleRule.Value == "service" {
singleRuleResult = true
}
}
if singleRuleResult {
expr = expr[:singleRule.Start] + "T" + expr[singleRule.End:]
} else {
expr = expr[:singleRule.Start] + "F" + expr[singleRule.End:]
}
}
return boolEval(expr)
}