-
Notifications
You must be signed in to change notification settings - Fork 0
/
tie-threatbus-bridge.go
163 lines (143 loc) · 3.22 KB
/
tie-threatbus-bridge.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
// tie-threatbus-bridge
// Copyright (c) 2020, 2023 DCSO GmbH
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
zmq "github.com/pebbe/zmq4"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
var (
tc Collector
updating bool
socket *zmq.Socket
)
func update(iocChan chan IOC) {
defer func() {
updating = false
}()
log.WithFields(log.Fields{
"domain": "status",
}).Info("update started")
count, sent, err := tc.Fetch(iocChan)
if err != nil {
log.Error(err)
}
log.WithFields(log.Fields{
"domain": "metrics",
"iocs-processed": count,
"iocs-sent": sent,
}).Info("update done")
}
type IOCJSON struct {
TS time.Time `json:"ts"`
ID string `json:"id"`
Data struct {
Indicator []string `json:"indicator"`
IntelType int `json:"intel_type"`
} `json:"data"`
Operation string `json:"operation"`
}
func sendZMQ(ioc *IOC, conv IOCConverter) error {
j, err := conv.FromIOC(ioc)
if err != nil {
return err
}
msg := fmt.Sprintf("%s %s", conv.Topic(), string(j))
i, err := socket.Send(msg, 0)
log.WithFields(log.Fields{
"domain": "status",
}).Debug("sending ", msg)
if err != nil {
return err
}
log.WithFields(log.Fields{
"domain": "status",
}).Debug(i, " bytes sent")
return nil
}
func main() {
var err error
var configFilename = flag.String("config", "config.yaml", "configuration file")
var verbose = flag.Bool("verbose", false, "be verbose")
flag.Parse()
iocChan := make(chan IOC, 10000)
if *verbose {
log.SetLevel(log.DebugLevel)
}
yamlFile, err := ioutil.ReadFile(*configFilename)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(yamlFile, &Config)
if err != nil {
log.Fatal(err)
}
conv, err := MakeIOCConverter(Config.ThreatBusConfig.Format)
if err != nil {
log.Fatal(err)
}
if len(Config.Logfile) > 0 {
log.Infof("Switching to log file %s", Config.Logfile)
file, err := os.OpenFile(Config.Logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.SetFormatter(UTCFormatter{&log.JSONFormatter{}})
log.SetOutput(file)
} else {
log.SetFormatter(UTCFormatter{&log.JSONFormatter{}})
}
go func(myIocChan chan IOC) {
for ioc := range myIocChan {
err := sendZMQ(&ioc, conv)
if err != nil {
log.Error(err)
}
}
}(iocChan)
context, _ := zmq.NewContext()
socket, err = context.NewSocket(zmq.PUB)
if err != nil {
log.Fatal(err)
}
err = socket.Connect(fmt.Sprintf("tcp://%s:%d", Config.ThreatBusConfig.Host, Config.ThreatBusConfig.Port))
if err != nil {
log.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
tc = &TIECollector{}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)
go func() {
for sig := range c {
log.Debug(sig)
if sig == syscall.SIGTERM || sig == syscall.SIGINT {
log.WithFields(log.Fields{
"domain": "status",
}).Info("shutting down")
if socket != nil {
socket.Close()
}
os.Exit(0)
} else if sig == syscall.SIGUSR1 {
if !updating {
updating = true
go update(iocChan)
} else {
log.WithFields(log.Fields{
"domain": "status",
}).Error("update in progress")
}
}
}
}()
select {}
}