crowdsec/pkg/parser/enrich_geoip.go

131 lines
3.5 KiB
Go
Raw Normal View History

2020-05-15 09:39:16 +00:00
package parser
import (
"fmt"
"net"
"strconv"
"github.com/oschwald/geoip2-golang"
"github.com/oschwald/maxminddb-golang"
log "github.com/sirupsen/logrus"
"github.com/crowdsecurity/crowdsec/pkg/types"
2020-05-15 09:39:16 +00:00
)
func IpToRange(field string, p *types.Event, ctx interface{}, plog *log.Entry) (map[string]string, error) {
2020-05-15 09:39:16 +00:00
var dummy interface{}
ret := make(map[string]string)
if field == "" {
return nil, nil
}
ip := net.ParseIP(field)
if ip == nil {
plog.Infof("Can't parse ip %s, no range enrich", field)
2020-05-15 09:39:16 +00:00
return nil, nil
}
net, ok, err := ctx.(*maxminddb.Reader).LookupNetwork(ip, &dummy)
2020-05-15 09:39:16 +00:00
if err != nil {
plog.Errorf("Failed to fetch network for %s : %v", ip.String(), err)
2020-05-15 09:39:16 +00:00
return nil, nil
}
if !ok {
plog.Debugf("Unable to find range of %s", ip.String())
2020-05-15 09:39:16 +00:00
return nil, nil
}
ret["SourceRange"] = net.String()
return ret, nil
}
func GeoIpASN(field string, p *types.Event, ctx interface{}, plog *log.Entry) (map[string]string, error) {
2020-05-15 09:39:16 +00:00
ret := make(map[string]string)
if field == "" {
return nil, nil
}
ip := net.ParseIP(field)
if ip == nil {
plog.Infof("Can't parse ip %s, no ASN enrich", ip)
return nil, nil
}
record, err := ctx.(*geoip2.Reader).ASN(ip)
2020-05-15 09:39:16 +00:00
if err != nil {
plog.Errorf("Unable to enrich ip '%s'", field)
return nil, nil //nolint:nilerr
2020-05-15 09:39:16 +00:00
}
ret["ASNNumber"] = fmt.Sprintf("%d", record.AutonomousSystemNumber)
ret["ASNumber"] = fmt.Sprintf("%d", record.AutonomousSystemNumber)
2020-05-15 09:39:16 +00:00
ret["ASNOrg"] = record.AutonomousSystemOrganization
plog.Tracef("geoip ASN %s -> %s, %s", field, ret["ASNNumber"], ret["ASNOrg"])
2020-05-15 09:39:16 +00:00
return ret, nil
}
func GeoIpCity(field string, p *types.Event, ctx interface{}, plog *log.Entry) (map[string]string, error) {
2020-05-15 09:39:16 +00:00
ret := make(map[string]string)
if field == "" {
return nil, nil
}
ip := net.ParseIP(field)
if ip == nil {
plog.Infof("Can't parse ip %s, no City enrich", ip)
return nil, nil
}
record, err := ctx.(*geoip2.Reader).City(ip)
2020-05-15 09:39:16 +00:00
if err != nil {
plog.Debugf("Unable to enrich ip '%s'", ip)
return nil, nil //nolint:nilerr
2020-05-15 09:39:16 +00:00
}
if record.Country.IsoCode != "" {
ret["IsoCode"] = record.Country.IsoCode
ret["IsInEU"] = strconv.FormatBool(record.Country.IsInEuropeanUnion)
} else if record.RegisteredCountry.IsoCode != "" {
ret["IsoCode"] = record.RegisteredCountry.IsoCode
ret["IsInEU"] = strconv.FormatBool(record.RegisteredCountry.IsInEuropeanUnion)
} else if record.RepresentedCountry.IsoCode != "" {
ret["IsoCode"] = record.RepresentedCountry.IsoCode
ret["IsInEU"] = strconv.FormatBool(record.RepresentedCountry.IsInEuropeanUnion)
} else {
ret["IsoCode"] = ""
ret["IsInEU"] = strconv.FormatBool(false)
}
2020-05-15 09:39:16 +00:00
ret["Latitude"] = fmt.Sprintf("%f", record.Location.Latitude)
ret["Longitude"] = fmt.Sprintf("%f", record.Location.Longitude)
plog.Tracef("geoip City %s -> %s, %s", field, ret["IsoCode"], ret["IsInEU"])
2020-05-15 09:39:16 +00:00
return ret, nil
}
func GeoIPCityInit(cfg map[string]string) (interface{}, error) {
dbCityReader, err := geoip2.Open(cfg["datadir"] + "/GeoLite2-City.mmdb")
2020-05-15 09:39:16 +00:00
if err != nil {
log.Debugf("couldn't open geoip : %v", err)
2020-05-15 09:39:16 +00:00
return nil, err
}
return dbCityReader, nil
}
func GeoIPASNInit(cfg map[string]string) (interface{}, error) {
dbASReader, err := geoip2.Open(cfg["datadir"] + "/GeoLite2-ASN.mmdb")
2020-05-15 09:39:16 +00:00
if err != nil {
log.Debugf("couldn't open geoip : %v", err)
2020-05-15 09:39:16 +00:00
return nil, err
}
return dbASReader, nil
}
func IpToRangeInit(cfg map[string]string) (interface{}, error) {
ipToRangeReader, err := maxminddb.Open(cfg["datadir"] + "/GeoLite2-ASN.mmdb")
2020-05-15 09:39:16 +00:00
if err != nil {
log.Debugf("couldn't open geoip : %v", err)
2020-05-15 09:39:16 +00:00
return nil, err
}
return ipToRangeReader, nil
2020-05-15 09:39:16 +00:00
}