65 lines
2.0 KiB
Go
Raw Normal View History

2024-05-27 11:36:33 +02:00
package notification
2024-05-27 11:44:24 +02:00
import (
"bytes"
"encoding/json"
2024-05-27 13:01:39 +02:00
"fmt"
2024-05-27 11:44:24 +02:00
"log"
"net/http"
"os"
"slices"
"strconv"
"strings"
"time"
"wazuh-notify/types"
)
func SendSlack(params types.Params) {
var embedDescription string
2024-05-27 13:01:39 +02:00
if slices.Contains(strings.Split(params.General.FullAlert, ","), "slack") {
2024-05-27 11:44:24 +02:00
fullAlert, _ := json.MarshalIndent(params.WazuhMessage, "", " ")
fullAlertString := strings.ReplaceAll(string(fullAlert), `"`, "")
fullAlertString = strings.ReplaceAll(fullAlertString, "{", "")
fullAlertString = strings.ReplaceAll(fullAlertString, "}", "")
fullAlertString = strings.ReplaceAll(fullAlertString, "[", "")
fullAlertString = strings.ReplaceAll(fullAlertString, "]", "")
fullAlertString = strings.ReplaceAll(fullAlertString, " ,", "")
embedDescription = "\n\n ```" +
fullAlertString +
"```\n\n" +
"Priority: " + strconv.Itoa(params.Priority) + "\n" +
"Tags: " + params.Tags + "\n\n" +
2024-05-27 13:01:39 +02:00
params.General.Click
2024-05-27 11:44:24 +02:00
} else {
embedDescription = "\n\n" +
"**Timestamp: **" + time.Now().Format(time.DateTime) + "\n" +
"**Agent:** " + params.WazuhMessage.Parameters.Alert.Agent.Name + "\n" +
"**Event id:** " + params.WazuhMessage.Parameters.Alert.Rule.ID + "\n" +
"**Rule:** " + params.WazuhMessage.Parameters.Alert.Rule.Description + "\n" +
"**Description: **" + params.WazuhMessage.Parameters.Alert.FullLog + "\n" +
"**Threat level:** " + strconv.Itoa(params.WazuhMessage.Parameters.Alert.Rule.Level) + "\n" +
"**Times fired:** " + strconv.Itoa(params.WazuhMessage.Parameters.Alert.Rule.Firedtimes) +
"\n\n" +
"Priority: " + strconv.Itoa(params.Priority) + "\n" +
"Tags: " + params.Tags + "\n\n" +
2024-05-27 13:01:39 +02:00
params.General.Click
2024-05-27 11:44:24 +02:00
}
2024-05-27 13:01:39 +02:00
message := fmt.Sprintf("{\"text\": %s}", embedDescription)
2024-05-27 11:44:24 +02:00
payload := new(bytes.Buffer)
err := json.NewEncoder(payload).Encode(message)
if err != nil {
return
}
_, err = http.Post(os.Getenv("SLACK_URL"), "application/json", payload)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
}