-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
287 lines (241 loc) · 6.94 KB
/
flags.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
package flags
import (
"crypto/tls"
"flag"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"
mapset "github.com/deckarep/golang-set/v2"
"github.com/starkzarn/gau/v2/pkg/providers"
"github.com/lynxsecurity/pflag"
"github.com/lynxsecurity/viper"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)
type URLScanConfig struct {
Host string `mapstructure:"host"`
APIKey string `mapstructure:"apikey"`
}
type Config struct {
Filters providers.Filters `mapstructure:"filters"`
Proxy string `mapstructure:"proxy"`
Threads uint `mapstructure:"threads"`
Timeout uint `mapstructure:"timeout"`
Verbose bool `mapstructure:"verbose"`
MaxRetries uint `mapstructure:"retries"`
IncludeSubdomains bool `mapstructure:"subdomains"`
RemoveParameters bool `mapstructure:"parameters"`
Providers []string `mapstructure:"providers"`
Blacklist []string `mapstructure:"blacklist"`
JSON bool `mapstructure:"json"`
URLScan URLScanConfig `mapstructure:"urlscan"`
OTX string `mapstructure:"otx"`
Outfile string // output file to write to
}
func (c *Config) ProviderConfig() (*providers.Config, error) {
var dialer fasthttp.DialFunc
if c.Proxy != "" {
parse, err := url.Parse(c.Proxy)
if err != nil {
return nil, fmt.Errorf("proxy url: %v", err)
}
switch parse.Scheme {
case "http":
dialer = fasthttpproxy.FasthttpHTTPDialer(strings.ReplaceAll(c.Proxy, "http://", ""))
case "socks5":
dialer = fasthttpproxy.FasthttpSocksDialer(c.Proxy)
default:
return nil, fmt.Errorf("unsupported proxy scheme: %s", parse.Scheme)
}
}
pc := &providers.Config{
Threads: c.Threads,
Timeout: c.Timeout,
MaxRetries: c.MaxRetries,
IncludeSubdomains: c.IncludeSubdomains,
RemoveParameters: c.RemoveParameters,
Client: &fasthttp.Client{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
Dial: dialer,
},
Providers: c.Providers,
Output: c.Outfile,
JSON: c.JSON,
URLScan: providers.URLScan{
Host: c.URLScan.Host,
APIKey: c.URLScan.APIKey,
},
OTX: c.OTX,
}
log.SetLevel(log.ErrorLevel)
if c.Verbose {
log.SetLevel(log.InfoLevel)
}
pc.Blacklist = mapset.NewThreadUnsafeSet(c.Blacklist...)
pc.Blacklist.Add("")
return pc, nil
}
type Options struct {
viper *viper.Viper
}
func New() *Options {
v := viper.New()
pflag.String("o", "", "filename to write results to")
pflag.Uint("threads", 1, "number of workers to spawn")
pflag.Uint("timeout", 45, "timeout (in seconds) for HTTP client")
pflag.Uint("retries", 0, "retries for HTTP client")
pflag.String("proxy", "", "http proxy to use")
pflag.StringSlice("blacklist", []string{}, "list of extensions to skip")
pflag.StringSlice("providers", []string{}, "list of providers to use (wayback,commoncrawl,otx,urlscan)")
pflag.Bool("subs", false, "include subdomains of target domain")
pflag.Bool("fp", false, "remove different parameters of the same endpoint")
pflag.Bool("verbose", false, "show verbose output")
pflag.Bool("json", false, "output as json")
// filter flags
pflag.StringSlice("mc", []string{}, "list of status codes to match")
pflag.StringSlice("fc", []string{}, "list of status codes to filter")
pflag.StringSlice("mt", []string{}, "list of mime-types to match")
pflag.StringSlice("ft", []string{}, "list of mime-types to filter")
pflag.String("from", "", "fetch urls from date (format: YYYYMM)")
pflag.String("to", "", "fetch urls to date (format: YYYYMM)")
pflag.Bool("version", false, "show gau version")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
if err := v.BindPFlags(pflag.CommandLine); err != nil {
log.Fatal(err)
}
return &Options{viper: v}
}
func Args() []string {
return pflag.Args()
}
func (o *Options) ReadInConfig() (*Config, error) {
home, err := os.UserHomeDir()
if err != nil {
return o.DefaultConfig(), err
}
confFile := filepath.Join(home, ".gau.toml")
return o.ReadConfigFile(confFile)
}
func (o *Options) ReadConfigFile(name string) (*Config, error) {
o.viper.SetConfigFile(name)
if err := o.viper.ReadInConfig(); err != nil {
return o.DefaultConfig(), err
}
var c Config
if err := o.viper.Unmarshal(&c); err != nil {
return o.DefaultConfig(), err
}
o.getFlagValues(&c)
return &c, nil
}
func (o *Options) DefaultConfig() *Config {
c := &Config{
Filters: providers.Filters{},
Proxy: "",
Timeout: 45,
Threads: 1,
Verbose: false,
MaxRetries: 5,
IncludeSubdomains: false,
RemoveParameters: false,
Providers: []string{"wayback", "commoncrawl", "otx", "urlscan"},
Blacklist: []string{},
JSON: false,
Outfile: "",
}
o.getFlagValues(c)
return c
}
func (o *Options) getFlagValues(c *Config) {
version := o.viper.GetBool("version")
verbose := o.viper.GetBool("verbose")
json := o.viper.GetBool("json")
retries := o.viper.GetUint("retries")
proxy := o.viper.GetString("proxy")
outfile := o.viper.GetString("o")
fetchers := o.viper.GetStringSlice("providers")
threads := o.viper.GetUint("threads")
blacklist := o.viper.GetStringSlice("blacklist")
subs := o.viper.GetBool("subs")
fp := o.viper.GetBool("fp")
if version {
fmt.Printf("gau version: %s\n", providers.Version)
os.Exit(0)
}
if proxy != "" {
c.Proxy = proxy
}
if outfile != "" {
c.Outfile = outfile
}
// set if --threads flag is set, otherwise use default
if threads > 1 {
c.Threads = threads
}
// set if --blacklist flag is specified, otherwise use default
if len(blacklist) > 0 {
c.Blacklist = blacklist
}
// set if --providers flag is specified, otherwise use default
if len(fetchers) > 0 {
c.Providers = fetchers
}
if retries > 0 {
c.MaxRetries = retries
}
if subs {
c.IncludeSubdomains = subs
}
if fp {
c.RemoveParameters = fp
}
c.JSON = json
c.Verbose = verbose
// get filter flags
mc := o.viper.GetStringSlice("mc")
fc := o.viper.GetStringSlice("fc")
mt := o.viper.GetStringSlice("mt")
ft := o.viper.GetStringSlice("ft")
from := o.viper.GetString("from")
to := o.viper.GetString("to")
var seenFilterFlag bool
var filters providers.Filters
if len(mc) > 0 {
seenFilterFlag = true
filters.MatchStatusCodes = mc
}
if len(fc) > 0 {
seenFilterFlag = true
filters.FilterStatusCodes = fc
}
if len(mt) > 0 {
seenFilterFlag = true
filters.MatchMimeTypes = mt
}
if len(ft) > 0 {
seenFilterFlag = true
filters.FilterMimeTypes = ft
}
if from != "" {
seenFilterFlag = true
if _, err := time.Parse("200601", from); err == nil {
filters.From = from
}
}
if to != "" {
seenFilterFlag = true
if _, err := time.Parse("200601", to); err == nil {
filters.To = to
}
}
if seenFilterFlag {
c.Filters = filters
}
}