Skip to content

Commit

Permalink
Return sourceMap to make subfinder go module more useful (#1466)
Browse files Browse the repository at this point in the history
Co-authored-by: Sandeep Singh <[email protected]>
  • Loading branch information
mielverkerken and ehsandeep authored Jan 14, 2025
1 parent 3e36d7a commit 1620aaa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
12 changes: 11 additions & 1 deletion v2/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func main() {
}

output := &bytes.Buffer{}
var sourceMap map[string]map[string]struct{}
// To run subdomain enumeration on a single domain
if err = subfinder.EnumerateSingleDomainWithCtx(context.Background(), "hackerone.com", []io.Writer{output}); err != nil {
if sourceMap, err = subfinder.EnumerateSingleDomainWithCtx(context.Background(), "hackerone.com", []io.Writer{output}); err != nil {
log.Fatalf("failed to enumerate single domain: %v", err)
}

Expand All @@ -47,4 +48,13 @@ func main() {

// print the output
log.Println(output.String())

// Or use sourceMap to access the results in your application
for subdomain, sources := range sourceMap {
sourcesList := make([]string, 0, len(sources))
for source := range sources {
sourcesList = append(sourcesList, source)
}
log.Printf("%s %s (%d)\n", subdomain, sourcesList, len(sources))
}
}
8 changes: 4 additions & 4 deletions v2/pkg/runner/enumerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ var replacer = strings.NewReplacer(
)

// EnumerateSingleDomain wraps EnumerateSingleDomainWithCtx with an empty context
func (r *Runner) EnumerateSingleDomain(domain string, writers []io.Writer) error {
func (r *Runner) EnumerateSingleDomain(domain string, writers []io.Writer) (map[string]map[string]struct{}, error) {
return r.EnumerateSingleDomainWithCtx(context.Background(), domain, writers)
}

// EnumerateSingleDomainWithCtx performs subdomain enumeration against a single domain
func (r *Runner) EnumerateSingleDomainWithCtx(ctx context.Context, domain string, writers []io.Writer) error {
func (r *Runner) EnumerateSingleDomainWithCtx(ctx context.Context, domain string, writers []io.Writer) (map[string]map[string]struct{}, error) {
gologger.Info().Msgf("Enumerating subdomains for %s\n", domain)

// Check if the user has asked to remove wildcards explicitly.
Expand Down Expand Up @@ -148,7 +148,7 @@ func (r *Runner) EnumerateSingleDomainWithCtx(ctx context.Context, domain string
}
if err != nil {
gologger.Error().Msgf("Could not write results for %s: %s\n", domain, err)
return err
return nil, err
}
}

Expand Down Expand Up @@ -189,7 +189,7 @@ func (r *Runner) EnumerateSingleDomainWithCtx(ctx context.Context, domain string
printStatistics(statistics)
}

return nil
return sourceMap, nil
}

func (r *Runner) filterAndMatchSubdomain(subdomain string) bool {
Expand Down
6 changes: 3 additions & 3 deletions v2/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (r *Runner) EnumerateMultipleDomainsWithCtx(ctx context.Context, reader io.
return err
}

err = r.EnumerateSingleDomainWithCtx(ctx, domain, append(writers, file))
_, err = r.EnumerateSingleDomainWithCtx(ctx, domain, append(writers, file))

file.Close()
} else if r.options.OutputDirectory != "" {
Expand All @@ -154,11 +154,11 @@ func (r *Runner) EnumerateMultipleDomainsWithCtx(ctx context.Context, reader io.
return err
}

err = r.EnumerateSingleDomainWithCtx(ctx, domain, append(writers, file))
_, err = r.EnumerateSingleDomainWithCtx(ctx, domain, append(writers, file))

file.Close()
} else {
err = r.EnumerateSingleDomainWithCtx(ctx, domain, writers)
_, err = r.EnumerateSingleDomainWithCtx(ctx, domain, writers)
}
if err != nil {
return err
Expand Down

0 comments on commit 1620aaa

Please sign in to comment.