Skip to content

Commit

Permalink
[CWS] make windows self test run from outside of system-probe (#31128)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux authored Nov 26, 2024
1 parent 8cd4352 commit c1ac65c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
18 changes: 14 additions & 4 deletions pkg/security/probe/selftests/create_file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package selftests
import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/DataDog/datadog-agent/pkg/security/secl/compiler/eval"
Expand Down Expand Up @@ -37,7 +38,7 @@ func (o *WindowsCreateFileSelfTest) GetRuleDefinition() *rules.RuleDefinition {

return &rules.RuleDefinition{
ID: o.ruleID,
Expression: fmt.Sprintf(`create.file.name == "%s" && create.file.device_path =~ "%s" && process.pid == %d`, basename, filepath.ToSlash(devicePath), os.Getpid()),
Expression: fmt.Sprintf(`create.file.name == "%s" && create.file.device_path =~ "%s"`, basename, filepath.ToSlash(devicePath)),
Silent: true,
}
}
Expand All @@ -46,12 +47,21 @@ func (o *WindowsCreateFileSelfTest) GetRuleDefinition() *rules.RuleDefinition {
func (o *WindowsCreateFileSelfTest) GenerateEvent() error {
o.isSuccess = false

file, err := os.Create(o.filename)
if err != nil {
cmd := exec.Command(
"powershell",
"-c",
"New-Item",
"-Path",
o.filename,
"-ItemType",
"file",
)
if err := cmd.Run(); err != nil {
log.Debugf("error creating file: %v", err)
return err
}
return file.Close()

return os.Remove(o.filename)
}

// HandleEvent handles self test events
Expand Down
20 changes: 13 additions & 7 deletions pkg/security/probe/selftests/open_registry_key_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ package selftests

import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/DataDog/datadog-agent/pkg/security/secl/compiler/eval"
"github.com/DataDog/datadog-agent/pkg/security/secl/rules"
"github.com/DataDog/datadog-agent/pkg/util/log"

"golang.org/x/sys/windows/registry"
)

// WindowsOpenRegistryKeyTest defines a windows open registry key self test
Expand All @@ -33,7 +31,7 @@ func (o *WindowsOpenRegistryKeyTest) GetRuleDefinition() *rules.RuleDefinition {

return &rules.RuleDefinition{
ID: o.ruleID,
Expression: fmt.Sprintf(`open.registry.key_name == "%s" && process.pid == %d`, filepath.Base(o.keyPath), os.Getpid()),
Expression: fmt.Sprintf(`open.registry.key_name == "%s"`, filepath.Base(o.keyPath)),
Silent: true,
}
}
Expand All @@ -42,12 +40,20 @@ func (o *WindowsOpenRegistryKeyTest) GetRuleDefinition() *rules.RuleDefinition {
func (o *WindowsOpenRegistryKeyTest) GenerateEvent() error {
o.isSuccess = false

key, err := registry.OpenKey(registry.LOCAL_MACHINE, o.keyPath, registry.READ)
if err != nil {
path := fmt.Sprintf("Registry::HKEY_LOCAL_MACHINE:\\%s", o.keyPath)

cmd := exec.Command(
"powershell",
"-c",
"Get-ItemProperty",
"-Path",
path,
)
if err := cmd.Run(); err != nil {
log.Debugf("error opening registry key: %v", err)
return err
}
defer key.Close()

return nil
}

Expand Down

0 comments on commit c1ac65c

Please sign in to comment.