-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
276 lines (239 loc) · 7.07 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"syscall"
"time"
"github.com/caffix/resolve"
)
var DefaultBaselineResolvers = []string{
"8.8.8.8", // Google
"1.1.1.1", // Cloudflare
"9.9.9.9", // Quad9
"208.67.222.222", // Cisco OpenDNS
"209.244.0.3", // Level3
"64.6.64.6", // Verisign
"84.200.69.80", // DNS.WATCH
"8.26.56.26", // Comodo Secure DNS
"109.69.8.51", // puntCAT
"74.82.42.42", // Hurricane Electric
"77.88.8.8", // Yandex.DNS
}
const DefaultQueriesPerPublicResolver = 15
const DefaultQueriesPerBaselineResolver = 50
var PublicResolvers []string
func init() {
addrs := getPublicDNS()
loop:
for _, addr := range addrs {
for _, baseline := range DefaultBaselineResolvers {
if addr == baseline {
continue loop
}
}
PublicResolvers = append(PublicResolvers, addr)
}
}
func main() {
var (
input string
mainDomain string
maxDNSPerSec int
threads int
)
flag.StringVar(&input, "i", "-", "Subdomains list. Default is stdin")
flag.StringVar(&mainDomain, "d", "", "Main domain")
flag.IntVar(&threads, "t", 10, "Threads")
flag.IntVar(&maxDNSPerSec, "rate", 20000, "Max DNS Limit per second")
flag.Parse()
if mainDomain == "" {
fmt.Fprintln(os.Stderr, "Main domain is require")
os.Exit(1)
}
var sc *bufio.Scanner
if input == "" {
fmt.Fprintln(os.Stderr, "Check your input again")
os.Exit(1)
}
if input == "-" {
sc = bufio.NewScanner(os.Stdin)
} else {
subsFile, err := os.Open(input)
if err != nil {
fmt.Println("Please check your input file.")
os.Exit(1)
}
defer subsFile.Close()
sc = bufio.NewScanner(subsFile)
}
pool := publicResolverSetup(maxDNSPerSec)
if pool == nil {
fmt.Fprintln(os.Stderr, "The system was unable to build the pool of resolvers")
os.Exit(1)
}
var wg sync.WaitGroup
jobChan := make(chan string, threads)
for i := 0; i < threads; i++ {
go func() {
for sub := range jobChan {
msg := resolve.QueryMsg(sub, 1)
resp, err := pool.Query(context.Background(), msg, resolve.PriorityNormal, nil)
if err != nil {
return
}
if pool.WildcardType(context.Background(), resp, mainDomain) == resolve.WildcardTypeNone {
fmt.Println(sub)
}
}
}()
}
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if strings.HasPrefix(line, "http") {
u, err := url.Parse(line)
if err != nil {
continue
}
line = u.Hostname()
}
// subDomain := strings.ToLower(dns.RemoveAsteriskLabel(line))
line = strings.Trim(line, ".")
jobChan <- line
}
close(jobChan)
wg.Wait()
}
func getPublicDNS() []string {
var resolversList []string
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
cc := "us"
if result := getCountryCode(client); result != "" {
cc = result
}
url := "https://public-dns.info/nameserver/" + cc + ".txt"
resp, err := client.Get(url)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to get public DNS list\n")
return []string{}
}
if resp.StatusCode != http.StatusOK {
_, _ = fmt.Fprintf(os.Stderr, "Failed to get public DNS list\n")
return []string{}
}
sc := bufio.NewScanner(resp.Body)
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if err := sc.Err(); err == nil && line != "" {
resolversList = append(resolversList, line)
}
}
return resolversList
}
func getCountryCode(client *http.Client) string {
req, err := http.NewRequest("GET", "https://ipapi.co/json", nil)
if err != nil {
return ""
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ""
}
var ipinfo struct {
CountryCode string `json:"country"`
}
json.Unmarshal(body, &ipinfo)
return strings.ToLower(ipinfo.CountryCode)
}
func publicResolverSetup(maxDNSQueries int) resolve.Resolver {
max := int(float64(GetFileLimit()) * 0.7)
num := len(PublicResolvers)
if num > max {
num = max
}
if maxDNSQueries == 0 {
maxDNSQueries = num * DefaultQueriesPerPublicResolver
} else if maxDNSQueries < num {
maxDNSQueries = num
}
var trusted []resolve.Resolver
for _, addr := range DefaultBaselineResolvers {
if r := resolve.NewBaseResolver(addr, DefaultQueriesPerBaselineResolver, nil); r != nil {
trusted = append(trusted, r)
}
}
baseline := resolve.NewResolverPool(trusted, time.Second, nil, 1, nil)
r := setupResolvers(PublicResolvers, max, DefaultQueriesPerPublicResolver, nil)
return resolve.NewResolverPool(r, 2*time.Second, baseline, 2, nil)
}
func setupResolvers(addrs []string, max, rate int, log *log.Logger) []resolve.Resolver {
if len(addrs) <= 0 {
return nil
}
finished := make(chan resolve.Resolver, 10)
for _, addr := range addrs {
if _, _, err := net.SplitHostPort(addr); err != nil {
// Add the default port number to the IP address
addr = net.JoinHostPort(addr, "53")
}
go func(ip string, ch chan resolve.Resolver) {
if err := resolve.ClientSubnetCheck(ip); err == nil {
if n := resolve.NewBaseResolver(ip, rate, log); n != nil {
ch <- n
}
}
ch <- nil
}(addr, finished)
}
l := len(addrs)
var count int
var resolvers []resolve.Resolver
for i := 0; i < l; i++ {
if r := <-finished; r != nil {
if count < max {
resolvers = append(resolvers, r)
count++
continue
}
r.Stop()
}
}
if len(resolvers) == 0 {
return nil
}
return resolvers
}
// GetFileLimit attempts to raise the ulimit to the maximum hard limit and returns that value.
func GetFileLimit() int {
limit := 50000
var lim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err == nil {
lim.Cur = lim.Max
limit = int(lim.Cur)
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
return limit
}
}
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err == nil {
if cur := int(lim.Cur); cur < limit {
limit = cur
}
}
return limit
}