subfinder/v2/pkg/subscraping/sources/certspotter/certspotter.go

97 lines
2.6 KiB
Go
Raw Normal View History

2019-12-03 12:37:11 +00:00
package certspotter
import (
"context"
"fmt"
jsoniter "github.com/json-iterator/go"
2020-09-24 17:02:02 +00:00
"github.com/projectdiscovery/subfinder/v2/pkg/subscraping"
2019-12-03 12:37:11 +00:00
)
type certspotterObject struct {
ID string `json:"id"`
DNSNames []string `json:"dns_names"`
}
// 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() {
2020-08-10 08:15:30 +00:00
defer close(results)
2019-12-03 12:37:11 +00:00
if session.Keys.Certspotter == "" {
return
}
2020-01-10 13:55:28 +00:00
resp, err := session.Get(ctx, fmt.Sprintf("https://api.certspotter.com/v1/issuances?domain=%s&include_subdomains=true&expand=dns_names", domain), "", map[string]string{"Authorization": "Bearer " + session.Keys.Certspotter})
2019-12-03 12:37:11 +00:00
if err != nil {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
2020-07-22 13:52:41 +00:00
session.DiscardHTTPResponse(resp)
2019-12-03 12:37:11 +00:00
return
}
2020-08-10 08:15:30 +00:00
var response []certspotterObject
2019-12-03 12:37:11 +00:00
err = jsoniter.NewDecoder(resp.Body).Decode(&response)
if err != nil {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
2019-12-06 11:57:26 +00:00
resp.Body.Close()
2019-12-03 12:37:11 +00:00
return
}
resp.Body.Close()
for _, cert := range response {
for _, subdomain := range cert.DNSNames {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Subdomain, Value: subdomain}
}
}
2019-12-14 19:25:28 +00:00
// if the number of responses is zero, close the channel and return.
if len(response) == 0 {
return
}
2019-12-03 12:37:11 +00:00
id := response[len(response)-1].ID
for {
reqURL := fmt.Sprintf("https://api.certspotter.com/v1/issuances?domain=%s&include_subdomains=true&expand=dns_names&after=%s", domain, id)
2020-01-10 13:55:28 +00:00
resp, err := session.Get(ctx, reqURL, "", map[string]string{"Authorization": "Bearer " + session.Keys.Certspotter})
2019-12-03 12:37:11 +00:00
if err != nil {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
return
}
2020-08-10 08:15:30 +00:00
var response []certspotterObject
2019-12-03 12:37:11 +00:00
err = jsoniter.NewDecoder(resp.Body).Decode(&response)
if err != nil {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}
2019-12-06 11:57:26 +00:00
resp.Body.Close()
2019-12-03 12:37:11 +00:00
return
}
resp.Body.Close()
if len(response) == 0 {
break
}
for _, cert := range response {
for _, subdomain := range cert.DNSNames {
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Subdomain, Value: subdomain}
}
}
id = response[len(response)-1].ID
}
}()
return results
}
// Name returns the name of the source
func (s *Source) Name() string {
return "certspotter"
}