subfinder/v2/pkg/runner/config.go

180 lines
4.6 KiB
Go
Raw Normal View History

package runner
import (
"os"
2019-12-03 18:12:45 +00:00
"gopkg.in/yaml.v3"
"github.com/projectdiscovery/sliceutil"
"github.com/projectdiscovery/subfinder/v2/pkg/subscraping"
)
2020-07-22 13:20:41 +00:00
// MultipleKeyPartsLength is the max length for multiple keys
const MultipleKeyPartsLength = 2
// Providers contains the providers stored in the configuration file
type Providers struct {
// API keys for different sources
Bufferover []string `yaml:"bufferover"`
Binaryedge []string `yaml:"binaryedge"`
2021-12-21 10:36:45 +00:00
C99 []string `yaml:"c99"`
2019-12-05 10:09:42 +00:00
Censys []string `yaml:"censys"`
Certspotter []string `yaml:"certspotter"`
Chaos []string `yaml:"chaos"`
2021-07-30 12:38:25 +00:00
Chinaz []string `yaml:"chinaz"`
2020-05-16 03:12:20 +00:00
DNSDB []string `yaml:"dnsdb"`
Fofa []string `yaml:"fofa"`
FullHunt []string `json:"fullhunt"`
GitHub []string `yaml:"github"`
IntelX []string `yaml:"intelx"`
PassiveTotal []string `yaml:"passivetotal"`
Robtex []string `yaml:"robtex"`
SecurityTrails []string `yaml:"securitytrails"`
2019-12-04 18:42:55 +00:00
Shodan []string `yaml:"shodan"`
2020-09-13 19:55:31 +00:00
ThreatBook []string `yaml:"threatbook"`
2019-12-04 11:39:53 +00:00
URLScan []string `yaml:"urlscan"`
Virustotal []string `yaml:"virustotal"`
2020-05-16 05:32:03 +00:00
ZoomEye []string `yaml:"zoomeye"`
2021-12-20 16:34:49 +00:00
ZoomEyeApi []string `yaml:"zoomeyeapi"`
}
// GetConfigDirectory gets the subfinder config directory for a user
func GetConfigDirectory() (string, error) {
var config string
directory, err := os.UserHomeDir()
if err != nil {
return config, err
}
config = directory + "/.config/subfinder"
// Create All directory for subfinder even if they exist
err = os.MkdirAll(config, os.ModePerm)
if err != nil {
return config, err
}
return config, nil
}
// MarshalTo writes the marshaled yaml config to disk
func (c *Providers) MarshalTo(file string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
2019-12-03 18:12:45 +00:00
return yaml.NewEncoder(f).Encode(c)
}
// MarshalTo writes the marshaled yaml config to disk
func (c *Providers) UnmarshalFrom(file string) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
return yaml.NewDecoder(f).Decode(c)
}
2019-12-04 11:39:53 +00:00
// GetKeys gets the API keys from config file and creates a Keys struct
// We use random selection of api keys from the list of keys supplied.
// Keys that require 2 options are separated by colon (:).
func (c *Providers) GetKeys() subscraping.Keys {
2019-12-04 11:39:53 +00:00
keys := subscraping.Keys{}
if len(c.Binaryedge) > 0 {
keys.Binaryedge = sliceutil.PickRandom(c.Binaryedge)
}
2021-12-21 10:36:45 +00:00
if len(c.C99) > 0 {
keys.C99 = sliceutil.PickRandom(c.C99)
2021-12-21 10:36:45 +00:00
}
2019-12-04 11:39:53 +00:00
if len(c.Bufferover) > 0 {
keys.Bufferover = sliceutil.PickRandom(c.Bufferover)
}
2019-12-05 10:09:42 +00:00
if len(c.Censys) > 0 {
censysKeys := sliceutil.PickRandom(c.Censys)
if keyPartA, keyPartB, ok := multipartKey(censysKeys); ok {
keys.CensysToken = keyPartA
keys.CensysSecret = keyPartB
}
}
2019-12-05 10:09:42 +00:00
if len(c.Certspotter) > 0 {
keys.Certspotter = sliceutil.PickRandom(c.Certspotter)
2019-12-05 10:09:42 +00:00
}
if len(c.Chaos) > 0 {
keys.Chaos = sliceutil.PickRandom(c.Chaos)
}
2021-07-30 12:38:25 +00:00
if len(c.Chinaz) > 0 {
keys.Chinaz = sliceutil.PickRandom(c.Chinaz)
2021-07-30 12:38:25 +00:00
}
2020-05-16 03:12:20 +00:00
if (len(c.DNSDB)) > 0 {
keys.DNSDB = sliceutil.PickRandom(c.DNSDB)
2020-05-16 03:12:20 +00:00
}
if (len(c.GitHub)) > 0 {
keys.GitHub = c.GitHub
}
2020-05-16 03:12:20 +00:00
if len(c.IntelX) > 0 {
intelxKeys := sliceutil.PickRandom(c.IntelX)
if keyPartA, keyPartB, ok := multipartKey(intelxKeys); ok {
keys.IntelXHost = keyPartA
keys.IntelXKey = keyPartB
}
}
if len(c.PassiveTotal) > 0 {
passiveTotalKeys := sliceutil.PickRandom(c.PassiveTotal)
if keyPartA, keyPartB, ok := multipartKey(passiveTotalKeys); ok {
keys.PassiveTotalUsername = keyPartA
keys.PassiveTotalPassword = keyPartB
}
2019-12-04 11:39:53 +00:00
}
if len(c.Robtex) > 0 {
keys.Robtex = sliceutil.PickRandom(c.Robtex)
}
if len(c.SecurityTrails) > 0 {
keys.Securitytrails = sliceutil.PickRandom(c.SecurityTrails)
}
2019-12-04 18:42:55 +00:00
if len(c.Shodan) > 0 {
keys.Shodan = sliceutil.PickRandom(c.Shodan)
2019-12-04 18:42:55 +00:00
}
2020-09-13 19:55:31 +00:00
if len(c.ThreatBook) > 0 {
keys.ThreatBook = sliceutil.PickRandom(c.ThreatBook)
2020-09-13 19:55:31 +00:00
}
if len(c.URLScan) > 0 {
keys.URLScan = sliceutil.PickRandom(c.URLScan)
}
if len(c.Virustotal) > 0 {
keys.Virustotal = sliceutil.PickRandom(c.Virustotal)
}
2020-05-16 05:32:03 +00:00
if len(c.ZoomEye) > 0 {
zoomEyeKeys := sliceutil.PickRandom(c.ZoomEye)
if keyPartA, keyPartB, ok := multipartKey(zoomEyeKeys); ok {
keys.ZoomEyeUsername = keyPartA
keys.ZoomEyePassword = keyPartB
2020-05-16 05:32:03 +00:00
}
}
2021-12-20 16:34:49 +00:00
if len(c.ZoomEyeApi) > 0 {
keys.ZoomEyeKey = sliceutil.PickRandom(c.ZoomEyeApi)
2021-12-20 16:34:49 +00:00
}
2021-05-17 03:51:14 +00:00
if len(c.Fofa) > 0 {
fofaKeys := sliceutil.PickRandom(c.Fofa)
if keyPartA, keyPartB, ok := multipartKey(fofaKeys); ok {
keys.FofaUsername = keyPartA
keys.FofaSecret = keyPartB
2021-05-17 03:51:14 +00:00
}
}
2021-12-17 03:12:05 +00:00
if len(c.FullHunt) > 0 {
keys.FullHunt = sliceutil.PickRandom(c.FullHunt)
2021-12-17 03:12:05 +00:00
}
2019-12-04 11:39:53 +00:00
return keys
}