forked from projectdiscovery/katana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
253 lines (223 loc) · 6.92 KB
/
base.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
package common
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/erikowen/katana/pkg/engine/parser"
"github.com/erikowen/katana/pkg/engine/parser/files"
"github.com/erikowen/katana/pkg/navigation"
"github.com/erikowen/katana/pkg/output"
"github.com/erikowen/katana/pkg/types"
"github.com/erikowen/katana/pkg/utils"
"github.com/erikowen/katana/pkg/utils/queue"
"github.com/go-rod/rod"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/retryablehttp-go"
errorutil "github.com/projectdiscovery/utils/errors"
mapsutil "github.com/projectdiscovery/utils/maps"
urlutil "github.com/projectdiscovery/utils/url"
"github.com/remeh/sizedwaitgroup"
)
type Shared struct {
Headers map[string]string
KnownFiles *files.KnownFiles
Options *types.CrawlerOptions
}
func NewShared(options *types.CrawlerOptions) (*Shared, error) {
shared := &Shared{
Headers: options.Options.ParseCustomHeaders(),
Options: options,
}
if options.Options.KnownFiles != "" {
httpclient, _, err := BuildHttpClient(options.Dialer, options.Options, nil)
if err != nil {
return nil, errorutil.New("could not create http client").Wrap(err)
}
shared.KnownFiles = files.New(httpclient, options.Options.KnownFiles)
}
return shared, nil
}
func (s *Shared) Enqueue(queue *queue.Queue, navigationRequests ...*navigation.Request) {
for _, nr := range navigationRequests {
if nr.URL == "" || !utils.IsURL(nr.URL) {
continue
}
reqUrl := nr.RequestURL()
if s.Options.Options.IgnoreQueryParams {
reqUrl = utils.ReplaceAllQueryParam(reqUrl, "")
}
// Ignore blank URL items and only work on unique items
if !s.Options.UniqueFilter.UniqueURL(reqUrl) && len(nr.CustomFields) == 0 {
continue
}
// - URLs stuck in a loop
if s.Options.UniqueFilter.IsCycle(nr.RequestURL()) {
continue
}
// skip crawling if the endpoint is not in scope
inScope := s.ValidateScope(nr.URL, nr.RootHostname)
if !inScope {
// if the user requested anyway out of scope items
// they are sent to output without visiting
if s.Options.Options.DisplayOutScope {
s.Output(nr, nil, ErrOutOfScope)
}
continue
}
// Do not add to crawl queue if max items are reached
if nr.Depth >= s.Options.Options.MaxDepth {
continue
}
queue.Push(nr, nr.Depth)
}
}
func (s *Shared) ValidateScope(URL string, root string) bool {
parsed, err := urlutil.Parse(URL)
if err != nil {
gologger.Warning().Msgf("failed to parse url while validating scope: %v", err)
return false
}
scopeValidated, err := s.Options.ScopeManager.Validate(parsed.URL, root)
return err == nil && scopeValidated
}
func (s *Shared) Output(navigationRequest *navigation.Request, navigationResponse *navigation.Response, err error) {
var errData string
if err != nil {
errData = err.Error()
}
// Write the found result to output
result := &output.Result{
Timestamp: time.Now(),
Request: navigationRequest,
Response: navigationResponse,
Error: errData,
}
_ = s.Options.OutputWriter.Write(result)
if s.Options.Options.OnResult != nil {
s.Options.Options.OnResult(*result)
}
}
type CrawlSession struct {
Ctx context.Context
CancelFunc context.CancelFunc
URL *url.URL
Hostname string
Queue *queue.Queue
HttpClient *retryablehttp.Client
Browser *rod.Browser
}
func (s *Shared) NewCrawlSessionWithURL(URL string) (*CrawlSession, error) {
ctx, cancel := context.WithCancel(context.Background())
if s.Options.Options.CrawlDuration.Seconds() > 0 {
//nolint
ctx, cancel = context.WithTimeout(ctx, s.Options.Options.CrawlDuration)
}
parsed, err := urlutil.Parse(URL)
if err != nil {
cancel()
return nil, errorutil.New("could not parse root URL").Wrap(err)
}
hostname := parsed.Hostname()
queue, err := queue.New(s.Options.Options.Strategy, s.Options.Options.Timeout)
if err != nil {
cancel()
return nil, err
}
queue.Push(&navigation.Request{Method: http.MethodGet, URL: URL, Depth: 0}, 0)
if s.KnownFiles != nil {
navigationRequests, err := s.KnownFiles.Request(URL)
if err != nil {
gologger.Warning().Msgf("Could not parse known files for %s: %s\n", URL, err)
}
s.Enqueue(queue, navigationRequests...)
}
httpclient, _, err := BuildHttpClient(s.Options.Dialer, s.Options.Options, func(resp *http.Response, depth int) {
body, _ := io.ReadAll(resp.Body)
reader, _ := goquery.NewDocumentFromReader(bytes.NewReader(body))
technologies := s.Options.Wappalyzer.Fingerprint(resp.Header, body)
navigationResponse := &navigation.Response{
Depth: depth + 1,
RootHostname: hostname,
Resp: resp,
Body: string(body),
Reader: reader,
Technologies: mapsutil.GetKeys(technologies),
StatusCode: resp.StatusCode,
Headers: utils.FlattenHeaders(resp.Header),
}
navigationRequests := parser.ParseResponse(navigationResponse)
s.Enqueue(queue, navigationRequests...)
})
if err != nil {
cancel()
return nil, errorutil.New("could not create http client").Wrap(err)
}
crawlSession := &CrawlSession{
Ctx: ctx,
CancelFunc: cancel,
URL: parsed.URL,
Hostname: hostname,
Queue: queue,
HttpClient: httpclient,
}
return crawlSession, nil
}
type DoRequestFunc func(crawlSession *CrawlSession, req *navigation.Request) (*navigation.Response, error)
func (s *Shared) Do(crawlSession *CrawlSession, doRequest DoRequestFunc) error {
wg := sizedwaitgroup.New(s.Options.Options.Concurrency)
for item := range crawlSession.Queue.Pop() {
if ctxErr := crawlSession.Ctx.Err(); ctxErr != nil {
return ctxErr
}
req, ok := item.(*navigation.Request)
if !ok {
continue
}
if !utils.IsURL(req.URL) {
gologger.Debug().Msgf("`%v` not a url. skipping", req.URL)
continue
}
if ok, err := s.Options.ValidateScope(req.URL, crawlSession.Hostname); err != nil || !ok {
gologger.Debug().Msgf("`%v` not in scope. skipping", req.URL)
continue
}
wg.Add()
// gologger.Debug().Msgf("Visting: %v", req.URL) // not sure if this is needed
go func() {
defer wg.Done()
s.Options.RateLimit.Take()
// Delay if the user has asked for it
if s.Options.Options.Delay > 0 {
time.Sleep(time.Duration(s.Options.Options.Delay) * time.Second)
}
resp, err := doRequest(crawlSession, req)
s.Output(req, resp, err)
if err != nil {
gologger.Warning().Msgf("Could not request seed URL %s: %s\n", req.URL, err)
outputError := &output.Error{
Timestamp: time.Now(),
Endpoint: req.RequestURL(),
Source: req.Source,
Error: err.Error(),
}
_ = s.Options.OutputWriter.WriteErr(outputError)
return
}
if resp.Resp == nil || resp.Reader == nil {
return
}
isRedirectResponse := resp.StatusCode >= 300 && resp.StatusCode < 400
if s.Options.Options.DisableRedirects && isRedirectResponse {
return
}
navigationRequests := parser.ParseResponse(resp)
s.Enqueue(crawlSession.Queue, navigationRequests...)
}()
}
wg.Wait()
return nil
}