-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomainlabeler.go
593 lines (492 loc) · 15.7 KB
/
domainlabeler.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package certificate_searcher
import (
"bufio"
"encoding/json"
"fmt"
"github.com/src-d/go-oniguruma"
"golang.org/x/net/idna"
"golang.org/x/net/publicsuffix"
"log"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"unicode"
"unicode/utf8"
)
type DomainLabel int
type LabelsSources map[DomainLabel][]string
func (ls LabelsSources) MarshalJSON() ([]byte, error) {
stringLabels := make(map[string][]string)
for dl, slice := range ls {
stringLabels[dl.String()] = slice
}
return json.Marshal(stringLabels)
}
const (
// No identifiable label
UNLABELED DomainLabel = iota
// Seven Months’ Worth of Mistakes: A Longitudinal Study of Typosquatting Abuse - NDSS 2015
TYPOSQUATTING_MISSING_DOT
TYPOSQUATTING_CHAR_OMISSION
TYPOSQUATTING_CHAR_PERMUTATION
TYPOSQUATTING_CHAR_SUBSTITUTION
TYPOSQUATTING_CHAR_DUPLICATION
// You Are Who You Appear to Be: A Longitudinal Study of Domain Impersonation in TLS Certificates - CCS 2019
TARGET_EMBEDDING
// Hiding in Plain Sight: A Longitudinal Study of Combosquatting Abuse - CCS 2017
COMBOSQUATTING
// Cutting through the Confusion: A Measurement Study of Homograph Attacks - ATC 2006
HOMOGRAPH
// Needle in a Haystack: Tracking Down Elite Phishing Domains in the Wild - IMC 2018
WRONGTLD
// Bitsquatting: Exploiting bit-flips for fun, or profit? WWW 2013
BITSQUATTING
// Phishtank
PHISHTANK
// SSLBL
SSL_BLACKLIST
// Google SafeBrowsing
GOOGLE_SAFEBROWSING
)
func (dl DomainLabel) String() string {
return [...]string{
"Unlabeled",
"TYPOSQUATTING_MISSING_DOT",
"TYPOSQUATTING_CHAR_OMISSION",
"TYPOSQUATTING_CHAR_PERMUTATION",
"TYPOSQUATTING_CHAR_SUBSTITUTION",
"TYPOSQUATTING_CHAR_DUPLICATION",
"TARGET_EMBEDDING",
"COMBOSQUATTING",
"HOMOGRAPH",
"WRONGTLD",
"BITSQUATTING",
"PHISHTANK",
"SSL_BLACKLIST",
"GOOGLE_SAFEBROWSING",
}[dl]
}
func (dl *DomainLabel) MarshalJSON() ([]byte, error) {
fmt.Println("Marshalling %d", dl)
return []byte(dl.String()), nil
}
type DomainLabeler interface {
//LabelDomain(domain string) []DomainLabel
LabelDomain(domain string) map[DomainLabel][]string
}
type BaseDomains map[string]struct{}
type Mutation string
type MutatedDomains map[Mutation]BaseDomains
type DomainMutator interface {
GetMutations() MutatedDomains
}
func AddMutation(mutatedDomains MutatedDomains, mutation Mutation, baseDomain string) {
if _, present := mutatedDomains[mutation]; !present {
mutatedDomains[mutation] = make(BaseDomains)
}
mutatedDomains[mutation][baseDomain] = struct{}{}
}
type TypoSquattingLabeler struct {
BaseDomains *[]string
MissingDotDomains MutatedDomains
CharOmissionDomains MutatedDomains
CharPermutationDomains MutatedDomains
CharSubstitutionDomains MutatedDomains
CharDuplicationDomains MutatedDomains
AllMutatedDomains MutatedDomains
}
func NewTypoSquattingLabeler(baseDomains *[]string) *TypoSquattingLabeler {
tsl := &TypoSquattingLabeler{
BaseDomains: baseDomains,
MissingDotDomains: make(MutatedDomains),
CharOmissionDomains: make(MutatedDomains),
CharPermutationDomains: make(MutatedDomains),
CharSubstitutionDomains: make(MutatedDomains),
CharDuplicationDomains: make(MutatedDomains),
AllMutatedDomains: make(MutatedDomains),
}
for _, domain := range *baseDomains {
if strings.HasPrefix(domain, "www.") {
AddMutation(tsl.MissingDotDomains, Mutation("www"+domain[4:]), domain)
AddMutation(tsl.AllMutatedDomains, Mutation("www"+domain[4:]), domain)
}
runeDomain := []rune(domain)
for idx, r := range runeDomain {
if !unicode.IsLetter(r) {
continue
}
tempSlice := append(make([]rune, 0), runeDomain[:idx]...)
omittedCharDomain := string(append(tempSlice, runeDomain[idx+1:]...))
AddMutation(tsl.CharOmissionDomains, Mutation(omittedCharDomain), domain)
AddMutation(tsl.AllMutatedDomains, Mutation(omittedCharDomain), domain)
}
for idx, char := range runeDomain {
if idx >= len(domain)-1 {
break
}
if !unicode.IsLetter(char) || !unicode.IsLetter(runeDomain[idx+1]) {
continue
}
if runeDomain[idx] == runeDomain[idx+1] {
continue
}
tempSlice := append(make([]rune, 0), runeDomain[:idx]...)
tempSlice = append(tempSlice, runeDomain[idx+1], runeDomain[idx])
permutedCharDomain := string(append(tempSlice, runeDomain[idx+2:]...))
AddMutation(tsl.CharPermutationDomains, Mutation(permutedCharDomain), domain)
AddMutation(tsl.AllMutatedDomains, Mutation(permutedCharDomain), domain)
}
for idx, char := range runeDomain {
if !unicode.IsLetter(char) {
continue
}
tempSlice := append(make([]rune, 0), runeDomain[:idx]...)
tempSlice = append(tempSlice, runeDomain[idx])
duplicatedCharDomain := string(append(tempSlice, runeDomain[idx:]...))
AddMutation(tsl.CharDuplicationDomains, Mutation(duplicatedCharDomain), domain)
AddMutation(tsl.AllMutatedDomains, Mutation(duplicatedCharDomain), domain)
}
for idx, char := range runeDomain {
if !QwertyAlphanumeric(char) {
continue
}
adjacentChars, err := QwertyAdjacentRunes(char)
if err != nil {
continue
}
for _, adjacentChar := range adjacentChars {
tempSlice := append(make([]rune, 0), runeDomain[:idx]...)
tempSlice = append(tempSlice, adjacentChar)
substitutedCharDomain := string(append(tempSlice, runeDomain[idx+1:]...))
AddMutation(tsl.CharSubstitutionDomains, Mutation(substitutedCharDomain), domain)
AddMutation(tsl.AllMutatedDomains, Mutation(substitutedCharDomain), domain)
}
}
}
return tsl
}
func (t *TypoSquattingLabeler) LabelDomain(domain string) map[DomainLabel][]string {
domainLabels := make(map[DomainLabel][]string)
mutation := Mutation(domain)
if _, present := t.MissingDotDomains[mutation]; present {
domainLabels[TYPOSQUATTING_MISSING_DOT] = make([]string, 0)
for k, _ := range t.MissingDotDomains[mutation] {
domainLabels[TYPOSQUATTING_MISSING_DOT] = append(domainLabels[TYPOSQUATTING_MISSING_DOT], k)
}
}
if _, present := t.CharOmissionDomains[mutation]; present {
domainLabels[TYPOSQUATTING_CHAR_OMISSION] = make([]string, 0)
for k, _ := range t.CharOmissionDomains[mutation] {
domainLabels[TYPOSQUATTING_CHAR_OMISSION] = append(domainLabels[TYPOSQUATTING_CHAR_OMISSION], k)
}
}
if _, present := t.CharPermutationDomains[mutation]; present {
domainLabels[TYPOSQUATTING_CHAR_PERMUTATION] = make([]string, 0)
for k, _ := range t.CharPermutationDomains[mutation] {
domainLabels[TYPOSQUATTING_CHAR_PERMUTATION] = append(domainLabels[TYPOSQUATTING_CHAR_PERMUTATION], k)
}
}
if _, present := t.CharDuplicationDomains[mutation]; present {
domainLabels[TYPOSQUATTING_CHAR_DUPLICATION] = make([]string, 0)
for k, _ := range t.CharDuplicationDomains[mutation] {
domainLabels[TYPOSQUATTING_CHAR_DUPLICATION] = append(domainLabels[TYPOSQUATTING_CHAR_DUPLICATION], k)
}
}
if _, present := t.CharSubstitutionDomains[mutation]; present {
domainLabels[TYPOSQUATTING_CHAR_SUBSTITUTION] = make([]string, 0)
for k, _ := range t.CharSubstitutionDomains[mutation] {
domainLabels[TYPOSQUATTING_CHAR_SUBSTITUTION] = append(domainLabels[TYPOSQUATTING_CHAR_SUBSTITUTION], k)
}
}
return domainLabels
}
func (t *TypoSquattingLabeler) GetMutations() MutatedDomains {
return t.AllMutatedDomains
}
type TargetEmbeddingLabeler struct {
BaseDomains *[]string
CombinedRegex *rubex.Regexp
RegexString string
Mux sync.Mutex
}
func NewTargetEmbeddingLabeler(baseDomains *[]string) *TargetEmbeddingLabeler {
tel := &TargetEmbeddingLabeler{
BaseDomains: baseDomains,
}
regexExpressions := make([]string, len(*baseDomains))
for idx, domain := range *baseDomains {
regexDomain := rubex.QuoteMeta(domain)
regexExpressions[idx] = `(?:[-\.]|^)` + regexDomain + `[-\.]`
}
tel.RegexString = strings.Join(regexExpressions, "|")
tel.CombinedRegex = rubex.MustCompile(strings.Join(regexExpressions, "|"))
return tel
}
func (t *TargetEmbeddingLabeler) LabelDomain(domain string) map[DomainLabel][]string {
t.Mux.Lock()
firstMatch := t.CombinedRegex.FindString(domain)
t.Mux.Unlock()
if len(firstMatch) > 0 {
return map[DomainLabel][]string{
TARGET_EMBEDDING: {firstMatch},
}
}
return make(map[DomainLabel][]string)
}
type HomoGraphLabeler struct {
BaseDomainMap map[string]struct{}
HomographDomains MutatedDomains
}
/*
Ingests an array of unicode strings, and generates a list of
punycode (ASCII) homographs for labeling domains.
*/
func NewHomoGraphLabeler(baseDomains *[]string) *HomoGraphLabeler {
domains := make(map[string]struct{})
hl := &HomoGraphLabeler{
HomographDomains: make(MutatedDomains),
}
for _, domain := range *baseDomains {
mutations := make(chan Mutation)
domains[domain] = struct{}{}
go GenerateASCIIHomographs(mutations, domain, 2)
for mutation := range mutations {
if _, present := hl.HomographDomains[mutation]; !present {
hl.HomographDomains[mutation] = make(BaseDomains)
}
hl.HomographDomains[mutation][domain] = struct{}{}
}
}
hl.BaseDomainMap = domains
return hl
}
func (t *HomoGraphLabeler) LabelDomain(domain string) map[DomainLabel][]string {
domainLabels := make(map[DomainLabel][]string)
mutation := Mutation(domain)
if _, present := t.HomographDomains[mutation]; present {
domainLabels[HOMOGRAPH] = make([]string, 0)
for k, _ := range t.HomographDomains[mutation] {
domainLabels[HOMOGRAPH] = append(domainLabels[HOMOGRAPH], k)
}
}
return domainLabels
}
type BitSquattingLabeler struct {
BaseDomains *[]string
BitSquattedDomains MutatedDomains
}
func uint8Exp2(pow int) uint8 {
output := uint8(1)
for i := 0; i < pow; i++ {
output = output * 2
}
return output
}
func NewBitSquattingLabeler(baseDomains *[]string) *BitSquattingLabeler {
bsl := &BitSquattingLabeler{
BaseDomains: baseDomains,
BitSquattedDomains: make(MutatedDomains),
}
for _, domain := range *baseDomains {
for idx := range domain {
for offset := 0; offset < 8; offset++ {
tempSlice := make([]byte, 0)
tempSlice = append(tempSlice, domain[:idx]...)
bitFlippedByte := uint8Exp2(offset) ^ domain[idx]
tempSlice = append(tempSlice, bitFlippedByte)
bitFlippedDomain := append(tempSlice, domain[idx+1:]...)
if utf8.Valid(bitFlippedDomain) && ValidHostname(string(bitFlippedDomain)) {
AddMutation(bsl.BitSquattedDomains, Mutation(bitFlippedDomain), domain)
}
}
}
}
return bsl
}
func (b *BitSquattingLabeler) LabelDomain(domain string) map[DomainLabel][]string {
domainLabels := make(map[DomainLabel][]string)
mutation := Mutation(domain)
if _, present := b.BitSquattedDomains[mutation]; present {
domainLabels[BITSQUATTING] = make([]string, 0)
for k, _ := range b.BitSquattedDomains[mutation] {
domainLabels[BITSQUATTING] = append(domainLabels[BITSQUATTING], k)
}
}
return domainLabels
}
func (b *BitSquattingLabeler) GetMutations() MutatedDomains {
return b.BitSquattedDomains
}
type WrongTLDLabeler struct {
BaseDomains *[]string
OneOrTwoTLDs map[string]struct{}
WrongTLDDomains MutatedDomains
}
func NewWrongTLDLabeler(baseDomains *[]string) *WrongTLDLabeler {
wtl := &WrongTLDLabeler{
BaseDomains: baseDomains,
WrongTLDDomains: make(MutatedDomains),
OneOrTwoTLDs: make(map[string]struct{}),
}
PublicSuffixListUrl := "https://publicsuffix.org/list/public_suffix_list.dat"
resp, err := http.Get(PublicSuffixListUrl)
defer resp.Body.Close()
if err != nil {
log.Fatalf("Unable to download PSL list from %s", PublicSuffixListUrl)
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || strings.HasPrefix(line, "//") {
continue
}
if len(strings.Split(line, ".")) > 2 {
continue
}
wtl.OneOrTwoTLDs[line] = struct{}{}
}
for _, domain := range *baseDomains {
publicSuffix, _ := publicsuffix.PublicSuffix(domain)
eTLDplus1, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil {
log.Fatalf("Unable to extract eTLD+1 from %s: %s", domain, err.Error())
}
domainNameSansETLD := strings.TrimSuffix(eTLDplus1, "."+publicSuffix)
for eTLD, _ := range wtl.OneOrTwoTLDs {
if eTLD != publicSuffix {
punyDomain, err := idna.ToASCII(domainNameSansETLD + "." + eTLD)
if err != nil {
log.Fatalf("Error converting %s to ascii/punycode", domainNameSansETLD+"."+eTLD)
}
AddMutation(wtl.WrongTLDDomains, Mutation(punyDomain), domain)
}
}
}
return wtl
}
func (w *WrongTLDLabeler) LabelDomain(domain string) map[DomainLabel][]string {
domainLabels := make(map[DomainLabel][]string)
mutation := Mutation(domain)
if _, present := w.WrongTLDDomains[mutation]; present {
domainLabels[WRONGTLD] = make([]string, 0)
for k, _ := range w.WrongTLDDomains[mutation] {
domainLabels[WRONGTLD] = append(domainLabels[WRONGTLD], k)
}
}
return domainLabels
}
func (w *WrongTLDLabeler) GetMutations() MutatedDomains {
return w.WrongTLDDomains
}
type ComboSquattingLabeler struct {
}
func NewComboSquattingLabeler(baseDomains *[]string) *ComboSquattingLabeler {
tel := &ComboSquattingLabeler{
}
return tel
}
func (t *ComboSquattingLabeler) LabelDomain(domain string) {
}
type PhishTankLabeler struct {
blacklistedDomains map[string]struct{}
}
func NewPhishTankLabeler() *PhishTankLabeler {
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("No caller information")
}
file, err := os.Open(filepath.Join(path.Dir(filename), "domainlists/phishtank-hostnames-2018-09-19-to-2020-05-26.sanitized.txt"))
if err != nil {
panic(err)
}
defer file.Close()
ptl := &PhishTankLabeler{
blacklistedDomains: make(map[string]struct{}),
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if domain == "" {
continue
}
ptl.blacklistedDomains[domain] = struct{}{}
}
return ptl
}
func (p *PhishTankLabeler) LabelDomain(domain string) map[DomainLabel][]string {
domainLabels := make(map[DomainLabel][]string)
if _, present := p.blacklistedDomains[domain]; present {
domainLabels[PHISHTANK] = nil
}
return domainLabels
}
type OpenPhishLabeler struct {
blacklistedDomains map[string]struct{}
}
func NewOpenPhishLabeler() *OpenPhishLabeler {
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("No caller information")
}
file, err := os.Open(filepath.Join(path.Dir(filename), "domainlists/openphish-hostnames-2018-09-19-to-2020-05-26.sanitized.txt"))
if err != nil {
panic(err)
}
defer file.Close()
ptl := &OpenPhishLabeler{
blacklistedDomains: make(map[string]struct{}),
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if domain == "" {
continue
}
ptl.blacklistedDomains[domain] = struct{}{}
}
return ptl
}
func (p *OpenPhishLabeler) LabelDomain(domain string) map[DomainLabel][]string {
domainLabels := make(map[DomainLabel][]string)
if _, present := p.blacklistedDomains[domain]; present {
domainLabels[PHISHTANK] = nil
}
return domainLabels
}
type SafeBrowsingLabeler struct {
blacklistedDomains map[string]struct{}
}
func NewSafeBrowsingLabeler() *SafeBrowsingLabeler {
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("No caller information")
}
file, err := os.Open(filepath.Join(path.Dir(filename), "domainlists/gsb_blacklist_2018-10-29-to-2020-05-26.sanitized.txt"))
if err != nil {
panic(err)
}
defer file.Close()
ptl := &SafeBrowsingLabeler{
blacklistedDomains: make(map[string]struct{}),
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if domain == "" {
continue
}
ptl.blacklistedDomains[domain] = struct{}{}
}
return ptl
}
func (p *SafeBrowsingLabeler) LabelDomain(domain string) map[DomainLabel][]string {
domainLabels := make(map[DomainLabel][]string)
if _, present := p.blacklistedDomains[domain]; present {
domainLabels[GOOGLE_SAFEBROWSING] = nil
}
return domainLabels
}