-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuricata-notify.c
426 lines (376 loc) · 13.3 KB
/
suricata-notify.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#define DEFAULT_MAX_LINE_LENGTH 4096
#define DEFAULT_TIMEZONE_OFFSET_SECONDS 0
#define DEFAULT_ALERT_WINDOW_SECONDS 60
// Global variables for configuration
int verbose = 0;
size_t max_line_length = DEFAULT_MAX_LINE_LENGTH;
int timezone_offset_seconds = DEFAULT_TIMEZONE_OFFSET_SECONDS;
int alert_window_seconds = DEFAULT_ALERT_WINDOW_SECONDS;
// Function prototypes
void send_notification(const char *alert_message);
time_t convert_iso8601_to_unix(const char *iso8601_timestamp);
void process_alerts(const char *log_file);
void print_help(void);
// Add the help message function
void print_help(void)
{
printf("Usage: suricata-notify [options]\n");
printf("Options:\n");
printf(" -h, --help Show this help message and exit\n");
printf(" -v, --verbose Enable verbose output\n");
printf(" -t, --test Run in test mode (send a test notification)\n");
printf(" -e, --eve-json <file> Specify the Suricata EVE JSON log file (default: /var/log/suricata/eve.json)\n");
printf(" -l, --line-length <length> Set the maximum line length for reading the log file (default: %d)\n", DEFAULT_MAX_LINE_LENGTH);
printf(" -z, --timezone-offset <s> Set the timezone offset in seconds (default: %d)\n", DEFAULT_TIMEZONE_OFFSET_SECONDS);
printf(" -w, --alert-window <s> Set the alert window in seconds (default: %d)\n", DEFAULT_ALERT_WINDOW_SECONDS);
}
// Function to send a desktop notification with signature and category
void send_notification(const char *alert_message)
{
if (verbose)
{
printf("[DEBUG] Sending notification: %s\n", alert_message);
}
pid_t pid = fork();
if (pid < 0)
{
perror("fork failed");
return;
}
if (pid == 0)
{ // Child process
execlp("notify-send", "notify-send", "Suricata Alert", alert_message, (char *)NULL);
perror("execlp failed");
exit(EXIT_FAILURE);
}
else
{ // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
{
fprintf(stderr, "notify-send failed with exit code %d\n", WEXITSTATUS(status));
}
}
}
// Function to convert ISO 8601 timestamp to time_t (Unix timestamp)
time_t convert_iso8601_to_unix(const char *iso8601_timestamp)
{
struct tm tm_time;
memset(&tm_time, 0, sizeof(struct tm));
if (verbose)
{
printf("[DEBUG] Converting timestamp: %s\n", iso8601_timestamp);
}
// Parse the timestamp up to seconds
if (strptime(iso8601_timestamp, "%Y-%m-%dT%H:%M:%S", &tm_time) == NULL)
{
fprintf(stderr, "Failed to parse timestamp: %s\n", iso8601_timestamp);
return (time_t)-1;
}
// Convert to time_t (Unix timestamp)
time_t converted_time = mktime(&tm_time) + timezone_offset_seconds;
if (verbose)
{
printf("[DEBUG] Converted time: %ld\n", (long)converted_time);
}
return converted_time;
}
// Function to get ISO 8601 timestamp
void get_iso8601_timestamp(char *buffer, size_t buffer_size)
{
struct timeval tv;
gettimeofday(&tv, NULL); // Get the current time including microseconds
struct tm tm_info;
gmtime_r(&tv.tv_sec, &tm_info); // Convert to UTC
// Format date and time including seconds
strftime(buffer, buffer_size, "%Y-%m-%dT%H:%M:%S", &tm_info);
// Append microseconds and timezone
snprintf(buffer + strlen(buffer), buffer_size - strlen(buffer), ".%06ld+0000", tv.tv_usec);
}
// Sanitize file inputs
int sanitize_file_input(const char *log_file)
{
if (log_file == NULL)
{
fprintf(stderr, "Error: log_file is NULL.\n");
return -1;
}
// Check for potential path traversal (../)
if (strstr(log_file, "../") != NULL)
{
fprintf(stderr, "Error: log_file path contains path traversal components.\n");
return -1;
}
// Check file permissions and type
struct stat file_stat;
if (stat(log_file, &file_stat) != 0)
{
perror("Error: stat failed");
return -1;
}
// Ensure it's a regular file
if (!S_ISREG(file_stat.st_mode))
{
fprintf(stderr, "Error: log_file is not a regular file.\n");
return -1;
}
// Ensure the file is readable
if ((file_stat.st_mode & S_IRUSR) == 0)
{
fprintf(stderr, "Error: log_file is not readable by the user.\n");
return -1;
}
return 0; // Return 0 if all checks pass
}
// Function to process Suricata alerts and trigger notifications
void process_alerts(const char *log_file)
{
// Sanitize the file path before opening
if (sanitize_file_input(log_file) != 0)
{
exit(EXIT_FAILURE);
}
FILE *file = fopen(log_file, "r");
if (file == NULL)
{
perror("Error opening file");
return;
}
if (verbose)
{
printf("[DEBUG] Processing alerts from log file: %s\n", log_file);
}
char *line = (char *)malloc(max_line_length);
if (line == NULL)
{
perror("Error allocating memory for line buffer");
fclose(file);
return;
}
time_t current_time = time(NULL); // Get the current time
if (verbose)
{
printf("[DEBUG] Current Time: %ld\n", (long)current_time);
char iso8601_timestamp[40];
get_iso8601_timestamp(iso8601_timestamp, sizeof(iso8601_timestamp));
printf("[DEBUG] Current Time: %s\n", iso8601_timestamp);
}
while (fgets(line, max_line_length, file) != NULL)
{
if (verbose)
{
printf("[DEBUG] Reading line: %s\n", line);
}
// Load the JSON object from the line
json_error_t error;
json_t *root = json_loads(line, 0, &error);
if (root == NULL)
{
// Handle invalid JSON line gracefully
fprintf(stderr, "Error parsing JSON: %s\n", error.text);
continue;
}
if (verbose)
{
printf("[DEBUG] Successfully parsed JSON object.\n");
}
// Check if the JSON object has the "event_type" field and it is "alert"
json_t *event_type = json_object_get(root, "event_type");
if (event_type && json_is_string(event_type))
{
if (verbose)
{
printf("[DEBUG] Event type found: %s\n", json_string_value(event_type));
}
if (strcmp(json_string_value(event_type), "alert") == 0)
{
// Extract the timestamp
json_t *alert_timestamp_json = json_object_get(root, "timestamp");
if (alert_timestamp_json && json_is_string(alert_timestamp_json))
{
if (verbose)
{
printf("[DEBUG] Alert timestamp found: %s\n", json_string_value(alert_timestamp_json));
}
time_t alert_timestamp = convert_iso8601_to_unix(json_string_value(alert_timestamp_json));
// Check if the alert occurred within the last ALERT_WINDOW_SECONDS
if (difftime(current_time, alert_timestamp) <= alert_window_seconds)
{
if (verbose)
{
double diff = difftime(current_time, alert_timestamp);
printf("[DEBUG] Alert occurred within the last %d seconds with a diff of %.0f seconds.\n", alert_window_seconds, diff);
}
json_t *alert = json_object_get(root, "alert");
if (alert && json_is_object(alert))
{
// Extract the signature and category
json_t *signature_json = json_object_get(alert, "signature");
json_t *category_json = json_object_get(alert, "category");
if (signature_json && json_is_string(signature_json) && json_is_string(category_json))
{
if (verbose)
{
printf("[DEBUG] Alert signature: %s\n", json_string_value(signature_json));
printf("[DEBUG] Alert category: %s\n", json_string_value(category_json));
}
// Create the alert message
char alert_message[max_line_length];
snprintf(alert_message, sizeof(alert_message), "Category: %s\nSignature: %s", json_string_value(category_json), json_string_value(signature_json));
if (verbose)
{
printf("[DEBUG] Sending notification: %s\n", alert_message);
}
send_notification(alert_message);
}
else
{
if (verbose)
{
printf("[DEBUG] Missing or invalid 'signature' or 'category' field in alert.\n");
}
}
}
else
{
if (verbose)
{
printf("[DEBUG] 'alert' field is missing or is not an object.\n");
}
}
}
else
{
if (verbose)
{
printf("[DEBUG] Alert is older than %d seconds, skipping notification.\n", alert_window_seconds);
}
}
}
else
{
if (verbose)
{
printf("[DEBUG] Missing or invalid 'timestamp' field in alert.\n");
}
}
}
}
else
{
if (verbose)
{
printf("[DEBUG] 'event_type' field is missing or is not a string.\n");
}
}
json_decref(root);
}
if (verbose)
{
printf("[DEBUG] Finished processing alerts.\n");
}
free(line);
fclose(file);
}
int main(int argc, char *argv[])
{
int is_test = 0;
const char *suricata_log = "/var/log/suricata/eve.json"; // Default log file path
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
{
print_help();
return EXIT_SUCCESS;
}
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0)
{
verbose = 1;
}
else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--test") == 0)
{
is_test = 1;
}
else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--eve-json") == 0)
{
if (i + 1 < argc)
{
suricata_log = argv[i + 1]; // Get the log file name
i++; // Skip the log file argument
}
else
{
fprintf(stderr, "Error: Missing argument for %s\n", argv[i]);
return EXIT_FAILURE;
}
}
else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--line-length") == 0)
{
if (i + 1 < argc)
{
max_line_length = strtoul(argv[i + 1], NULL, 10);
i++;
}
else
{
fprintf(stderr, "Error: Missing argument for %s\n", argv[i]);
return EXIT_FAILURE;
}
}
else if (strcmp(argv[i], "-z") == 0 || strcmp(argv[i], "--timezone-offset") == 0)
{
if (i + 1 < argc)
{
timezone_offset_seconds = atoi(argv[i + 1]);
i++;
}
else
{
fprintf(stderr, "Error: Missing argument for %s\n", argv[i]);
return EXIT_FAILURE;
}
}
else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--alert-window") == 0)
{
if (i + 1 < argc)
{
alert_window_seconds = atoi(argv[i + 1]);
i++;
}
else
{
fprintf(stderr, "Error: Missing argument for %s\n", argv[i]);
return EXIT_FAILURE;
}
}
else
{
fprintf(stderr, "Unknown argument: %s\n", argv[i]);
return EXIT_FAILURE;
}
}
if (is_test)
{
// Test mode doesn't need the log file, so ignore the -e argument if present
send_notification("Test notification");
}
else
{
// Process the alerts from the specified log file
process_alerts(suricata_log);
}
return 0;
}