forked from s-rah/onionscan
/
onionspider.go
222 lines (190 loc) · 6.39 KB
/
onionspider.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
package spider
import (
"crypto/tls"
"fmt"
"github.com/s-rah/onionscan/config"
"github.com/s-rah/onionscan/model"
"github.com/s-rah/onionscan/report"
"golang.org/x/net/proxy"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
type OnionSpider struct {
client *http.Client
}
func (os *OnionSpider) Crawl(hiddenservice string, osc *config.OnionScanConfig, report *report.OnionScanReport) {
torDialer, err := proxy.SOCKS5("tcp", osc.TorProxyAddress, nil, proxy.Direct)
if err != nil {
osc.LogError(err)
}
basepath := osc.CrawlConfigs[hiddenservice].Base
if basepath == "" {
basepath = "/"
}
base, err := url.Parse("http://" + hiddenservice + basepath)
if err != nil {
osc.LogError(err)
return
}
transportConfig := &http.Transport{
Dial: torDialer.Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
cookieJar, _ := cookiejar.New(nil)
cookieJar.SetCookies(base, osc.Cookies)
os.client = &http.Client{
Transport: transportConfig,
Jar: cookieJar,
}
addCrawl := func(uri string, id int, err error) {
if err == nil {
report.Crawls[uri] = id
} else {
osc.LogError(err)
}
}
// Extract interesting details from the first page
result, id := osc.Database.HasCrawlRecord(base.String(), osc.RescanDuration)
if result {
osc.LogInfo("Already crawled URL recently - reusing existing crawl")
report.Crawls[base.String()] = id
} else {
osc.LogInfo(fmt.Sprintf("Starting to Scan Page: %s", base.String()))
id, err = os.GetPage(base.String(), base, osc, true)
addCrawl(base.String(), id, err)
}
var scanDir func(string)
scanDir = func(uri string) {
resourceURI, err := url.Parse(uri)
if err == nil {
term := strings.LastIndex(resourceURI.Path, "/")
if term > 0 {
if term == len(resourceURI.Path)-1 {
term = strings.LastIndex(resourceURI.Path[len(resourceURI.Path)-2:], "/")
}
potentialDirectory := NormalizeURI(resourceURI.Path[:term], base)
_, exists := report.Crawls[potentialDirectory]
if !exists {
result, cid := osc.Database.HasCrawlRecord(potentialDirectory, osc.RescanDuration)
if !result {
osc.LogInfo(fmt.Sprintf("Scanning Directory: %s", potentialDirectory))
id, err := os.GetPage(potentialDirectory, base, osc, false)
addCrawl(potentialDirectory, id, err)
scanDir(potentialDirectory)
} else {
osc.LogInfo(fmt.Sprintf("Already crawled %s (%s) recently - reusing existing crawl", resourceURI.Path[:term], potentialDirectory))
addCrawl(potentialDirectory, cid, nil)
}
}
}
}
}
processURI := func(uri string, base *url.URL) {
target, err := url.Parse(uri)
if err == nil && base.Host == target.Host {
normalizeTarget := NormalizeURI(target.String(), base)
_, exists := report.Crawls[normalizeTarget]
if strings.HasPrefix(target.Path, base.Path) && !exists {
result, cid := osc.Database.HasCrawlRecord(normalizeTarget, osc.RescanDuration)
if !result {
osc.LogInfo(fmt.Sprintf("Scanning URI: %s", target.String()))
id, err := os.GetPage(normalizeTarget, base, osc, true)
addCrawl(normalizeTarget, id, err)
scanDir(normalizeTarget)
} else {
osc.LogInfo(fmt.Sprintf("Already crawled %s (%s) recently - reusing existing crawl", target.String(), normalizeTarget))
addCrawl(normalizeTarget, cid, nil)
}
}
}
}
exclude := func(uri string) bool {
for _, rule := range osc.CrawlConfigs[hiddenservice].Exclude {
if strings.Contains(uri, rule) {
return true
}
}
return false
}
// Grab Server Status if it Exists
// We add it as a resource so we can pull any information out of it later.
mod_status, _ := url.Parse("http://" + hiddenservice + "/server-status")
osc.LogInfo(fmt.Sprintf("Scanning URI: %s", mod_status.String()))
id, err = os.GetPage(mod_status.String(), base, osc, true)
addCrawl(mod_status.String(), id, err)
// Grab Private Key if it Exists
// This would be a major security fail
private_key, _ := url.Parse("http://" + hiddenservice + "/private_key")
osc.LogInfo(fmt.Sprintf("Scanning URI: %s", private_key.String()))
id, err = os.GetPage(private_key.String(), base, osc, true)
addCrawl(private_key.String(), id, err)
processed := make(map[string]bool)
// The rest of the site
for i := 0; i < osc.Depth; i++ {
// Process all the images we can find
osc.LogInfo(fmt.Sprintf("Scanning Depth: %d", i))
// Copy to Prevent Map Updating from Influencing Depth
crawlMap := make(map[string]int)
for k, v := range report.Crawls {
crawlMap[k] = v
}
for url, id := range crawlMap {
_, exists := processed[url]
if !exists {
crawlRecord, _ := osc.Database.GetCrawlRecord(id)
for _, image := range crawlRecord.Page.Images {
if !exclude(image.Target) {
processURI(image.Target, base)
}
}
for _, anchor := range crawlRecord.Page.Anchors {
if !exclude(anchor.Target) {
processURI(anchor.Target, base)
}
}
for _, link := range crawlRecord.Page.Links {
if !exclude(link.Target) {
processURI(link.Target, base)
}
}
for _, script := range crawlRecord.Page.Scripts {
if !exclude(script.Target) {
processURI(script.Target, base)
}
}
processed[url] = true
}
}
}
}
func (os *OnionSpider) GetPage(uri string, base *url.URL, osc *config.OnionScanConfig, snapshot bool) (int, error) {
response, err := os.client.Get(uri)
// Sometimes Weird Things Happen
if err != nil {
page := model.Page{}
page.Status = -1
page.Snapshot = err.Error()
id, err := osc.Database.InsertCrawlRecord(uri, &page)
return id, err
}
defer response.Body.Close()
page := model.Page{}
if strings.Contains(response.Header.Get("Content-Type"), "text/html") {
page = ParsePage(response.Body, base, snapshot)
osc.LogInfo(fmt.Sprintf("Grabbed %d byte document", len(page.Snapshot)))
} else if strings.Contains(response.Header.Get("Content-Type"), "image/jpeg") {
page = SnapshotBinaryResource(response.Body)
osc.LogInfo(fmt.Sprintf("Fetched %d byte image", len(page.Raw)))
} else if snapshot {
page = SnapshotResource(response.Body)
osc.LogInfo(fmt.Sprintf("Grabbed %d byte document", len(page.Snapshot)))
} else {
osc.LogInfo(fmt.Sprintf("Content type of %s does not have a special handler: %v - minimal data will be collected", uri, response.Header["Content-Type"]))
}
page.Status = response.StatusCode
page.Headers = response.Header
id, err := osc.Database.InsertCrawlRecord(uri, &page)
return id, err
}