Skip to content

Commit 76cd328

Browse files
committed
add check of domain age to avoid flood
Signed-off-by: Issif <issif_github@gadz.org>
1 parent ac6411c commit 76cd328

File tree

12 files changed

+106
-181
lines changed

12 files changed

+106
-181
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ Two methods are available for configuration and can be mixed :
2727

2828
### With config file
2929

30+
The available settings are:
3031
```bash
3132
---
32-
SlackWebhookURL: "" #Slack Webhook URL
33-
SlackIconURL: "" #Slack Icon (Avatar) URL
34-
SlackUsername: "" #Slack Username
35-
Regexp: ".*\\.fr$" #Regexp to match. Can't be empty. It uses Golang regexp format
33+
SlackWebhookURL: "" # Slack Webhook URL
34+
SlackIconURL: "" # Slack Icon (Avatar) URL
35+
SlackUsername: "Cercat" # Slack Username
36+
Regexp: ".*\\.fr$" # Regexp to match. Can't be empty. It uses Golang regexp format
37+
Workers: 20 # Number of workers to compare the certificates with the Regexp
38+
TakeScreenshot: false # Try to take a screenshot of the website
39+
ScreenshotsFolder: "." # Folder to store the screenshots
40+
IgnoreOlderThan: 10 # Ignore domains older than
3641
```
3742

3843
### With env vars
@@ -41,6 +46,10 @@ Regexp: ".*\\.fr$" #Regexp to match. Can't be empty. It uses Golang regexp forma
4146
- **SLACKICONURL**: Slack Icon (Avatar) URL
4247
- **SLACKUSERNAME**: Slack Username
4348
- **REGEXP**: Regexp to match. Can't be empty. It uses Golang regexp format
49+
- **WORKERS**: Number of workers to compare the certificates with the Regexp
50+
- **TAKESCREENSHOT**: Try to take a screenshot of the website
51+
- **SCREENSHOTSFOLDER**: Folder to store the screenshots
52+
- **IGNOREOLDERTHAN**: Ignore domains older than
4453

4554
## Run
4655

cmd/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"cercat/pkg/model"
77
"cercat/pkg/slack"
88
"cercat/pkg/worker"
9-
"encoding/json"
109
"fmt"
1110
"net/http"
1211
"os"
@@ -16,7 +15,6 @@ import (
1615

1716
"github.com/arl/statsviz"
1817
"github.com/pkg/errors"
19-
log "github.com/sirupsen/logrus"
2018
"gopkg.in/alecthomas/kingpin.v2"
2119
)
2220

@@ -38,6 +36,7 @@ func main() {
3836
}
3937

