Merge remote-tracking branch 'origin/TOML' into TOML

This commit is contained in:
Rudi klein 2024-05-27 20:24:08 +02:00
commit 7afba8210c
8 changed files with 29 additions and 30 deletions

View File

@ -3,8 +3,6 @@ module wazuh-notify
go 1.22 go 1.22
require ( require (
github.com/BurntSushi/toml v1.4.0
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
gopkg.in/yaml.v2 v2.4.0
) )
require github.com/BurntSushi/toml v1.4.0 // indirect

View File

@ -10,10 +10,11 @@ import (
) )
func main() { func main() {
//Read config file and .env
configParams := services.ReadConfig() configParams := services.ReadConfig()
//Parse command line flags
inputParams := services.ParseFlags(configParams) inputParams := services.ParseFlags(configParams)
//Parse wazuh input data from stdin
Params := services.ParseWazuhInput(inputParams) Params := services.ParseWazuhInput(inputParams)
for _, target := range strings.Split(Params.General.Targets, ", ") { for _, target := range strings.Split(Params.General.Targets, ", ") {

View File

@ -12,12 +12,12 @@ import (
func ReadConfig() types.Params { func ReadConfig() types.Params {
var configParams types.Params var configParams types.Params
//Get Path of executable location
baseFilePath, _ := os.Executable() baseFilePath, _ := os.Executable()
baseDirPath := path.Dir(baseFilePath) baseDirPath := path.Dir(baseFilePath)
//Open log file and set first message
log.OpenLogFile(baseDirPath) log.OpenLogFile(baseDirPath)
//Load .env into environment variables
err := godotenv.Load(path.Join(baseDirPath, "../../etc/.env")) err := godotenv.Load(path.Join(baseDirPath, "../../etc/.env"))
if err != nil { if err != nil {
log.Log("env failed to load") log.Log("env failed to load")
@ -25,7 +25,7 @@ func ReadConfig() types.Params {
} else { } else {
log.Log("env loaded") log.Log("env loaded")
} }
//Read config file
tomlFile, err := os.ReadFile(path.Join(baseDirPath, "../../etc/wazuh-notify-config.toml")) tomlFile, err := os.ReadFile(path.Join(baseDirPath, "../../etc/wazuh-notify-config.toml"))
if err != nil { if err != nil {
log.Log("toml failed to load") log.Log("toml failed to load")

View File

@ -7,17 +7,14 @@ import (
) )
func ParseFlags(params types.Params) types.Params { func ParseFlags(params types.Params) types.Params {
//Set command line flags
flag.StringVar(&params.Url, "url", "", "is the webhook URL of the Discord server. It is stored in .env.")
flag.StringVar(&params.General.Click, "click", params.General.Click, "is a link (URL) that can be followed by tapping/clicking inside the message. Default is https://google.com.") flag.StringVar(&params.General.Click, "click", params.General.Click, "is a link (URL) that can be followed by tapping/clicking inside the message. Default is https://google.com.")
flag.IntVar(&params.Priority, "priority", 0, "is the priority of the message, ranging from 1 (highest), to 5 (lowest). Default is 5.")
flag.StringVar(&params.General.Sender, "sender", params.General.Sender+" Golang", "is the sender of the message, either an app name or a person. The default is \"Security message\".") flag.StringVar(&params.General.Sender, "sender", params.General.Sender+" Golang", "is the sender of the message, either an app name or a person. The default is \"Security message\".")
flag.StringVar(&params.Tags, "tags", "", "is an arbitrary strings of tags (keywords), seperated by a \",\" (comma). Default is \"informational,testing,hard-coded\".")
flag.StringVar(&params.General.Targets, "targets", params.General.Targets, "is a list of targets to send notifications to. Default is \"discord\".") flag.StringVar(&params.General.Targets, "targets", params.General.Targets, "is a list of targets to send notifications to. Default is \"discord\".")
//Get flag values
flag.Parse() flag.Parse()
log.Log("params loaded") log.Log("flags loaded")
return params return params
} }

View File

@ -13,23 +13,26 @@ import (
func ParseWazuhInput(params types.Params) types.Params { func ParseWazuhInput(params types.Params) types.Params {
var wazuhData types.WazuhMessage var wazuhData types.WazuhMessage
//Read stdin
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
//Decode stdin to wazuhData
json.NewDecoder(reader).Decode(&wazuhData) json.NewDecoder(reader).Decode(&wazuhData)
//Parse tags
params.Tags += strings.Join(wazuhData.Parameters.Alert.Rule.Groups, ",") params.Tags += strings.Join(wazuhData.Parameters.Alert.Rule.Groups, ",")
params.WazuhMessage = wazuhData params.WazuhMessage = wazuhData
//Map priority and color based on config
for i := range params.PriorityMap { for i := range params.PriorityMap {
if slices.Contains(params.PriorityMap[i].ThreatMap, wazuhData.Parameters.Alert.Rule.Level) { if slices.Contains(params.PriorityMap[i].ThreatMap, wazuhData.Parameters.Alert.Rule.Level) {
//Check notify threshold
if params.WazuhMessage.Parameters.Alert.Rule.Firedtimes%params.PriorityMap[i].NotifyThreshold != 0 { if params.WazuhMessage.Parameters.Alert.Rule.Firedtimes%params.PriorityMap[i].NotifyThreshold != 0 {
log.Log("threshold not met") log.Log("threshold not met")
log.CloseLogFile() log.CloseLogFile()
os.Exit(0) os.Exit(0)
} }
//Set color based on config map
params.Color = params.PriorityMap[i].Color params.Color = params.PriorityMap[i].Color
//Check mention threshold
if params.WazuhMessage.Parameters.Alert.Rule.Firedtimes >= params.PriorityMap[i].MentionThreshold { if params.WazuhMessage.Parameters.Alert.Rule.Firedtimes >= params.PriorityMap[i].MentionThreshold {
params.Mention = "@here" params.Mention = "@here"
} }
@ -38,7 +41,7 @@ func ParseWazuhInput(params types.Params) types.Params {
} }
log.Log("Wazuh data loaded") log.Log("Wazuh data loaded")
//Filter messages based on rules defined in config
Filter(params) Filter(params)
return params return params

View File

@ -11,11 +11,11 @@ import (
) )
func SendDiscord(params types.Params) { func SendDiscord(params types.Params) {
//Build message content
embedDescription := services.BuildMessage(params, "discord", params.MarkdownEmphasis.Discord) + embedDescription := services.BuildMessage(params, "discord", params.MarkdownEmphasis.Discord) +
"**Tags:** " + params.Tags + "\n\n" + "**Tags:** " + params.Tags + "\n\n" +
params.General.Click params.General.Click
//Build message
message := DiscordMessage{ message := DiscordMessage{
Username: params.General.Sender, Username: params.General.Sender,
Content: params.Mention, Content: params.Mention,
@ -29,12 +29,12 @@ func SendDiscord(params types.Params) {
} }
payload := new(bytes.Buffer) payload := new(bytes.Buffer)
//Parse message to json
err := json.NewEncoder(payload).Encode(message) err := json.NewEncoder(payload).Encode(message)
if err != nil { if err != nil {
return return
} }
//Send message to webhook
_, err = http.Post(os.Getenv("DISCORD_URL"), "application/json", payload) _, err = http.Post(os.Getenv("DISCORD_URL"), "application/json", payload)
if err != nil { if err != nil {
log.Fatalf("An Error Occured %v", err) log.Fatalf("An Error Occured %v", err)

View File

@ -10,14 +10,14 @@ import (
) )
func SendNtfy(params types.Params) { func SendNtfy(params types.Params) {
//Create request and build message
req, _ := http.NewRequest( req, _ := http.NewRequest(
"POST", "POST",
os.Getenv("NTFY_URL"), os.Getenv("NTFY_URL"),
strings.NewReader(" "+services.BuildMessage(params, "ntfy", params.MarkdownEmphasis.Ntfy))) strings.NewReader(" "+services.BuildMessage(params, "ntfy", params.MarkdownEmphasis.Ntfy)))
req.Header.Set("Content-Type", "text/markdown") req.Header.Set("Content-Type", "text/markdown")
//Set headers if not empty
if params.General.Sender != "" { if params.General.Sender != "" {
req.Header.Add("Title", params.General.Sender) req.Header.Add("Title", params.General.Sender)
} }
@ -30,6 +30,6 @@ func SendNtfy(params types.Params) {
if params.Priority != 0 { if params.Priority != 0 {
req.Header.Add("Priority", strconv.Itoa(params.Priority)) req.Header.Add("Priority", strconv.Itoa(params.Priority))
} }
//Send request
http.DefaultClient.Do(req) http.DefaultClient.Do(req)
} }

View File

@ -11,7 +11,7 @@ import (
) )
func SendSlack(params types.Params) { func SendSlack(params types.Params) {
//Build message
message := SlackMessage{ message := SlackMessage{
Text: services.BuildMessage(params, "slack", params.MarkdownEmphasis.Slack) + Text: services.BuildMessage(params, "slack", params.MarkdownEmphasis.Slack) +
"*Tags:* " + params.Tags + "\n\n" + "*Tags:* " + params.Tags + "\n\n" +
@ -19,12 +19,12 @@ func SendSlack(params types.Params) {
} }
payload := new(bytes.Buffer) payload := new(bytes.Buffer)
//Parse message to json
err := json.NewEncoder(payload).Encode(message) err := json.NewEncoder(payload).Encode(message)
if err != nil { if err != nil {
return return
} }
//Send message to webhook
_, err = http.Post(os.Getenv("SLACK_URL"), "application/json", payload) _, err = http.Post(os.Getenv("SLACK_URL"), "application/json", payload)
if err != nil { if err != nil {
log.Fatalf("An Error Occured %v", err) log.Fatalf("An Error Occured %v", err)