2020-05-15 09:39:16 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2020-11-30 09:37:17 +00:00
|
|
|
"fmt"
|
2022-09-06 11:55:03 +00:00
|
|
|
"os"
|
2023-07-26 08:29:58 +00:00
|
|
|
"path/filepath"
|
2023-01-04 15:50:02 +00:00
|
|
|
"sort"
|
2022-09-14 13:26:26 +00:00
|
|
|
"strings"
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-06-13 11:16:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/grokky"
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
2023-01-04 15:50:02 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
2023-03-28 14:26:47 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
2020-05-15 09:39:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UnixParserCtx struct {
|
2020-05-27 14:31:08 +00:00
|
|
|
Grok grokky.Host
|
|
|
|
Stages []string
|
|
|
|
Profiling bool
|
|
|
|
DataFolder string
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
type Parsers struct {
|
|
|
|
Ctx *UnixParserCtx
|
|
|
|
Povfwctx *UnixParserCtx
|
|
|
|
StageFiles []Stagefile
|
|
|
|
PovfwStageFiles []Stagefile
|
|
|
|
Nodes []Node
|
|
|
|
Povfwnodes []Node
|
2021-09-09 14:27:30 +00:00
|
|
|
EnricherCtx EnricherCtx
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func Init(c map[string]interface{}) (*UnixParserCtx, error) {
|
2020-05-15 09:39:16 +00:00
|
|
|
r := UnixParserCtx{}
|
|
|
|
r.Grok = grokky.NewBase()
|
2023-03-28 14:26:47 +00:00
|
|
|
r.Grok.UseRe2 = fflag.Re2GrokSupport.IsEnabled()
|
2022-09-06 11:55:03 +00:00
|
|
|
files, err := os.ReadDir(c["patterns"].(string))
|
2020-05-15 09:39:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-27 14:31:08 +00:00
|
|
|
r.DataFolder = c["data"].(string)
|
2020-05-15 09:39:16 +00:00
|
|
|
for _, f := range files {
|
2022-09-14 13:26:26 +00:00
|
|
|
if strings.Contains(f.Name(), ".") {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-26 08:29:58 +00:00
|
|
|
if err := r.Grok.AddFromFile(filepath.Join(c["patterns"].(string), f.Name())); err != nil {
|
2020-05-15 09:39:16 +00:00
|
|
|
log.Errorf("failed to load pattern %s : %v", f.Name(), err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Debugf("Loaded %d pattern files", len(files))
|
2020-05-15 09:39:16 +00:00
|
|
|
return &r, nil
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-01-04 15:50:02 +00:00
|
|
|
// Return new parsers
|
|
|
|
// nodes and povfwnodes are already initialized in parser.LoadStages
|
2023-11-24 14:57:32 +00:00
|
|
|
func NewParsers(hub *cwhub.Hub) *Parsers {
|
2023-01-04 15:50:02 +00:00
|
|
|
parsers := &Parsers{
|
|
|
|
Ctx: &UnixParserCtx{},
|
|
|
|
Povfwctx: &UnixParserCtx{},
|
|
|
|
StageFiles: make([]Stagefile, 0),
|
|
|
|
PovfwStageFiles: make([]Stagefile, 0),
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
for _, itemType := range []string{cwhub.PARSERS, cwhub.POSTOVERFLOWS} {
|
|
|
|
for _, hubParserItem := range hub.GetItemMap(itemType) {
|
|
|
|
if hubParserItem.State.Installed {
|
2023-01-04 15:50:02 +00:00
|
|
|
stagefile := Stagefile{
|
2023-11-24 14:57:32 +00:00
|
|
|
Filename: hubParserItem.State.LocalPath,
|
2023-01-04 15:50:02 +00:00
|
|
|
Stage: hubParserItem.Stage,
|
|
|
|
}
|
|
|
|
if itemType == cwhub.PARSERS {
|
|
|
|
parsers.StageFiles = append(parsers.StageFiles, stagefile)
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
if itemType == cwhub.POSTOVERFLOWS {
|
2023-01-04 15:50:02 +00:00
|
|
|
parsers.PovfwStageFiles = append(parsers.PovfwStageFiles, stagefile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if parsers.StageFiles != nil {
|
|
|
|
sort.Slice(parsers.StageFiles, func(i, j int) bool {
|
|
|
|
return parsers.StageFiles[i].Filename < parsers.StageFiles[j].Filename
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if parsers.PovfwStageFiles != nil {
|
|
|
|
sort.Slice(parsers.PovfwStageFiles, func(i, j int) bool {
|
|
|
|
return parsers.PovfwStageFiles[i].Filename < parsers.PovfwStageFiles[j].Filename
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsers
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
func LoadParsers(cConfig *csconfig.Config, parsers *Parsers) (*Parsers, error) {
|
2020-11-30 09:37:17 +00:00
|
|
|
var err error
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
patternsDir := filepath.Join(cConfig.ConfigPaths.ConfigDir, "patterns/")
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Infof("Loading grok library %s", patternsDir)
|
2020-11-30 09:37:17 +00:00
|
|
|
/* load base regexps for two grok parsers */
|
2021-08-25 16:30:05 +00:00
|
|
|
parsers.Ctx, err = Init(map[string]interface{}{"patterns": patternsDir,
|
2023-11-24 14:57:32 +00:00
|
|
|
"data": cConfig.ConfigPaths.DataDir})
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return parsers, fmt.Errorf("failed to load parser patterns : %v", err)
|
|
|
|
}
|
2021-08-25 16:30:05 +00:00
|
|
|
parsers.Povfwctx, err = Init(map[string]interface{}{"patterns": patternsDir,
|
2023-11-24 14:57:32 +00:00
|
|
|
"data": cConfig.ConfigPaths.DataDir})
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return parsers, fmt.Errorf("failed to load postovflw parser patterns : %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Load enrichers
|
|
|
|
*/
|
|
|
|
log.Infof("Loading enrich plugins")
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
parsers.EnricherCtx, err = Loadplugin(cConfig.ConfigPaths.DataDir)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2023-06-13 11:16:13 +00:00
|
|
|
return parsers, fmt.Errorf("failed to load enrich plugin : %v", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Load the actual parsers
|
|
|
|
*/
|
|
|
|
|
2022-07-01 14:56:13 +00:00
|
|
|
log.Infof("Loading parsers from %d files", len(parsers.StageFiles))
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
parsers.Nodes, err = LoadStages(parsers.StageFiles, parsers.Ctx, parsers.EnricherCtx)
|
|
|
|
if err != nil {
|
|
|
|
return parsers, fmt.Errorf("failed to load parser config : %v", err)
|
|
|
|
}
|
|
|
|
|
2022-07-01 14:56:13 +00:00
|
|
|
if len(parsers.PovfwStageFiles) > 0 {
|
|
|
|
log.Infof("Loading postoverflow parsers")
|
|
|
|
parsers.Povfwnodes, err = LoadStages(parsers.PovfwStageFiles, parsers.Povfwctx, parsers.EnricherCtx)
|
|
|
|
} else {
|
|
|
|
log.Infof("No postoverflow parsers to load")
|
2023-06-13 11:16:13 +00:00
|
|
|
parsers.Povfwnodes = []Node{}
|
2022-07-01 14:56:13 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return parsers, fmt.Errorf("failed to load postoverflow config : %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cConfig.Prometheus != nil && cConfig.Prometheus.Enabled {
|
|
|
|
parsers.Ctx.Profiling = true
|
|
|
|
parsers.Povfwctx.Profiling = true
|
|
|
|
}
|
2023-10-13 11:53:42 +00:00
|
|
|
/*
|
|
|
|
Reset CTX grok to reduce memory footprint after we compile all the patterns
|
|
|
|
*/
|
|
|
|
parsers.Ctx.Grok = grokky.Host{}
|
|
|
|
parsers.Povfwctx.Grok = grokky.Host{}
|
|
|
|
parsers.StageFiles = []Stagefile{}
|
|
|
|
parsers.PovfwStageFiles = []Stagefile{}
|
2020-11-30 09:37:17 +00:00
|
|
|
return parsers, nil
|
|
|
|
}
|