-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebhook.cpp
More file actions
82 lines (64 loc) · 2.43 KB
/
Copy pathWebhook.cpp
File metadata and controls
82 lines (64 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "Includes/Webhook.h"
#include <iostream>
#include <ctime>
#include <nlohmann/json.hpp>
#include <unistd.h>
#include "curl/curl.h"
Webhook::Webhook(const std::string& url) : m_url(url) {}
bool Webhook::sendAlert(const std::string& username, const std::string& ip)
{
CURL* curl = curl_easy_init();
if (!curl)
return false;
std::string timestamp;
{
char buffer[64];
std::time_t now = std::time(nullptr);
std::tm* utc = std::gmtime(&now);
std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", utc);
timestamp = buffer;
}
nlohmann::json payload;
nlohmann::json embed;
embed["title"] = "SSH Unauthorized Access";
embed["description"] = "Une tentative de connexion SSH inconues a été détectée.";
embed["color"] = 15158332;
embed["fields"] = nlohmann::json::array();
embed["fields"].push_back({{"name", "Utilisateur"}, {"value", username}, {"inline", true}});
embed["fields"].push_back({{"name", "Adresse IP"}, {"value", ip}, {"inline", true}});
char hostname[256] = {0};
if (gethostname(hostname, sizeof(hostname)) != 0) {
embed["fields"].push_back({{"name", "Host"}, {"value", std::string("unknown")}, {"inline", true}});
} else {
embed["fields"].push_back({{"name", "Host"}, {"value", std::string(hostname)}, {"inline", true}});
}
embed["footer"] = nlohmann::json::object();
embed["footer"]["text"] = "SSH Security Monitor";
embed["timestamp"] = timestamp;
payload["embeds"] = nlohmann::json::array();
payload["embeds"].push_back(embed);
std::string json = payload.dump();
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
CURLcode result = curl_easy_perform(curl);
long httpCode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
if (result != CURLE_OK)
{
std::cerr << "CURL Error: "
<< curl_easy_strerror(result)
<< std::endl;
}
else
{
std::cout << "Discord HTTP Code: "
<< httpCode
<< std::endl;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return result == CURLE_OK && httpCode == 204;
}