-
Notifications
You must be signed in to change notification settings - Fork 0
/
spf.go
210 lines (179 loc) · 5.47 KB
/
spf.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
package spf
import (
"fmt"
"regexp"
"slices"
"strings"
"github.com/lwears/gospoofcheck/emailprotections/shared"
"github.com/miekg/dns"
)
type SpfRecord struct {
Version string
Record string
Mechanisms []string
AllString string
Domain string
RecursionDepth int8
}
func FromDomain(opts *shared.Options) (*SpfRecord, error) {
spfString, err := GetSpfStringForDomain(opts)
if err != nil {
return nil, fmt.Errorf("error getting spf string for domain %s", err)
}
return FromSpfString(spfString, opts.Domain)
}
func FromSpfString(spfString *string, domain string) (*SpfRecord, error) {
if spfString == nil {
return &SpfRecord{Domain: domain}, nil
}
mechanisms := extractMechanisms(*spfString)
return &SpfRecord{
Domain: domain,
Record: *spfString,
Mechanisms: mechanisms,
Version: extractVersion(*spfString),
AllString: extractAllMechanism(mechanisms),
}, nil
}
func GetSpfStringForDomain(opts *shared.Options) (*string, error) {
if !shared.IsValidDomainName(opts.Domain) {
return nil, shared.InvalidDomainError
}
fqnd := dns.Fqdn(opts.Domain)
m := dns.Msg{}
m.SetQuestion(fqnd, dns.TypeTXT)
m.RecursionDesired = true
// Whatever this is fixed it
m.SetEdns0(4096, false)
r, err := dns.Exchange(&m, opts.DnsResolver)
if err != nil {
return nil, err
}
result := findSpfStringFromAnswers(r.Answer)
return result, nil
}
// func (spf *SpfRecord) GetRedirectedRecord() string {
// if spf.RecursionDepth >= 10 {
// return spf.GetRedirectDomain()
// } else {
// rd := spf.GetRedirectDomain()
// if rd != "" {
// rd = FromDomain(rd)
// }
// }
// }
func (spf *SpfRecord) GetRedirectDomain() string {
if len(spf.Mechanisms) > 0 {
for _, m := range spf.Mechanisms {
matches := regexp.MustCompile("redirect=(.*)").FindStringSubmatch(m)
if len(matches) >= 1 {
return matches[1]
}
}
}
return ""
}
func (spf *SpfRecord) IsRedirectMechanismStrong(dnsResolver string) (bool, error) {
if redirectDomain := spf.GetRedirectDomain(); redirectDomain != "" {
redirectDomainSpf, err := FromDomain(&shared.Options{DnsResolver: dnsResolver, Domain: redirectDomain})
if err != nil {
return false, fmt.Errorf("error fetching redirect domain %s", err)
}
isRedirectDomainSpfStrong, err := redirectDomainSpf.IsRecordStrong(&shared.Options{DnsResolver: dnsResolver})
if err != nil {
return false, fmt.Errorf("error checking redirect domains spf %s", err)
}
return redirectDomainSpf != nil && isRedirectDomainSpfStrong, nil
}
return false, nil
}
func (spf *SpfRecord) IsRecordStrong(opts *shared.Options) (bool, error) {
allStrength := spf.IsAllMechanismStrong()
redirectStrength, err := spf.IsRedirectMechanismStrong(opts.DnsResolver)
if err != nil {
return false, fmt.Errorf("error checking redirect mechanism %s", err)
}
includeStrength, err := spf.AreIncludeMechanismsStrong(opts)
if err != nil {
return false, fmt.Errorf("error checking include mechanisms %s", err)
}
return allStrength && redirectStrength && includeStrength, nil
}
func (spf *SpfRecord) IsAllMechanismStrong() bool {
return slices.Contains([]string{"~all", "-all"}, spf.AllString)
}
func (spf *SpfRecord) AreIncludeMechanismsStrong(opts *shared.Options) (bool, error) {
includeRecords, err := spf.GetIncludeRecords(opts.DnsResolver)
if err != nil {
return false, fmt.Errorf("error fetching include record spf %s", err)
}
for _, r := range includeRecords {
if _, ok := includeRecords[r.Domain]; ok {
return includeRecords[r.Domain].IsRecordStrong(opts)
}
}
return false, nil
}
func (spf *SpfRecord) GetIncludeDomains() []string {
includeDomains := make([]string, 0)
if len(spf.Mechanisms) > 0 {
for _, m := range spf.Mechanisms {
includeMechanism := regexp.MustCompile(`include:(\S+)`).FindStringSubmatch(m)
if len(includeMechanism) >= 1 {
includeDomains = append(includeDomains, includeMechanism[1])
}
}
}
return includeDomains
}
func (spf *SpfRecord) GetIncludeRecords(dnsResolver string) (map[string]*SpfRecord, error) {
start := make(map[string]*SpfRecord)
if spf.RecursionDepth >= 10 {
return start, nil
} else {
includeDomains := spf.GetIncludeDomains()
for _, domain := range includeDomains {
var err error
start[domain], err = FromDomain(&shared.Options{Domain: domain, DnsResolver: dnsResolver})
if err != nil {
return nil, fmt.Errorf("error fetching include record spf %s", err)
}
start[domain].RecursionDepth = spf.RecursionDepth
}
}
return start, nil
}
func findSpfStringFromAnswers(txtRecords []dns.RR) *string {
for _, a := range txtRecords {
if x, ok := a.(*dns.TXT); ok {
// If spf is longer than 255 bytes its split into multiple strings
if len(x.Txt) > 1 {
x.Txt = []string{strings.Join(x.Txt, "")}
}
for _, t := range x.Txt {
if strings.Contains(t, "v=spf1") {
return &t
}
}
}
}
return nil
}
func extractMechanisms(spfString string) []string {
spfMechanismPattern := regexp.MustCompile(`(?:((?:\+|-|~)?(?:a|mx|ptr|include|ip4|ip6|exists|redirect|exp|all)(?:(?::|=|/)?(?:\S*))?) ?)`)
return spfMechanismPattern.FindAllString(spfString, -1)
}
func extractVersion(spfString string) string {
spfVersionPattern := regexp.MustCompile("^v=(spf.)")
spfVersion := spfVersionPattern.FindString(spfString)
return spfVersion
}
func extractAllMechanism(mechanisms []string) string {
allMachanism := ""
for _, m := range mechanisms {
if m == regexp.MustCompile(".*all.*").FindString(m) {
allMachanism = strings.TrimSpace(m)
}
}
return allMachanism
}