forked from projectdiscovery/uncover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuncover.go
296 lines (279 loc) · 10 KB
/
uncover.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
package uncover
import (
"context"
sliceutil "github.com/projectdiscovery/utils/slice"
anubis_spider "github.com/wjlin0/uncover/sources/agent/anubis-spider"
baidu_spider "github.com/wjlin0/uncover/sources/agent/baidu-spider"
"github.com/wjlin0/uncover/sources/agent/binaryedge"
bing_spider "github.com/wjlin0/uncover/sources/agent/bing-spider"
chinaz_spider "github.com/wjlin0/uncover/sources/agent/chinaz-spider"
"github.com/wjlin0/uncover/sources/agent/daydaymap"
fofa_spider "github.com/wjlin0/uncover/sources/agent/fofa-spider"
"github.com/wjlin0/uncover/sources/agent/fullhunt"
"github.com/wjlin0/uncover/sources/agent/github"
google_spider "github.com/wjlin0/uncover/sources/agent/google-spider"
ip138_spider "github.com/wjlin0/uncover/sources/agent/ip138-spider"
qianxun_spider "github.com/wjlin0/uncover/sources/agent/qianxun-spider"
rapiddns_spider "github.com/wjlin0/uncover/sources/agent/rapiddns-spider"
"github.com/wjlin0/uncover/sources/agent/sitedossier-spider"
yahoo_spider "github.com/wjlin0/uncover/sources/agent/yahoo-spider"
"github.com/wjlin0/uncover/sources/agent/zone0"
zoomeye_spider "github.com/wjlin0/uncover/sources/agent/zoomeye-spider"
"github.com/wjlin0/uncover/utils/strings"
"sync"
"time"
"github.com/projectdiscovery/gologger"
errorutil "github.com/projectdiscovery/utils/errors"
stringsutil "github.com/projectdiscovery/utils/strings"
"github.com/wjlin0/uncover/sources"
"github.com/wjlin0/uncover/sources/agent/censys"
"github.com/wjlin0/uncover/sources/agent/criminalip"
"github.com/wjlin0/uncover/sources/agent/fofa"
"github.com/wjlin0/uncover/sources/agent/hunter"
"github.com/wjlin0/uncover/sources/agent/hunterhow"
"github.com/wjlin0/uncover/sources/agent/netlas"
"github.com/wjlin0/uncover/sources/agent/publicwww"
"github.com/wjlin0/uncover/sources/agent/quake"
"github.com/wjlin0/uncover/sources/agent/shodan"
"github.com/wjlin0/uncover/sources/agent/shodanidb"
"github.com/wjlin0/uncover/sources/agent/zoomeye"
)
var DefaultChannelBuffSize = 32
var DefaultCallback = func(query string, agent string) string {
return query
}
type Options struct {
Agents []string // Uncover Agents to use
Queries []string // Queries to pass to Agents
Limit int
MaxRetry int
Timeout int
// Note these ratelimits are used as fallback in case agent
// ratelimit is not available in DefaultRateLimits
RateLimit uint // default 30 req
RateLimitUnit time.Duration // default unit
ProviderConfigLocation string
Proxy string
ProxyAuth string
}
// Service handler of all uncover Agents
type Service struct {
Options *Options
Agents []sources.Agent
Session *sources.Session
Provider *sources.Provider
Keys sources.Keys
}
// New creates new uncover service instance
func New(opts *Options) (*Service, error) {
s := &Service{Agents: []sources.Agent{}, Options: opts}
for _, v := range opts.Agents {
switch v {
case "shodan":
s.Agents = append(s.Agents, &shodan.Agent{})
case "censys":
s.Agents = append(s.Agents, &censys.Agent{})
case "fofa":
s.Agents = append(s.Agents, &fofa.Agent{})
case "shodan-idb":
s.Agents = append(s.Agents, &shodanidb.Agent{})
case "quake":
s.Agents = append(s.Agents, &quake.Agent{})
case "hunter":
s.Agents = append(s.Agents, &hunter.Agent{})
case "zoomeye":
s.Agents = append(s.Agents, &zoomeye.Agent{})
case "netlas":
s.Agents = append(s.Agents, &netlas.Agent{})
case "criminalip":
s.Agents = append(s.Agents, &criminalip.Agent{})
case "publicwww":
s.Agents = append(s.Agents, &publicwww.Agent{})
case "hunterhow":
s.Agents = append(s.Agents, &hunterhow.Agent{})
case "binaryedge":
s.Agents = append(s.Agents, &binaryedge.Agent{})
case "zone0":
s.Agents = append(s.Agents, &zone0.Agent{})
case "daydaymap":
s.Agents = append(s.Agents, &daydaymap.Agent{})
case "github":
s.Agents = append(s.Agents, &github.Agent{})
case "fullhunt":
s.Agents = append(s.Agents, &fullhunt.Agent{})
case "anubis-spider":
s.Agents = append(s.Agents, &anubis_spider.Agent{})
case "sitedossier-spider":
s.Agents = append(s.Agents, &sitedossier_spider.Agent{})
case "fofa-spider":
s.Agents = append(s.Agents, &fofa_spider.Agent{})
case "bing-spider":
s.Agents = append(s.Agents, &bing_spider.Agent{})
case "chinaz-spider":
s.Agents = append(s.Agents, &chinaz_spider.Agent{})
case "google-spider":
s.Agents = append(s.Agents, &google_spider.Agent{})
case "ip138-spider":
s.Agents = append(s.Agents, &ip138_spider.Agent{})
case "qianxun-spider":
s.Agents = append(s.Agents, &qianxun_spider.Agent{})
case "rapiddns-spider":
s.Agents = append(s.Agents, &rapiddns_spider.Agent{})
case "baidu-spider":
s.Agents = append(s.Agents, &baidu_spider.Agent{})
case "yahoo-spider":
s.Agents = append(s.Agents, &yahoo_spider.Agent{})
case "zoomeye-spider":
s.Agents = append(s.Agents, &zoomeye_spider.Agent{})
}
}
s.Provider = sources.NewProvider(opts.ProviderConfigLocation)
s.Keys = s.Provider.GetKeys()
if opts.RateLimit == 0 {
opts.RateLimit = 30
}
if opts.RateLimitUnit == 0 {
opts.RateLimitUnit = time.Minute
}
var err error
s.Session, err = sources.NewSession(&s.Keys, opts.MaxRetry, opts.Timeout, 10, opts.Agents, opts.RateLimitUnit, opts.Proxy, opts.ProxyAuth)
if err != nil {
return nil, err
}
return s, nil
}
func (s *Service) Execute(ctx context.Context) (<-chan sources.Result, error) {
// unlikely but as a precaution to handle random panics check all types
if err := s.nilCheck(); err != nil {
return nil, err
}
switch {
case len(s.Agents) == 0:
return nil, errorutil.NewWithTag("uncover", "no agent/source specified")
case !s.hasAnyAnonymousProvider() && !s.Provider.HasKeys():
return nil, errorutil.NewWithTag("uncover", "agents %v requires keys but no keys were found. please read docs %s on how to add keys", s.Options.Agents, "https://github.com/wjlin0/uncover?tab=readme-ov-file#provider-configuration")
case s.hasOnlyDestructAgent():
return nil, errorutil.NewWithTag("uncover", "destructive agents %v cannot be used with uncover. please use command show to see destruct agents (-da)", s.Options.Agents)
}
megaChan := make(chan sources.Result, DefaultChannelBuffSize)
// iterate and run all sources
wg := &sync.WaitGroup{}
for _, q := range s.Options.Queries {
agentLabel:
for _, agent := range s.Agents {
if strings.Contains(DestructAgents(), agent.Name()) {
gologger.Warning().Msgf("destructive agent %s cannot be used with uncover", agent.Name())
continue agentLabel
}
keys := s.Provider.GetKeys()
if keys.Empty() && !(stringsutil.EqualFoldAny(agent.Name(), AnonymousAgents()...)) {
gologger.Error().Msgf("%s agent given but keys not found", agent.Name())
continue agentLabel
}
ch, err := agent.Query(s.Session, &sources.Query{
Query: DefaultCallback(q, agent.Name()),
Limit: s.Options.Limit,
})
if err != nil {
gologger.Error().Msgf("%s\n", err)
continue agentLabel
}
wg.Add(1)
go func(source, relay chan sources.Result, ctx context.Context) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case res, ok := <-source:
res.Timestamp = time.Now().Unix()
if !ok {
return
}
relay <- res
}
}
}(ch, megaChan, ctx)
}
}
// close channel when all sources return
go func(wg *sync.WaitGroup, megaChan chan sources.Result) {
wg.Wait()
defer close(megaChan)
}(wg, megaChan)
return megaChan, nil
}
// ExecuteWithCallback ExecuteWithWriters writes output to writer along with stdout
func (s *Service) ExecuteWithCallback(ctx context.Context, callback func(result sources.Result)) error {
ch, err := s.Execute(ctx)
if err != nil {
return err
}
if callback == nil {
return errorutil.NewWithTag("uncover", "result callback cannot be nil")
}
for {
select {
case <-ctx.Done():
return nil
case result, ok := <-ch:
if !ok {
return nil
}
callback(result)
}
}
}
// AllAgents returns all supported uncover Agents
func (s *Service) AllAgents() []string {
return []string{
"shodan", "censys", "fofa", "quake", "hunter", "zoomeye", "netlas", "criminalip", "publicwww", "hunterhow", "binaryedge", "github", "fullhunt", "zone0", "daydaymap",
"shodan-idb", "anubis-spider", "sitedossier-spider", "fofa-spider", "google-spider", "bing-spider", "chinaz-spider", "ip138-spider", "qianxun-spider", "rapiddns-spider", "baidu-spider", "yahoo-spider", "zoomeye-spider",
}
}
func DestructAgents() []string {
return []string{
"qianxun-spider",
}
}
func UncoverAgents() []string {
return []string{
"shodan", "censys", "fofa", "quake", "hunter", "zoomeye", "netlas", "criminalip", "publicwww", "hunterhow", "binaryedge", "github", "fullhunt", "zone0", "daydaymap",
}
}
func AnonymousAgents() []string {
return []string{
"shodan-idb", "sitedossier-spider", "fofa-spider", "bing-spider", "chinaz-spider", "google-spider", "ip138-spider", "qianxun-spider", "rapiddns-spider", "anubis-spider", "baidu-spider", "yahoo-spider", "zoomeye-spider",
}
}
func (s *Service) nilCheck() error {
if s.Provider == nil {
return errorutil.NewWithTag("uncover", "provider cannot be nil")
}
if s.Options == nil {
return errorutil.NewWithTag("uncover", "options cannot be nil")
}
if s.Session == nil {
return errorutil.NewWithTag("uncover", "session cannot be nil")
}
return nil
}
func (s *Service) hasAnyAnonymousProvider() bool {
return func() bool {
for _, str := range AnonymousAgents() {
if stringsutil.EqualFoldAny(str, s.Options.Agents...) {
return true
}
}
return false
}()
}
func AllAgents() []string {
return []string{
"shodan", "censys", "fofa", "quake", "hunter", "zoomeye", "netlas", "criminalip", "publicwww", "hunterhow", "binaryedge", "github", "fullhunt", "zone0", "daydaymap",
"shodan-idb", "anubis-spider", "sitedossier-spider", "fofa-spider", "bing-spider", "chinaz-spider", "google-spider", "ip138-spider", "qianxun-spider", "rapiddns-spider", "baidu-spider", "yahoo-spider", "zoomeye-spider",
}
}
func (s *Service) hasOnlyDestructAgent() bool {
return sliceutil.ContainsItems(DestructAgents(), s.Options.Agents)
}