Skip to content

Commit

Permalink
update(pkg/runner/helper): SpawnAsWithSymlink and SpawnAs to copy the…
Browse files Browse the repository at this point in the history
… binary

This can be used to trigger rules that are based on proc.exepath rather than proc.name
for better detection purposes.

Signed-off-by: Lorenzo Susini <[email protected]>
  • Loading branch information
loresuso committed Apr 9, 2024
1 parent 786ea1c commit f3a33e6
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions events/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type Helper interface {
// The child runs the given action as a different program name.
SpawnAs(name string, action string, args ...string) error

// SpawnAsWithSymlink works like SpawnAs, except that it does make a copy,
// but creates a symlink to the current event-generator binary.
SpawnAsWithSymlink(name string, action string, args ...string) error

// Spawned returns true if the action is running in a child process.
Spawned() bool

Expand Down
2 changes: 1 addition & 1 deletion events/syscall/db_program_spawned_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ var _ = events.Register(
)

func DbProgramSpawnedProcess(h events.Helper) error {
return h.SpawnAs("mysqld", "helper.ExecLs")
return h.SpawnAsWithSymlink("mysqld", "helper.ExecLs")
}
2 changes: 1 addition & 1 deletion events/syscall/non_sudo_setuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func NonSudoSetuid(h events.Helper) error {
h.Log().WithError(err).Debug("ignore root setuid error")
return nil
} else {
return h.SpawnAs("child", "syscall.NonSudoSetuid")
return h.SpawnAsWithSymlink("child", "syscall.NonSudoSetuid")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
var _ = events.Register(ReadSensitiveFileTrustedAfterStartup)

func ReadSensitiveFileTrustedAfterStartup(h events.Helper) error {
return h.SpawnAs("httpd", "syscall.ReadSensitiveFileUntrusted", "--sleep", "6s")
return h.SpawnAsWithSymlink("httpd", "syscall.ReadSensitiveFileUntrusted", "--sleep", "6s")
}
2 changes: 1 addition & 1 deletion events/syscall/run_shell_untrusted.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ import (
var _ = events.Register(RunShellUntrusted)

func RunShellUntrusted(h events.Helper) error {
return h.SpawnAs("httpd", "helper.RunShell")
return h.SpawnAsWithSymlink("httpd", "helper.RunShell")
}
2 changes: 1 addition & 1 deletion events/syscall/system_procs_network_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ var _ = events.Register(
)

func SystemProcsNetworkActivity(h events.Helper) error {
return h.SpawnAs("sha1sum", "helper.NetworkActivity")
return h.SpawnAsWithSymlink("sha1sum", "helper.NetworkActivity")
}
2 changes: 1 addition & 1 deletion events/syscall/user_mgmt_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func UserMgmtBinaries(h events.Helper) error {
Reason: "'User mgmt binaries' is excluded in containers",
}
}
return h.SpawnAs("vipw", "helper.ExecLs")
return h.SpawnAsWithSymlink("vipw", "helper.ExecLs")
}
33 changes: 33 additions & 0 deletions pkg/runner/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ func (h *helper) SpawnAs(name string, action string, args ...string) error {
}
defer os.RemoveAll(tmpDir)

name = filepath.Join(tmpDir, name)
var data []byte
if data, err = os.ReadFile(h.runner.exePath); err != nil {
return err
}
if err = os.WriteFile(name, data, 0755); err != nil {
return err
}

cmd := exec.Command(name, append(h.runner.exeArgs, fullArgs...)...)

out := h.runner.log.Out
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return err
}

return nil
}

func (h *helper) SpawnAsWithSymlink(name string, action string, args ...string) error {
fullArgs := append([]string{fmt.Sprintf("^%s$", action)}, args...)
h.Log().WithField("args", strings.Join(fullArgs, " ")).Infof(`spawn as "%s"`, name)
if h.Spawned() {
return ErrChildSpawn
}
tmpDir, err := os.MkdirTemp(os.TempDir(), "falco-event-generator")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

name = filepath.Join(tmpDir, name)
if err := os.Symlink(h.runner.exePath, name); err != nil {
return err
Expand Down

0 comments on commit f3a33e6

Please sign in to comment.