|
@@ -6,53 +6,53 @@ import (
|
|
|
"strconv"
|
|
|
|
|
|
"github.com/oschwald/geoip2-golang"
|
|
|
- "github.com/oschwald/maxminddb-golang"
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
+ "github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
)
|
|
|
|
|
|
-func IpToRange(field string, p *types.Event, ctx interface{}, plog *log.Entry) (map[string]string, error) {
|
|
|
- var dummy interface{}
|
|
|
- ret := make(map[string]string)
|
|
|
-
|
|
|
+func IpToRange(field string, p *types.Event, plog *log.Entry) (map[string]string, error) {
|
|
|
if field == "" {
|
|
|
return nil, nil
|
|
|
}
|
|
|
- ip := net.ParseIP(field)
|
|
|
- if ip == nil {
|
|
|
- plog.Infof("Can't parse ip %s, no range enrich", field)
|
|
|
- return nil, nil
|
|
|
- }
|
|
|
- net, ok, err := ctx.(*maxminddb.Reader).LookupNetwork(ip, &dummy)
|
|
|
+
|
|
|
+ r, err := exprhelpers.GeoIPRangeEnrich(field)
|
|
|
+
|
|
|
if err != nil {
|
|
|
- plog.Errorf("Failed to fetch network for %s : %v", ip.String(), err)
|
|
|
- return nil, nil
|
|
|
+ plog.Errorf("Unable to enrich ip '%s'", field)
|
|
|
+ return nil, nil //nolint:nilerr
|
|
|
}
|
|
|
- if !ok {
|
|
|
- plog.Debugf("Unable to find range of %s", ip.String())
|
|
|
- return nil, nil
|
|
|
+
|
|
|
+ if r == nil {
|
|
|
+ plog.Warnf("No range found for ip '%s'", field)
|
|
|
+ return nil, nil //nolint:nilerr
|
|
|
}
|
|
|
- ret["SourceRange"] = net.String()
|
|
|
+
|
|
|
+ record := r.(*net.IPNet)
|
|
|
+
|
|
|
+ ret := make(map[string]string)
|
|
|
+ ret["SourceRange"] = record.String()
|
|
|
+
|
|
|
return ret, nil
|
|
|
}
|
|
|
|
|
|
-func GeoIpASN(field string, p *types.Event, ctx interface{}, plog *log.Entry) (map[string]string, error) {
|
|
|
- ret := make(map[string]string)
|
|
|
+func GeoIpASN(field string, p *types.Event, plog *log.Entry) (map[string]string, error) {
|
|
|
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)
|
|
|
+ r, err := exprhelpers.GeoIPASNEnrich(field)
|
|
|
+
|
|
|
if err != nil {
|
|
|
plog.Errorf("Unable to enrich ip '%s'", field)
|
|
|
return nil, nil //nolint:nilerr
|
|
|
}
|
|
|
+
|
|
|
+ record := r.(*geoip2.ASN)
|
|
|
+
|
|
|
+ ret := make(map[string]string)
|
|
|
+
|
|
|
ret["ASNNumber"] = fmt.Sprintf("%d", record.AutonomousSystemNumber)
|
|
|
ret["ASNumber"] = fmt.Sprintf("%d", record.AutonomousSystemNumber)
|
|
|
ret["ASNOrg"] = record.AutonomousSystemOrganization
|
|
@@ -62,21 +62,21 @@ func GeoIpASN(field string, p *types.Event, ctx interface{}, plog *log.Entry) (m
|
|
|
return ret, nil
|
|
|
}
|
|
|
|
|
|
-func GeoIpCity(field string, p *types.Event, ctx interface{}, plog *log.Entry) (map[string]string, error) {
|
|
|
- ret := make(map[string]string)
|
|
|
+func GeoIpCity(field string, p *types.Event, plog *log.Entry) (map[string]string, error) {
|
|
|
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)
|
|
|
+
|
|
|
+ r, err := exprhelpers.GeoIPEnrich(field)
|
|
|
+
|
|
|
if err != nil {
|
|
|
- plog.Debugf("Unable to enrich ip '%s'", ip)
|
|
|
+ plog.Errorf("Unable to enrich ip '%s'", field)
|
|
|
return nil, nil //nolint:nilerr
|
|
|
}
|
|
|
+
|
|
|
+ record := r.(*geoip2.City)
|
|
|
+ ret := make(map[string]string)
|
|
|
+
|
|
|
if record.Country.IsoCode != "" {
|
|
|
ret["IsoCode"] = record.Country.IsoCode
|
|
|
ret["IsInEU"] = strconv.FormatBool(record.Country.IsInEuropeanUnion)
|
|
@@ -88,7 +88,7 @@ func GeoIpCity(field string, p *types.Event, ctx interface{}, plog *log.Entry) (
|
|
|
ret["IsInEU"] = strconv.FormatBool(record.RepresentedCountry.IsInEuropeanUnion)
|
|
|
} else {
|
|
|
ret["IsoCode"] = ""
|
|
|
- ret["IsInEU"] = strconv.FormatBool(false)
|
|
|
+ ret["IsInEU"] = "false"
|
|
|
}
|
|
|
|
|
|
ret["Latitude"] = fmt.Sprintf("%f", record.Location.Latitude)
|
|
@@ -98,33 +98,3 @@ func GeoIpCity(field string, p *types.Event, ctx interface{}, plog *log.Entry) (
|
|
|
|
|
|
return ret, nil
|
|
|
}
|
|
|
-
|
|
|
-func GeoIPCityInit(cfg map[string]string) (interface{}, error) {
|
|
|
- dbCityReader, err := geoip2.Open(cfg["datadir"] + "/GeoLite2-City.mmdb")
|
|
|
- if err != nil {
|
|
|
- log.Debugf("couldn't open geoip : %v", err)
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- return dbCityReader, nil
|
|
|
-}
|
|
|
-
|
|
|
-func GeoIPASNInit(cfg map[string]string) (interface{}, error) {
|
|
|
- dbASReader, err := geoip2.Open(cfg["datadir"] + "/GeoLite2-ASN.mmdb")
|
|
|
- if err != nil {
|
|
|
- log.Debugf("couldn't open geoip : %v", err)
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- return dbASReader, nil
|
|
|
-}
|
|
|
-
|
|
|
-func IpToRangeInit(cfg map[string]string) (interface{}, error) {
|
|
|
- ipToRangeReader, err := maxminddb.Open(cfg["datadir"] + "/GeoLite2-ASN.mmdb")
|
|
|
- if err != nil {
|
|
|
- log.Debugf("couldn't open geoip : %v", err)
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- return ipToRangeReader, nil
|
|
|
-}
|