-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
224 lines (197 loc) · 5.11 KB
/
main.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
package main
import (
gc "crypto/x509"
"database/sql"
"encoding/csv"
"encoding/hex"
"flag"
"fmt"
"log"
"os"
"path"
"strings"
"sync"
"time"
"golang.org/x/text/language"
"golang.org/x/text/message"
"github.com/zmap/zcrypto/x509"
"github.com/zmap/zlint/v3/lint"
_ "github.com/zmap/zlint/v3/lints/apple"
_ "github.com/zmap/zlint/v3/lints/cabf_br"
_ "github.com/zmap/zlint/v3/lints/cabf_ev"
_ "github.com/zmap/zlint/v3/lints/community"
_ "github.com/zmap/zlint/v3/lints/etsi"
_ "github.com/zmap/zlint/v3/lints/mozilla"
_ "github.com/zmap/zlint/v3/lints/rfc"
"github.com/zmap/zlint/v3/test"
"github.com/cloudflare/cfssl/revoke"
_ "github.com/lib/pq"
)
type service struct {
wgWrite sync.WaitGroup
wgWork sync.WaitGroup
writer chan []string
worker chan []byte
lintName string
lintConfig lint.Configuration
}
func (s *service) doWork() {
for {
der, more := <-s.worker
if !more {
s.wgWork.Done()
return
}
cert, err := x509.ParseCertificate(der)
if err != nil {
log.Println(err)
continue
}
result := test.TestLintCert(s.lintName, cert, s.lintConfig)
if result.Status != lint.NA && result.Status != lint.Pass {
gcert, err := gc.ParseCertificate(cert.Raw)
if err != nil {
log.Println("Failed to parse in native go")
gcert = &gc.Certificate{}
}
revoked, ok, err := revoke.VerifyCertificateError(gcert)
s.writer <- []string{
fmt.Sprintf("https://crt.sh?sha256=%s", hex.EncodeToString(cert.FingerprintSHA256)),
cert.ValidationLevel.String(),
cert.Issuer.String(),
strings.ToUpper(strings.Join(cert.Subject.Country, ", ")),
cert.Subject.String(),
cert.NotBefore.String(),
cert.NotAfter.String(),
result.Details,
fmt.Sprintf("%t", revoked),
fmt.Sprintf("%t", ok),
fmt.Sprintf("%v", err),
}
}
}
}
func (s *service) doWrite(w *csv.Writer) {
for {
line, more := <-s.writer
if !more {
w.Flush()
s.wgWrite.Done()
return
}
err := w.Write(line)
if err != nil {
log.Println(err)
}
}
}
func main() {
var crtID int64
var nw, batch int
var filename, lintName string
flag.Int64Var(&crtID, "offset", 0, "Last crt.sh ID processed")
flag.IntVar(&nw, "workers", 10, "Number of concurrent worker")
flag.IntVar(&batch, "batch", 1000, "Number of certificates to ask for per query")
flag.StringVar(&filename, "out", "result.csv", "Output filename")
flag.StringVar(&lintName, "lint", "", "Lint name (required)")
flag.Usage = func() {
fmt.Printf("usage: `%s [flags]`\n", path.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
if len(lintName) == 0 {
log.Fatal("a lint name must be provided with `-lint e_name_of_the_lint`")
}
p := message.NewPrinter(language.English)
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
writer := csv.NewWriter(f)
defer f.Close()
if crtID == 0 {
err = writer.Write([]string{
"crt.sh",
"Validation",
"Issuer",
"Country",
"Subject",
"NotBefore",
"NotAfter",
"Error details",
"Rev. revoked",
"Rev. ok",
"Rev. error",
})
if err != nil {
log.Fatal(err)
}
}
s := service{
writer: make(chan []string, 10*nw),
worker: make(chan []byte, 100*nw),
lintName: lintName,
lintConfig: lint.NewEmptyConfig(),
}
s.wgWrite.Add(1)
go s.doWrite(writer)
for i := 0; i < nw; i++ {
s.wgWork.Add(1)
go s.doWork()
}
total_processed := 0
i := batch
for i == batch {
connStr := "postgres://guest@crt.sh/certwatch?port=5432&sslmode=disable&binary_parameters=yes"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Println(err)
time.Sleep(1 * time.Minute)
continue
}
defer db.Close()
db.SetConnMaxLifetime(time.Second * 60)
db.SetMaxOpenConns(1)
// TODO: Add configurable query filters
// AND (SELECT x509_nameAttributes(c.CERTIFICATE, 'organizationName', TRUE) LIMIT 1) IS NOT NULL
rows, err := db.Query(`SELECT c.ID, c.CERTIFICATE FROM certificate c WHERE
coalesce(x509_notAfter(c.CERTIFICATE), 'infinity'::timestamp) >= date_trunc('year', now() AT TIME ZONE 'UTC')
AND x509_notAfter(c.CERTIFICATE) >= now() AT TIME ZONE 'UTC'
AND x509_hasExtension(c.CERTIFICATE, '1.3.6.1.4.1.11129.2.4.3', TRUE)
AND c.ID > $1
ORDER BY c.ID
LIMIT $2`, crtID, batch)
if err != nil {
log.Println("in query:", err)
time.Sleep(1 * time.Minute)
continue
}
defer rows.Close()
p.Printf("Query completed (for crt.sh ID > %d)\n", crtID)
i = 0
for rows.Next() {
i++
var der []byte
if err := rows.Scan(&crtID, &der); err != nil {
log.Println(err)
continue
}
s.worker <- der
}
total_processed += i
p.Printf("Processed %d (up to crt.sh ID %d)\n", i, crtID)
p.Printf("[%s] Total Processed: %d\n", time.Now().Format("2006-01-02 15:04:05"), total_processed)
if err := rows.Err(); err != nil {
p.Printf("[%s] in row %d: %s", time.Now().Format("2006-01-02 15:04:05"), i, err)
time.Sleep(1 * time.Minute)
i = batch
}
}
close(s.worker)
fmt.Println("Waiting on worker(s) to finish")
s.wgWork.Wait()
close(s.writer)
fmt.Println("Waiting on writer to finish")
s.wgWrite.Wait()
fmt.Println("Done")
}