-
Notifications
You must be signed in to change notification settings - Fork 158
/
main.go
564 lines (536 loc) · 14.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/zema1/watchvuln/push"
"gopkg.in/yaml.v3"
"os"
"os/signal"
"strings"
"time"
"github.com/zema1/watchvuln/ctrl"
"github.com/kataras/golog"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
var log = golog.Child("[main]")
var Version = "v2.3.0"
func main() {
golog.Default.SetLevel("info")
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "print the version",
Category: "[Other Options]",
}
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "show help",
Category: "[Other Options]",
}
app := cli.NewApp()
app.Name = "watchvuln"
app.Usage = "A high valuable vulnerability watcher and pusher"
app.Version = Version
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "config file path, support json or yaml",
},
&cli.StringFlag{
Name: "dingding-access-token",
Aliases: []string{"dt"},
Usage: "webhook access token of dingding bot",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "dingding-sign-secret",
Aliases: []string{"ds"},
Usage: "sign secret of dingding bot",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "wechatwork-key",
Aliases: []string{"wk"},
Usage: "webhook key of wechat work",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "lark-access-token",
Aliases: []string{"lt"},
Usage: "webhook access token/url of lark",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "lark-sign-secret",
Aliases: []string{"ls"},
Usage: "sign secret of lark",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "serverchan-key",
Aliases: []string{"sk"},
Usage: "send key for server chan",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "pushplus-key",
Aliases: []string{"pk"},
Usage: "send key for push plus",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "webhook-url",
Aliases: []string{"webhook"},
Usage: "your webhook server url, ex: http://127.0.0.1:1111/webhook",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "lanxin-domain",
Aliases: []string{"lxd"},
Usage: "your lanxin server url, ex: https://apigw-example.domain",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "lanxin-hook-token",
Aliases: []string{"lxt"},
Usage: "lanxin hook token",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "lanxin-sign-secret",
Aliases: []string{"lxs"},
Usage: "sign secret of lanxin",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "bark-url",
Aliases: []string{"bark"},
Usage: "your bark server url, ex: http://127.0.0.1:1111/DeviceKey",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "telegram-bot-token",
Aliases: []string{"tgtk"},
Usage: "telegram bot token, ex: 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "telegram-chat-ids",
Aliases: []string{"tgids"},
Usage: "chat ids want to send on telegram, ex: 123456,4312341,123123",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "whitelist-file",
Aliases: []string{"wf"},
Usage: "specify a file that contains some keywords, vulns with these keywords will be pushed",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "blacklist-file",
Aliases: []string{"bf"},
Usage: "specify a file that contains some keywords, vulns with these products will NOT be pushed",
Category: "[\x00Push Options]",
},
&cli.StringFlag{
Name: "db-conn",
Aliases: []string{"db"},
Usage: "database connection string",
Value: "sqlite3://vuln_v3.sqlite3",
Category: "[Launch Options]",
},
&cli.StringFlag{
Name: "sources",
Aliases: []string{"s"},
Usage: "set vuln sources",
Value: "avd,nox,oscs,threatbook,seebug,struts2,kev,venustech",
Category: "[Launch Options]",
},
&cli.StringFlag{
Name: "interval",
Aliases: []string{"i"},
Usage: "checking every [interval], supported format like 30s, 30m, 1h",
Value: "30m",
Category: "[Launch Options]",
},
&cli.StringFlag{
Name: "proxy",
Aliases: []string{"x"},
Usage: "set request proxy, support socks5://xxx or http(s)://",
Category: "[Launch Options]",
},
&cli.BoolFlag{
Name: "enable-cve-filter",
Usage: "enable a filter that vulns from multiple sources with same cve id will be sent only once",
Value: true,
Category: "[Launch Options]",
},
&cli.BoolFlag{
Name: "no-start-message",
Aliases: []string{"nm"},
Usage: "disable the hello message when server starts",
Category: "[Launch Options]",
},
&cli.BoolFlag{
Name: "no-filter",
Aliases: []string{"nf"},
Usage: "ignore the valuable filter and push all discovered vulns",
Category: "[Launch Options]",
},
&cli.BoolFlag{
Name: "no-github-search",
Aliases: []string{"ng"},
Usage: "don't search github repos and pull requests for every cve vuln",
Category: "[Launch Options]",
},
&cli.BoolFlag{
Name: "diff",
Usage: "skip init vuln db, push new vulns then exit",
Category: "[Launch Options]",
},
&cli.BoolFlag{
Name: "insecure",
Aliases: []string{"k"},
Usage: "allow insecure server connections when using SSL/TLS",
Category: "[Other Options]",
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "set log level to debug, print more details",
Value: false,
Category: "[Other Options]",
},
}
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 {
if errors.Is(err, context.Canceled) {
log.Fatal("user canceled")
} else {
log.Fatal(err)
}
}
}
func Action(c *cli.Context) error {
ctx, cancel := signalCtx()
defer cancel()
var config *ctrl.WatchVulnAppConfig
var err error
if c.String("config") != "" {
config, err = initConfigFromFile(c)
} else {
config, err = initConfigFromCli(c)
}
if err != nil {
return errors.Wrap(err, "failed to init config")
}
app, err := ctrl.NewApp(config)
if err != nil {
return errors.Wrap(err, "failed to create app")
}
defer app.Close()
if err = app.Run(ctx); err != nil {
return errors.Wrap(err, "failed to run app")
}
return nil
}
func initConfigFromFile(c *cli.Context) (*ctrl.WatchVulnAppConfig, error) {
configFile := c.String("config")
if configFile == "" {
return nil, fmt.Errorf("config file is required")
}
data, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
var config ctrl.WatchVulnAppConfig
if strings.HasSuffix(configFile, ".json") {
err = json.Unmarshal(data, &config)
}
if strings.HasSuffix(configFile, ".yaml") || strings.HasSuffix(configFile, ".yml") {
err = yaml.Unmarshal(data, &config)
}
if err != nil {
return nil, err
}
config.IntervalParsed, err = time.ParseDuration(config.Interval)
if err != nil {
return nil, err
}
if config.IntervalParsed.Minutes() < 1 && !c.Bool("debug") {
return nil, fmt.Errorf("interval is too small, at least 1m")
}
return &config, nil
}
func initConfigFromCli(c *cli.Context) (*ctrl.WatchVulnAppConfig, error) {
pusher, err := initPusher(c)
if err != nil {
return nil, err
}
sources := c.String("sources")
if os.Getenv("SOURCES") != "" {
sources = os.Getenv("SOURCES")
}
sourcesParts := strings.Split(sources, ",")
noStartMessage := c.Bool("no-start-message")
noFilter := c.Bool("no-filter")
noGithubSearch := c.Bool("no-github-search")
cveFilter := c.Bool("enable-cve-filter")
debug := c.Bool("debug")
iv := c.String("interval")
db := c.String("db")
proxy := c.String("proxy")
diff := c.Bool("diff")
whitelistFile := c.String("whitelist-file")
blacklistFile := c.String("blacklist-file")
insecure := c.Bool("insecure")
if os.Getenv("INTERVAL") != "" {
iv = os.Getenv("INTERVAL")
}
if os.Getenv("NO_FILTER") != "" {
noFilter = true
}
if os.Getenv("NO_START_MESSAGE") != "" {
noStartMessage = true
}
if os.Getenv("NO_GITHUB_SEARCH") != "" {
noGithubSearch = true
}
if os.Getenv("ENABLE_CVE_FILTER") == "false" {
cveFilter = false
}
if os.Getenv("DIFF") != "" {
diff = true
}
if os.Getenv("DB_CONN") != "" {
db = os.Getenv("DB_CONN")
}
if proxy != "" {
must(os.Setenv("HTTP_PROXY", proxy))
must(os.Setenv("HTTPS_PROXY", proxy))
}
if os.Getenv("HTTPS_PROXY") != "" {
must(os.Setenv("HTTP_PROXY", os.Getenv("HTTPS_PROXY")))
}
if insecure {
// 这个环境变量仅内部使用,go 本身并不支持
must(os.Setenv("GO_SKIP_TLS_CHECK", "1"))
}
log.Infof("config: INTERVAL=%s, NO_FILTER=%v, NO_START_MESSAGE=%v, NO_GITHUB_SEARCH=%v, ENABLE_CVE_FILTER=%v",
iv, noFilter, noStartMessage, noGithubSearch, cveFilter)
interval, err := time.ParseDuration(iv)
if err != nil {
return nil, err
}
if interval.Minutes() < 1 && !debug {
return nil, fmt.Errorf("interval is too small, at least 1m")
}
// 白名单关键字
if os.Getenv("WHITELIST_FILE") != "" {
whitelistFile = os.Getenv("WHITELIST_FILE")
}
whiteKeywords, err := splitLines(whitelistFile)
if err != nil {
return nil, err
}
if len(whiteKeywords) != 0 {
log.Infof("using whitelist keywords: %v", whiteKeywords)
}
// 黑名单关键字
if os.Getenv("BLACKLIST_FILE") != "" {
blacklistFile = os.Getenv("BLACKLIST_FILE")
}
blackKeywords, err := splitLines(blacklistFile)
if err != nil {
return nil, err
}
if len(blackKeywords) != 0 {
log.Infof("using blacklist keywords: %v", blackKeywords)
}
config := &ctrl.WatchVulnAppConfig{
DBConn: db,
Sources: sourcesParts,
Interval: iv,
IntervalParsed: interval,
EnableCVEFilter: &cveFilter,
NoStartMessage: &noStartMessage,
NoGithubSearch: &noGithubSearch,
NoFilter: noFilter,
DiffMode: &diff,
Version: Version,
WhiteKeywords: whiteKeywords,
BlackKeywords: blackKeywords,
Pusher: pusher,
}
return config, nil
}
func initPusher(c *cli.Context) ([]map[string]string, error) {
dingToken := c.String("dingding-access-token")
dingSecret := c.String("dingding-sign-secret")
wxWorkKey := c.String("wechatwork-key")
webhook := c.String("webhook-url")
lanxinDomain := c.String("lanxin-domain")
lanxinToken := c.String("lanxin-hook-token")
lanxinSecret := c.String("lanxin-sign-secret")
bark := c.String("bark-url")
larkToken := c.String("lark-access-token")
larkSecret := c.String("lark-sign-secret")
serverChanKey := c.String("serverchan-key")
pushPlusKey := c.String("pushplus-key")
telegramBotTokey := c.String("telegram-bot-token")
telegramChatIDs := c.String("telegram-chat-ids")
if os.Getenv("DINGDING_ACCESS_TOKEN") != "" {
dingToken = os.Getenv("DINGDING_ACCESS_TOKEN")
}
if os.Getenv("DINGDING_SECRET") != "" {
dingSecret = os.Getenv("DINGDING_SECRET")
}
if os.Getenv("WECHATWORK_KEY") != "" {
wxWorkKey = os.Getenv("WECHATWORK_KEY")
}
if os.Getenv("WEBHOOK_URL") != "" {
webhook = os.Getenv("WEBHOOK_URL")
}
if os.Getenv("LANXIN_DOMAIN") != "" {
lanxinDomain = os.Getenv("LANXIN_DOMAIN")
}
if os.Getenv("LANXIN_TOKEN") != "" {
lanxinToken = os.Getenv("LANXIN_TOKEN")
}
if os.Getenv("LANXIN_SECRET") != "" {
lanxinSecret = os.Getenv("LANXIN_SECRET")
}
if os.Getenv("BARK_URL") != "" {
bark = os.Getenv("BARK_URL")
}
if os.Getenv("LARK_ACCESS_TOKEN") != "" {
larkToken = os.Getenv("LARK_ACCESS_TOKEN")
}
if os.Getenv("LARK_SECRET") != "" {
larkSecret = os.Getenv("LARK_SECRET")
}
if os.Getenv("SERVERCHAN_KEY") != "" {
serverChanKey = os.Getenv("SERVERCHAN_KEY")
}
if os.Getenv("PUSHPLUS_KEY") != "" {
pushPlusKey = os.Getenv("PUSHPLUS_KEY")
}
if os.Getenv("TELEGRAM_BOT_TOKEN") != "" {
telegramBotTokey = os.Getenv("TELEGRAM_BOT_TOKEN")
}
if os.Getenv("TELEGRAM_CHAT_IDS") != "" {
telegramChatIDs = os.Getenv("TELEGRAM_CHAT_IDS")
}
var pusherConfig []any
if dingToken != "" && dingSecret != "" {
pusherConfig = append(pusherConfig, &push.DingDingConfig{
Type: push.TypeDingDing,
AccessToken: dingToken,
SignSecret: dingSecret,
})
}
if wxWorkKey != "" {
pusherConfig = append(pusherConfig, &push.WechatWorkConfig{
Type: push.TypeWechatWork,
Key: wxWorkKey,
})
}
if webhook != "" {
pusherConfig = append(pusherConfig, &push.WebhookConfig{
Type: push.TypeWebhook,
URL: webhook,
})
}
if lanxinDomain != "" && lanxinToken != "" && lanxinSecret != "" {
pusherConfig = append(pusherConfig, &push.LanxinConfig{
Type: push.TypeLanxin,
Domain: lanxinDomain,
AccessToken: lanxinToken,
SignSecret: lanxinSecret,
})
}
if bark != "" {
pusherConfig = append(pusherConfig, &push.BarkConfig{
Type: push.TypeBark,
URL: bark,
})
}
if larkToken != "" && larkSecret != "" {
pusherConfig = append(pusherConfig, &push.LarkConfig{
Type: push.TypeLark,
AccessToken: larkToken,
SignSecret: larkSecret,
})
}
if serverChanKey != "" {
pusherConfig = append(pusherConfig, &push.ServerChanConfig{
Type: push.TypeServerChan,
Key: serverChanKey,
})
}
if pushPlusKey != "" {
pusherConfig = append(pusherConfig, &push.PushPlusConfig{
Type: push.TypePushPlus,
Token: pushPlusKey,
})
}
if telegramBotTokey != "" && telegramChatIDs != "" {
pusherConfig = append(pusherConfig, &push.TelegramConfig{
Type: push.TypeTelegram,
BotToken: telegramBotTokey,
ChatIDs: telegramChatIDs,
})
}
data, err := json.Marshal(pusherConfig)
if err != nil {
return nil, err
}
var pusher []map[string]string
err = json.Unmarshal(data, &pusher)
if err != nil {
return nil, err
}
return pusher, 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
}
func must(err error) {
if err != nil {
panic(err)
}
}
func splitLines(path string) ([]string, error) {
var products []string
if path != "" {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
for _, p := range strings.Split(string(data), "\n") {
p = strings.TrimSpace(p)
if p != "" {
products = append(products, p)
}
}
}
return products, nil
}