From ee320d3702546ed0993d7b36b28626be683abedb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 28 Aug 2023 13:28:58 +0000 Subject: [PATCH] Use API v3 --- .../sources/virustotal/virustotal.go | 62 ++++++++++++------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/v2/pkg/subscraping/sources/virustotal/virustotal.go b/v2/pkg/subscraping/sources/virustotal/virustotal.go index 0abe5c5bb..82b9c81bf 100644 --- a/v2/pkg/subscraping/sources/virustotal/virustotal.go +++ b/v2/pkg/subscraping/sources/virustotal/virustotal.go @@ -12,7 +12,16 @@ import ( ) type response struct { - Subdomains []string `json:"subdomains"` + Data []Object `json:"data"` + Meta Meta `json:"meta"` +} + +type Object struct { + Id string `json:"id"` +} + +type Meta struct { + Cursor string `json:"cursor"` } // Source is the passive scraping agent @@ -40,29 +49,40 @@ func (s *Source) Run(ctx context.Context, domain string, session *subscraping.Se if randomApiKey == "" { return } + var url string = fmt.Sprintf("https://www.virustotal.com/api/v3/domains/%s/subdomains?limit=1000", domain) + var hasMore bool = true + var cursor string = "" + for hasMore { + if cursor != "" { + url = fmt.Sprintf("%s&cursor=%s", url, cursor) + } + resp, err := session.Get(ctx, url, "", map[string]string{"x-apikey": randomApiKey}) + if err != nil { + results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} + s.errors++ + session.DiscardHTTPResponse(resp) + return + } + + var data response + err = jsoniter.NewDecoder(resp.Body).Decode(&data) + if err != nil { + results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} + s.errors++ + resp.Body.Close() + return + } - resp, err := session.SimpleGet(ctx, fmt.Sprintf("https://www.virustotal.com/vtapi/v2/domain/report?apikey=%s&domain=%s", randomApiKey, domain)) - if err != nil { - results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} - s.errors++ - session.DiscardHTTPResponse(resp) - return - } - - var data response - err = jsoniter.NewDecoder(resp.Body).Decode(&data) - if err != nil { - results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} - s.errors++ resp.Body.Close() - return - } - - resp.Body.Close() - for _, subdomain := range data.Subdomains { - results <- subscraping.Result{Source: s.Name(), Type: subscraping.Subdomain, Value: subdomain} - s.results++ + for _, subdomain := range data.Data { + results <- subscraping.Result{Source: s.Name(), Type: subscraping.Subdomain, Value: subdomain.Id} + s.results++ + } + cursor = data.Meta.Cursor + if cursor == "" { + hasMore = false + } } }()