-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dns.go
199 lines (157 loc) · 4.24 KB
/
dns.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
package dns
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hidracloud/hidra/v2/pkg/models"
"github.com/hidracloud/hidra/v2/pkg/scenarios"
"github.com/hidracloud/hidra/v2/pkg/utils"
"github.com/likexian/whois"
whoisparser "github.com/likexian/whois-parser"
"github.com/StalkR/dnssec-analyzer/dnssec"
)
// Scenario Represent an ssl scenario
type Scenario struct {
models.Scenario
domain string
whoisInfo *whoisparser.WhoisInfo
}
func (s *Scenario) whoisFrom(ctx context.Context, c map[string]string) ([]models.Metric, error) {
if c["domain"] == "" {
return nil, fmt.Errorf("domain is required")
}
whoisResult, err := whois.Whois(c["domain"])
if err != nil {
return nil, err
}
result, err := whoisparser.Parse(whoisResult)
if err != nil {
return nil, err
}
s.whoisInfo = &result
s.domain = c["domain"]
return nil, nil
}
func (s *Scenario) dnsSecShouldBeValid(ctx context.Context, c map[string]string) ([]models.Metric, error) {
if c["domain"] == "" {
return nil, fmt.Errorf("domain is required")
}
analysis, err := dnssec.Analyze(c["domain"])
if err != nil {
return nil, err
}
utils.LogDebug("dnssec analysis", analysis)
if strings.Contains(analysis.String(), "No DNSKEY records found") {
utils.LogDebug("No DNSKEY records found")
return nil, nil
}
if analysis.Status() != dnssec.OK {
return nil, fmt.Errorf("domain has invalid dnssec configuration")
}
return nil, nil
}
// RCA generate RCAs for scenario
func (s *Scenario) RCA(result *models.ScenarioResult) error {
log.Println("WHOIS RCA")
return nil
}
func (s *Scenario) dumpMetrics(ctx context.Context, c map[string]string) ([]models.Metric, error) {
customMetrics := make([]models.Metric, 0)
dateFormat := "2006-01-02T15:04:05.999Z"
if c["dateFormat"] != "" {
dateFormat = c["dateFormat"]
}
expirationDate, err := time.Parse(dateFormat, s.whoisInfo.Domain.ExpirationDate)
if err != nil {
return nil, err
}
customMetrics = append(customMetrics, models.Metric{
Name: "whois_expiration_date",
Value: float64(expirationDate.Unix()),
Labels: map[string]string{
"domain": s.domain,
},
})
return customMetrics, nil
}
func (s *Scenario) shouldBeValidFor(ctx context.Context, c map[string]string) ([]models.Metric, error) {
duration, err := utils.ParseDuration(c["for"])
if err != nil {
return nil, err
}
limitDate := time.Now().Add(duration)
dateFormat := "2006-01-02T15:04:05.999Z"
if c["dateFormat"] != "" {
dateFormat = c["dateFormat"]
}
expirationDate, err := time.Parse(dateFormat, s.whoisInfo.Domain.ExpirationDate)
if err != nil {
return nil, err
}
if limitDate.After(expirationDate) {
return nil, fmt.Errorf("domain will be invalid after %s, and your limit date is %s", expirationDate, limitDate)
}
return nil, nil
}
// Description return the description of the scenario
func (s *Scenario) Description() string {
return "Run a Whois scenario"
}
// Close closes the scenario
func (s *Scenario) Close() {
// nothing to do
}
// Init initialize the scenario
func (s *Scenario) Init() {
s.StartPrimitives()
s.RegisterStep("whoisFrom", models.StepDefinition{
Description: "Get whois from domain",
Params: []models.StepParam{
{
Name: "domain",
Description: "Domain to get whois",
Optional: false,
},
},
Fn: s.whoisFrom,
})
s.RegisterStep("shouldBeValidFor", models.StepDefinition{
Description: "Check if domain is valid for a given duration",
Params: []models.StepParam{
{
Name: "for",
Description: "Duration to check",
Optional: false,
},
{
Name: "dateFormat",
Description: "Date format to parse, default is 2006-01-02T15:04:05.999Z",
Optional: true,
},
},
Fn: s.shouldBeValidFor,
})
s.RegisterStep("dumpMetrics", models.StepDefinition{
Description: "Dump metrics",
Params: []models.StepParam{},
Fn: s.dumpMetrics,
})
s.RegisterStep("dnsSecShouldBeValid", models.StepDefinition{
Description: "Check if domain has valid dnssec configuration",
Params: []models.StepParam{
{
Name: "domain",
Description: "Domain to get whois",
Optional: false,
},
},
Fn: s.dnsSecShouldBeValid,
})
}
func init() {
scenarios.Add("dns", func() models.IScenario {
return &Scenario{}
})
}