This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 233
/
gryffin.go
425 lines (361 loc) · 10.4 KB
/
gryffin.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
// Copyright 2015, Yahoo Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package gryffin is an application scanning infrastructure.
*/
package gryffin
import (
"bytes"
"encoding/json"
"fmt"
"hash/fnv"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
distance "github.com/yahoo/gryffin/html-distance"
)
// A Scan consists of the job, target, request and response.
type Scan struct {
// ID is a random ID to identify this particular scan.
// if ID is empty, this scan should not be performed (but record for rate limiting).
ID string
Job *Job
Request *http.Request
RequestBody string
Response *http.Response
ResponseBody string
Cookies []*http.Cookie
Fingerprint Fingerprint
HitCount int
}
// Job stores the job id and config (if any).
type Job struct {
ID string
DomainsAllowed []string // Domains that we would crawl
}
// Fingerprint contains all the different types of hash for the Scan (Request & Response)
type Fingerprint struct {
Origin uint64 // origin
URL uint64 // origin + path
Request uint64 // method, url, body
RequestFull uint64 // request + header
ResponseSimilarity uint64
}
// HTTPDoer interface is to be implemented by http.Client
type HTTPDoer interface {
Do(*http.Request) (*http.Response, error)
}
// Fuzzer runs the fuzzing.
type Fuzzer interface {
Fuzz(*Scan) (int, error)
}
// Renderer is an interface for implementation HTML DOM renderer and obtain the response body and links.
// Since DOM construction is very likely to be asynchronous, we return the channels to receive response and links.
type Renderer interface {
Do(*Scan)
GetRequestBody() <-chan *Scan
GetLinks() <-chan *Scan
}
// LogMessage contains the data fields to be marshalled as JSON for forwarding to the log processor.
type LogMessage struct {
Service string
Msg string
Method string
Url string
JobID string
// Fingerprint Fingerprint
}
// NewScan creates a scan.
func NewScan(method, url, post string) *Scan {
// ensure we got a memory store..
memoryStoreMu.Lock()
if memoryStore == nil {
memoryStore = NewGryffinStore()
}
memoryStoreMu.Unlock()
id := GenRandomID()
job := &Job{ID: GenRandomID()}
req, err := http.NewRequest(method, url, ioutil.NopCloser(strings.NewReader(post)))
if err != nil {
// s.Log("Invalid url for NewScan: %s", err)
return nil
}
// put the host component of the url as the domains to be allowed
host, _, err := net.SplitHostPort(req.URL.Host)
if err != nil {
job.DomainsAllowed = []string{req.URL.Host}
} else {
job.DomainsAllowed = []string{host}
}
// Add chrome user agent
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36")
return &Scan{
ID: id,
Job: job,
Request: req,
RequestBody: post,
}
}
// getOrigin returns the Origin of the URL (scheme, hostname, port )
func getOrigin(u *url.URL) string {
return u.Scheme + "://" + u.Host
}
// MergeRequest merge the request field in scan with the existing one.
func (s *Scan) MergeRequest(req *http.Request) {
// set cookie from response (if it is not done..)
if s.Response != nil {
s.Cookies = append(s.Cookies, s.Response.Cookies()...)
// s.CookieJar.SetCookies(s.Request.URL, s.Response.Cookies())
}
// read the request body, and then reset the reader
var post []byte
if req.Body != nil {
if post, err := ioutil.ReadAll(req.Body); err == nil {
req.Body = ioutil.NopCloser(bytes.NewReader(post))
} else {
// only possible error is bytes.ErrTooLarge from ioutil package.
s.Error("MergeRequest", err)
}
}
// resolve relative url.
if !req.URL.IsAbs() {
req.URL = s.Request.URL.ResolveReference(req.URL)
}
// TODO - drop if Method, URL, Body are same..
// if req == s.Request {
// s.Logf("Result after merge generate same request.", nil)
// }
// swap
prevReq := s.Request
s.Request = req
s.RequestBody = string(post)
// TODO - handle relative URL .
// Create a cookie jar, add cookie list (so cookie jar reject invalid cookie.)
jar, _ := cookiejar.New(nil)
jar.SetCookies(req.URL, s.Cookies)
// reset cookies
s.Cookies = make([]*http.Cookie, 0)
for _, c := range jar.Cookies(req.URL) {
req.AddCookie(c)
s.Cookies = append(s.Cookies, c)
}
// Add user agent
req.Header.Set("User-Agent", prevReq.UserAgent())
// Add referrer - TODO, perhaps we don't need this!
// remove Response.
s.Response = nil
s.ResponseBody = ""
}
// Spawn spawns a new scan object with a different ID.
func (s *Scan) Spawn() *Scan {
id := GenRandomID()
job := *s.Job
req := *s.Request // copy the value.
post := s.RequestBody
s.Request.Body = ioutil.NopCloser(strings.NewReader(post))
// get the cookiejar, save the new cookies
// jar := s.CookieJar
cookies := s.Cookies[:]
if s.Response != nil {
cookies = append(cookies, s.Response.Cookies()...)
// jar.SetCookies(s.Request.URL, s.Response.Cookies())
}
return &Scan{
ID: id,
Job: &job,
Request: &req,
RequestBody: post,
Cookies: cookies,
}
}
// Poke checks if the target is up.
func (s *Scan) Poke(client HTTPDoer) (err error) {
s.Logm("Poke", "Poking")
// Add 5s timeout if it is http.Client
switch client := client.(type) {
case *http.Client:
client.Timeout = time.Duration(3) * time.Second
}
// delete the similarity case for the domain.
// s.Session.DelPrefix("hash/unique/" + s.Request.URL.Host)
// http.Request is embeded in a Request embeded in a Scan.
s.Response, err = client.Do(s.Request)
if err != nil {
s.Logm("Poke", "Failed")
return
}
s.ReadResponseBody()
s.HitCount++
return
}
// ReadResponseBody read Response.Body and fill it to ReadResponseBody.
// It will also reconstruct the io.ReaderCloser stream.
func (s *Scan) ReadResponseBody() {
if s.ResponseBody == "" && s.Response != nil {
if b, err := ioutil.ReadAll(s.Response.Body); err == nil {
s.ResponseBody = string(b)
s.Response.Body = ioutil.NopCloser(bytes.NewReader(b))
}
}
}
func hash(s string) uint64 {
h := fnv.New64()
h.Write([]byte(s))
return h.Sum64()
}
// UpdateFingerprint updates the fingerprint field.
func (s *Scan) UpdateFingerprint() {
f := &s.Fingerprint
if s.Request != nil {
if f.Origin == 0 {
f.Origin = hash(getOrigin(s.Request.URL))
}
if f.URL == 0 {
f.URL = hash(s.Request.URL.String())
}
if f.Request == 0 {
f.Request = hash(s.Request.URL.String() + "\n" + s.RequestBody)
}
// if f.RequestFull == 0 {
// TODO
// }
}
if f.ResponseSimilarity == 0 {
if r := strings.NewReader(s.ResponseBody); s.ResponseBody != "" && r != nil {
f.ResponseSimilarity = distance.Fingerprint(r, 3)
s.Logm("Fingerprint", "Computed")
}
}
}
// RateLimit checks whether we are under the allowed rate for crawling the site.
// It returns a delay time to wait to check for ReadyToCrawl again.
func (s *Scan) RateLimit() int {
if memoryStore.Hit(s.Request.URL.Host) {
return 0
}
return 5
// store := s.Session
// // for each 5 second epoch, we create a key and see how many crawls are done.
// ts := time.Now().Truncate(5 * time.Second).Unix()
// k := "rate/" + s.Request.URL.Host + "/" + strconv.FormatInt(ts, 10)
// if v, ok := store.Get(k); ok {
// if v.(int64) >= 5 {
// // s.Logm("RateLimit", "Delay 5 second")
// // s.Logf("Wait for 5 second for %s (v:%d)", s.Request.URL, v)
// return 5
// }
// // ready to crawl.
// // TODO - this is not atomic.
// c, _ := store.Get(k)
// store.Set(k, c.(int64)+1)
// // s.Logm("RateLimit", "No Delay")
// return 0
// }
// store.Set(k, 1)
// // s.Logm("RateLimit", "No Delay")
// return 0
}
// IsScanAllowed check if the request URL is allowed per Job.DomainsAllowed.
func (s *Scan) IsScanAllowed() bool {
// relative URL
if !s.Request.URL.IsAbs() {
return true
}
host, _, err := net.SplitHostPort(s.Request.URL.Host)
if err != nil {
host = s.Request.URL.Host
}
for _, allowed := range s.Job.DomainsAllowed {
if host == allowed {
return true
}
}
return false
}
// CrawlAsync run the crawling asynchronously.
func (s *Scan) CrawlAsync(r Renderer) {
s.Logm("CrawlAsync", "Started")
if s.IsScanAllowed() {
r.Do(s)
} else {
s.Logm("CrawlAsync", "Scan Not Allowed")
}
}
// IsDuplicatedPage checks if we should proceed based on the Response
func (s *Scan) IsDuplicatedPage() bool {
s.UpdateFingerprint()
f := s.Fingerprint.ResponseSimilarity
if !memoryStore.Seen(s.Job.ID, "oracle", f, 2) {
memoryStore.See(s.Job.ID, "oracle", f)
s.Logm("IsDuplicatedPage", "Unique Page")
return false
}
s.Logm("IsDuplicatedPage", "Duplicate Page")
return true
}
// Fuzz runs the vulnerability fuzzer, return the issue count.
func (s *Scan) Fuzz(fuzzer Fuzzer) (int, error) {
c, err := fuzzer.Fuzz(s)
return c, err
}
// // ExtractLinks extracts the list of links found from the responseText in the Scan.
// func (s *Scan) ExtractLinks() (scans []Scan, err error) {
// return
// }
// ShouldCrawl checks if the links should be queued for next crawl.
func (s *Scan) ShouldCrawl() bool {
s.UpdateFingerprint()
f := s.Fingerprint.URL
if !memoryStore.Seen(s.Job.ID, "hash", f, 0) {
memoryStore.See(s.Job.ID, "hash", f)
s.Logm("ShouldCrawl", "Unique Link")
return true
}
s.Logm("ShouldCrawl", "Duplicate Link")
return false
}
// TODO - LogFmt (fmt string)
// TODO - LogI (interface)
// Error logs the error for the given service.
func (s *Scan) Error(service string, err error) {
errmsg := fmt.Sprint(err)
s.Logm(service, errmsg)
}
// Logmf logs the message for the given service.
func (s *Scan) Logmf(service, format string, a ...interface{}) {
s.Logm(service, fmt.Sprintf(format, a...))
}
// Logm sends a LogMessage to Log processor.
func (s *Scan) Logm(service, msg string) {
// TODO - improve the efficiency of this.
m := &LogMessage{
Service: service,
Msg: msg,
// Fingerprint: s.Fingerprint,
Method: s.Request.Method,
Url: s.Request.URL.String(),
JobID: s.Job.ID,
}
s.Log(m)
}
// Logf logs using the given format string.
func (s *Scan) Logf(format string, a ...interface{}) {
str := fmt.Sprintf(format, a...)
s.Log(str)
}
// Log encodes the given argument as JSON and writes it to
// the log writer.
func (s *Scan) Log(v interface{}) {
if logWriter == nil {
return
}
logWriterMu.Lock()
encoder := json.NewEncoder(logWriter)
encoder.Encode(v)
logWriterMu.Unlock()
}