/
fingerprint_nmaprule_match.go
182 lines (163 loc) · 4.43 KB
/
fingerprint_nmaprule_match.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
package fp
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/netx"
utils2 "github.com/yaklang/yaklang/common/utils"
"net"
"sync"
"time"
)
func tcpConnectionMaker(host string, port interface{}, proxy []string, timeout time.Duration) (net.Conn, error) {
return netx.DialTCPTimeout(timeout, utils2.HostPort(host, port), proxy...)
}
func (f *Matcher) matchWithContext(ctx context.Context, ip net.IP, port int, config *Config) (*MatchResult, error) {
host := ip.String()
result := &MatchResult{
Target: host,
Port: port,
State: UNKNOWN,
}
// 获取需要匹配的指纹
firstBlock, blocks, bestMode := GetRuleBlockByConfig(port, config)
if len(blocks) <= 0 && firstBlock == nil {
return nil, errors.New("empty rules is not allowed")
}
_ = bestMode
if firstBlock != nil {
if config.CanScanUDP() {
blocks = append(blocks, firstBlock)
} else if config.CanScanTCP() {
log.Debugf("%s - %v ", firstBlock.Probe.Name, firstBlock.Probe.Proto)
state, info, err := f.matchBlock(ctx, ip, port, firstBlock, config)
result.State = state
result.Fingerprint = info
if err != nil {
result.Reason = err.Error()
}
if (result.Fingerprint != nil && result.Fingerprint.Banner != "") || result.State == CLOSED {
return specialCase(result, config), nil
}
}
}
// active mode is allowed to send packet
if !config.ActiveMode {
return specialCase(result, config), nil
}
var collectResultLock = new(sync.Mutex)
var states []PortState
var infos []*FingerprintInfo
var errs []error
swgCon := config.ProbesConcurrentMax
if swgCon <= 1 {
swgCon = 1
}
var probeSwg = utils2.NewSizedWaitGroup(swgCon)
for _, block := range blocks {
block := block
if block == nil || block.Probe.Payload == "" {
continue
}
select {
case <-ctx.Done():
return specialCase(result, config), nil
default:
}
// 处理结果
probeSwg.Add()
go func() {
defer probeSwg.Done()
defer func() {
if err := recover(); err != nil {
log.Errorf("probe failed: %s", err)
}
}()
if block.Probe != nil {
if block.Probe.Rarity > config.RarityMax && !utils2.IntArrayContains(block.Probe.DefaultPorts, port) {
log.Debugf("filter probe[%v] for raritymax: %v current: %v", block.Probe.Name, config.RarityMax, block.Probe.Rarity)
return
}
}
log.Debugf("try %s probe[%v] rarity[%v] %#v", utils2.HostPort(host, port), block.Probe.Index, block.Probe.Rarity, block.Probe.Payload)
state, info, err := f.matchBlock(ctx, ip, port, block, config)
collectResultLock.Lock()
defer collectResultLock.Unlock()
states = append(states, state)
infos = append(infos, info)
if err != nil {
log.Errorf("fingerprint match met err: %s", err)
errs = append(errs, err)
}
}()
}
probeSwg.Wait()
if result.State != OPEN {
result.State = mergeStates(states...)
}
result.Fingerprint = mergeInfo(infos...)
if result.State != OPEN && len(errs) > 0 {
result.Reason = mergeError(errs...).Error()
}
if result.State == CLOSED {
return specialCase(result, config), nil
}
if result.Fingerprint != nil && result.Fingerprint.ServiceName != "tcp" && result.Fingerprint.ServiceName != "udp" {
return specialCase(result, config), nil
}
return specialCase(result, config), nil
}
func mergeStates(states ...PortState) PortState {
for _, state := range states {
if state == OPEN {
return OPEN
}
}
return CLOSED
}
// 当 ProbesMax 设置的比较大时,后探测的结果中可能是存在正确的指纹信息的
func mergeInfo(rawInfos ...*FingerprintInfo) *FingerprintInfo {
var info []*FingerprintInfo
for _, r := range rawInfos {
if r == nil {
continue
}
info = append(info, r)
}
if len(info) <= 0 {
return nil
}
if len(info) == 1 {
return info[0]
}
root := info[0]
for _, infoIns := range info[1:] {
if infoIns == nil {
continue
}
if infoIns.IP != root.IP {
continue
}
if infoIns.Port != root.Port {
continue
}
// 当有 Raw 字段时,代表和 match rule 匹配成功了
if len(root.Raw) == 0 && len(infoIns.Raw) != 0 {
root = infoIns
}
root.HttpFlows = append(root.HttpFlows, infoIns.HttpFlows...)
root.CPEs = append(root.CPEs, infoIns.CPEs...)
}
return root
}
func mergeError(info ...error) error {
if len(info) <= 0 {
return nil
}
msg := fmt.Sprintf("Merged Error: \n")
for index, i := range info {
msg += fmt.Sprintf(" %v. %v\n", index, i)
}
return errors.New(msg)
}