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"
"github.com/joho/godotenv"
"gopkg.in/yaml.v2"
"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-09 23:19:59 +02:00
yamlFile , err := os . ReadFile ( path . Join ( BaseDirPath , "../../etc/wazuh-notify-config.yaml" ) )
2024-05-09 17:52:16 +02:00
if err != nil {
log . Log ( "yaml failed to load" )
2024-05-09 23:19:59 +02:00
yamlFile , err = os . ReadFile ( path . Join ( BaseDirPath , "wazuh-notify-config.yaml" ) )
2024-05-09 17:52:16 +02:00
}
2024-05-13 14:44:32 +02:00
err = yaml . Unmarshal ( yamlFile , & configParams )
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-08 15:09:35 +02:00
flag . StringVar ( & inputParams . Click , "click" , configParams . 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-08 15:09:35 +02:00
flag . StringVar ( & inputParams . Sender , "sender" , configParams . 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\"." )
flag . StringVar ( & inputParams . Targets , "targets" , "" , "is a list of targets to send notifications to. Default is \"discord\"." )
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-09 12:24:44 +02:00
inputParams . Targets = configParams . Targets
2024-05-10 14:23:54 +02:00
inputParams . FullMessage = configParams . FullMessage
inputParams . ExcludedAgents = configParams . ExcludedAgents
inputParams . ExcludedRules = configParams . ExcludedRules
2024-05-13 14:44:32 +02:00
inputParams . PriorityMaps = configParams . PriorityMaps
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-13 16:03:00 +02:00
for i , _ := range configParams . PriorityMaps {
if slices . Contains ( configParams . PriorityMaps [ i ] . ThreatMap , wazuhData . Parameters . Alert . Rule . Level ) {
inputParams . Color = inputParams . PriorityMaps [ i ] . Color
if inputParams . WazuhMessage . Parameters . Alert . Rule . Firedtimes >= inputParams . PriorityMaps [ i ] . MentionThreshold {
inputParams . Mention = "@here"
}
inputParams . Priority = 5 - i
}
}
2024-05-09 21:00:24 +02:00
inputParams . Tags += strings . Join ( wazuhData . Parameters . Alert . Rule . Groups , "," )
2024-05-09 15:27:45 +02:00
inputParams . WazuhMessage = wazuhData
2024-05-09 23:27:21 +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
}