-
Notifications
You must be signed in to change notification settings - Fork 42
/
nmap-rule-parser.go
127 lines (106 loc) · 2.8 KB
/
nmap-rule-parser.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
package utils
import (
"bufio"
"bytes"
"github.com/yaklang/yaklang/common/log"
"regexp"
"strings"
)
type ProtoType string
var (
TCPProbe ProtoType = "tcp"
UDPProbe ProtoType = "udp"
probesRegex, _ = regexp.Compile(`Probe (UDP|TCP) (.*?) q\|(.*?)\|`)
matchedRegex, _ = regexp.Compile(`.*m\|(.*?)\|.*`)
)
type ProbeRule struct {
Type ProtoType
Payload []byte
Matched []*MatchedRule
}
type MatchedRule struct {
Matched *regexp.Regexp
}
func ParseNmapServiceMatchedRule(raw []byte) []*MatchedRule {
var rules []*MatchedRule
for line := range ParseLines(string(raw)) {
line = strings.TrimSpace(line)
// skip comment
if strings.HasPrefix(line, "#") || len(line) <= 0 {
continue
}
if strings.HasPrefix(strings.ToLower(line), "match") {
for _, raw := range matchedRegex.FindAllStringSubmatch(line, 1) {
r, err := regexp.Compile(string(raw[1]))
if err != nil {
log.Errorf("failed to compile rule[%s]: %s", line, err)
continue
}
rules = append(rules, &MatchedRule{
Matched: r,
})
}
} else if strings.HasPrefix(strings.ToLower(line), "probe") {
log.Error("failed to parse Probes(use ParseNmapServiceProbeRule)")
}
}
return rules
}
func ParseNmapServiceProbeRule(raw []byte) []*ProbeRule {
scanner := bufio.NewScanner(bytes.NewBuffer(raw))
scanner.Split(bufio.ScanLines)
var rules []*ProbeRule
var currentRule *ProbeRule
var matchedRules []*MatchedRule
for scanner.Scan() {
line := scanner.Bytes()
line = bytes.TrimSpace(line)
// skip comment
if bytes.HasPrefix(line, []byte("#")) || len(line) <= 0 {
continue
}
log.Infof("line: %s", line)
if bytes.HasPrefix(bytes.ToLower(line), []byte("match")) {
for _, raw := range matchedRegex.FindAllSubmatch(line, 1) {
r, err := regexp.Compile(string(raw[1]))
if err != nil {
log.Errorf("failed to compile rule[%s]: %s", line, err)
continue
}
matchedRules = append(matchedRules, &MatchedRule{
Matched: r,
})
}
} else if bytes.HasPrefix(line, []byte("Probe ")) {
if currentRule != nil {
currentRule.Matched = matchedRules[:]
rules = append(rules, currentRule)
// test
matchedRules = []*MatchedRule{}
currentRule = nil
}
currentRule = &ProbeRule{}
for _, results := range probesRegex.FindAllSubmatch(line, 1) {
protoRaw := string(results[1])
//productRaw := results[2]
payloadRaw := results[3]
switch protoRaw {
case "TCP":
currentRule.Type = TCPProbe
break
case "UDP":
currentRule.Type = UDPProbe
break
default:
continue
}
currentRule.Payload = ParseCStyleBinaryRawToBytes(payloadRaw)
}
}
}
if currentRule != nil && len(matchedRules) > 0 {
currentRule.Matched = matchedRules
rules = append(rules, currentRule)
}
return rules
}