-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
check.go
308 lines (269 loc) · 7.36 KB
/
check.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
package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
)
// main function
func (a *app) checkPackage(registryType registryType, p string, checkExpired bool) error {
a.log.Debugf("checking package %s", p)
var maintainer []string
var downloads int64
var err error
switch registryType {
case RegistryNPM:
maintainer, err = a.npmGetPackageMaintainer(p)
if err != nil {
return fmt.Errorf("could not get maintainer for package %s: %w", p, err)
}
downloads, err = a.npmGetDownloadCountLastYear(p)
if err != nil {
return fmt.Errorf("could not get downloadcount for package %s: %w", p, err)
}
case RegistryPypi:
maintainer, err = a.pypiGetPackageMaintainer(p)
if err != nil {
return fmt.Errorf("could not get maintainer for package %s: %w", p, err)
}
// not returned by API, need to use google bigquery for this which complicates things a lot
downloads = -1
default:
return fmt.Errorf("unknown registry type")
}
for _, m := range maintainer {
m = strings.TrimSpace(m)
// npm user if it starts with @
if m == "" || strings.HasPrefix(m, "@") {
continue
}
split := strings.Split(m, "@")
if len(split) != 2 {
a.log.Debugf("maintainer %s is no valid email address", m)
continue
}
domain := strings.TrimSpace(split[1])
if domain == "users.noreply.github.com" {
continue
}
if domain == "" {
continue
}
maintainerReturn, err := a.checkDomain(domain, checkExpired)
if err != nil {
a.log.WithError(err).Error("")
continue
}
// we return nil on repeated errors
if maintainerReturn == nil {
continue
}
var text string
fields := logrus.Fields{
"package": p,
"maintainer": m,
}
switch registryType {
case RegistryNPM:
fields["link"] = getNPMLink(p)
case RegistryPypi:
fields["link"] = getPypiLink(p)
default:
return fmt.Errorf("invalid registry type")
}
if downloads >= 0 {
// pypi downloads are currently always -1
fields["downloads"] = downloads
}
if maintainerReturn.unregistered {
text = "[HIT] DOMAIN UNREGISTERED"
fields["domain"] = maintainerReturn.domain
} else if maintainerReturn.expired && checkExpired {
text = "[POSSIBLE HIT] DOMAIN EXPIRES WITHIN 7 DAYS OR IS ALREADY EXPIRED"
fields["domain"] = maintainerReturn.domain
fields["expiration"] = maintainerReturn.expireDate
fields["registrar"] = maintainerReturn.registrar
} else if maintainerReturn.unregisteredMX {
text = "[HIT] UNREGISTERED MX"
fields["domain"] = maintainerReturn.domain
fields["mx"] = strings.Join(maintainerReturn.mxDomains, ", ")
}
if text != "" {
a.printResult(downloads, text, fields)
}
}
return nil
}
type checkReturn struct {
unregistered bool
domain string
expired bool
registrar string
expireDate string
unregisteredMX bool
mxDomains []string
}
func (a *app) checkDomain(domain string, checkExpired bool) (*checkReturn, error) {
unregistered, err := a.checkDomainUnregistered(domain)
if err != nil {
var whoiserr *WhoisError
if errors.As(err, &whoiserr) {
if whoiserr.repeatedError {
// ignore already printed errors
return nil, nil
}
}
return nil, fmt.Errorf("could not check domain %s for unregistered state: %w", domain, err)
}
mxUnregisteredDomains, err := a.checkDomainMXUnregistered(domain)
if err != nil {
return nil, fmt.Errorf("could not check domain %s for unregistered MX state: %w", domain, err)
}
ret := checkReturn{
unregistered: unregistered,
domain: domain,
unregisteredMX: len(mxUnregisteredDomains) > 0,
mxDomains: mxUnregisteredDomains,
}
if checkExpired {
expired, date, registrar, err := a.checkDomainExpiresSoon(domain)
if err != nil {
var whoiserr *WhoisError
if errors.As(err, &whoiserr) {
if whoiserr.repeatedError {
// ignore already printed errors
return nil, nil
}
}
return nil, fmt.Errorf("could not check domain %s for expiry: %w", domain, err)
}
ret.expired = expired
ret.expireDate = date
ret.registrar = registrar
}
return &ret, nil
}
func (a *app) checkMXUnregistered(mx string, domain string) (bool, error) {
// check A entry of MX, if it exists we do not need to do a whois which is rate limited
aRecords, err := a.domainResolve(mx)
if err != nil {
return false, err
}
if len(aRecords) > 0 {
return false, nil
}
whois, err := a.whois(mx)
if err != nil {
var whoiserr *WhoisError
if errors.As(err, &whoiserr) {
if whoiserr.repeatedError {
// ignore already printed errors
return false, nil
}
}
return false, fmt.Errorf("error on checking mx whois for %s: %w", mx, err)
}
if whois == nil {
return true, nil
}
return false, err
}
func (a *app) checkDomainExpiresSoon(domain string) (bool, string, string, error) {
// make sure we check the root domain here and not a subdomain
rootDomain, err := getRootDomain(domain)
if err != nil {
return false, "", "", fmt.Errorf("could not get root domain for %s: %w", domain, err)
}
whois, err := a.whois(rootDomain)
if err != nil {
return false, "", "", fmt.Errorf("error on whois for %s: %w", rootDomain, err)
}
if whois == nil {
return false, "", "", nil
}
expires, err := a.compareDomainExpiryDate(rootDomain, whois.Domain.ExpirationDateInTime, 7)
if err != nil {
return false, "", "", err
}
if expires {
expirationDate := ""
registrar := ""
if whois.Domain != nil {
expirationDate = whois.Domain.ExpirationDate
}
if whois.Registrar != nil {
registrar = whois.Registrar.Name
}
return true, expirationDate, registrar, nil
}
return false, "", "", nil
}
func (a *app) checkDomainUnregistered(domain string) (bool, error) {
// make sure we check the root domain here and not a subdomain
rootDomain, err := getRootDomain(domain)
if err != nil {
return false, fmt.Errorf("could not get root domain for %s: %w", domain, err)
}
nameServer, err := a.domainNS(rootDomain)
if err != nil {
return false, err
}
// no nameservers returned, do a whois
if len(nameServer) == 0 {
whois, err := a.whois(rootDomain)
if err != nil {
return false, fmt.Errorf("error on whois for %s: %w", rootDomain, err)
}
if whois == nil {
// domain unregistered, return this
return true, nil
}
}
return false, nil
}
func (a *app) checkDomainMXUnregistered(domain string) ([]string, error) {
var unregisteredDomains []string
// check mx records
mx, err := a.domainMX(domain)
if err != nil {
return unregisteredDomains, err
}
if len(mx) > 0 {
for _, mxEntry := range mx {
if mxEntry == "" {
continue
}
unregistered, err := a.checkMXUnregistered(mxEntry, domain)
if err != nil {
a.log.WithError(err).Error("")
continue
}
if unregistered {
unregisteredDomains = append(unregisteredDomains, mxEntry)
}
}
}
return unregisteredDomains, nil
}
func (a *app) compareDomainExpiryDate(domain string, expirationDate *time.Time, daysBefore int) (bool, error) {
if expirationDate == nil {
return false, nil
}
date := time.Now()
alertDate := date.AddDate(0, 0, daysBefore)
if expirationDate.Before(alertDate) { // nolint: gosimple
return true, nil
}
return false, nil
}
func (a *app) printResult(downloads int64, text string, fields logrus.Fields) {
switch {
case downloads >= 1000000:
a.log.WithFields(fields).Error(text)
case downloads >= 100000 && downloads < 1000000:
a.log.WithFields(fields).Warn(text)
default:
a.log.WithFields(fields).Info(text)
}
}