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
|
package processing
// DCSO FEVER
// Copyright (c) 2017, 2020, DCSO GmbH
import (
"fmt"
"io"
"net/url"
"strings"
"sync"
"github.com/DCSO/fever/types"
"github.com/DCSO/fever/util"
"github.com/buger/jsonparser"
"github.com/DCSO/bloom"
log "github.com/sirupsen/logrus"
)
// BloomHandler is a Handler which is meant to check for the presence of
// event type-specific keywords in a Bloom filter, raising new 'alert' type
// events when matches are found.
type BloomHandler struct {
sync.Mutex
Logger *log.Entry
Name string
EventType string
IocBloom *bloom.BloomFilter
BloomFilename string
BloomFileIsCompressed bool
DatabaseEventChan chan types.Entry
ForwardHandler Handler
AlertPrefix string
Alertifier *util.Alertifier
BlacklistIOCs map[string]struct{}
}
// BloomNoFileErr is an error thrown when a file-based operation (e.g.
// reloading) is attempted on a bloom filter object with no file information
// attached.
type BloomNoFileErr struct {
s string
}
// Error returns the error message.
func (e *BloomNoFileErr) Error() string {
return e.s
}
func bloomExtraModifier(inputAlert *types.Entry, ioc string) error {
iocEscaped, err := util.EscapeJSON(ioc)
if err != nil {
return err
}
val, err := jsonparser.Set([]byte(inputAlert.JSONLine), iocEscaped,
"_extra", "bloom-ioc")
if err != nil {
return err
}
inputAlert.JSONLine = string(val)
return nil
}
// MakeBloomHandler returns a new BloomHandler, checking against the given
// Bloom filter and sending alerts to databaseChan as well as forwarding them
// to a given forwarding handler.
func MakeBloomHandler(iocBloom *bloom.BloomFilter,
databaseChan chan types.Entry, forwardHandler Handler, alertPrefix string) *BloomHandler {
bh := &BloomHandler{
Logger: log.WithFields(log.Fields{
"domain": "bloom",
}),
IocBloom: iocBloom,
DatabaseEventChan: databaseChan,
ForwardHandler: forwardHandler,
AlertPrefix: alertPrefix,
Alertifier: util.MakeAlertifier(alertPrefix),
BlacklistIOCs: make(map[string]struct{}),
}
bh.Alertifier.SetExtraModifier(bloomExtraModifier)
bh.Alertifier.RegisterMatchType("dns-req", util.AlertJSONProviderDNSReq{})
bh.Alertifier.RegisterMatchType("dns-resp", util.AlertJSONProviderDNSResp{})
bh.Alertifier.RegisterMatchType("tls-sni", util.AlertJSONProviderTLSSNI{})
bh.Alertifier.RegisterMatchType("tls-fingerprint", util.AlertJSONProviderTLSFingerprint{})
bh.Alertifier.RegisterMatchType("http-host", util.AlertJSONProviderHTTPHost{})
bh.Alertifier.RegisterMatchType("http-url", util.AlertJSONProviderHTTPURL{})
log.WithFields(log.Fields{
"N": iocBloom.N,
"domain": "bloom",
}).Info("Bloom filter loaded")
return bh
}
// MakeBloomHandlerFromFile returns a new BloomHandler created from a new
// Bloom filter specified by the given file name.
func MakeBloomHandlerFromFile(bloomFilename string, compressed bool,
databaseChan chan types.Entry, forwardHandler Handler, alertPrefix string,
blacklistIOCs []string) (*BloomHandler, error) {
log.WithFields(log.Fields{
"domain": "bloom",
"filename": bloomFilename,
}).Info("loading Bloom filter from file")
iocBloom, err := bloom.LoadFilter(bloomFilename, compressed)
if err != nil {
if err == io.EOF {
log.Warnf("file is empty, using empty default one")
myBloom := bloom.Initialize(100, 0.00000001)
iocBloom = &myBloom
} else if strings.Contains(err.Error(), "value of k (number of hash functions) is too high") {
log.Warnf("malformed Bloom filter file, using empty default one")
myBloom := bloom.Initialize(100, 0.00000001)
iocBloom = &myBloom
} else {
return nil, err
}
}
bh := MakeBloomHandler(iocBloom, databaseChan, forwardHandler, alertPrefix)
for _, v := range blacklistIOCs {
if bh.IocBloom.Check([]byte(v)) {
bh.Logger.Warnf("filter contains blacklisted indicator '%s'", v)
}
bh.BlacklistIOCs[v] = struct{}{}
}
bh.BloomFilename = bloomFilename
bh.BloomFileIsCompressed = compressed
bh.Logger.WithFields(log.Fields{}).Info("filter file loaded successfully")
return bh, nil
}
// Reload triggers a reload of the contents of the file with the name.
func (a *BloomHandler) Reload() error {
if a.BloomFilename == "" {
return &BloomNoFileErr{"BloomHandler was not created from a file, no reloading possible"}
}
iocBloom, err := bloom.LoadFilter(a.BloomFilename, a.BloomFileIsCompressed)
if err != nil {
if err == io.EOF {
log.Warnf("file is empty, using empty default one")
myBloom := bloom.Initialize(100, 0.00000001)
iocBloom = &myBloom
} else if strings.Contains(err.Error(), "value of k (number of hash functions) is too high") {
log.Warnf("malformed Bloom filter file, using empty default one")
myBloom := bloom.Initialize(100, 0.00000001)
iocBloom = &myBloom
} else {
return err
}
}
a.Lock()
a.IocBloom = iocBloom
for k := range a.BlacklistIOCs {
if a.IocBloom.Check([]byte(k)) {
a.Logger.Warnf("filter contains blacklisted indicator '%s'", k)
}
}
a.Unlock()
log.WithFields(log.Fields{
"N": iocBloom.N,
}).Info("Bloom filter reloaded")
return nil
}
// Consume processes an Entry, emitting alerts if there is a match
func (a *BloomHandler) Consume(e *types.Entry) error {
if e.EventType == "http" {
var fullURL string
a.Lock()
// check HTTP host first: foo.bar.de
if a.IocBloom.Check([]byte(e.HTTPHost)) {
if _, present := a.BlacklistIOCs[e.HTTPHost]; !present {
if n, err := a.Alertifier.MakeAlert(*e, e.HTTPHost,
"http-host"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
}
}
// we sometimes see full 'URLs' in the corresponding EVE field when
// observing requests via proxies. In this case there is no need to
// canonicalize the URL, it is already qualified.
if strings.Contains(e.HTTPUrl, "://") {
fullURL = e.HTTPUrl
} else {
// in all other cases, we need to create a full URL from the components
fullURL = "http://" + e.HTTPHost + e.HTTPUrl
}
// we now should have a full URL regardless of where it came from:
// http://foo.bar.de:123/baz
u, err := url.Parse(fullURL)
if err != nil {
log.Warnf("could not parse URL '%s': %s", fullURL, err.Error())
a.Unlock()
return nil
}
hostPath := fmt.Sprintf("%s%s", u.Host, u.Path)
// http://foo.bar.de:123/baz
if a.IocBloom.Check([]byte(fullURL)) {
if _, present := a.BlacklistIOCs[fullURL]; !present {
if n, err := a.Alertifier.MakeAlert(*e, fullURL,
"http-url"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
}
} else
// foo.bar.de:123/baz
if a.IocBloom.Check([]byte(hostPath)) {
if _, present := a.BlacklistIOCs[hostPath]; !present {
if n, err := a.Alertifier.MakeAlert(*e, hostPath,
"http-url"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
}
} else
// /baz
if a.IocBloom.Check([]byte(u.Path)) {
if _, present := a.BlacklistIOCs[u.Path]; !present {
if n, err := a.Alertifier.MakeAlert(*e, u.Path,
"http-url"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
}
}
a.Unlock()
} else if e.EventType == "dns" {
a.Lock()
if a.IocBloom.Check([]byte(e.DNSRRName)) {
if _, present := a.BlacklistIOCs[e.DNSRRName]; !present {
if e.DNSType == "query" {
if n, err := a.Alertifier.MakeAlert(*e, e.DNSRRName,
"dns-req"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
} else if e.DNSType == "answer" {
if n, err := a.Alertifier.MakeAlert(*e, e.DNSRRName,
"dns-resp"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
} else {
log.Warnf("invalid DNS type: '%s'", e.DNSType)
a.Unlock()
return nil
}
}
}
a.Unlock()
} else if e.EventType == "tls" {
a.Lock()
if a.IocBloom.Check([]byte(e.TLSSNI)) {
if _, present := a.BlacklistIOCs[e.TLSSNI]; !present {
if n, err := a.Alertifier.MakeAlert(*e, e.TLSSNI,
"tls-sni"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
}
} else if a.IocBloom.Check([]byte(e.TLSFingerprint)) {
if _, present := a.BlacklistIOCs[e.TLSFingerprint]; !present {
if n, err := a.Alertifier.MakeAlert(*e, e.TLSFingerprint,
"tls-fingerprint"); err == nil {
a.DatabaseEventChan <- *n
a.ForwardHandler.Consume(n)
} else {
log.Warn(err)
}
}
}
a.Unlock()
}
return nil
}
// GetName returns the name of the handler
func (a *BloomHandler) GetName() string {
return "Bloom filter handler"
}
// GetEventTypes returns a slice of event type strings that this handler
// should be applied to
func (a *BloomHandler) GetEventTypes() []string {
return []string{"http", "dns", "tls"}
}
|