2019-12-03 11:17:19 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2019-12-03 18:12:45 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-04-26 08:00:09 +00:00
|
|
|
|
|
|
|
"github.com/projectdiscovery/sliceutil"
|
|
|
|
"github.com/projectdiscovery/subfinder/v2/pkg/subscraping"
|
2019-12-03 11:17:19 +00:00
|
|
|
)
|
|
|
|
|
2020-07-22 13:20:41 +00:00
|
|
|
// MultipleKeyPartsLength is the max length for multiple keys
|
|
|
|
const MultipleKeyPartsLength = 2
|
|
|
|
|
2022-02-24 10:44:16 +00:00
|
|
|
// Providers contains the providers stored in the configuration file
|
|
|
|
type Providers struct {
|
2019-12-03 11:17:19 +00:00
|
|
|
// API keys for different sources
|
2022-02-21 14:31:13 +00:00
|
|
|
Bufferover []string `yaml:"bufferover"`
|
2019-12-03 11:17:19 +00:00
|
|
|
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"`
|
2019-12-03 11:17:19 +00:00
|
|
|
Certspotter []string `yaml:"certspotter"`
|
2020-05-17 21:18:09 +00:00
|
|
|
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"`
|
2022-07-21 14:30:28 +00:00
|
|
|
Fofa []string `yaml:"fofa"`
|
|
|
|
FullHunt []string `json:"fullhunt"`
|
2020-07-05 18:04:20 +00:00
|
|
|
GitHub []string `yaml:"github"`
|
2020-06-07 00:48:15 +00:00
|
|
|
IntelX []string `yaml:"intelx"`
|
2019-12-03 11:17:19 +00:00
|
|
|
PassiveTotal []string `yaml:"passivetotal"`
|
2020-09-21 18:34:42 +00:00
|
|
|
Robtex []string `yaml:"robtex"`
|
2019-12-03 11:17:19 +00:00
|
|
|
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"`
|
2019-12-03 11:17:19 +00:00
|
|
|
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"`
|
2019-12-03 11:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
2020-07-22 15:09:55 +00:00
|
|
|
|
2019-12-03 11:17:19 +00:00
|
|
|
// Create All directory for subfinder even if they exist
|
2020-07-22 15:09:55 +00:00
|
|
|
err = os.MkdirAll(config, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
2019-12-03 11:17:19 +00:00
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2022-02-24 10:44:16 +00:00
|
|
|
// MarshalTo writes the marshaled yaml config to disk
|
|
|
|
func (c *Providers) MarshalTo(file string) error {
|
|
|
|
f, err := os.Create(file)
|
2019-12-03 11:17:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-24 10:44:16 +00:00
|
|
|
defer f.Close()
|
2019-12-03 18:12:45 +00:00
|
|
|
|
2022-02-24 10:44:16 +00:00
|
|
|
return yaml.NewEncoder(f).Encode(c)
|
2019-12-03 11:17:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 10:44:16 +00:00
|
|
|
// MarshalTo writes the marshaled yaml config to disk
|
|
|
|
func (c *Providers) UnmarshalFrom(file string) error {
|
2019-12-03 11:17:19 +00:00
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
2022-02-24 10:44:16 +00:00
|
|
|
return err
|
2019-12-03 11:17:19 +00:00
|
|
|
}
|
2022-02-24 10:44:16 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return yaml.NewDecoder(f).Decode(c)
|
2019-12-03 11:17:19 +00:00
|
|
|
}
|
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 (:).
|
2022-02-24 10:44:16 +00:00
|
|
|
func (c *Providers) GetKeys() subscraping.Keys {
|
2019-12-04 11:39:53 +00:00
|
|
|
keys := subscraping.Keys{}
|
|
|
|
|
2019-12-04 14:51:52 +00:00
|
|
|
if len(c.Binaryedge) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.Binaryedge = sliceutil.PickRandom(c.Binaryedge)
|
2019-12-04 14:51:52 +00:00
|
|
|
}
|
2021-12-21 10:36:45 +00:00
|
|
|
if len(c.C99) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.C99 = sliceutil.PickRandom(c.C99)
|
2021-12-21 10:36:45 +00:00
|
|
|
}
|
2019-12-04 11:39:53 +00:00
|
|
|
|
2022-02-21 14:31:13 +00:00
|
|
|
if len(c.Bufferover) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.Bufferover = sliceutil.PickRandom(c.Bufferover)
|
2022-02-21 14:31:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 10:09:42 +00:00
|
|
|
if len(c.Censys) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
censysKeys := sliceutil.PickRandom(c.Censys)
|
|
|
|
if keyPartA, keyPartB, ok := multipartKey(censysKeys); ok {
|
|
|
|
keys.CensysToken = keyPartA
|
|
|
|
keys.CensysSecret = keyPartB
|
2019-12-04 14:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 10:09:42 +00:00
|
|
|
|
|
|
|
if len(c.Certspotter) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.Certspotter = sliceutil.PickRandom(c.Certspotter)
|
2019-12-05 10:09:42 +00:00
|
|
|
}
|
2020-05-17 21:18:09 +00:00
|
|
|
if len(c.Chaos) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.Chaos = sliceutil.PickRandom(c.Chaos)
|
2020-05-17 21:18:09 +00:00
|
|
|
}
|
2021-07-30 12:38:25 +00:00
|
|
|
if len(c.Chinaz) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
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 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.DNSDB = sliceutil.PickRandom(c.DNSDB)
|
2020-05-16 03:12:20 +00:00
|
|
|
}
|
2020-07-05 18:04:20 +00:00
|
|
|
if (len(c.GitHub)) > 0 {
|
2020-07-15 11:19:31 +00:00
|
|
|
keys.GitHub = c.GitHub
|
2020-07-05 18:04:20 +00:00
|
|
|
}
|
2020-05-16 03:12:20 +00:00
|
|
|
|
2020-06-07 00:48:15 +00:00
|
|
|
if len(c.IntelX) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
intelxKeys := sliceutil.PickRandom(c.IntelX)
|
|
|
|
if keyPartA, keyPartB, ok := multipartKey(intelxKeys); ok {
|
|
|
|
keys.IntelXHost = keyPartA
|
|
|
|
keys.IntelXKey = keyPartB
|
2020-06-07 00:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:51:52 +00:00
|
|
|
if len(c.PassiveTotal) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
passiveTotalKeys := sliceutil.PickRandom(c.PassiveTotal)
|
|
|
|
if keyPartA, keyPartB, ok := multipartKey(passiveTotalKeys); ok {
|
|
|
|
keys.PassiveTotalUsername = keyPartA
|
|
|
|
keys.PassiveTotalPassword = keyPartB
|
2019-12-04 14:51:52 +00:00
|
|
|
}
|
2019-12-04 11:39:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 18:34:42 +00:00
|
|
|
if len(c.Robtex) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.Robtex = sliceutil.PickRandom(c.Robtex)
|
2020-09-21 18:34:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 14:51:52 +00:00
|
|
|
if len(c.SecurityTrails) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.Securitytrails = sliceutil.PickRandom(c.SecurityTrails)
|
2019-12-04 14:51:52 +00:00
|
|
|
}
|
2019-12-04 18:42:55 +00:00
|
|
|
if len(c.Shodan) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
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 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.ThreatBook = sliceutil.PickRandom(c.ThreatBook)
|
2020-09-13 19:55:31 +00:00
|
|
|
}
|
2019-12-04 14:51:52 +00:00
|
|
|
if len(c.URLScan) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.URLScan = sliceutil.PickRandom(c.URLScan)
|
2019-12-04 14:51:52 +00:00
|
|
|
}
|
|
|
|
if len(c.Virustotal) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.Virustotal = sliceutil.PickRandom(c.Virustotal)
|
2019-12-04 14:51:52 +00:00
|
|
|
}
|
2020-05-16 05:32:03 +00:00
|
|
|
if len(c.ZoomEye) > 0 {
|
2022-04-26 08:00:09 +00:00
|
|
|
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 {
|
2022-04-26 08:00:09 +00:00
|
|
|
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 {
|
2022-04-26 08:00:09 +00:00
|
|
|
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 {
|
2022-04-26 08:00:09 +00:00
|
|
|
keys.FullHunt = sliceutil.PickRandom(c.FullHunt)
|
2021-12-17 03:12:05 +00:00
|
|
|
}
|
2019-12-04 11:39:53 +00:00
|
|
|
return keys
|
|
|
|
}
|