-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Added netlas.io #829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added netlas.io #829
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5aca90d
Merge pull request #827 from projectdiscovery/dev
ehsandeep 2cd1c15
added netlas to sources
4a4e4be
added netlas to sources
6829006
added netlas to sources
a0a2276
Merge branch 'dev' into main
ehsandeep 64f5748
tried to solve lint errors
3afd044
Improved Readability
2ec5a12
fix nil panics
tarunKoyalwar 70aaf51
netlas: use session to send http request
tarunKoyalwar e29ab3e
update dev version
tarunKoyalwar f37c527
Revert "update dev version"
tarunKoyalwar ff74e51
make provider-config error non fatal
tarunKoyalwar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ var ( | |
| "github", | ||
| "hackertarget", | ||
| "intelx", | ||
| "netlas", | ||
| "passivetotal", | ||
| "quake", | ||
| "rapiddns", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| // Package netlas logic | ||
| package netlas | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "encoding/json" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/projectdiscovery/subfinder/v2/pkg/subscraping" | ||
| ) | ||
|
|
||
| type Item struct { | ||
| Data struct { | ||
| A []string `json:"a,omitempty"` | ||
| Txt []string `json:"txt,omitempty"` | ||
| LastUpdated string `json:"last_updated,omitempty"` | ||
| Timestamp string `json:"@timestamp,omitempty"` | ||
| Ns []string `json:"ns,omitempty"` | ||
| Level int `json:"level,omitempty"` | ||
| Zone string `json:"zone,omitempty"` | ||
| Domain string `json:"domain,omitempty"` | ||
| Cname []string `json:"cname,omitempty"` | ||
| Mx []string `json:"mx,omitempty"` | ||
| } `json:"data"` | ||
| } | ||
|
|
||
| type Response struct { | ||
| Items []Item `json:"items"` | ||
| Took int `json:"took"` | ||
| } | ||
|
|
||
| type DomainsCountResponse struct { | ||
| Count int `json:"count"` | ||
| } | ||
|
|
||
| // Source is the passive scraping agent | ||
| type Source struct { | ||
| apiKeys []string | ||
| timeTaken time.Duration | ||
| errors int | ||
| results int | ||
| skipped bool | ||
| } | ||
|
|
||
| // func get_domains(offset string, domain string) int { | ||
Devang-Solanki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // return resp.StatusCode | ||
| // } | ||
|
|
||
| func (s *Source) Run(ctx context.Context, domain string, session *subscraping.Session) <-chan subscraping.Result { | ||
| results := make(chan subscraping.Result) | ||
| s.errors = 0 | ||
| s.results = 0 | ||
|
|
||
| go func() { | ||
| defer func(startTime time.Time) { | ||
| s.timeTaken = time.Since(startTime) | ||
| close(results) | ||
| }(time.Now()) | ||
|
|
||
| // To get count of domains | ||
| endpoint := "https://app.netlas.io/api/domains_count/" | ||
| paramss := url.Values{} | ||
| paramss.Set("q", "domain:(domain:*."+domain+" "+"AND NOT domain:"+domain+")") | ||
Devang-Solanki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| countUrl := endpoint + "?" + paramss.Encode() | ||
|
|
||
| client := &http.Client{} | ||
| req, _ := http.NewRequest("GET", countUrl, nil) | ||
| req.Header.Set("accept", "application/json") | ||
|
|
||
| // Pick an API key | ||
| randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name()) | ||
| if randomApiKey != "" { | ||
| req.Header.Set("X-API-Key", randomApiKey) | ||
| } | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} | ||
| s.errors++ | ||
| return | ||
| } else if resp.StatusCode != 200 { | ||
| results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("request rate limited with status code %d", resp.StatusCode)} | ||
| s.errors++ | ||
| return | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := ioutil.ReadAll(resp.Body) | ||
| if err != nil { | ||
| results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("Error Reading ressponse body")} | ||
| s.errors++ | ||
| return | ||
| } | ||
|
|
||
| // Parse the JSON response | ||
| var domainsCount DomainsCountResponse | ||
| err = json.Unmarshal(body, &domainsCount) | ||
| if err != nil { | ||
| results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} | ||
| s.errors++ | ||
| return | ||
| } | ||
|
|
||
| //Define the API endpoint URL and query parameters | ||
|
|
||
| for i := 0; i < domainsCount.Count; i += 20 { | ||
|
|
||
| time.Sleep(1200 * time.Millisecond) | ||
Devang-Solanki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| offset := strconv.Itoa(i) | ||
|
|
||
| endpoint := "https://app.netlas.io/api/domains/" | ||
| params := url.Values{} | ||
| params.Set("q", "domain:(domain:*."+domain+" "+"AND NOT domain:"+domain+")") | ||
| params.Set("source_type", "include") | ||
| params.Set("start", offset) | ||
| params.Set("fields", "*") | ||
| apiUrl := endpoint + "?" + params.Encode() | ||
|
|
||
| // Send the HTTP request and read the response body | ||
| client := &http.Client{} | ||
| req, _ := http.NewRequest("GET", apiUrl, nil) | ||
| req.Header.Set("accept", "application/json") | ||
|
|
||
| // Pick an API key | ||
| randomApiKey := subscraping.PickRandom(s.apiKeys, s.Name()) | ||
| if randomApiKey != "" { | ||
| req.Header.Set("X-API-Key", randomApiKey) | ||
| } | ||
|
|
||
| resp, _ := client.Do(req) | ||
| body, _ := ioutil.ReadAll(resp.Body) | ||
|
|
||
| if resp.StatusCode == 429 { | ||
| results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("request rate limited with status code %d", resp.StatusCode)} | ||
| s.errors++ | ||
| break | ||
| } | ||
|
|
||
| // Parse the response body and extract the domain values | ||
| var data Response | ||
| err := json.Unmarshal(body, &data) | ||
| if err != nil { | ||
| results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} | ||
| s.errors++ | ||
| return | ||
| } | ||
|
|
||
| for _, item := range data.Items { | ||
| results <- subscraping.Result{ | ||
| Source: s.Name(), Type: subscraping.Subdomain, Value: item.Data.Domain, | ||
| } | ||
| s.results++ | ||
| } | ||
| } | ||
|
|
||
| }() | ||
|
|
||
| return results | ||
| } | ||
|
|
||
| // Name returns the name of the source | ||
| func (s *Source) Name() string { | ||
| return "netlas" | ||
| } | ||
|
|
||
| func (s *Source) IsDefault() bool { | ||
| return false | ||
| } | ||
|
|
||
| func (s *Source) HasRecursiveSupport() bool { | ||
| return false | ||
| } | ||
|
|
||
| func (s *Source) NeedsKey() bool { | ||
| return true | ||
| } | ||
|
|
||
| func (s *Source) AddApiKeys(keys []string) { | ||
| s.apiKeys = keys | ||
| } | ||
|
|
||
| func (s *Source) Statistics() subscraping.Statistics { | ||
| return subscraping.Statistics{ | ||
| Errors: s.errors, | ||
| Results: s.results, | ||
| TimeTaken: s.timeTaken, | ||
| Skipped: s.skipped, | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.