/
censys.go
96 lines (79 loc) · 2.55 KB
/
censys.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
package censys
import (
"bytes"
"context"
"net/http"
"strconv"
jsoniter "github.com/json-iterator/go"
"github.com/projectdiscovery/subfinder/pkg/subscraping"
)
const maxCensysPages = 10
type resultsq struct {
Data []string `json:"parsed.extensions.subject_alt_name.dns_names"`
Data1 []string `json:"parsed.names"`
}
type response struct {
Results []resultsq `json:"results"`
Metadata struct {
Pages int `json:"pages"`
} `json:"metadata"`
}
// Source is the passive scraping agent
type Source struct{}
// Run function returns all subdomains found with the service
func (s *Source) Run(ctx context.Context, domain string, session *subscraping.Session) <-chan subscraping.Result {
results := make(chan subscraping.Result)
go func() {
if session.Keys.CensysToken == "" || session.Keys.CensysSecret == "" {
close(results)
return
}
var response response
currentPage := 1
for {
var request = []byte(`{"query":"` + domain + `", "page":` + strconv.Itoa(currentPage) + `, "fields":["parsed.names","parsed.extensions.subject_alt_name.dns_names"], "flatten":true}`)
req, err := http.NewRequestWithContext(ctx, "POST", "https://www.censys.io/api/v1/search/certificates", bytes.NewReader(request))
if err != nil {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
close(results)
return
}
req.SetBasicAuth(session.Keys.CensysToken, session.Keys.CensysSecret)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := session.Client.Do(req)
if err != nil {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
close(results)
return
}
err = jsoniter.NewDecoder(resp.Body).Decode(&response)
if err != nil {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
resp.Body.Close()
close(results)
return
}
resp.Body.Close()
// Exit the censys enumeration if max pages is reached
if currentPage >= response.Metadata.Pages || currentPage >= maxCensysPages {
break
}
for _, res := range response.Results {
for _, part := range res.Data {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Subdomain, Value: part}
}
for _, part := range res.Data1 {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Subdomain, Value: part}
}
}
currentPage++
}
close(results)
}()
return results
}
// Name returns the name of the source
func (s *Source) Name() string {
return "censys"
}