forked from jaeles-project/gospider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (172 loc) · 6 KB
/
main.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
package main
import (
"bufio"
"fmt"
"github.com/jaeles-project/gospider/core"
"io/ioutil"
"net/url"
"os"
"strings"
"sync"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var commands = &cobra.Command{
Use: core.CLIName,
Long: fmt.Sprintf("Fast web spider written in Go - %v by %v", core.VERSION, core.AUTHOR),
Run: run,
}
func main() {
commands.Flags().StringP("site", "s", "", "Site to crawl")
commands.Flags().StringP("sites", "S", "", "Site list to crawl")
commands.Flags().StringP("proxy", "p", "", "Proxy (Ex: http://127.0.0.1:8080)")
commands.Flags().StringP("output", "o", "", "Output folder")
commands.Flags().StringP("user-agent", "u", "web", "User Agent to use\n\tweb: random web user-agent\n\tmobi: random mobile user-agent\n\tor you can set your special user-agent")
commands.Flags().StringP("cookie", "", "", "Cookie to use (testA=a; testB=b)")
commands.Flags().StringArrayP("header", "H", []string{}, "Header to use (Use multiple flag to set multiple header)")
commands.Flags().StringP("burp", "", "", "Load headers and cookie from burp raw http request")
commands.Flags().StringP("blacklist", "", "", "Blacklist URL Regex")
commands.Flags().IntP("threads", "t", 1, "Number of threads (Run sites in parallel)")
commands.Flags().IntP("concurrent", "c", 5, "The number of the maximum allowed concurrent requests of the matching domains")
commands.Flags().IntP("depth", "d", 1, "MaxDepth limits the recursion depth of visited URLs. (Set it to 0 for infinite recursion)")
commands.Flags().IntP("delay", "k", 0, "Delay is the duration to wait before creating a new request to the matching domains (second)")
commands.Flags().IntP("random-delay", "K", 0, "RandomDelay is the extra randomized duration to wait added to Delay before creating a new request (second)")
commands.Flags().IntP("timeout", "m", 10, "Request timeout (second)")
commands.Flags().BoolP("sitemap", "", false, "Try to crawl sitemap.xml")
commands.Flags().BoolP("robots", "", true, "Try to crawl robots.txt")
commands.Flags().BoolP("other-source", "a", false, "Find URLs from 3rd party (Archive.org, CommonCrawl.org, VirusTotal.com, AlienVault.com)")
commands.Flags().BoolP("include-subs", "w", false, "Include subdomains crawled from 3rd party. Default is main domain")
commands.Flags().BoolP("include-other-source", "r", false, "Also include other-source's urls (still crawl and request)")
commands.Flags().BoolP("debug", "", false, "Turn on debug mode")
commands.Flags().BoolP("verbose", "v", false, "Turn on verbose")
commands.Flags().BoolP("no-redirect", "", false, "Disable redirect")
commands.Flags().BoolP("version", "", false, "Check version")
commands.Flags().SortFlags = false
if err := commands.Execute(); err != nil {
core.Logger.Error(err)
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) {
if cmd.Flags().NFlag() == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
version, _ := cmd.Flags().GetBool("version")
if version {
fmt.Printf("Version: %s\n", core.VERSION)
os.Exit(0)
}
isDebug, _ := cmd.Flags().GetBool("debug")
if isDebug {
core.Logger.SetLevel(logrus.DebugLevel)
} else {
core.Logger.SetLevel(logrus.InfoLevel)
}
verbose, _ := cmd.Flags().GetBool("verbose")
if !verbose && !isDebug {
core.Logger.SetOutput(ioutil.Discard)
}
// Create output folder when save file option selected
outputFolder, _ := cmd.Flags().GetString("output")
if outputFolder != "" {
if _, err := os.Stat(outputFolder); os.IsNotExist(err) {
_ = os.Mkdir(outputFolder, os.ModePerm)
}
}
// Parse sites input
var siteList []string
siteInput, _ := cmd.Flags().GetString("site")
if siteInput != "" {
siteList = append(siteList, siteInput)
}
sitesListInput, _ := cmd.Flags().GetString("sites")
if sitesListInput != "" {
sitesFile, err := os.Open(sitesListInput)
if err != nil {
core.Logger.Error(err)
os.Exit(1)
}
defer sitesFile.Close()
sc := bufio.NewScanner(sitesFile)
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if err := sc.Err(); err == nil && line != "" {
siteList = append(siteList, line)
}
}
}
// Check again to make sure at least one site in slice
if len(siteList) == 0 {
core.Logger.Info("No site in list. Please check your site input again")
os.Exit(1)
}
threads, _ := cmd.Flags().GetInt("threads")
sitemap, _ := cmd.Flags().GetBool("sitemap")
robots, _ := cmd.Flags().GetBool("robots")
otherSource, _ := cmd.Flags().GetBool("other-source")
includeSubs, _ := cmd.Flags().GetBool("include-subs")
includeOtherSourceResult, _ := cmd.Flags().GetBool("include-other-source")
var wg sync.WaitGroup
inputChan := make(chan string, threads)
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for rawSite := range inputChan {
site, err := url.Parse(rawSite)
if err != nil {
logrus.Errorf("Failed to parse %s: %s", rawSite, err)
continue
}
var siteWg sync.WaitGroup
crawler := core.NewCrawler(site, cmd)
siteWg.Add(1)
go func() {
crawler.Start()
defer siteWg.Done()
}()
// Brute force Sitemap path
if sitemap {
siteWg.Add(1)
go core.ParseSiteMap(site, crawler.Output, crawler.C, &siteWg)
}
// Find Robots.txt
if robots {
siteWg.Add(1)
go core.ParseRobots(site, crawler.Output, crawler.C, &siteWg)
}
if otherSource {
siteWg.Add(1)
go func() {
defer siteWg.Done()
urls := core.OtherSources(site.Hostname(), includeSubs)
for _, url := range urls {
url = strings.TrimSpace(url)
if len(url) == 0 {
continue
}
outputFormat := fmt.Sprintf("[other-sources] - %s", url)
if includeOtherSourceResult {
fmt.Println(outputFormat)
if crawler.Output != nil {
crawler.Output.WriteToFile(outputFormat)
}
}
_ = crawler.C.Visit(url)
}
}()
}
siteWg.Wait()
crawler.C.Wait()
crawler.LinkFinderCollector.Wait()
}
}()
}
for _, site := range siteList {
inputChan <- site
}
close(inputChan)
wg.Wait()
core.Logger.Info("Done!!!")
}