-
Notifications
You must be signed in to change notification settings - Fork 0
/
passbleed.go
143 lines (128 loc) · 3.06 KB
/
passbleed.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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"net/url"
"os"
"sort"
"strings"
"golang.org/x/net/publicsuffix"
)
// Different CSV formats
const (
UnknownFormat = -1 + iota
LastPassFormat
_
_
KeePass1Format
KeePassXFormat
_
_
_
OnePasswordFormat
)
// Domain to check
// +gen set
type Domain string
func findCSVType(reader *csv.Reader) (int, error) {
header, err := reader.Read()
if err == io.EOF {
return UnknownFormat, fmt.Errorf("empty CSV file")
} else if err != nil {
return UnknownFormat, err
}
switch {
case len(header) >= 1 && header[0] == "url":
return LastPassFormat, nil
case len(header) >= 4 && header[3] == "Web Site":
return KeePass1Format, nil
case len(header) >= 5 && header[4] == "URL":
return KeePassXFormat, nil
case len(header) >= 9 && header[8] == "urls":
return OnePasswordFormat, nil
default:
return UnknownFormat, fmt.Errorf("unknown CSV format, please use a supported export format (see README)")
}
}
func buildKeepass(filename string) (*DomainSet, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
reader.LazyQuotes = true
col, err := findCSVType(reader)
if err != nil {
return nil, err
}
kpDomains := NewDomainSet()
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
// If we get a bare hostname without scheme, url.Parse sets the host
// as empty and puts the full hostname in Path. Add a "http://" in
// front of bare hosts to prevent that.
if !strings.Contains(record[col], "//") {
record[col] = "http://" + record[col]
}
potential, err := url.Parse(record[col])
if err != nil {
continue
}
tld, err := publicsuffix.EffectiveTLDPlusOne(potential.Hostname())
if err != nil {
continue
}
kpDomains.Add(Domain(tld))
}
return &kpDomains, nil
}
func buildCloudBleed(filename string) (*DomainSet, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
cbDomains := NewDomainSet()
for scanner.Scan() {
cbDomains.Add(Domain(scanner.Text()))
}
return &cbDomains, scanner.Err()
}
func main() {
if len(os.Args) < 3 {
usage()
os.Exit(2)
}
fmt.Print("Building KeePass domain list... ")
keepass, err := buildKeepass(os.Args[1])
if err != nil {
fmt.Println("Error building KeePass domain list:", err)
os.Exit(1)
}
fmt.Println(keepass.Cardinality(), "domains found.")
fmt.Print("Building CloudBleed domain list... ")
cloudBleed, err := buildCloudBleed(os.Args[2])
if err != nil {
fmt.Println("Error building CloudBleed domain list:", err)
os.Exit(1)
}
fmt.Print(cloudBleed.Cardinality(), " domains found.\n\n")
inDanger := keepass.Intersect(*cloudBleed).ToSlice()
sort.Slice(inDanger, func(i, j int) bool { return inDanger[i] < inDanger[j] })
fmt.Println(len(inDanger), "potentially endangered domains:")
for _, k := range inDanger {
fmt.Println(k)
}
}
func usage() {
fmt.Println(`usage: ` + os.Args[0] + ` <keepass.csv> <sorted_unique_cf.txt>`)
}