-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.go
164 lines (148 loc) · 3.82 KB
/
lib.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
package lib
import (
"cercat/config"
"cercat/pkg/homoglyph"
"cercat/pkg/model"
"cercat/pkg/slack"
"context"
"encoding/json"
"net"
"regexp"
"strings"
"time"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
log "github.com/sirupsen/logrus"
"golang.org/x/net/idna"
)
// the websocket stream from calidog
const certInput = "wss://certstream.calidog.io"
// CertCheckWorker parses certificates and raises alert if matches config
func CertCheckWorker(r string, homoglyph *map[string]string, msgChan chan []byte, bufferChan chan *model.Result) {
reg, _ := regexp.Compile(r)
for {
msg := <-msgChan
result, err := ParseResultCertificate(msg)
if err != nil {
log.Warnf("Error parsing message: %s", err)
continue
}
if result == nil {
continue
}
if !IsMatchingCert(homoglyph, result, reg) {
continue
}
result.Addresses = fetchIPAddresses(result.Domain)
bufferChan <- result
}
}
// ParseResultCertificate parses certificate details
func ParseResultCertificate(msg []byte) (*model.Result, error) {
var c model.Certificate
var r *model.Result
err := json.Unmarshal(msg, &c)
if err != nil || c.MessageType == "heartbeat" {
return nil, err
}
r = &model.Result{
Domain: c.Data.LeafCert.Subject["CN"],
Issuer: c.Data.LeafCert.Issuer["O"],
SAN: c.Data.LeafCert.AllDomains,
Addresses: []string{},
}
return r, nil
}
// isIPv4Net checks if IP is IPv4
func isIPv4Net(host string) bool {
return net.ParseIP(host) != nil
}
// fetchIPAddresses resolves domain to get IP
func fetchIPAddresses(name string) []string {
var ipsList []string
ips, err := net.LookupIP(name)
if err != nil || len(ips) == 0 {
log.Debugf("Could not fetch IP addresses of domain %s", name)
return ipsList
}
for _, j := range ips {
if isIPv4Net(j.String()) {
ipsList = append(ipsList, j.String())
}
}
return ipsList
}
// isIDN checks if domain is an IDN
func isIDN(domain string) bool {
s := strings.Split(domain, ".")
for _, i := range s {
if strings.HasPrefix(i, "xn--") {
return true
}
}
return false
}
// IsMatchingCert checks if certificate matches the regexp
func IsMatchingCert(homoglyphs *map[string]string, result *model.Result, reg *regexp.Regexp) bool {
domainList := append(result.SAN, result.Domain)
for _, domain := range domainList {
if isIDN(domain) {
result.IDN, _ = idna.ToUnicode(domain)
domain = homoglyph.ReplaceHomoglyph(result.IDN, *homoglyphs)
}
if reg.MatchString(domain) {
return true
}
}
return false
}
// LoopCertStream gathers messages from CertStream
func LoopCertStream(msgBuf chan []byte) {
dial := ws.Dialer{
ReadBufferSize: 8192,
WriteBufferSize: 512,
Timeout: 1 * time.Second,
}
for {
// conn, _, _, err := ws.DefaultDialer.Dial(context.Background(), certInput)
conn, _, _, err := dial.Dial(context.Background(), certInput)
if err != nil {
log.Warn("Error connecting to CertStream! Sleeping a few seconds and reconnecting...")
time.Sleep(1 * time.Second)
// conn.Close()
continue
}
for {
msg, _, err := wsutil.ReadServerData(conn)
if err != nil {
log.Warn("Error reading message from CertStream")
break
}
msgBuf <- msg
}
conn.Close()
}
}
// Notifier is a worker that receives cert, depduplicates and sends to Slack the event
func Notifier(cfg *config.Configuration) {
for {
result := <-cfg.Buffer
duplicate := false
cfg.PreviousCerts.Do(func(d interface{}) {
if result.Domain == d {
duplicate = true
}
})
if !duplicate {
cfg.PreviousCerts = cfg.PreviousCerts.Prev()
cfg.PreviousCerts.Value = result.Domain
j, _ := json.Marshal(result)
log.Infof("A certificate for '%v' has been issued : %v\n", result.Domain, string(j))
if cfg.SlackWebHookURL != "" {
go func(c *config.Configuration, r *model.Result) {
slack.NewPayload(c, result).Post(c)
}(cfg, result)
}
}
}
}