forked from projectdiscovery/subfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
projectdiscovery#408 Add fofa for SubFinder
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,74 @@ | ||
// Package fofa logic | ||
package fofa | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"strings" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/projectdiscovery/subfinder/v2/pkg/subscraping" | ||
) | ||
|
||
type fofaResponse struct { | ||
Error bool `json:"error"` | ||
ErrMsg string `json:"errmsg"` | ||
Size int `json:"size"` | ||
Results []string `json:"results"` | ||
} | ||
|
||
// 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() { | ||
defer close(results) | ||
|
||
if session.Keys.FofaUsername == "" || session.Keys.FofaSecret == "" { | ||
return | ||
} | ||
|
||
// fofa api doc https://fofa.so/static_pages/api_help | ||
qbase64 := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("domain=\"%s\"", domain))) | ||
resp, err := session.SimpleGet(ctx, fmt.Sprintf("https://fofa.so/api/v1/search/all?full=true&fields=host&page=1&size=10000&email=%s&key=%s&qbase64=%s", session.Keys.FofaUsername, session.Keys.FofaSecret, qbase64)) | ||
if err != nil && resp == nil { | ||
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} | ||
session.DiscardHTTPResponse(resp) | ||
return | ||
} | ||
|
||
var response fofaResponse | ||
err = jsoniter.NewDecoder(resp.Body).Decode(&response) | ||
if err != nil { | ||
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err} | ||
resp.Body.Close() | ||
return | ||
} | ||
resp.Body.Close() | ||
|
||
if response.Error { | ||
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf("%s", response.ErrMsg)} | ||
return | ||
} | ||
|
||
if response.Size > 0 { | ||
for _, subdomain := range response.Results { | ||
if strings.HasPrefix(strings.ToLower(subdomain), "http://") || strings.HasPrefix(strings.ToLower(subdomain), "https://") { | ||
subdomain = subdomain[strings.Index(subdomain, "//")+2:] | ||
} | ||
results <- subscraping.Result{Source: s.Name(), Type: subscraping.Subdomain, Value: subdomain} | ||
} | ||
} | ||
}() | ||
|
||
return results | ||
} | ||
|
||
// Name returns the name of the source | ||
func (s *Source) Name() string { | ||
return "fofa" | ||
} |
This file contains 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