123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- package main
- import (
- "context"
- "errors"
- "fmt"
- "net/url"
- "os"
- "sort"
- "strings"
- "github.com/go-openapi/strfmt"
- log "github.com/sirupsen/logrus"
- "github.com/spf13/cobra"
- "gopkg.in/yaml.v2"
- "slices"
- "github.com/crowdsecurity/go-cs-lib/version"
- "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
- "github.com/crowdsecurity/crowdsec/pkg/alertcontext"
- "github.com/crowdsecurity/crowdsec/pkg/apiclient"
- "github.com/crowdsecurity/crowdsec/pkg/csconfig"
- "github.com/crowdsecurity/crowdsec/pkg/cwhub"
- "github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
- "github.com/crowdsecurity/crowdsec/pkg/models"
- "github.com/crowdsecurity/crowdsec/pkg/parser"
- )
- const LAPIURLPrefix = "v1"
- type cliLapi struct {
- cfg configGetter
- }
- func NewCLILapi(cfg configGetter) *cliLapi {
- return &cliLapi{
- cfg: cfg,
- }
- }
- func (cli *cliLapi) status() error {
- cfg := cli.cfg()
- password := strfmt.Password(cfg.API.Client.Credentials.Password)
- apiurl, err := url.Parse(cfg.API.Client.Credentials.URL)
- login := cfg.API.Client.Credentials.Login
- if err != nil {
- return fmt.Errorf("parsing api url: %w", err)
- }
- hub, err := require.Hub(cfg, nil, nil)
- if err != nil {
- return err
- }
- scenarios, err := hub.GetInstalledItemNames(cwhub.SCENARIOS)
- if err != nil {
- return fmt.Errorf("failed to get scenarios: %w", err)
- }
- Client, err = apiclient.NewDefaultClient(apiurl,
- LAPIURLPrefix,
- fmt.Sprintf("crowdsec/%s", version.String()),
- nil)
- if err != nil {
- return fmt.Errorf("init default client: %w", err)
- }
- t := models.WatcherAuthRequest{
- MachineID: &login,
- Password: &password,
- Scenarios: scenarios,
- }
- log.Infof("Loaded credentials from %s", cfg.API.Client.CredentialsFilePath)
- log.Infof("Trying to authenticate with username %s on %s", login, apiurl)
- _, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
- if err != nil {
- return fmt.Errorf("failed to authenticate to Local API (LAPI): %w", err)
- }
- log.Infof("You can successfully interact with Local API (LAPI)")
- return nil
- }
- func (cli *cliLapi) register(apiURL string, outputFile string, machine string) error {
- var err error
- lapiUser := machine
- cfg := cli.cfg()
- if lapiUser == "" {
- lapiUser, err = generateID("")
- if err != nil {
- return fmt.Errorf("unable to generate machine id: %w", err)
- }
- }
- password := strfmt.Password(generatePassword(passwordLength))
- if apiURL == "" {
- if cfg.API.Client == nil || cfg.API.Client.Credentials == nil || cfg.API.Client.Credentials.URL == "" {
- return fmt.Errorf("no Local API URL. Please provide it in your configuration or with the -u parameter")
- }
- apiURL = cfg.API.Client.Credentials.URL
- }
- /*URL needs to end with /, but user doesn't care*/
- if !strings.HasSuffix(apiURL, "/") {
- apiURL += "/"
- }
- /*URL needs to start with http://, but user doesn't care*/
- if !strings.HasPrefix(apiURL, "http://") && !strings.HasPrefix(apiURL, "https://") {
- apiURL = "http://" + apiURL
- }
- apiurl, err := url.Parse(apiURL)
- if err != nil {
- return fmt.Errorf("parsing api url: %w", err)
- }
- _, err = apiclient.RegisterClient(&apiclient.Config{
- MachineID: lapiUser,
- Password: password,
- UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
- URL: apiurl,
- VersionPrefix: LAPIURLPrefix,
- }, nil)
- if err != nil {
- return fmt.Errorf("api client register: %w", err)
- }
- log.Printf("Successfully registered to Local API (LAPI)")
- var dumpFile string
- if outputFile != "" {
- dumpFile = outputFile
- } else if cfg.API.Client.CredentialsFilePath != "" {
- dumpFile = cfg.API.Client.CredentialsFilePath
- } else {
- dumpFile = ""
- }
- apiCfg := csconfig.ApiCredentialsCfg{
- Login: lapiUser,
- Password: password.String(),
- URL: apiURL,
- }
- apiConfigDump, err := yaml.Marshal(apiCfg)
- if err != nil {
- return fmt.Errorf("unable to marshal api credentials: %w", err)
- }
- if dumpFile != "" {
- err = os.WriteFile(dumpFile, apiConfigDump, 0o600)
- if err != nil {
- return fmt.Errorf("write api credentials to '%s' failed: %w", dumpFile, err)
- }
- log.Printf("Local API credentials written to '%s'", dumpFile)
- } else {
- fmt.Printf("%s\n", string(apiConfigDump))
- }
- log.Warning(ReloadMessage())
- return nil
- }
- func (cli *cliLapi) newStatusCmd() *cobra.Command {
- cmdLapiStatus := &cobra.Command{
- Use: "status",
- Short: "Check authentication to Local API (LAPI)",
- Args: cobra.MinimumNArgs(0),
- DisableAutoGenTag: true,
- RunE: func(cmd *cobra.Command, args []string) error {
- return cli.status()
- },
- }
- return cmdLapiStatus
- }
- func (cli *cliLapi) newRegisterCmd() *cobra.Command {
- var (
- apiURL string
- outputFile string
- machine string
- )
- cmd := &cobra.Command{
- Use: "register",
- Short: "Register a machine to Local API (LAPI)",
- Long: `Register your machine to the Local API (LAPI).
- Keep in mind the machine needs to be validated by an administrator on LAPI side to be effective.`,
- Args: cobra.MinimumNArgs(0),
- DisableAutoGenTag: true,
- RunE: func(_ *cobra.Command, _ []string) error {
- return cli.register(apiURL, outputFile, machine)
- },
- }
- flags := cmd.Flags()
- flags.StringVarP(&apiURL, "url", "u", "", "URL of the API (ie. http://127.0.0.1)")
- flags.StringVarP(&outputFile, "file", "f", "", "output file destination")
- flags.StringVar(&machine, "machine", "", "Name of the machine to register with")
- return cmd
- }
- func (cli *cliLapi) NewCommand() *cobra.Command {
- cmd := &cobra.Command{
- Use: "lapi [action]",
- Short: "Manage interaction with Local API (LAPI)",
- Args: cobra.MinimumNArgs(1),
- DisableAutoGenTag: true,
- PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
- if err := cli.cfg().LoadAPIClient(); err != nil {
- return fmt.Errorf("loading api client: %w", err)
- }
- return nil
- },
- }
- cmd.AddCommand(cli.newRegisterCmd())
- cmd.AddCommand(cli.newStatusCmd())
- cmd.AddCommand(cli.newContextCmd())
- return cmd
- }
- func AddContext(key string, values []string) error {
- if err := alertcontext.ValidateContextExpr(key, values); err != nil {
- return fmt.Errorf("invalid context configuration :%s", err)
- }
- if _, ok := csConfig.Crowdsec.ContextToSend[key]; !ok {
- csConfig.Crowdsec.ContextToSend[key] = make([]string, 0)
- log.Infof("key '%s' added", key)
- }
- data := csConfig.Crowdsec.ContextToSend[key]
- for _, val := range values {
- if !slices.Contains(data, val) {
- log.Infof("value '%s' added to key '%s'", val, key)
- data = append(data, val)
- }
- csConfig.Crowdsec.ContextToSend[key] = data
- }
- if err := csConfig.Crowdsec.DumpContextConfigFile(); err != nil {
- return err
- }
- return nil
- }
- func (cli *cliLapi) newContextAddCmd() *cobra.Command {
- var keyToAdd string
- var valuesToAdd []string
- cmd := &cobra.Command{
- Use: "add",
- Short: "Add context to send with alerts. You must specify the output key with the expr value you want",
- Example: `cscli lapi context add --key source_ip --value evt.Meta.source_ip
- cscli lapi context add --key file_source --value evt.Line.Src
- cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
- `,
- DisableAutoGenTag: true,
- RunE: func(cmd *cobra.Command, args []string) error {
- hub, err := require.Hub(cli.cfg(), nil, nil)
- if err != nil {
- return err
- }
- if err = alertcontext.LoadConsoleContext(cli.cfg(), hub); err != nil {
- return fmt.Errorf("while loading context: %w", err)
- }
- if keyToAdd != "" {
- if err := AddContext(keyToAdd, valuesToAdd); err != nil {
- return err
- }
- return nil
- }
- for _, v := range valuesToAdd {
- keySlice := strings.Split(v, ".")
- key := keySlice[len(keySlice)-1]
- value := []string{v}
- if err := AddContext(key, value); err != nil {
- return err
- }
- }
- return nil
- },
- }
- flags := cmd.Flags()
- flags.StringVarP(&keyToAdd, "key", "k", "", "The key of the different values to send")
- flags.StringSliceVar(&valuesToAdd, "value", []string{}, "The expr fields to associate with the key")
- cmd.MarkFlagRequired("value")
- return cmd
- }
- func (cli *cliLapi) newContextCmd() *cobra.Command {
- cmd := &cobra.Command{
- Use: "context [command]",
- Short: "Manage context to send with alerts",
- DisableAutoGenTag: true,
- PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
- if err := csConfig.LoadCrowdsec(); err != nil {
- fileNotFoundMessage := fmt.Sprintf("failed to open context file: open %s: no such file or directory", csConfig.Crowdsec.ConsoleContextPath)
- if err.Error() != fileNotFoundMessage {
- return fmt.Errorf("unable to load CrowdSec agent configuration: %w", err)
- }
- }
- if csConfig.DisableAgent {
- return errors.New("agent is disabled and lapi context can only be used on the agent")
- }
- return nil
- },
- Run: func(cmd *cobra.Command, args []string) {
- printHelp(cmd)
- },
- }
- cmd.AddCommand(cli.newContextAddCmd())
- cmdContextStatus := &cobra.Command{
- Use: "status",
- Short: "List context to send with alerts",
- DisableAutoGenTag: true,
- RunE: func(cmd *cobra.Command, args []string) error {
- hub, err := require.Hub(csConfig, nil, nil)
- if err != nil {
- return err
- }
- if err = alertcontext.LoadConsoleContext(csConfig, hub); err != nil {
- return fmt.Errorf("while loading context: %w", err)
- }
- if len(csConfig.Crowdsec.ContextToSend) == 0 {
- fmt.Println("No context found on this agent. You can use 'cscli lapi context add' to add context to your alerts.")
- return nil
- }
- dump, err := yaml.Marshal(csConfig.Crowdsec.ContextToSend)
- if err != nil {
- return fmt.Errorf("unable to show context status: %w", err)
- }
- fmt.Print(string(dump))
- return nil
- },
- }
- cmd.AddCommand(cmdContextStatus)
- var detectAll bool
- cmdContextDetect := &cobra.Command{
- Use: "detect",
- Short: "Detect available fields from the installed parsers",
- Example: `cscli lapi context detect --all
- cscli lapi context detect crowdsecurity/sshd-logs
- `,
- DisableAutoGenTag: true,
- RunE: func(cmd *cobra.Command, args []string) error {
- if !detectAll && len(args) == 0 {
- log.Infof("Please provide parsers to detect or --all flag.")
- printHelp(cmd)
- }
- // to avoid all the log.Info from the loaders functions
- log.SetLevel(log.WarnLevel)
- if err := exprhelpers.Init(nil); err != nil {
- return fmt.Errorf("failed to init expr helpers: %w", err)
- }
- hub, err := require.Hub(csConfig, nil, nil)
- if err != nil {
- return err
- }
- csParsers := parser.NewParsers(hub)
- if csParsers, err = parser.LoadParsers(csConfig, csParsers); err != nil {
- return fmt.Errorf("unable to load parsers: %w", err)
- }
- fieldByParsers := make(map[string][]string)
- for _, node := range csParsers.Nodes {
- if !detectAll && !slices.Contains(args, node.Name) {
- continue
- }
- if !detectAll {
- args = removeFromSlice(node.Name, args)
- }
- fieldByParsers[node.Name] = make([]string, 0)
- fieldByParsers[node.Name] = detectNode(node, *csParsers.Ctx)
- subNodeFields := detectSubNode(node, *csParsers.Ctx)
- for _, field := range subNodeFields {
- if !slices.Contains(fieldByParsers[node.Name], field) {
- fieldByParsers[node.Name] = append(fieldByParsers[node.Name], field)
- }
- }
- }
- fmt.Printf("Acquisition :\n\n")
- fmt.Printf(" - evt.Line.Module\n")
- fmt.Printf(" - evt.Line.Raw\n")
- fmt.Printf(" - evt.Line.Src\n")
- fmt.Println()
- parsersKey := make([]string, 0)
- for k := range fieldByParsers {
- parsersKey = append(parsersKey, k)
- }
- sort.Strings(parsersKey)
- for _, k := range parsersKey {
- if len(fieldByParsers[k]) == 0 {
- continue
- }
- fmt.Printf("%s :\n\n", k)
- values := fieldByParsers[k]
- sort.Strings(values)
- for _, value := range values {
- fmt.Printf(" - %s\n", value)
- }
- fmt.Println()
- }
- if len(args) > 0 {
- for _, parserNotFound := range args {
- log.Errorf("parser '%s' not found, can't detect fields", parserNotFound)
- }
- }
- return nil
- },
- }
- cmdContextDetect.Flags().BoolVarP(&detectAll, "all", "a", false, "Detect evt field for all installed parser")
- cmd.AddCommand(cmdContextDetect)
- cmdContextDelete := &cobra.Command{
- Use: "delete",
- DisableAutoGenTag: true,
- RunE: func(_ *cobra.Command, _ []string) error {
- filePath := csConfig.Crowdsec.ConsoleContextPath
- if filePath == "" {
- filePath = "the context file"
- }
- fmt.Printf("Command \"delete\" is deprecated, please manually edit %s.", filePath)
- return nil
- },
- }
- cmd.AddCommand(cmdContextDelete)
- return cmd
- }
- func detectStaticField(GrokStatics []parser.ExtraField) []string {
- ret := make([]string, 0)
- for _, static := range GrokStatics {
- if static.Parsed != "" {
- fieldName := fmt.Sprintf("evt.Parsed.%s", static.Parsed)
- if !slices.Contains(ret, fieldName) {
- ret = append(ret, fieldName)
- }
- }
- if static.Meta != "" {
- fieldName := fmt.Sprintf("evt.Meta.%s", static.Meta)
- if !slices.Contains(ret, fieldName) {
- ret = append(ret, fieldName)
- }
- }
- if static.TargetByName != "" {
- fieldName := static.TargetByName
- if !strings.HasPrefix(fieldName, "evt.") {
- fieldName = "evt." + fieldName
- }
- if !slices.Contains(ret, fieldName) {
- ret = append(ret, fieldName)
- }
- }
- }
- return ret
- }
- func detectNode(node parser.Node, parserCTX parser.UnixParserCtx) []string {
- ret := make([]string, 0)
- if node.Grok.RunTimeRegexp != nil {
- for _, capturedField := range node.Grok.RunTimeRegexp.Names() {
- fieldName := fmt.Sprintf("evt.Parsed.%s", capturedField)
- if !slices.Contains(ret, fieldName) {
- ret = append(ret, fieldName)
- }
- }
- }
- if node.Grok.RegexpName != "" {
- grokCompiled, err := parserCTX.Grok.Get(node.Grok.RegexpName)
- // ignore error (parser does not exist?)
- if err == nil {
- for _, capturedField := range grokCompiled.Names() {
- fieldName := fmt.Sprintf("evt.Parsed.%s", capturedField)
- if !slices.Contains(ret, fieldName) {
- ret = append(ret, fieldName)
- }
- }
- }
- }
- if len(node.Grok.Statics) > 0 {
- staticsField := detectStaticField(node.Grok.Statics)
- for _, staticField := range staticsField {
- if !slices.Contains(ret, staticField) {
- ret = append(ret, staticField)
- }
- }
- }
- if len(node.Statics) > 0 {
- staticsField := detectStaticField(node.Statics)
- for _, staticField := range staticsField {
- if !slices.Contains(ret, staticField) {
- ret = append(ret, staticField)
- }
- }
- }
- return ret
- }
- func detectSubNode(node parser.Node, parserCTX parser.UnixParserCtx) []string {
- var ret = make([]string, 0)
- for _, subnode := range node.LeavesNodes {
- if subnode.Grok.RunTimeRegexp != nil {
- for _, capturedField := range subnode.Grok.RunTimeRegexp.Names() {
- fieldName := fmt.Sprintf("evt.Parsed.%s", capturedField)
- if !slices.Contains(ret, fieldName) {
- ret = append(ret, fieldName)
- }
- }
- }
- if subnode.Grok.RegexpName != "" {
- grokCompiled, err := parserCTX.Grok.Get(subnode.Grok.RegexpName)
- if err == nil {
- // ignore error (parser does not exist?)
- for _, capturedField := range grokCompiled.Names() {
- fieldName := fmt.Sprintf("evt.Parsed.%s", capturedField)
- if !slices.Contains(ret, fieldName) {
- ret = append(ret, fieldName)
- }
- }
- }
- }
- if len(subnode.Grok.Statics) > 0 {
- staticsField := detectStaticField(subnode.Grok.Statics)
- for _, staticField := range staticsField {
- if !slices.Contains(ret, staticField) {
- ret = append(ret, staticField)
- }
- }
- }
- if len(subnode.Statics) > 0 {
- staticsField := detectStaticField(subnode.Statics)
- for _, staticField := range staticsField {
- if !slices.Contains(ret, staticField) {
- ret = append(ret, staticField)
- }
- }
- }
- }
- return ret
- }
|