forked from zema1/watchvuln
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
329 lines (298 loc) · 7.98 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
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
package main
import (
"context"
"database/sql"
entSql "entgo.io/ent/dialect/sql"
"fmt"
"github.com/kataras/golog"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"github.com/zema1/watchvuln/ent"
"github.com/zema1/watchvuln/ent/migrate"
"github.com/zema1/watchvuln/ent/vulninformation"
"github.com/zema1/watchvuln/grab"
"github.com/zema1/watchvuln/push"
"golang.org/x/sync/errgroup"
"modernc.org/sqlite"
"os"
"os/signal"
"sync"
"time"
)
func init() {
sql.Register("sqlite3", &sqlite.Driver{})
}
var log = golog.Child("[main]")
func main() {
golog.Default.SetLevel("info")
app := cli.NewApp()
app.Name = "watchvuln"
app.Usage = "A high valuable vulnerability watcher and pusher"
app.Version = "v0.1.0"
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "set log level to debug, print more details",
Value: false,
},
&cli.StringFlag{
Name: "interval",
Aliases: []string{"i"},
Usage: "checking every [interval], supported format like 30s, 30m, 1h",
Value: "30m",
},
&cli.StringFlag{
Name: "dingding-access-token",
Aliases: []string{"dt"},
Usage: "access token of dingding bot",
},
&cli.StringFlag{
Name: "dingding-sign-secret",
Aliases: []string{"ds"},
Usage: "sign secret of dingding bot",
},
}
app.Before = func(c *cli.Context) error {
if c.Bool("debug") {
golog.Default.SetLevel("debug")
}
return nil
}
app.Action = Action
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func Action(c *cli.Context) error {
ctx, cancel := signalCtx()
defer cancel()
dingToken := c.String("dingding-access-token")
dingSecret := c.String("dingding-sign-secret")
if os.Getenv("DINGDING_ACCESS_TOKEN") != "" {
dingToken = os.Getenv("DINGDING_ACCESS_TOKEN")
}
if os.Getenv("DINGDING_SECRET") != "" {
dingSecret = os.Getenv("DINGDING_SECRET")
}
if dingToken == "" || dingSecret == "" {
return fmt.Errorf("invalid dingding token, \nusage: %s -dt DINGDING_ACCESS_TOKEN -ds DINGDING_SECRET \nor set via env", os.Args[0])
}
debug := c.Bool("debug")
iv := c.String("interval")
if os.Getenv("INTERVAL") != "" {
iv = os.Getenv("INTERVAL")
}
interval, err := time.ParseDuration(iv)
if err != nil {
return err
}
if interval.Minutes() < 1 && !debug {
return fmt.Errorf("interval is too small, at least 1m")
}
drv, err := entSql.Open("sqlite3", "file:vuln_v1.sqlite3?cache=shared&_pragma=foreign_keys(1)")
if err != nil {
return errors.Wrap(err, "failed opening connection to sqlite")
}
db := drv.DB()
db.SetMaxOpenConns(1)
dbClient := ent.NewClient(ent.Driver(drv))
defer dbClient.Close()
if err := dbClient.Schema.Create(ctx, migrate.WithDropIndex(true), migrate.WithDropColumn(true)); err != nil {
return errors.Wrap(err, "failed creating schema resources")
}
grabbers := []grab.Grabber{
grab.NewAVDCrawler(),
grab.NewTiCrawler(),
grab.NewOSCSCrawler(),
}
count, err := dbClient.VulnInformation.Query().Count(ctx)
if err != nil {
return errors.Wrap(err, "failed creating schema resources")
}
log.Infof("local database has %d vulns", count)
if count < 20000 {
log.Infof("local data is outdated, init database")
eg, initCtx := errgroup.WithContext(ctx)
eg.SetLimit(len(grabbers))
for _, grabber := range grabbers {
grabber := grabber
eg.Go(func() error {
return initData(initCtx, dbClient, grabber)
})
}
err = eg.Wait()
if err != nil {
return errors.Wrap(err, "init data")
}
log.Infof("grabber finished successfully, local db has %d vulns", dbClient.VulnInformation.Query().CountX(ctx))
}
// 初次启动不要推送数据, 以免长时间没运行狂发消息
vulns, err := collectUpdate(ctx, dbClient, grabbers)
if err != nil {
return errors.Wrap(err, "initial collect")
}
log.Infof("system init finished, found %d new vulns (skip pushing)", len(vulns))
log.Infof("ticking every %s", interval)
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
log.Infof("next checking at %s\n", time.Now().Add(interval).Format("2006-01-02 15:04:05"))
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
hour := time.Now().Hour()
if hour > 0 && hour < 7 {
// we must sleep in this period
log.Infof("sleeping..")
continue
}
vulns, err = collectUpdate(ctx, dbClient, grabbers)
if err != nil {
log.Errorf("failed to get updates, %s", err)
continue
}
log.Infof("found %d new vulns in this ticking", len(vulns))
for _, v := range vulns {
if v.Creator.IsValuable(v) {
log.Infof("publishing new vuln %s", v)
err = push.DingDingSend(v, dingToken, dingSecret)
if err != nil {
log.Errorf("send dingding msg error, %s", err)
break
}
}
}
}
}
}
func initData(ctx context.Context, dbClient *ent.Client, grabber grab.Grabber) error {
pageSize := 100
source := grabber.SourceInfo()
total, err := grabber.GetPageCount(ctx, pageSize)
if err != nil {
return nil
}
log.Infof("%s total page: %d", source.Name, total)
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(20)
for i := 1; i <= total; i++ {
i := i
eg.Go(func() error {
dataChan, err := grabber.ParsePage(ctx, i, pageSize)
if err != nil {
return err
}
for data := range dataChan {
if _, err = createOrUpdate(ctx, dbClient, source, data); err != nil {
return errors.Wrap(err, data.String())
}
}
return nil
})
}
err = eg.Wait()
if err != nil {
return err
}
return nil
}
func collectUpdate(ctx context.Context, dbClient *ent.Client, grabbers []grab.Grabber) ([]*grab.VulnInfo, error) {
pageSize := 10
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(len(grabbers))
var mu sync.Mutex
var newVulns []*grab.VulnInfo
for _, grabber := range grabbers {
grabber := grabber
eg.Go(func() error {
source := grabber.SourceInfo()
pageCount, err := grabber.GetPageCount(ctx, pageSize)
if err != nil {
return err
}
for i := 1; i <= pageCount; i++ {
dataChan, err := grabber.ParsePage(ctx, i, pageSize)
if err != nil {
return err
}
hasNewVuln := false
for data := range dataChan {
isNewVuln, err := createOrUpdate(ctx, dbClient, source, data)
if err != nil {
return err
}
if isNewVuln {
log.Infof("found new vuln: %s", data)
mu.Lock()
newVulns = append(newVulns, data)
mu.Unlock()
hasNewVuln = true
}
}
// 如果一整页漏洞都是旧的,说明没有更新,不必再继续下一页了
if !hasNewVuln {
return nil
}
}
return nil
})
}
err := eg.Wait()
return newVulns, err
}
func createOrUpdate(ctx context.Context, dbClient *ent.Client, source *grab.SourceInfo, data *grab.VulnInfo) (bool, error) {
vuln, err := dbClient.VulnInformation.Query().
Where(vulninformation.Key(data.UniqueKey)).
First(ctx)
// not exist
if err != nil {
newVuln, err := dbClient.VulnInformation.
Create().
SetKey(data.UniqueKey).
SetTitle(data.Title).
SetDescription(data.Description).
SetSeverity(string(data.Severity)).
SetCve(data.CVE).
SetDisclosure(data.Disclosure).
SetSolutions(data.Solutions).
SetReferences(data.References).
SetTags(data.Tags).
SetFrom(data.From).
Save(ctx)
if err != nil {
return false, err
}
log.Debugf("vuln %d created from %s %s", newVuln.ID, newVuln.Key, source.Name)
return true, nil
}
// update
newVuln, err := vuln.Update().SetKey(data.UniqueKey).
SetTitle(data.Title).
SetDescription(data.Description).
SetSeverity(string(data.Severity)).
SetCve(data.CVE).
SetDisclosure(data.Disclosure).
SetSolutions(data.Solutions).
SetReferences(data.References).
SetTags(data.Tags).
SetFrom(data.From).
Save(ctx)
if err != nil {
return false, err
}
log.Debugf("vuln %d updated from %s %s", newVuln.ID, newVuln.Key, source.Name)
return false, nil
}
func signalCtx() (context.Context, func()) {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
cancel()
}()
return ctx, cancel
}