-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
188 lines (172 loc) · 5.47 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
package main
/*
* Copyright 2018 Global Cyber Alliance
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITION OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"encoding/json"
"fmt"
"log"
"net"
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/GlobalCyberAlliance/GCADMARCRiskScanner/scanner"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)
var (
useOpenDNS, useGoogleDNS, useLevel3DNS bool
nameservers []string
timeout time.Duration
batchSize int
zonep bool
showProgress bool
verbose bool
rootCmd = &cobra.Command{
Use: "drs",
Short: "Scan a domain for SPF, DMARC, or DKIM records.",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
spfCmd = &cobra.Command{
Use: "spf [domain]",
Short: "Scan a domain for an SPF record.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
runScan("SPF", args[0], "")
},
}
dmarcCmd = &cobra.Command{
Use: "dmarc [domain]",
Short: "Scan a domain for a DMARC record.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
runScan("DMARC", args[0], "")
},
}
dkimCmd = &cobra.Command{
Use: "dkim [domain] [selector]",
Short: "Scan a domain for an DKIM record given a specific selector.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
runScan("DKIM", args[0], args[1])
},
}
bulkCmd = &cobra.Command{
Use: "bulk",
Short: "Scan any number of domains for SPF and DMARC records. Defaults to STDIN",
Run: func(cmd *cobra.Command, args []string) {
runBulkScan()
},
}
)
func main() {
bulkCmd.Flags().BoolVar(&useOpenDNS, "opendns", false, "Use OpenDNS's nameservers")
bulkCmd.Flags().BoolVar(&useGoogleDNS, "google", false, "Use Google's nameservers")
bulkCmd.Flags().BoolVar(&useLevel3DNS, "level3", false, "Use Level3's nameservers")
bulkCmd.Flags().StringSliceVarP(&nameservers, "nameservers", "n", nil, "Use specific nameservers, in `host[:port]` format; may be specified multiple times")
bulkCmd.Flags().DurationVarP(&timeout, "timeout", "t", 15*time.Second, "Timeout duration for a DNS query")
bulkCmd.Flags().IntVarP(&batchSize, "concurrent", "c", runtime.NumCPU(), "The number of domains to scan concurrently")
bulkCmd.Flags().BoolVarP(&zonep, "zonefile", "z", false, "Input file/pipe contains an RFC 1035 zone file")
bulkCmd.Flags().BoolVarP(&showProgress, "progress", "p", false, "Show a progress bar (disabled when reading from STDIN)")
rootCmd.AddCommand(
spfCmd,
dmarcCmd,
dkimCmd,
bulkCmd,
)
rootCmd.Execute()
}
// runScan scans a domain for one of three record types based on argument input.
// Domains are formatted to match the RFC specifications of each record type.
// SPF - RFC7208, DMARC - RFC7489, DKIM - RFC4871
func runScan(record string, domain string, selector string) {
var d string
u, err := url.Parse(domain)
if err != nil {
log.Fatalln("Please enter a valid domain:", err)
}
if u.Scheme == "http" || u.Scheme == "https" {
d = u.Host
} else {
d = u.Path
}
switch record {
case "SPF":
d = d
case "DMARC":
d = "_dmarc." + d
case "DKIM":
d = selector + "._domainkey." + d
}
results, err := net.LookupTXT(d)
if err != nil {
log.Fatalln("An error occured scanning domain for", record, "records:", err)
}
for _, v := range results {
if strings.Contains(v, record) || strings.Contains(v, strings.ToLower(record)) {
fmt.Println(v)
}
}
}
// runBulkScan scans multiple domains from STDIN by default.
// DNS services can be specified, as well as different input methods.
// Outputs to STDOUT by default.
func runBulkScan() {
opts := []scanner.ScannerOption{
scanner.WithTimeout(timeout),
scanner.ConcurrentScans(batchSize),
}
if useOpenDNS {
nameservers = append(nameservers, scanner.OpenDNS...)
}
if useGoogleDNS {
nameservers = append(nameservers, scanner.GoogleDNS...)
}
if useLevel3DNS {
nameservers = append(nameservers, scanner.Level3...)
}
opts = append(opts, scanner.UseNameservers(nameservers))
// Decide where we want to read the list of domain names from.
//
// If there are no arguments, read from STDIN.
var source scanner.Source
if flag.NArg() == 0 && zonep {
source = scanner.ZonefileSource(os.Stdin)
} else if flag.NArg() > 0 && zonep {
fmt.Fprintln(os.Stderr, "error: -z flag provided, but not reading from STDIN")
os.Exit(1)
} else if flag.NArg() == 0 {
source = scanner.TextSource(os.Stdin)
} else {
sr := strings.NewReader(strings.Join(flag.Args(), "\n"))
source = scanner.TextSource(sr)
}
sc, err := scanner.New(opts...)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(2)
}
// Set up a *json.Encoder that encodes scan results to STDOUT.
jsenc := json.NewEncoder(os.Stdout)
for result := range sc.Start(source) {
if err := jsenc.Encode(result); err != nil {
fmt.Fprintln(os.Stderr, "error encoding scan result:", err)
}
}
}