-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(processor): added key-value and CEF processor (#49)
- Loading branch information
Showing
7 changed files
with
521 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,49 @@ | ||
package app | ||
|
||
import ( | ||
filein "github.com/ThoronicLLC/collector/internal/input/file" | ||
"github.com/ThoronicLLC/collector/internal/input/msgraph" | ||
pubsubin "github.com/ThoronicLLC/collector/internal/input/pubsub" | ||
"github.com/ThoronicLLC/collector/internal/input/syslog" | ||
fileout "github.com/ThoronicLLC/collector/internal/output/file" | ||
"github.com/ThoronicLLC/collector/internal/output/gcs" | ||
"github.com/ThoronicLLC/collector/internal/output/log_analytics" | ||
pubsubout "github.com/ThoronicLLC/collector/internal/output/pubsub" | ||
"github.com/ThoronicLLC/collector/internal/output/s3" | ||
"github.com/ThoronicLLC/collector/internal/output/stdout" | ||
"github.com/ThoronicLLC/collector/internal/processor/cel" | ||
syslog_processor "github.com/ThoronicLLC/collector/internal/processor/syslog" | ||
"github.com/ThoronicLLC/collector/pkg/core" | ||
|
||
file_input "github.com/ThoronicLLC/collector/internal/input/file" | ||
msgraph_input "github.com/ThoronicLLC/collector/internal/input/msgraph" | ||
pubsub_input "github.com/ThoronicLLC/collector/internal/input/pubsub" | ||
syslog_input "github.com/ThoronicLLC/collector/internal/input/syslog" | ||
|
||
cel_processor "github.com/ThoronicLLC/collector/internal/processor/cel" | ||
kv_processor "github.com/ThoronicLLC/collector/internal/processor/kv" | ||
syslog_processor "github.com/ThoronicLLC/collector/internal/processor/syslog" | ||
|
||
file_output "github.com/ThoronicLLC/collector/internal/output/file" | ||
gcs_output "github.com/ThoronicLLC/collector/internal/output/gcs" | ||
log_analytics_output "github.com/ThoronicLLC/collector/internal/output/log_analytics" | ||
pubsub_output "github.com/ThoronicLLC/collector/internal/output/pubsub" | ||
s3_output "github.com/ThoronicLLC/collector/internal/output/s3" | ||
stdout_output "github.com/ThoronicLLC/collector/internal/output/stdout" | ||
) | ||
|
||
func AddInternalInputs() map[string]core.InputHandler { | ||
return map[string]core.InputHandler{ | ||
filein.InputName: filein.Handler(), | ||
pubsubin.InputName: pubsubin.Handler(), | ||
syslog.InputName: syslog.Handler(), | ||
msgraph.InputName: msgraph.Handler(), | ||
file_input.InputName: file_input.Handler(), | ||
pubsub_input.InputName: pubsub_input.Handler(), | ||
syslog_input.InputName: syslog_input.Handler(), | ||
msgraph_input.InputName: msgraph_input.Handler(), | ||
} | ||
} | ||
|
||
func AddInternalProcessors() map[string]core.ProcessHandler { | ||
return map[string]core.ProcessHandler{ | ||
cel.ProcessorName: cel.Handler(), | ||
cel_processor.ProcessorName: cel_processor.Handler(), | ||
syslog_processor.ProcessorName: syslog_processor.Handler(), | ||
kv_processor.ProcessorName: kv_processor.Handler(), | ||
} | ||
} | ||
|
||
func AddInternalOutputs() map[string]core.OutputHandler { | ||
return map[string]core.OutputHandler{ | ||
fileout.OutputName: fileout.Handler(), | ||
stdout.OutputName: stdout.Handler(), | ||
s3.OutputName: s3.Handler(), | ||
gcs.OutputName: gcs.Handler(), | ||
log_analytics.OutputName: log_analytics.Handler(), | ||
pubsubout.OutputName: pubsubout.Handler(), | ||
file_output.OutputName: file_output.Handler(), | ||
stdout_output.OutputName: stdout_output.Handler(), | ||
s3_output.OutputName: s3_output.Handler(), | ||
gcs_output.OutputName: gcs_output.Handler(), | ||
log_analytics_output.OutputName: log_analytics_output.Handler(), | ||
pubsub_output.OutputName: pubsub_output.Handler(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package kv | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/dlclark/regexp2" | ||
) | ||
|
||
type cefEvent struct { | ||
Version string | ||
DeviceVendor string | ||
DeviceProduct string | ||
DeviceVersion string | ||
DeviceEventClassId string | ||
Name string | ||
Severity string | ||
Extensions map[string]string | ||
} | ||
|
||
func parseCef(event string) ([]byte, error) { | ||
cefMsg, err := cefStringToObject(event) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Marshal JSON string | ||
jsonString, err := json.Marshal(cefMsg) | ||
|
||
// Handle errors | ||
if err != nil { | ||
fmt.Println(3) | ||
return nil, err | ||
} | ||
|
||
return jsonString, nil | ||
} | ||
|
||
func cefStringToObject(cefString string) (*cefEvent, error) { | ||
// Split by CEF separator | ||
arr := strings.Split(cefString, "|") | ||
|
||
if len(arr) < 7 { | ||
return nil, fmt.Errorf("invalid CEF format") | ||
} | ||
|
||
version := "" | ||
|
||
if strings.Contains(arr[0], ":") { | ||
// Split first field to validate CEF | ||
validate := strings.Split(arr[0], ":") | ||
|
||
// Validate that it is a valid CEF message | ||
if validate[0] != "CEF" { | ||
return nil, fmt.Errorf("invalid CEF format") | ||
} | ||
|
||
version = validate[1] | ||
} else { | ||
if _, err := strconv.Atoi(arr[0]); err != nil { | ||
return nil, fmt.Errorf("invalid CEF format") | ||
} | ||
version = arr[0] | ||
} | ||
|
||
// Get extensions | ||
extensions := strings.Join(arr[7:], "|") | ||
|
||
// Replace colons with {{COLON}} | ||
safeExtensions := strings.ReplaceAll(extensions, ":", "{{COLON}}") | ||
safeExtensions = strings.ReplaceAll(safeExtensions, `\\=`, "{{EQUAL_ESCAPE_2}}") | ||
safeExtensions = strings.ReplaceAll(safeExtensions, `\=`, "{{EQUAL_ESCAPE_1}}") | ||
|
||
// Replace non KV spaces with {{SPACE}} | ||
re := regexp2.MustCompile(`\s(?!([\w\-]+)\=)`, 0) | ||
safeExtensions2, err := re.Replace(safeExtensions, "{{SPACE}}", -1, -1) | ||
|
||
// Parse extensions in key value format | ||
keyValueMap, err := parseKeyValue(safeExtensions2, true) | ||
|
||
// Handle error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Restore colons | ||
newKeyValueMap := make(map[string]string, 0) | ||
for k, v := range keyValueMap { | ||
newKey := strings.ReplaceAll(k, `{{SPACE}}`, " ") | ||
newKey = strings.ReplaceAll(newKey, `{{EQUAL_ESCAPE_1}}`, `\=`) | ||
newKey = strings.ReplaceAll(newKey, `{{EQUAL_ESCAPE_2}}`, `\\=`) | ||
newKey = strings.ReplaceAll(newKey, `{{COLON}}`, ":") | ||
|
||
newValue := strings.ReplaceAll(v, `{{SPACE}}`, " ") | ||
newValue = strings.ReplaceAll(newValue, `{{EQUAL_ESCAPE_1}}`, `\=`) | ||
newValue = strings.ReplaceAll(newValue, `{{EQUAL_ESCAPE_2}}`, `\\=`) | ||
newValue = strings.ReplaceAll(newValue, `{{COLON}}`, ":") | ||
newValue = strings.TrimSpace(newValue) | ||
|
||
newKeyValueMap[newKey] = newValue | ||
} | ||
|
||
// Build CEF event | ||
cefEvent := &cefEvent{ | ||
Version: version, | ||
DeviceVendor: cefEscapeField(arr[1]), | ||
DeviceProduct: cefEscapeField(arr[2]), | ||
DeviceVersion: cefEscapeField(arr[3]), | ||
DeviceEventClassId: cefEscapeField(arr[4]), | ||
Name: cefEscapeField(arr[5]), | ||
Severity: cefEscapeField(arr[6]), | ||
Extensions: newKeyValueMap, | ||
} | ||
|
||
return cefEvent, nil | ||
} | ||
|
||
// Unescape CEF fields | ||
func cefEscapeField(field string) string { | ||
|
||
replacer := strings.NewReplacer( | ||
"\\\\", "\\", | ||
"\\|", "|", | ||
"\\n", "\n", | ||
) | ||
|
||
return replacer.Replace(field) | ||
} | ||
|
||
// Unescape CEF extensions | ||
func cefEscapeExtension(field string) string { | ||
|
||
replacer := strings.NewReplacer( | ||
"\\\\", "\\", | ||
"\\n", "\n", | ||
"\\=", "=", | ||
) | ||
|
||
return replacer.Replace(field) | ||
} |
Oops, something went wrong.