-
Notifications
You must be signed in to change notification settings - Fork 19
/
dmarc.go
333 lines (281 loc) · 8.36 KB
/
dmarc.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Copyright 2019 Adevinta
*/
package main
import (
"fmt"
"net"
"net/mail"
"strconv"
"strings"
report "github.com/adevinta/vulcan-report"
)
// DMARC represents a DMARC dns record
// https://tools.ietf.org/html/rfc7489#section-6.4
type DMARC struct {
version string
request string
sp string
rua string
ruf string
adkim string
aspf string
ri string
fo string
rf string
pct string
otherFields map[string]string
vulnerabilities []report.Vulnerability
target string
}
// Domain Owner DMARC preferences are stored as DNS TXT records in subdomains named "_dmarc".
// https://tools.ietf.org/html/rfc7489#section-6.1
const prefix = "_dmarc."
const sep = ";"
func (dmarc *DMARC) evaluate() {
if dmarc.request == "none" {
vuln := vulns["tag-p-is-none"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if dmarc.request == "quarantine" {
vuln := vulns["tag-p-is-quarantine"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.rua) == 0 {
vuln := vulns["tag-rua-not-configured"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.ruf) == 0 {
vuln := vulns["tag-ruf-not-configured"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.pct) > 0 && dmarc.pct != "100" {
vuln := vulns["tag-pct-not-100"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.adkim) > 0 && dmarc.adkim != "r" && dmarc.adkim != "s" {
vuln := vulns["tag-adkim-not-valid"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.aspf) > 0 && dmarc.aspf != "r" && dmarc.aspf != "s" {
vuln := vulns["tag-aspf-not-valid"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.sp) > 0 && dmarc.sp != "none" && dmarc.sp != "reject" && dmarc.sp != "quarantine" {
vuln := vulns["tag-sp-not-valid"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.fo) > 0 && dmarc.fo != "0" && dmarc.fo != "1" && dmarc.fo != "d" && dmarc.fo != "s" {
vuln := vulns["tag-fo-not-valid"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.rf) > 0 && dmarc.rf != "afrf" {
vuln := vulns["tag-rf-not-valid"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.ri) > 0 {
_, err := strconv.Atoi(dmarc.ri)
if err != nil {
vuln := vulns["tag-ri-not-valid"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
}
if len(dmarc.rua) > 0 {
vuln := vulns["tag-rua-not-valid-mailto"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
if len(dmarc.ruf) > 0 {
vuln := vulns["tag-ruf-not-valid-mailto"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
}
func (dmarc *DMARC) validateEmailList(list, vulnerabilityName string) {
emails := strings.Split(list, ",")
for _, email := range emails {
if strings.HasPrefix(email, "mailto:") {
_, err := mail.ParseAddress(email[7:])
if err != nil {
vuln := vulns[vulnerabilityName]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
break
}
} else {
vuln := vulns[vulnerabilityName]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
break
}
}
}
func (dmarc *DMARC) parse(domain string) bool {
records, _ := net.LookupTXT(prefix + domain)
return dmarc.parseTxtRecords(records)
}
func (dmarc *DMARC) parseTxtRecords(records []string) bool {
foundDmarc := false
for _, record := range records {
if dmarc.parseFields(record) {
if foundDmarc {
vuln := vulns["multiple-dmarc-found"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
foundDmarc = true
}
}
if !foundDmarc && len(dmarc.vulnerabilities) == 0 {
vuln := vulns["dmarc-not-found"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
return true
}
func (dmarc *DMARC) parseFields(record string) bool {
// A DMARC policy record MUST comply with the formal specification found
// in Section 6.4 in that the "v" and "p" tags MUST be present and MUST
// appear in that order.
tagVersion, valueVersion, txtRecord, err := extractTagAndValue(record)
if err != nil {
// error trying to extract the first tag, score zero
vuln := vulns["unable-to-parse-tags"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
if tagVersion != "v" {
// the first tag is diferent than 'v', the spec requires the first field to be 'v'
vuln := vulns["v-and-p-invalid-or-missing"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
if valueVersion != "DMARC1" {
// the first tag is 'v' but the value is diferent than 'DMARC1' , the spec requires the version value to be set to 'DMARC1'
vuln := vulns["tag-v-wrong-value"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
tagRequest, valueRequest, txtRecord, err := extractTagAndValue(txtRecord)
if err != nil {
// error trying to extract the second tag, score zero
vuln := vulns["unable-to-parse-tags"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
isValidRequest := make(map[string]bool)
isValidRequest["none"] = true
isValidRequest["quarantine"] = true
isValidRequest["reject"] = true
if tagRequest != "p" {
// the second tag is diferent than 'p' , the spec requires the second field to be 'v'
vuln := vulns["v-and-p-invalid-or-missing"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
if !isValidRequest[valueRequest] {
// the second tag is 'p' but the value is not a valid one [none, quarantine, reject]
vuln := vulns["tag-p-wrong-value"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
}
dmarc.version = valueVersion
dmarc.request = valueRequest
dmarc.otherFields = make(map[string]string)
// parse the remaining fields
for len(txtRecord) > 0 {
var tag string
var value string
tag, value, txtRecord, err = extractTagAndValue(txtRecord)
if err != nil {
// error trying to extract the tag, this means a malformed record, score zero
vuln := vulns["unable-to-parse-tags"]
vuln.AffectedResource = dmarc.target
dmarc.vulnerabilities = append(dmarc.vulnerabilities, vuln)
return false
}
if tag == "sp" {
dmarc.sp = value
continue
}
if tag == "rua" {
dmarc.rua = value
continue
}
if tag == "ruf" {
dmarc.ruf = value
continue
}
if tag == "adkim" {
dmarc.adkim = value
continue
}
if tag == "aspf" {
dmarc.aspf = value
continue
}
if tag == "ri" {
dmarc.ri = value
continue
}
if tag == "fo" {
dmarc.fo = value
continue
}
if tag == "rf" {
dmarc.rf = value
continue
}
if tag == "pct" {
dmarc.pct = value
continue
}
dmarc.otherFields[tag] = value
}
return true
}
func extractTagAndValue(txtRecord string) (tag, value, remainingRecord string, err error) {
if len(strings.Trim(txtRecord, " ")) == 0 {
return
}
index := strings.Index(txtRecord, sep)
if index < 0 {
index = len(txtRecord)
}
tagAndValue := txtRecord[:index]
parts := strings.Split(tagAndValue, "=")
if len(parts) != 2 {
err = fmt.Errorf("Malformed tag: %v", tagAndValue)
return "", "", "", err
}
tag = strings.Trim(parts[0], " ")
value = strings.Trim(parts[1], " ")
if index+1 > len(txtRecord) {
remainingRecord = ""
} else {
remainingRecord = txtRecord[index+1:]
}
return tag, value, strings.TrimLeft(remainingRecord, " "), nil
}
func checkMX(domain string) bool {
records, _ := net.LookupMX(domain)
return len(records) > 0
}