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

James/interactive mode #841

Merged
merged 17 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5e234af
rough first pass at interactive mode
James-Pickett Jun 28, 2022
aee84f1
interactive mode working with launcher tables
James-Pickett Jun 30, 2022
ef2b715
Merge branch 'master' into james/interactive-mode
James-Pickett Jul 5, 2022
fde613e
moved interactive logic to pkg to facilitate testing, updated in resp…
James-Pickett Jul 6, 2022
47f6429
improving error messaging, fixing mis spelled comments
James-Pickett Jul 6, 2022
2fb1b3b
adding missing error handling to interactive cmd
James-Pickett Jul 6, 2022
611d911
updated download osquery cache dir to use os.TempDir() so it will wor…
James-Pickett Jul 6, 2022
86a65e5
updated socket too long test to be long enough to test on linux
James-Pickett Jul 6, 2022
7b79ec1
giving interactive test its own cache dir for binary to see if that s…
James-Pickett Jul 6, 2022
72b7017
disabling test on windows
James-Pickett Jul 6, 2022
e9960e0
adjusted max socket path lenght and added unit tests to verify that t…
James-Pickett Jul 6, 2022
512cf64
updated osquery-go pkg, moved pkg/interactive to pkg/osquery/interact…
James-Pickett Jul 19, 2022
8fa0095
Merge branch 'kolide:master' into james/interactive-mode
James-Pickett Jul 19, 2022
6759b1e
modified unit test to test flag parsing and error handling
James-Pickett Jul 19, 2022
c2bf9c6
fixed some ill formated fmt.Errorf statements
James-Pickett Jul 19, 2022
23a1f2a
Merge branch 'kolide:master' into james/interactive-mode
James-Pickett Jul 20, 2022
1ae476c
removed unneeded setting of proc to cwd, added call to shutdown exten…
James-Pickett Jul 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions cmd/launcher/interactive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"time"

"github.com/go-kit/kit/log"
"github.com/kolide/kit/env"
"github.com/kolide/kit/fs"
"github.com/kolide/launcher/pkg/osquery/table"
osquery "github.com/osquery/osquery-go"
"github.com/pkg/errors"
)

func interactive(args []string) error {
flagset := flag.NewFlagSet("interactive", flag.ExitOnError)
var (
flOsquerydPath = flagset.String(
"osqueryd_path",
"",
"The path to the oqueryd binary",
)
flSocketPath = flagset.String(
"socket_path",
env.String("SOCKET_PATH", filepath.Join(os.TempDir(), "osquery.sock")),
"The path to the socket",
)
)

flagset.Usage = commandUsage(flagset, "interactive")
if err := flagset.Parse(args); err != nil {
return err
}

osquerydPath := *flOsquerydPath
if osquerydPath == "" {
osquerydPath = findOsquery()
if osquerydPath == "" {
return errors.New("Could not find osqueryd binary")
}
}

if _, err := os.Stat(filepath.Dir(*flSocketPath)); os.IsNotExist(err) {
if err := os.Mkdir(filepath.Dir(*flSocketPath), fs.DirMode); err != nil {
return errors.Wrap(err, "creating socket path base directory")
}
}

// Transfer stdin, stdout, and stderr to the new process
// and also set target directory for the shell to start in.
pa := os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
}

// Start up a new shell.
fmt.Println(">> Starting osquery interactive with launcher tables")

osqueryProc, err := os.StartProcess(osquerydPath, []string{
"-S",
fmt.Sprintf("--extensions_socket=%s", *flSocketPath),
}, &pa)

if err != nil {
return fmt.Errorf("error starting osqueryd: %s", err)
}

extensionManagerServer, err := loadExtensions(*flSocketPath, osquerydPath)
if err != nil {
extensionManagerServer.Shutdown(context.Background())
return fmt.Errorf("error loading extensions: %s", err)
}

// Wait until user exits the shell
state, err := osqueryProc.Wait()
if err != nil {
return fmt.Errorf("error waiting for osqueryd: %s", err)
}

// Keep on keepin' on.
fmt.Printf("<< Exited osquery interactive with launcher tables: %s\n", state.String())

if err := extensionManagerServer.Shutdown(context.Background()); err != nil {
return fmt.Errorf("error shutting down extension manager: %s", err)
}

return nil
}

func loadExtensions(socketPath string, osquerydPath string) (*osquery.ExtensionManagerServer, error) {

extensionManagerServer, err := osquery.NewExtensionManagerServer(
"interactive",
socketPath,
osquery.ServerTimeout(10*time.Second),
)

if err != nil {
return extensionManagerServer, fmt.Errorf("error creating extension manager server: %s", err)
}

client, err := osquery.NewClient(socketPath, 10*time.Second)
if err != nil {
return extensionManagerServer, fmt.Errorf("error creating osquery client: %s", err)
}

extensionManagerServer.RegisterPlugin(table.PlatformTables(client, log.NewNopLogger(), osquerydPath)...)
extensionManagerServer.RegisterPlugin(table.LauncherTables(nil, nil)...)

if err := extensionManagerServer.Start(); err != nil {
return extensionManagerServer, errors.Wrap(err, "running extension server")
}

return extensionManagerServer, nil
}
2 changes: 2 additions & 0 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func runSubcommands() error {
run = runVersion
case "compactdb":
run = runCompactDb
case "interactive":
run = interactive
default:
return errors.Errorf("Unknown subcommand %s", os.Args[1])
}
Expand Down