-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from satta/rdns
implement active reverse DNS enrichment
- Loading branch information
Showing
10 changed files
with
287 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package processing | ||
|
||
// DCSO FEVER | ||
// Copyright (c) 2019, DCSO GmbH | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
|
||
"github.com/DCSO/fever/types" | ||
"github.com/DCSO/fever/util" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/yl2chen/cidranger" | ||
) | ||
|
||
// RDNSHandler is a handler that enriches events with reverse DNS | ||
// information looked up on the sensor, for both source and destination | ||
// IP addresses. | ||
type RDNSHandler struct { | ||
sync.Mutex | ||
Logger *log.Entry | ||
HostNamer *util.HostNamer | ||
PrivateRanges cidranger.Ranger | ||
PrivateRangesOnly bool | ||
} | ||
|
||
// MakeRDNSHandler returns a new RDNSHandler, backed by the passed HostNamer. | ||
func MakeRDNSHandler(hn *util.HostNamer) *RDNSHandler { | ||
rh := &RDNSHandler{ | ||
Logger: log.WithFields(log.Fields{ | ||
"domain": "rdns", | ||
}), | ||
PrivateRanges: cidranger.NewPCTrieRanger(), | ||
HostNamer: hn, | ||
} | ||
for _, cidr := range []string{ | ||
"10.0.0.0/8", | ||
"172.16.0.0/12", | ||
"192.168.0.0/16", | ||
"fc00::/7", | ||
} { | ||
_, block, err := net.ParseCIDR(cidr) | ||
if err != nil { | ||
log.Fatalf("cannot parse fixed private IP range %v", cidr) | ||
} | ||
rh.PrivateRanges.Insert(cidranger.NewBasicRangerEntry(*block)) | ||
} | ||
return rh | ||
} | ||
|
||
// EnableOnlyPrivateIPRanges ensures that only private (RFC1918) IP ranges | ||
// are enriched | ||
func (a *RDNSHandler) EnableOnlyPrivateIPRanges() { | ||
a.PrivateRangesOnly = true | ||
} | ||
|
||
// Consume processes an Entry and enriches it | ||
func (a *RDNSHandler) Consume(e *types.Entry) error { | ||
var res []string | ||
var err error | ||
var isPrivate bool | ||
|
||
if e.SrcIP != "" { | ||
ip := net.ParseIP(e.SrcIP) | ||
if ip != nil { | ||
isPrivate, err = a.PrivateRanges.Contains(ip) | ||
if err != nil { | ||
return err | ||
} | ||
if !a.PrivateRangesOnly || isPrivate { | ||
res, err = a.HostNamer.GetHostname(e.SrcIP) | ||
if err == nil { | ||
e.SrcHosts = res | ||
} | ||
} | ||
} else { | ||
log.Error("IP not valid") | ||
} | ||
} | ||
if e.DestIP != "" { | ||
ip := net.ParseIP(e.DestIP) | ||
if ip != nil { | ||
isPrivate, err = a.PrivateRanges.Contains(ip) | ||
if err != nil { | ||
return err | ||
} | ||
if !a.PrivateRangesOnly || isPrivate { | ||
res, err = a.HostNamer.GetHostname(e.DestIP) | ||
if err == nil { | ||
e.DestHosts = res | ||
} | ||
} | ||
} else { | ||
log.Error("IP not valid") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetName returns the name of the handler | ||
func (a *RDNSHandler) GetName() string { | ||
return "reverse DNS handler" | ||
} | ||
|
||
// GetEventTypes returns a slice of event type strings that this handler | ||
// should be applied to | ||
func (a *RDNSHandler) GetEventTypes() []string { | ||
return []string{"http", "dns", "tls", "smtp", "flow", "ssh", "tls", "smb", "alert"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package util | ||
|
||
import ( | ||
"net" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/patrickmn/go-cache" | ||
) | ||
|
||
// HostNamer is a component that provides cached hostnames for IP | ||
// addresses passed as strings. | ||
type HostNamer struct { | ||
Cache *cache.Cache | ||
Lock sync.Mutex | ||
} | ||
|
||
// NewHostNamer returns a new HostNamer with the given default expiration time. | ||
// Data entries will be purged after each cleanupInterval. | ||
func NewHostNamer(defaultExpiration, cleanupInterval time.Duration) *HostNamer { | ||
return &HostNamer{ | ||
Cache: cache.New(defaultExpiration, cleanupInterval), | ||
} | ||
} | ||
|
||
// GetHostname returns a list of host names for a given IP address. | ||
func (n *HostNamer) GetHostname(ipAddr string) ([]string, error) { | ||
n.Lock.Lock() | ||
defer n.Lock.Unlock() | ||
|
||
val, found := n.Cache.Get(ipAddr) | ||
if found { | ||
return val.([]string), nil | ||
} | ||
hns, err := net.LookupAddr(ipAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for i, hn := range hns { | ||
hns[i] = strings.TrimRight(hn, ".") | ||
} | ||
n.Cache.Set(ipAddr, hns, cache.DefaultExpiration) | ||
val = hns | ||
return val.([]string), nil | ||
} | ||
|
||
// Flush clears the cache of a HostNamer. | ||
func (n *HostNamer) Flush() { | ||
n.Cache.Flush() | ||
} |
Oops, something went wrong.