98 lines
3.1 KiB
Go
Raw Normal View History

2024-05-09 15:27:45 +02:00
package services
2024-05-08 01:56:48 +02:00
import (
2024-05-09 15:27:45 +02:00
"bufio"
"encoding/json"
2024-05-08 01:56:48 +02:00
"flag"
2024-05-27 13:01:39 +02:00
"github.com/BurntSushi/toml"
2024-05-08 01:56:48 +02:00
"github.com/joho/godotenv"
"os"
2024-05-09 17:52:16 +02:00
"path"
2024-05-13 16:03:00 +02:00
"slices"
2024-05-09 21:00:24 +02:00
"strings"
2024-05-09 15:27:45 +02:00
"wazuh-notify/log"
2024-05-08 01:56:48 +02:00
"wazuh-notify/types"
)
2024-05-09 15:27:45 +02:00
var inputParams types.Params
2024-05-08 01:56:48 +02:00
var configParams types.Params
2024-05-09 15:27:45 +02:00
var wazuhData types.WazuhMessage
2024-05-08 01:56:48 +02:00
2024-05-09 15:27:45 +02:00
func InitNotify() types.Params {
2024-05-09 23:19:59 +02:00
BaseFilePath, _ := os.Executable()
BaseDirPath := path.Dir(BaseFilePath)
2024-05-09 23:20:52 +02:00
2024-05-09 23:19:59 +02:00
log.OpenLogFile(BaseDirPath)
2024-05-09 17:52:16 +02:00
2024-05-09 23:19:59 +02:00
err := godotenv.Load(path.Join(BaseDirPath, "../../etc/.env"))
2024-05-08 01:56:48 +02:00
if err != nil {
2024-05-09 15:27:45 +02:00
log.Log("env failed to load")
2024-05-09 23:19:59 +02:00
godotenv.Load(path.Join(BaseDirPath, ".env"))
2024-05-09 15:27:45 +02:00
} else {
log.Log("env loaded")
2024-05-08 01:56:48 +02:00
}
2024-05-27 13:01:39 +02:00
tomlFile, err := os.ReadFile(path.Join(BaseDirPath, "../../etc/wazuh-notify-config.toml"))
2024-05-09 17:52:16 +02:00
if err != nil {
2024-05-27 13:01:39 +02:00
log.Log("toml failed to load")
tomlFile, err = os.ReadFile(path.Join(BaseDirPath, "wazuh-notify-config.toml"))
2024-05-09 17:52:16 +02:00
}
2024-05-27 13:01:39 +02:00
err = toml.Unmarshal(tomlFile, &configParams)
2024-05-13 14:44:32 +02:00
if err != nil {
print(err)
}
2024-05-08 15:09:35 +02:00
2024-05-09 15:27:45 +02:00
log.Log("yaml loaded")
2024-05-09 23:27:21 +02:00
configParamString, _ := json.Marshal(configParams)
log.Log(string(configParamString))
2024-05-09 15:27:45 +02:00
flag.StringVar(&inputParams.Url, "url", "", "is the webhook URL of the Discord server. It is stored in .env.")
2024-05-27 13:01:39 +02:00
flag.StringVar(&inputParams.General.Click, "click", configParams.General.Click, "is a link (URL) that can be followed by tapping/clicking inside the message. Default is https://google.com.")
2024-05-08 01:56:48 +02:00
flag.IntVar(&inputParams.Priority, "priority", 0, "is the priority of the message, ranging from 1 (highest), to 5 (lowest). Default is 5.")
2024-05-27 13:01:39 +02:00
flag.StringVar(&inputParams.General.Sender, "sender", configParams.General.Sender, "is the sender of the message, either an app name or a person. The default is \"Security message\".")
2024-05-08 01:56:48 +02:00
flag.StringVar(&inputParams.Tags, "tags", "", "is an arbitrary strings of tags (keywords), seperated by a \",\" (comma). Default is \"informational,testing,hard-coded\".")
2024-05-27 13:01:39 +02:00
flag.StringVar(&inputParams.General.Targets, "targets", "", "is a list of targets to send notifications to. Default is \"discord\".")
2024-05-08 01:56:48 +02:00
flag.Parse()
2024-05-09 15:27:45 +02:00
2024-05-09 17:52:16 +02:00
log.Log("params loaded")
2024-05-09 23:27:21 +02:00
inputParamString, _ := json.Marshal(inputParams)
log.Log(string(inputParamString))
2024-05-27 13:01:39 +02:00
inputParams.General.Targets = configParams.General.Targets
inputParams.General.FullAlert = configParams.General.FullAlert
inputParams.General.ExcludedAgents = configParams.General.ExcludedAgents
inputParams.General.ExcludedRules = configParams.General.ExcludedRules
inputParams.PriorityMap = configParams.PriorityMap
2024-05-09 15:27:45 +02:00
2024-05-09 21:00:24 +02:00
wazuhInput()
2024-05-09 15:27:45 +02:00
return inputParams
}
func wazuhInput() {
reader := bufio.NewReader(os.Stdin)
json.NewDecoder(reader).Decode(&wazuhData)
2024-05-17 18:42:16 +02:00
inputParams.Tags += strings.Join(wazuhData.Parameters.Alert.Rule.Groups, ",")
inputParams.WazuhMessage = wazuhData
2024-05-27 13:01:39 +02:00
for i, _ := range configParams.PriorityMap {
if slices.Contains(configParams.PriorityMap[i].ThreatMap, wazuhData.Parameters.Alert.Rule.Level) {
inputParams.Color = inputParams.PriorityMap[i].Color
if inputParams.WazuhMessage.Parameters.Alert.Rule.Firedtimes >= inputParams.PriorityMap[i].MentionThreshold {
2024-05-13 16:03:00 +02:00
inputParams.Mention = "@here"
}
inputParams.Priority = 5 - i
}
}
2024-05-09 21:00:24 +02:00
2024-05-10 14:23:54 +02:00
Filter()
2024-05-09 23:27:21 +02:00
log.Log("Wazuh data loaded")
inputParamString, _ := json.Marshal(inputParams)
log.Log(string(inputParamString))
2024-05-08 01:56:48 +02:00
}