Skip to content

Commit

Permalink
Merge pull request #358 from kubescape/feature/dns
Browse files Browse the repository at this point in the history
Adding a map to prevent duplicate dns events
  • Loading branch information
amitschendel authored Sep 2, 2024
2 parents 07caefc + 899b122 commit 1fc3466
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pkg/ruleengine/v1/r0005_unexpected_domain_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"slices"
"strings"

"github.com/goradd/maps"
"github.com/kubescape/node-agent/pkg/objectcache"
"github.com/kubescape/node-agent/pkg/ruleengine"
"github.com/kubescape/node-agent/pkg/utils"
Expand Down Expand Up @@ -36,6 +37,7 @@ var _ ruleengine.RuleEvaluator = (*R0005UnexpectedDomainRequest)(nil)

type R0005UnexpectedDomainRequest struct {
BaseRule
alertedDomains maps.SafeMap[string, bool]
}

func CreateRuleR0005UnexpectedDomainRequest() *R0005UnexpectedDomainRequest {
Expand All @@ -45,6 +47,7 @@ func CreateRuleR0005UnexpectedDomainRequest() *R0005UnexpectedDomainRequest {
func (rule *R0005UnexpectedDomainRequest) Name() string {
return R0005Name
}

func (rule *R0005UnexpectedDomainRequest) ID() string {
return R0005ID
}
Expand All @@ -68,6 +71,10 @@ func (rule *R0005UnexpectedDomainRequest) ProcessEvent(eventType utils.EventType
return nil
}

if rule.alertedDomains.Has(domainEvent.DNSName) {
return nil
}

// TODO: fix this, currently we are ignoring in-cluster communication
if strings.HasSuffix(domainEvent.DNSName, "svc.cluster.local.") {
return nil
Expand Down Expand Up @@ -123,6 +130,8 @@ func (rule *R0005UnexpectedDomainRequest) ProcessEvent(eventType utils.EventType
RuleID: rule.ID(),
}

rule.alertedDomains.Set(domainEvent.DNSName, true)

return &ruleFailure
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ruleengine/v1/r0005_unexpected_domain_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestR0005UnexpectedDomainRequest(t *testing.T) {
},
},
DNSName: "test.com",
Qr: tracerdnstype.DNSPktTypeQuery,
}

// Test with nil appProfileAccess
Expand Down Expand Up @@ -60,5 +61,4 @@ func TestR0005UnexpectedDomainRequest(t *testing.T) {
if ruleResult != nil {
t.Errorf("Expected ruleResult to be nil since domain is whitelisted")
}

}
9 changes: 9 additions & 0 deletions pkg/ruleengine/v1/r0011_unexpected_egress_network_traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (rule *R0011UnexpectedEgressNetworkTraffic) handleNetworkEvent(networkEvent

domain := objCache.DnsCache().ResolveIpToDomain(networkEvent.DstEndpoint.Addr)

if domain != "" {
return nil
}

// Check if the address is in the egress list and isn't in cluster.
for _, egress := range nnContainer.Egress {
if egress.IPAddress == networkEvent.DstEndpoint.Addr {
Expand Down Expand Up @@ -154,6 +158,11 @@ func isPrivateIP(ip string) bool {
return false
}

// Check if IP is localhost
if parsedIP.IsLoopback() {
return true
}

// Check if IP is in private IP ranges
privateIPRanges := []struct {
start net.IP
Expand Down
8 changes: 8 additions & 0 deletions pkg/ruleengine/v1/r1008_crypto_mining_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"slices"

"github.com/goradd/maps"
"github.com/kubescape/node-agent/pkg/objectcache"
"github.com/kubescape/node-agent/pkg/ruleengine"
"github.com/kubescape/node-agent/pkg/utils"
Expand Down Expand Up @@ -145,6 +146,7 @@ var _ ruleengine.RuleEvaluator = (*R1008CryptoMiningDomainCommunication)(nil)

type R1008CryptoMiningDomainCommunication struct {
BaseRule
alertedDomains maps.SafeMap[string, bool]
}

func CreateRuleR1008CryptoMiningDomainCommunication() *R1008CryptoMiningDomainCommunication {
Expand All @@ -168,6 +170,10 @@ func (rule *R1008CryptoMiningDomainCommunication) ProcessEvent(eventType utils.E
}

if dnsEvent, ok := event.(*tracerdnstype.Event); ok {
if rule.alertedDomains.Has(dnsEvent.DNSName) {
return nil
}

if slices.Contains(commonlyUsedCryptoMinersDomains, dnsEvent.DNSName) {
ruleFailure := GenericRuleFailure{
BaseRuntimeAlert: apitypes.BaseRuntimeAlert{
Expand Down Expand Up @@ -196,6 +202,8 @@ func (rule *R1008CryptoMiningDomainCommunication) ProcessEvent(eventType utils.E
RuleID: rule.ID(),
}

rule.alertedDomains.Set(dnsEvent.DNSName, true)

return &ruleFailure
}
}
Expand Down

0 comments on commit 1fc3466

Please sign in to comment.