4038
cfg := config.CreateConfig(configFile)
39+
// fmt.Printf("%#v\n", cfg)
4140
for i := 0; i < cfg.Workers; i++ {
4241
go worker.RunCertCheckWorker(cfg)
4342
}
@@ -58,8 +57,8 @@ func runNotifierWorker(cfg *config.Configuration) {
5857
if !duplicate {
5958
cfg.PreviousCerts = cfg.PreviousCerts.Prev()
6059
cfg.PreviousCerts.Value = result.Domain
61-
j, _ := json.Marshal(result)
62-
log.Infof("A certificate for '%v' has been issued : %v\n", result.Domain, string(j))
60+
// j, _ := json.Marshal(result)
61+
cfg.Log.Infof("A certificate for '%v' has been issued by %v\n", result.Domain, result.Registrar)
6362
if cfg.SlackWebHookURL != "" {
6463
go func(c *config.Configuration, r *model.Result) {
6564
slack.NewPayload(c, result).Post(c)

config/config.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"cercat/pkg/homoglyph"
55
"cercat/pkg/model"
66
"container/ring"
7+
"os"
78
"path"
89
"path/filepath"
910
"regexp"
1011

11-
log "github.com/sirupsen/logrus"
12+
"github.com/sirupsen/logrus"
1213
"github.com/spf13/viper"
1314
)
1415

@@ -18,14 +19,17 @@ type Configuration struct {
1819
SlackWebHookURL string
1920
SlackIconURL string
2021
SlackUsername string
21-
RegIP string
2222
Regexp string
23+
ScreenshotsFolder string
24+
TakeScreenshot bool
25+
IgnoreOlderThan int
26+
RegIP string
2327
RegexpC *regexp.Regexp
2428
PreviousCerts *ring.Ring
2529
Messages chan []byte
2630
Buffer chan *model.Result
2731
Homoglyph map[string]string
28-
ScreenshotsFolder string
32+
Log *logrus.Logger
2933
}
3034

3135
// CreateConfig provides a Configuration
@@ -34,16 +38,26 @@ func CreateConfig(configFile *string) *Configuration {
3438
Workers: 50,
3539
Homoglyph: homoglyph.GetHomoglyphMap(),
3640
PreviousCerts: ring.New(20),
37-
Messages: make(chan []byte, 50),
38-
Buffer: make(chan *model.Result, 50),
41+
Messages: make(chan []byte, 100),
42+
Buffer: make(chan *model.Result, 100),
43+
Log: logrus.New(),
3944
}
4045

46+
c.Log.SetFormatter(&logrus.TextFormatter{
47+
FullTimestamp: true,
48+
TimestampFormat: "2006-01-02 15:04:05",
49+
})
50+
c.Log.SetOutput(os.Stdout)
51+
4152
v := viper.New()
4253
v.SetDefault("SlackWebhookURL", "")
4354
v.SetDefault("SlackIconURL", "")
4455
v.SetDefault("SlackUsername", "Cercat")
4556
v.SetDefault("Regexp", "")
4657
v.SetDefault("Workers", 20)
58+
v.SetDefault("TakeScreenshot", false)
59+
v.SetDefault("ScreenshotsFolder", ".")
60+
v.SetDefault("IgnoreOlderThan", 10)
4761

4862
if *configFile != "" {
4963
d, f := path.Split(*configFile)
@@ -54,7 +68,7 @@ func CreateConfig(configFile *string) *Configuration {
5468
v.AddConfigPath(d)
5569
err := v.ReadInConfig()
5670
if err != nil {
57-
log.Fatalf("[ERROR] : Error when reading config file : %v\n", err)
71+
c.Log.Fatalf("[ERROR] : Error when reading config file : %v\n", err)
5872
}
5973
}
6074
v.AutomaticEnv()
@@ -65,14 +79,13 @@ func CreateConfig(configFile *string) *Configuration {
6579
}
6680

6781
if c.Regexp == "" {
68-
log.Fatal("Regexp can't be empty")
82+
c.Log.Fatal("Regexp can't be empty")
6983
}
7084

7185
reg, err := regexp.Compile(c.Regexp)
7286
if err != nil {
73-
log.Fatal("Bad regexp")
87+
c.Log.Fatal("Bad regexp")
7488
}
7589
c.RegexpC = reg
76-
7790
return c
7891
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
)
2121

2222
require (
23+
github.com/jpillora/go-tld v1.2.1
2324
github.com/likexian/whois v1.14.4
2425
github.com/likexian/whois-parser v1.24.2
2526
)
@@ -33,7 +34,6 @@ require (
3334
github.com/google/go-cmp v0.5.9 // indirect
3435
github.com/hashicorp/hcl v1.0.0 // indirect
3536
github.com/josharian/intern v1.0.0 // indirect
36-
github.com/jpillora/go-tld v1.2.1 // indirect
3737
github.com/likexian/gokit v0.25.9 // indirect
3838
github.com/magiconair/properties v1.8.7 // indirect
3939
github.com/mailru/easyjson v0.7.7 // indirect

lib/lib_suite_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/lib_test.go

Lines changed: 0 additions & 99 deletions
This file was deleted.

pkg/certstream/certstream.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package certstream
33
import (
44
"cercat/config"
55
"context"
6+
"fmt"
7+
"strings"
68
"time"
79

810
"github.com/gobwas/ws"
911
"github.com/gobwas/ws/wsutil"
10-
log "github.com/sirupsen/logrus"
1112
)
1213

1314
// the websocket stream from calidog
@@ -16,24 +17,22 @@ const certInput = "wss://certstream.calidog.io"
1617
// StartLoopCertStream gathers messages from CertStream
1718
func StartLoopCertStream(cfg *config.Configuration) {
1819
dial := ws.Dialer{
19-
ReadBufferSize: 8192,
20-
WriteBufferSize: 512,
21-
Timeout: 1 * time.Second,
20+
ReadBufferSize: 2048,
21+
WriteBufferSize: 128,
22+
Timeout: 5 * time.Second,
2223
}
2324
for {
24-
// conn, _, _, err := ws.DefaultDialer.Dial(context.Background(), certInput)
2525
conn, _, _, err := dial.Dial(context.Background(), certInput)
2626
if err != nil {
27-
log.Warn(err)
28-
log.Warn("Error connecting to CertStream! Sleeping a few seconds and reconnecting...")
27+
cfg.Log.Warn("Error connecting to CertStream! Sleeping a few seconds and reconnecting")
2928
time.Sleep(1 * time.Second)
30-
// conn.Close()
3129
continue
3230
}
3331
for {
3432
msg, _, err := wsutil.ReadServerData(conn)
33+
// fmt.Println(string(msg))
3534
if err != nil {
36-
log.Warn("Error reading message from CertStream")
35+
cfg.Log.Warn(fmt.Sprintf("Error reading message from CertStream (%v)", strings.TrimSuffix(err.Error(), " ")))
3736
break
3837
}
3938
cfg.Messages <- msg

0 commit comments

Comments
 (0)