-
Notifications
You must be signed in to change notification settings - Fork 46
/
check.go
298 lines (277 loc) · 7.19 KB
/
check.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
// Package crawlerx
// @Author bcy2007 2023/7/12 17:31
package crawlerx
import (
"github.com/yaklang/yaklang/common/crawlerx/tools"
"github.com/yaklang/yaklang/common/go-funk"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/yak/yaklib/codec"
u "net/url"
"regexp"
"strings"
)
func repeatCheckFunctionGenerate(pageVisitFilter *tools.StringCountFilter) func(string) bool {
return func(urlStr string) bool {
if urlStr == "" {
return false
}
sha256Url := codec.Sha256(urlStr)
if pageVisitFilter.Exist(sha256Url) {
return false
}
pageVisitFilter.Insert(sha256Url)
return true
}
}
func repeatCheckGenerator(level repeatLevel, extraParams ...string) func(HijackRequest) string {
switch level {
case extremeLevel:
return extremeLevelRepeatCheckGenerator()
case highLevel:
return highLevelRepeatCheckGenerator()
case midLevel:
return midLevelRepeatCheckGenerator(extraParams...)
case lowLevel:
return lowLevelRepeatCheckGenerator(extraParams...)
case unlimited:
return unLimitLevelRepeatCheckGenerator(extraParams...)
}
return nil
}
func urlRepeatCheckGenerator(level repeatLevel, extraParams ...string) func(string) string {
checkF := doCheck(extraParams...)
switch level {
case extremeLevel, highLevel:
return func(origin string) string {
url, err := u.Parse(origin)
if err != nil {
log.Errorf("url %s parse error: %s", origin, err)
return ""
}
return getUrlPage(url)
}
case midLevel:
return func(origin string) string {
url, err := u.Parse(origin)
if err != nil {
log.Errorf("url %s parse error: %s", origin, err)
return ""
}
return getUrlQueryName(url, checkF)
}
case lowLevel, unlimited:
return func(origin string) string {
url, err := u.Parse(origin)
if err != nil {
log.Errorf("url %s parse error: %s", origin, err)
return ""
}
return getUrlQueryValue(url, checkF)
}
}
return func(_ string) string {
return ""
}
}
func extremeLevelRepeatCheckGenerator() func(HijackRequest) string {
return func(request HijackRequest) string {
parsed := request.URL()
return getUrlPage(parsed)
}
}
func highLevelRepeatCheckGenerator() func(HijackRequest) string {
return func(request HijackRequest) string {
parsed := request.URL()
return request.Method() + " " + getUrlPage(parsed)
}
}
func midLevelRepeatCheckGenerator(extraParams ...string) func(HijackRequest) string {
checkF := doCheck(extraParams...)
return func(request HijackRequest) string {
parsed := request.URL()
return request.Method() + " " + getUrlQueryName(parsed, checkF)
}
}
func lowLevelRepeatCheckGenerator(extraParams ...string) func(HijackRequest) string {
if len(extraParams) == 0 {
return func(request HijackRequest) string {
return request.Method() + " " + request.URL().String()
}
}
checkF := doCheck(extraParams...)
return func(request HijackRequest) string {
parsed := request.URL()
return request.Method() + " " + getUrlQueryValue(parsed, checkF)
}
}
func unLimitLevelRepeatCheckGenerator(extraParams ...string) func(HijackRequest) string {
if len(extraParams) == 0 {
return func(request HijackRequest) string {
result := request.Method() + " " + request.URL().String()
if request.Body() != "" {
result += " " + request.Body()
}
return result
}
}
checkF := doCheck(extraParams...)
return func(request HijackRequest) string {
parsed := request.URL()
result := request.Method() + " " + getUrlQueryValue(parsed, checkF)
if request.Body() != "" {
result += " " + request.Body()
}
return result
}
}
//
// scan range
//
func scanRangeFunctionGenerate(targetUrl string, scanRange scanRangeLevel) func(string) bool {
rangeFunction, ok := generalScanRangeMap[scanRange]
if !ok {
return nil
}
rangeUrl := rangeFunction(targetUrl)
return func(checkUrl string) bool {
if StringSuffixList(checkUrl, extraUrlKeywords) {
return true
}
if StringPrefixList(checkUrl, rangeUrl) {
return true
}
return false
}
}
func generalMainDomainRange(targetUrl string) []string {
url, _ := u.Parse(targetUrl)
ranges := make([]string, 0)
ranges = append(ranges, url.Scheme+"://"+url.Host)
if !strings.HasPrefix(url.Host, "www.") {
ranges = append(ranges, url.Scheme+"://www."+url.Host)
}
return ranges
}
func generalSubDomainRange(targetUrl string) []string {
url, _ := u.Parse(targetUrl)
ranges := make([]string, 0)
var path string
if strings.HasSuffix(url.Path, "/") {
path = url.Path
} else {
tempPaths := strings.Split(url.Path, "/")
path = strings.Join(tempPaths[0:len(tempPaths)-1], "/") + "/"
}
ranges = append(ranges, url.Scheme+"://"+url.Host+path)
if !strings.HasPrefix(url.Host, "www.") {
ranges = append(ranges, url.Scheme+"://www."+url.Host+path)
}
return ranges
}
func whiteListCheckGenerator(whitelist []*regexp.Regexp) func(string) bool {
return func(url string) bool {
for _, whiteReg := range whitelist {
if whiteReg.MatchString(url) {
return true
}
}
return false
}
}
func blackListCheckGenerator(blacklist []*regexp.Regexp) func(string) bool {
return func(url string) bool {
for _, blackReg := range blacklist {
if blackReg.MatchString(url) {
return false
}
}
return true
}
}
func doCheck(doCheck ...string) func(k string) bool {
if len(doCheck) == 0 {
return func(k string) bool {
return false
}
}
return func(k string) bool {
return funk.Contains(doCheck, k)
}
}
func getUrlPage(url *u.URL) string {
urlStr := url.Scheme + "://" + url.Host + url.Path
if url.Fragment != "" {
urlStr += "#" + url.Fragment
}
return urlStr
}
func getUrlQueryName(url *u.URL, check func(string) bool) string {
if url.RawQuery == "" {
return getUrlPage(url)
}
query, _ := GetSortedQuery(url.RawQuery)
queryLength := len(query)
queryItems := make([]string, 0)
for i := 0; i < queryLength-1; i += 2 {
if check(query[i]) {
continue
}
queryItems = append(queryItems, query[i])
}
return getUrlPage(url) + "?" + strings.Join(queryItems, "&")
}
func getUrlQueryValue(url *u.URL, check func(string) bool) string {
if url.RawQuery == "" {
return getUrlPage(url)
}
query, _ := GetSortedQuery(url.RawQuery)
queryLength := len(query)
queryItems := make([]string, 0)
for i := 0; i < queryLength-1; i += 2 {
if check(query[i]) {
continue
}
queryItems = append(queryItems, query[i]+"="+query[i+1])
}
return getUrlPage(url) + "?" + strings.Join(queryItems, "&")
}
func extraUrlCheck(extraSuffix []string) func(string) bool {
return func(urlStr string) bool {
return StringSuffixList(urlStr, extraSuffix)
}
}
func GetSortedQuery(rawQuery string) (query []string, err error) {
query = make([]string, 0)
if rawQuery == "" {
return
}
for rawQuery != "" {
var key string
key, rawQuery, _ = strings.Cut(rawQuery, "&")
if strings.Contains(key, ";") {
err = utils.Error("invalid semicolon separator in query")
continue
}
if key == "" {
continue
}
key, value, _ := strings.Cut(key, "=")
key, err1 := u.QueryUnescape(key)
if err1 != nil {
if err == nil {
err = utils.Error(err1)
}
continue
}
value, err1 = u.QueryUnescape(value)
if err1 != nil {
if err == nil {
err = utils.Error(err1)
}
continue
}
query = append(query, key, value)
}
return
}