/
crawler.go
231 lines (185 loc) · 5.96 KB
/
crawler.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
package crawler
import (
"fmt"
"net/url"
"strings"
"sync"
"time"
"github.com/ditashi/jsbeautifier-go/jsbeautifier"
"github.com/evilsocket/islazy/tui"
"github.com/gocolly/colly"
"gopkg.in/resty.v1"
"mvdan.cc/xurls"
"github.com/muraenateam/muraena/proxy"
"github.com/muraenateam/muraena/session"
)
const (
// Name of this module
Name = "crawler"
// Description of this module
Description = "Crawls the target domain in order to retrieve most of the target external origins"
// Author of this module
Author = "Muraena Team"
)
// Crawler module
type Crawler struct {
session.SessionModule
Enabled bool
Depth int
UpTo int
}
var (
crawledDomains []string
subdomains, uniqueDomains []string
externalOrigins []string
baseDom string
waitGroup sync.WaitGroup
discoveredJsUrls []string
)
// Name returns the module name
func (module *Crawler) Name() string {
return Name
}
// Description returns the module description
func (module *Crawler) Description() string {
return Description
}
// Author returns the module author
func (module *Crawler) Author() string {
return Author
}
// Prompt prints module status based on the provided parameters
func (module *Crawler) Prompt(what string) {
}
// Load configures the module by initializing its main structure and variables
func Load(s *session.Session) (m *Crawler, err error) {
config := s.Config.Crawler
m = &Crawler{
SessionModule: session.NewSessionModule(Name, s),
Enabled: config.Enabled,
UpTo: config.UpTo,
Depth: config.Depth,
}
// Armor domains
config.ExternalOrigins = proxy.ArmorDomain(config.ExternalOrigins)
if !m.Enabled {
m.Debug("is disabled")
return
}
m.explore()
waitGroup.Wait()
config.ExternalOrigins = proxy.ArmorDomain(crawledDomains)
// save new config file with externalDomains to prevent crawling at next start
m.Info("Domain crawling stats:")
err = s.UpdateConfiguration(&config.ExternalOrigins, &subdomains, &uniqueDomains)
return
}
func (module *Crawler) explore() {
var config *session.Configuration
config = module.Session.Config
module.Info("Starting exploration of %s (crawlDepth:%d crawlMaxReq: %d), just a few seconds...",
config.Proxy.Target, module.Depth, module.UpTo)
c := colly.NewCollector(
colly.UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0"),
// MaxDepth is by default 1, so only the links on the scraped page
// is visited, and no further links are followed
colly.MaxDepth(module.Depth), // first page and also links in second pages
//colly.AllowedDomains(Config.Target),
)
numVisited := 0
c.OnRequest(func(r *colly.Request) {
numVisited++
if numVisited > module.UpTo {
//module.Info("Ending exploration...")
r.Abort()
return
}
})
c.OnHTML("script[src]", func(e *colly.HTMLElement) {
res := e.Attr("src")
if module.appendExternalDomain(res, &crawledDomains) {
// if it is a script from an external domain, make sure to fetch it
// beautify it and see it we need to replace things
waitGroup.Add(1)
go module.fetchJS(&waitGroup, res, config.Proxy.Target, &crawledDomains)
}
})
// all other tags with src attribute (img/video/iframe/etc..)
c.OnHTML("[src]", func(e *colly.HTMLElement) {
res := e.Attr("src")
module.appendExternalDomain(res, &crawledDomains)
})
c.OnHTML("link[href]", func(e *colly.HTMLElement) {
res := e.Attr("href")
module.appendExternalDomain(res, &crawledDomains)
})
c.OnHTML("meta[content]", func(e *colly.HTMLElement) {
res := e.Attr("content")
module.appendExternalDomain(res, &crawledDomains)
})
// Callback for links on scraped pages
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
res := e.Attr("href")
module.appendExternalDomain(res, &crawledDomains)
// crawl
if err := c.Visit(e.Request.AbsoluteURL(res)); err != nil {
// module.Debug("[Colly Visit]%s", err)
}
})
if err := c.Limit(&colly.LimitRule{DomainGlob: "*", RandomDelay: 500 * time.Millisecond}); err != nil {
module.Warning("[Colly Limit]%s", err)
}
c.OnResponse(func(r *colly.Response) {})
c.OnRequest(func(r *colly.Request) {})
dest := fmt.Sprintf("%s%s", config.Protocol, config.Proxy.Target)
err := c.Visit(dest)
if err != nil {
module.Info("Exploration error visiting %s: %s", dest, tui.Red(err.Error()))
}
}
func (module *Crawler) fetchJS(waitGroup *sync.WaitGroup, res string, dest string, crawledDomains *[]string) {
defer waitGroup.Done()
u, _ := url.Parse(res)
if u.Scheme == "" {
u.Scheme = "https://"
res = "https:" + res
}
nu := fmt.Sprintf("%s%s", u.Host, u.Path)
if !Contains(&discoveredJsUrls, nu) {
discoveredJsUrls = append(discoveredJsUrls, nu)
module.Info("Fetching new JS URL: %s", nu)
resp, err := resty.R().Get(res)
if err != nil {
module.Error("Error fetching JS at %s: %s", res, err)
}
body := string(resp.Body())
opts := jsbeautifier.DefaultOptions()
beautyBody, err := jsbeautifier.Beautify(&body, opts)
if err != nil {
module.Error("Error beautifying JS at %s", res)
}
jsUrls := xurls.Strict().FindAllString(beautyBody, -1)
if len(jsUrls) > 0 && len(jsUrls) < 100 { // prevent cases where we have a lots of domains
for _, jsURL := range jsUrls {
module.appendExternalDomain(jsURL, crawledDomains)
}
module.Info("Domains found in JS at %s: %d", res, len(jsUrls))
}
}
}
func (module *Crawler) appendExternalDomain(res string, crawledDomains *[]string) bool {
if strings.HasPrefix(res, "//") || strings.HasPrefix(res, "https://") || strings.HasPrefix(res, "http://") {
u, err := url.Parse(res)
if err != nil {
module.Error("url.Parse error, skipping external domain %s: %s", res, err)
return false
}
// update the crawledDomains after doing some minimal checks that might happen from xurls when
// parsing urls from JS files
if len(u.Host) > 2 && (strings.Contains(u.Host, ".") || strings.Contains(u.Host, ":")) {
*crawledDomains = append(*crawledDomains, u.Host)
}
return true
}
return false
}