/
config.go
155 lines (136 loc) · 4.13 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package runner
import (
"math/rand"
"os"
"strings"
"time"
"github.com/projectdiscovery/subfinder/pkg/subscraping"
"gopkg.in/yaml.v3"
)
// ConfigFile contains the fields stored in the configuration file
type ConfigFile struct {
// Resolvers contains the list of resolvers to use while resolving
Resolvers []string `yaml:"resolvers,omitempty"`
// Sources contains a list of sources to use for enumeration
Sources []string `yaml:"sources,omitempty"`
// ExcludeSources contains the sources to not include in the enumeration process
ExcludeSources []string `yaml:"exclude-sources,omitempty"`
// API keys for different sources
Binaryedge []string `yaml:"binaryedge"`
Censys []string `yaml:"censys"`
Certspotter []string `yaml:"certspotter"`
Chaos []string `yaml:"chaos"`
DNSDB []string `yaml:"dnsdb"`
PassiveTotal []string `yaml:"passivetotal"`
SecurityTrails []string `yaml:"securitytrails"`
Shodan []string `yaml:"shodan"`
Spyse []string `yaml:"spyse"`
URLScan []string `yaml:"urlscan"`
Virustotal []string `yaml:"virustotal"`
ZoomEye []string `yaml:"zoomeye"`
}
// GetConfigDirectory gets the subfinder config directory for a user
func GetConfigDirectory() (string, error) {
// Seed the random number generator
rand.Seed(time.Now().UnixNano())
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
os.MkdirAll(config, os.ModePerm)
return config, nil
}
// CheckConfigExists checks if the config file exists in the given path
func CheckConfigExists(configPath string) bool {
if _, err := os.Stat(configPath); err == nil {
return true
} else if os.IsNotExist(err) {
return false
}
return false
}
// MarshalWrite writes the marshalled yaml config to disk
func (c ConfigFile) MarshalWrite(file string) error {
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return err
}
// Indent the spaces too
enc := yaml.NewEncoder(f)
enc.SetIndent(4)
err = enc.Encode(&c)
f.Close()
return err
}
// UnmarshalRead reads the unmarshalled config yaml file from disk
func UnmarshalRead(file string) (ConfigFile, error) {
config := ConfigFile{}
f, err := os.Open(file)
if err != nil {
return config, err
}
err = yaml.NewDecoder(f).Decode(&config)
f.Close()
return config, err
}
// 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 ConfigFile) GetKeys() subscraping.Keys {
keys := subscraping.Keys{}
if len(c.Binaryedge) > 0 {
keys.Binaryedge = c.Binaryedge[rand.Intn(len(c.Binaryedge))]
}
if len(c.Censys) > 0 {
censysKeys := c.Censys[rand.Intn(len(c.Censys))]
parts := strings.Split(censysKeys, ":")
if len(parts) == 2 {
keys.CensysToken = parts[0]
keys.CensysSecret = parts[1]
}
}
if len(c.Certspotter) > 0 {
keys.Certspotter = c.Certspotter[rand.Intn(len(c.Certspotter))]
}
if len(c.Chaos) > 0 {
keys.Chaos = c.Chaos[rand.Intn(len(c.Chaos))]
}
if (len(c.DNSDB)) > 0 {
keys.DNSDB = c.DNSDB[rand.Intn(len(c.DNSDB))]
}
if len(c.PassiveTotal) > 0 {
passiveTotalKeys := c.PassiveTotal[rand.Intn(len(c.PassiveTotal))]
parts := strings.Split(passiveTotalKeys, ":")
if len(parts) == 2 {
keys.PassiveTotalUsername = parts[0]
keys.PassiveTotalPassword = parts[1]
}
}
if len(c.SecurityTrails) > 0 {
keys.Securitytrails = c.SecurityTrails[rand.Intn(len(c.SecurityTrails))]
}
if len(c.Shodan) > 0 {
keys.Shodan = c.Shodan[rand.Intn(len(c.Shodan))]
}
if len(c.Spyse) > 0 {
keys.Spyse = c.Spyse[rand.Intn(len(c.Spyse))]
}
if len(c.URLScan) > 0 {
keys.URLScan = c.URLScan[rand.Intn(len(c.URLScan))]
}
if len(c.Virustotal) > 0 {
keys.Virustotal = c.Virustotal[rand.Intn(len(c.Virustotal))]
}
if len(c.ZoomEye) > 0 {
zoomEyeKeys := c.ZoomEye[rand.Intn(len(c.ZoomEye))]
parts := strings.Split(zoomEyeKeys, ":")
if len(parts) == 2 {
keys.ZoomEyeUsername = parts[0]
keys.ZoomEyePassword = parts[1]
}
}
return keys
}