Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added event for default rule "Read SSH Information" #112

Merged
merged 9 commits into from
Apr 2, 2024
50 changes: 50 additions & 0 deletions events/syscall/read_ssh_information.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//go:build linux
// +build linux

// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2023 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package syscall

import (
"path/filepath"

"github.com/falcosecurity/event-generator/events"

"os"
)

var _ = events.Register(
ReadSshInformation,
events.WithDisabled(), // the rule is not enabled by default, so disable the action too
)

func ReadSshInformation(h events.Helper) error {
// Creates .ssh directory inside tempDirectory
sshDir, cleanup, err := createSshDirectoryUnderHome()
if err != nil {
return err
}
defer cleanup() // Cleanup after function return

GLVSKiriti marked this conversation as resolved.
Show resolved Hide resolved
// Create known_hosts file. os.Create is enough to trigger the rule
filename := filepath.Join(sshDir, "known_hosts")
if _, err := os.Create(filename); err != nil {
return err
}

h.Log().Info("attempting to simulate SSH information read")

return nil
}
23 changes: 23 additions & 0 deletions events/syscall/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ limitations under the License.
package syscall

import (
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
sys "syscall"

Expand Down Expand Up @@ -91,3 +93,24 @@ func runAsUser(h events.Helper, username string, cmdName string, cmdArgs ...stri
}
return cmd.Run()
}

// Creates a temp directory and .ssh directory inside it.
func createSshDirectoryUnderHome() (string, func(), error) {
// Creates temporary data for testing.
var (
tempDirectoryName string
err error
)
// Loop until a unique temporary directory is successfully created
if tempDirectoryName, err = os.MkdirTemp("/home", "falco-event-generator-"); err != nil {
return "", func() {}, err
}

// Create the SSH directory
sshDir := filepath.Join(tempDirectoryName, ".ssh")
if err := os.Mkdir(sshDir, 0755); err != nil {
return "", func() { _ = os.RemoveAll(tempDirectoryName) }, err
}

return sshDir, func() { _ = os.RemoveAll(tempDirectoryName) }, nil
}
Loading