forked from OISF/suricata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert-prelude.c
963 lines (785 loc) · 28.3 KB
/
alert-prelude.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/* Copyright (C) 2007-2014 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Pierre Chifflier <[email protected]>
* \author Yoann Vandoorselaere <[email protected]>
*
* Logs alerts to the Prelude system, using IDMEF (RFC 4765) messages.
*
* Each message contains the alert description and reference (using
* the SID/GID), and a normalized description (assessment, impact,
* sources etc.)
*
* libprelude handles the connection with the manager (collecting component),
* spooling and sending the event asynchronously. It also offers transport
* security (using TLS and trusted certificates) and reliability (events
* are retransmitted if not sent successfully).
*
* This modules requires a Prelude profile to work (see man prelude-admin
* and the Prelude Handbook for help).
*/
#include "suricata-common.h"
#include "debug.h"
#include "detect.h"
#include "flow.h"
#include "conf.h"
#include "threads.h"
#include "threadvars.h"
#include "tm-threads.h"
#include "util-unittest.h"
#include "util-time.h"
#include "util-debug.h"
#include "util-error.h"
#include "util-print.h"
#include "output.h"
#include "util-privs.h"
#include "util-optimize.h"
#include "stream.h"
#ifndef PRELUDE
/* Handle the case where no PRELUDE support is compiled in. */
void AlertPreludeRegister(void)
{
SCLogDebug("Can't register Prelude output thread - Prelude support was disabled during build.");
}
#else /* implied we do have PRELUDE support */
#include <libprelude/prelude.h>
#define ANALYZER_CLASS "NIDS"
#define ANALYZER_MODEL "Suricata"
#define ANALYZER_MANUFACTURER "http://www.openinfosecfoundation.org/"
#define ANALYZER_SID_URL "http://www.snort.org/search/sid/"
#define SNORT_MAX_OWNED_SID 1000000
#define DEFAULT_ANALYZER_NAME "suricata"
#define DEFAULT_PRELUDE_PROFILE "suricata"
static unsigned int info_priority = 4;
static unsigned int low_priority = 3;
static unsigned int mid_priority = 2;
/**
* This holds global structures and variables. Since libprelude is thread-safe,
* there is no need to store a mutex.
*/
typedef struct AlertPreludeCtx_ {
/** The client (which has the send function) */
prelude_client_t *client;
int log_packet_content;
int log_packet_header;
} AlertPreludeCtx;
/**
* This holds per-thread specific structures and variables.
*/
typedef struct AlertPreludeThread_ {
/** Pointer to the global context */
AlertPreludeCtx *ctx;
idmef_analyzer_t *analyzer;
} AlertPreludeThread;
/**
* \brief Initialize analyzer description
*
* \return 0 if ok
*/
static int SetupAnalyzer(idmef_analyzer_t *analyzer)
{
int ret;
prelude_string_t *string;
SCEnter();
ret = idmef_analyzer_new_model(analyzer, &string);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating analyzer model: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = prelude_string_set_constant(string, ANALYZER_MODEL);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error setting analyzer model: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = idmef_analyzer_new_class(analyzer, &string);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating analyzer class: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = prelude_string_set_constant(string, ANALYZER_CLASS);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error setting analyzer class: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = idmef_analyzer_new_manufacturer(analyzer, &string);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating analyzer manufacturer: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = prelude_string_set_constant(string, ANALYZER_MANUFACTURER);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error setting analyzer manufacturer: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = idmef_analyzer_new_version(analyzer, &string);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating analyzer version: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = prelude_string_set_constant(string, VERSION);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error setting analyzer version: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
SCReturnInt(0);
}
/**
* \brief Create event impact description (see section
* 4.2.6.1 of RFC 4765).
* The impact contains the severity, completion (succeeded or failed)
* and basic classification of the attack type.
* Here, we don't set the completion since we don't know it (default
* is unknown).
*
* \return 0 if ok
*/
static int EventToImpact(const PacketAlert *pa, const Packet *p, idmef_alert_t *alert)
{
int ret;
prelude_string_t *str;
idmef_impact_t *impact;
idmef_assessment_t *assessment;
idmef_impact_severity_t severity;
SCEnter();
ret = idmef_alert_new_assessment(alert, &assessment);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating assessment: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
ret = idmef_assessment_new_impact(assessment, &impact);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating assessment impact: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(ret);
}
if ( (unsigned int)pa->s->prio < mid_priority )
severity = IDMEF_IMPACT_SEVERITY_HIGH;
else if ( (unsigned int)pa->s->prio < low_priority )
severity = IDMEF_IMPACT_SEVERITY_MEDIUM;
else if ( (unsigned int)pa->s->prio < info_priority )
severity = IDMEF_IMPACT_SEVERITY_LOW;
else
severity = IDMEF_IMPACT_SEVERITY_INFO;
idmef_impact_set_severity(impact, severity);
if (PACKET_TEST_ACTION(p, ACTION_DROP) ||
PACKET_TEST_ACTION(p, ACTION_REJECT) ||
PACKET_TEST_ACTION(p, ACTION_REJECT_DST) ||
PACKET_TEST_ACTION(p, ACTION_REJECT_BOTH) ) {
idmef_action_t *action;
ret = idmef_action_new(&action);
if (unlikely(ret < 0))
SCReturnInt(ret);
idmef_action_set_category(action, IDMEF_ACTION_CATEGORY_BLOCK_INSTALLED);
idmef_assessment_set_action(assessment, action, 0);
}
if (pa->s->class_msg) {
ret = idmef_impact_new_description(impact, &str);
if (unlikely(ret < 0))
SCReturnInt(ret);
prelude_string_set_ref(str, pa->s->class_msg);
}
SCReturnInt(0);
}
/**
* \brief Add Source and Target fields to the IDMEF alert.
* These objects contains IP addresses, source and destination
* ports (see sections 4.2.4.3 and 4.2.4.4 of RFC 4765).
*
* \return 0 if ok
*/
static int EventToSourceTarget(const Packet *p, idmef_alert_t *alert)
{
int ret;
idmef_node_t *node;
idmef_source_t *source;
idmef_target_t *target;
idmef_address_t *address;
idmef_service_t *service;
prelude_string_t *string;
static char saddr[128], daddr[128];
uint8_t ip_vers;
uint8_t ip_proto;
SCEnter();
if ( !p )
SCReturnInt(0);
if ( ! IPH_IS_VALID(p) )
SCReturnInt(0);
if (PKT_IS_IPV4(p)) {
ip_vers = 4;
ip_proto = IPV4_GET_RAW_IPPROTO(p->ip4h);
PrintInet(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), saddr, sizeof(saddr));
PrintInet(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), daddr, sizeof(daddr));
} else if (PKT_IS_IPV6(p)) {
ip_vers = 6;
ip_proto = IPV6_GET_L4PROTO(p);
PrintInet(AF_INET6, (const void *)GET_IPV6_SRC_ADDR(p), saddr, sizeof(saddr));
PrintInet(AF_INET6, (const void *)GET_IPV6_DST_ADDR(p), daddr, sizeof(daddr));
} else
SCReturnInt(0);
ret = idmef_alert_new_source(alert, &source, IDMEF_LIST_APPEND);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_source_new_service(source, &service);
if (unlikely(ret < 0))
SCReturnInt(ret);
if ( p->tcph || p->udph )
idmef_service_set_port(service, p->sp);
idmef_service_set_ip_version(service, ip_vers);
idmef_service_set_iana_protocol_number(service, ip_proto);
ret = idmef_source_new_node(source, &node);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_node_new_address(node, &address, IDMEF_LIST_APPEND);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_address_new_address(address, &string);
if (unlikely(ret < 0))
SCReturnInt(ret);
prelude_string_set_ref(string, saddr);
ret = idmef_alert_new_target(alert, &target, IDMEF_LIST_APPEND);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_target_new_service(target, &service);
if (unlikely(ret < 0))
SCReturnInt(ret);
if ( p->tcph || p->udph )
idmef_service_set_port(service, p->dp);
idmef_service_set_ip_version(service, ip_vers);
idmef_service_set_iana_protocol_number(service, ip_proto);
ret = idmef_target_new_node(target, &node);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_node_new_address(node, &address, IDMEF_LIST_APPEND);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_address_new_address(address, &string);
if (unlikely(ret < 0))
SCReturnInt(ret);
prelude_string_set_ref(string, daddr);
SCReturnInt(0);
}
/**
* \brief Add binary data, to be stored in the Additional Data
* field of the IDMEF alert (see section 4.2.4.6 of RFC 4765).
*
* \return 0 if ok
*/
static int AddByteData(idmef_alert_t *alert, const char *meaning, const unsigned char *data, size_t size)
{
int ret;
prelude_string_t *str;
idmef_additional_data_t *ad;
SCEnter();
if ( ! data || ! size )
SCReturnInt(0);
ret = idmef_alert_new_additional_data(alert, &ad, IDMEF_LIST_APPEND);
if (unlikely(ret < 0))
SCReturnInt(0);
ret = idmef_additional_data_set_byte_string_ref(ad, data, size);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error setting byte string data: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(-1);
}
ret = idmef_additional_data_new_meaning(ad, &str);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating additional-data meaning: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(-1);
}
ret = prelude_string_set_ref(str, meaning);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error setting byte string data meaning: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(-1);
}
SCReturnInt(0);
}
/**
* \brief Add integer data, to be stored in the Additional Data
* field of the IDMEF alert (see section 4.2.4.6 of RFC 4765).
*
* \return 0 if ok
*/
static int AddIntData(idmef_alert_t *alert, const char *meaning, uint32_t data)
{
int ret;
prelude_string_t *str;
idmef_additional_data_t *ad;
SCEnter();
ret = idmef_alert_new_additional_data(alert, &ad, IDMEF_LIST_APPEND);
if (unlikely(ret < 0))
SCReturnInt(ret);
idmef_additional_data_set_integer(ad, data);
ret = idmef_additional_data_new_meaning(ad, &str);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error creating additional-data meaning: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(-1);
}
ret = prelude_string_set_ref(str, meaning);
if (unlikely(ret < 0)) {
SCLogDebug("%s: error setting integer data meaning: %s.",
prelude_strsource(ret), prelude_strerror(ret));
SCReturnInt(-1);
}
SCReturnInt(0);
}
/**
* \brief Add IPv4 header data, to be stored in the Additional Data
* field of the IDMEF alert (see section 4.2.4.6 of RFC 4765).
*
* \return 0 if ok
*/
static int PacketToDataV4(const Packet *p, const PacketAlert *pa, idmef_alert_t *alert)
{
SCEnter();
AddIntData(alert, "ip_ver", IPV4_GET_RAW_VER(p->ip4h));
AddIntData(alert, "ip_hlen", IPV4_GET_RAW_HLEN(p->ip4h));
AddIntData(alert, "ip_tos", IPV4_GET_RAW_IPTOS(p->ip4h));
AddIntData(alert, "ip_len", ntohs(IPV4_GET_RAW_IPLEN(p->ip4h)));
AddIntData(alert, "ip_id", ntohs(IPV4_GET_RAW_IPID(p->ip4h)));
AddIntData(alert, "ip_off", ntohs(IPV4_GET_RAW_IPOFFSET(p->ip4h)));
AddIntData(alert, "ip_ttl", IPV4_GET_RAW_IPTTL(p->ip4h));
AddIntData(alert, "ip_proto", IPV4_GET_RAW_IPPROTO(p->ip4h));
AddIntData(alert, "ip_sum", ntohs(p->ip4h->ip_csum));
SCReturnInt(0);
}
/**
* \brief Add IPv6 header data, to be stored in the Additional Data
* field of the IDMEF alert (see section 4.2.4.6 of RFC 4765).
*
* \return 0 if ok
*/
static int PacketToDataV6(const Packet *p, const PacketAlert *pa, idmef_alert_t *alert)
{
SCEnter();
AddIntData(alert, "ip_ver", IPV6_GET_VER(p));
AddIntData(alert, "ip_class", IPV6_GET_CLASS(p));
AddIntData(alert, "ip_flow", IPV6_GET_FLOW(p));
AddIntData(alert, "ip_nh", IPV6_GET_NH(p));
AddIntData(alert, "ip_plen", IPV6_GET_PLEN(p));
AddIntData(alert, "ip_hlim", IPV6_GET_HLIM(p));
AddIntData(alert, "ip_proto", IPV6_GET_L4PROTO(p));
SCReturnInt(0);
}
/**
* \brief Convert IP packet to an IDMEF alert (RFC 4765).
* This function stores the alert SID (description and reference),
* the payload of the packet, and pre-processed data.
*
* \return 0 if ok
*/
static int PacketToData(const Packet *p, const PacketAlert *pa, idmef_alert_t *alert, AlertPreludeCtx *ctx)
{
SCEnter();
if (unlikely(p == NULL))
SCReturnInt(0);
AddIntData(alert, "snort_rule_sid", pa->s->id);
AddIntData(alert, "snort_rule_rev", pa->s->rev);
if (ctx->log_packet_header) {
if ( PKT_IS_IPV4(p) )
PacketToDataV4(p, pa, alert);
else if ( PKT_IS_IPV6(p) )
PacketToDataV6(p, pa, alert);
if ( PKT_IS_TCP(p) ) {
AddIntData(alert, "tcp_seq", TCP_GET_SEQ(p));
AddIntData(alert, "tcp_ack", TCP_GET_ACK(p));
AddIntData(alert, "tcp_off", TCP_GET_OFFSET(p));
AddIntData(alert, "tcp_res", TCP_GET_X2(p));
AddIntData(alert, "tcp_flags", TCP_GET_FLAGS(p));
AddIntData(alert, "tcp_win", TCP_GET_WINDOW(p));
AddIntData(alert, "tcp_sum", TCP_GET_SUM(p));
AddIntData(alert, "tcp_urp", TCP_GET_URG_POINTER(p));
if (p->tcpvars.ts_val != 0) {
AddIntData(alert, "tcp_tsval", TCP_GET_TSVAL(p));
}
if (p->tcpvars.ts_ecr != 0) {
AddIntData(alert, "tcp_tsecr", TCP_GET_TSECR(p));
}
if (p->tcph != NULL) {
AddIntData(alert, "tcp_wscale", TCP_GET_WSCALE(p));
}
if (TCP_HAS_SACKOK(p)) {
AddIntData(alert, "tcp_sackok", TCP_GET_SACKOK(p));
}
if (TCP_HAS_SACK(p)) {
AddIntData(alert, "tcp_sack_cnt", TCP_GET_SACK_CNT(p));
}
AddIntData(alert, "tcp_hlen", TCP_GET_HLEN(p));
}
else if ( PKT_IS_UDP(p) ) {
AddIntData(alert, "udp_len", UDP_GET_LEN(p));
AddIntData(alert, "udp_sum", UDP_GET_SUM(p));
}
else if ( PKT_IS_ICMPV4(p) ) {
AddIntData(alert, "icmp_type", ICMPV4_GET_TYPE(p));
AddIntData(alert, "icmp_code", ICMPV4_GET_CODE(p));
AddIntData(alert, "icmp_sum", ICMPV4_GET_RAW_CSUM(p));
}
else if ( PKT_IS_ICMPV6(p) ) {
AddIntData(alert, "icmp_type", ICMPV6_GET_TYPE(p));
AddIntData(alert, "icmp_code", ICMPV6_GET_CODE(p));
AddIntData(alert, "icmp_csum", ICMPV6_GET_RAW_CSUM(p));
}
}
if (ctx->log_packet_content)
AddByteData(alert, "payload", p->payload, p->payload_len);
SCReturnInt(0);
}
/**
* \brief Store reference on rule (SID and GID) in the IDMEF alert,
* and embed an URL pointing to the rule description.
*
* \return 0 if ok
*/
static int AddSnortReference(idmef_classification_t *class, int gen_id, int sig_id)
{
int ret;
prelude_string_t *str;
idmef_reference_t *ref;
SCEnter();
if ( sig_id >= SNORT_MAX_OWNED_SID )
SCReturnInt(0);
ret = idmef_classification_new_reference(class, &ref, IDMEF_LIST_APPEND);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_reference_new_name(ref, &str);
if (unlikely(ret < 0))
SCReturnInt(ret);
idmef_reference_set_origin(ref, IDMEF_REFERENCE_ORIGIN_VENDOR_SPECIFIC);
if ( gen_id == 0 )
ret = prelude_string_sprintf(str, "%u", sig_id);
else
ret = prelude_string_sprintf(str, "%u:%u", gen_id, sig_id);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_reference_new_meaning(ref, &str);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = prelude_string_sprintf(str, "Snort Signature ID");
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = idmef_reference_new_url(ref, &str);
if (unlikely(ret < 0))
SCReturnInt(ret);
if ( gen_id == 0 )
ret = prelude_string_sprintf(str, ANALYZER_SID_URL "%u", sig_id);
else
ret = prelude_string_sprintf(str, ANALYZER_SID_URL "%u-%u", gen_id, sig_id);
SCReturnInt(ret);
}
/**
* \brief Create event classification description (see section
* 4.2.4.2 of RFC 4765).
* The classification is the "name" of the alert, identification of the
* rule signature, and additional information on the rule.
*
* \return 0 if ok
*/
static int EventToReference(const PacketAlert *pa, const Packet *p, idmef_classification_t *class)
{
int ret;
prelude_string_t *str;
SCEnter();
ret = idmef_classification_new_ident(class, &str);
if (unlikely(ret < 0))
SCReturnInt(ret);
if ( pa->s->gid == 0 )
ret = prelude_string_sprintf(str, "%u", pa->s->id);
else
ret = prelude_string_sprintf(str, "%u:%u", pa->s->gid, pa->s->id);
if (unlikely(ret < 0))
SCReturnInt(ret);
ret = AddSnortReference(class, pa->s->gid, pa->s->id);
if (unlikely(ret < 0))
SCReturnInt(ret);
SCReturnInt(0);
}
static int PreludePrintStreamSegmentCallback(const Packet *p, void *data, uint8_t *buf, uint32_t buflen)
{
int ret;
ret = AddByteData((idmef_alert_t *)data, "stream-segment", buf, buflen);
if (ret == 0)
return 1;
else
return -1;
}
/**
* \brief Initialize thread-specific data. Each thread structure contains
* a pointer to the \a AlertPreludeCtx context.
*
* \return TM_ECODE_OK if ok, else TM_ECODE_FAILED
*/
static TmEcode AlertPreludeThreadInit(ThreadVars *t, void *initdata, void **data)
{
AlertPreludeThread *aun;
SCEnter();
if (unlikely(initdata == NULL)) {
SCLogError(SC_ERR_INITIALIZATION,
"Error getting context for Prelude. \"initdata\" argument NULL");
SCReturnInt(TM_ECODE_FAILED);
}
aun = SCMalloc(sizeof(AlertPreludeThread));
if (unlikely(aun == NULL))
SCReturnInt(TM_ECODE_FAILED);
memset(aun, 0, sizeof(AlertPreludeThread));
/* Use the Ouput Context */
aun->ctx = ((OutputCtx *)initdata)->data;
/* Create a per-thread idmef analyzer */
if (unlikely(idmef_analyzer_new(&aun->analyzer) < 0)) {
SCLogError(SC_ERR_INITIALIZATION,
"Error creating idmef analyzer for Prelude.");
SCFree(aun);
SCReturnInt(TM_ECODE_FAILED);
}
/* Setup the per-thread idmef analyzer */
if (unlikely(SetupAnalyzer(aun->analyzer) < 0)) {
SCLogError(SC_ERR_INITIALIZATION,
"Error configuring idmef analyzer for Prelude.");
idmef_analyzer_destroy(aun->analyzer);
SCFree(aun);
SCReturnInt(TM_ECODE_FAILED);
}
*data = (void *)aun;
SCReturnInt(TM_ECODE_OK);
}
/**
* \brief Free thread-specific data.
*
* \return TM_ECODE_OK if ok, else TM_ECODE_FAILED
*/
static TmEcode AlertPreludeThreadDeinit(ThreadVars *t, void *data)
{
AlertPreludeThread *aun = (AlertPreludeThread *)data;
SCEnter();
if (unlikely(aun == NULL)) {
SCLogDebug("AlertPreludeThreadDeinit done (error)");
SCReturnInt(TM_ECODE_FAILED);
}
/* clear memory */
idmef_analyzer_destroy(aun->analyzer);
memset(aun, 0, sizeof(AlertPreludeThread));
SCFree(aun);
SCReturnInt(TM_ECODE_OK);
}
static void AlertPreludeDeinitCtx(OutputCtx *output_ctx)
{
AlertPreludeCtx *ctx = (AlertPreludeCtx *)output_ctx->data;
prelude_client_destroy(ctx->client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
SCFree(output_ctx);
}
/** \brief Initialize the Prelude logging module: initialize
* library, create the client and try to establish the connection
* to the Prelude Manager.
* Client flags are set to force asynchronous (non-blocking) mode for
* both alerts and heartbeats.
* This function requires an existing Prelude profile to work.
*
* \return A newly allocated AlertPreludeCtx structure, or NULL
*/
static OutputCtx *AlertPreludeInitCtx(ConfNode *conf)
{
int ret;
prelude_client_t *client;
AlertPreludeCtx *ctx;
const char *prelude_profile_name;
const char *log_packet_content;
const char *log_packet_header;
OutputCtx *output_ctx;
SCEnter();
ret = prelude_init(0, NULL);
if (unlikely(ret < 0)) {
prelude_perror(ret, "unable to initialize the prelude library");
SCReturnPtr(NULL, "AlertPreludeCtx");
}
prelude_profile_name = ConfNodeLookupChildValue(conf, "profile");
if (prelude_profile_name == NULL)
prelude_profile_name = DEFAULT_PRELUDE_PROFILE;
log_packet_content = ConfNodeLookupChildValue(conf, "log-packet-content");
log_packet_header = ConfNodeLookupChildValue(conf, "log-packet-header");
ret = prelude_client_new(&client, prelude_profile_name);
if ( unlikely(ret < 0 || client == NULL )) {
prelude_perror(ret, "Unable to create a prelude client object");
prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
SCReturnPtr(NULL, "AlertPreludeCtx");
}
ret = prelude_client_set_flags(client, prelude_client_get_flags(client) | PRELUDE_CLIENT_FLAGS_ASYNC_TIMER|PRELUDE_CLIENT_FLAGS_ASYNC_SEND);
if (unlikely(ret < 0)) {
SCLogDebug("Unable to set asynchronous send and timer.");
prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
SCReturnPtr(NULL, "AlertPreludeCtx");
}
ret = SetupAnalyzer(prelude_client_get_analyzer(client));
if (ret < 0) {
SCLogDebug("Unable to setup prelude client analyzer.");
prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
SCReturnPtr(NULL, "AlertPreludeCtx");
}
ret = prelude_client_start(client);
if (unlikely(ret < 0)) {
prelude_perror(ret, "Unable to start prelude client");
prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
SCReturnPtr(NULL, "AlertPreludeCtx");
}
ctx = SCMalloc(sizeof(AlertPreludeCtx));
if (unlikely(ctx == NULL)) {
prelude_perror(ret, "Unable to allocate memory");
prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
SCReturnPtr(NULL, "AlertPreludeCtx");
}
ctx->client = client;
ctx->log_packet_content = 0;
ctx->log_packet_header = 1;
if (log_packet_content && ConfValIsTrue(log_packet_content))
ctx->log_packet_content = 1;
if (log_packet_header && ConfValIsFalse(log_packet_header))
ctx->log_packet_header = 0;
output_ctx = SCMalloc(sizeof(OutputCtx));
if (unlikely(output_ctx == NULL)) {
SCFree(ctx);
prelude_perror(ret, "Unable to allocate memory");
prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
SCReturnPtr(NULL, "AlertPreludeCtx");
}
output_ctx->data = ctx;
output_ctx->DeInit = AlertPreludeDeinitCtx;
SCReturnPtr((void*)output_ctx, "OutputCtx");
}
static int AlertPreludeCondition(ThreadVars *tv, const Packet *p)
{
if (p->alerts.cnt == 0)
return FALSE;
if (!IPH_IS_VALID(p))
return FALSE;
return TRUE;
}
/**
* \brief Handle Suricata alert: convert it to and IDMEF alert (see RFC 4765)
* and send it asynchronously (so, this function does not block and returns
* immediately).
* If the destination Prelude Manager is not available, the alert is spooled
* (and the function also returns immediately).
* An IDMEF object is created, and all available information is added: IP packet
* header and data, rule signature ID, additional data like URL pointing to
* rule description, CVE, etc.
* The IDMEF alert has a reference to all created objects, so freeing it will
* automatically free all allocated memory.
*
* \note This function is thread safe.
*
* \return TM_ECODE_OK if ok, else TM_ECODE_FAILED
*/
static int AlertPreludeLogger(ThreadVars *tv, void *thread_data, const Packet *p)
{
AlertPreludeThread *apn = (AlertPreludeThread *)thread_data;
int ret;
idmef_time_t *time;
idmef_alert_t *alert;
prelude_string_t *str;
idmef_message_t *idmef = NULL;
idmef_classification_t *class;
const PacketAlert *pa;
SCEnter();
if (unlikely(apn == NULL || apn->ctx == NULL)) {
SCReturnInt(TM_ECODE_FAILED);
}
if (p->alerts.cnt == 0)
SCReturnInt(TM_ECODE_OK);
if ( !IPH_IS_VALID(p) )
SCReturnInt(TM_ECODE_OK);
/* XXX which one to add to this alert? Lets see how Snort solves this.
* For now just take last alert. */
pa = &p->alerts.alerts[p->alerts.cnt-1];
if (unlikely(pa->s == NULL))
goto err;
ret = idmef_message_new(&idmef);
if (unlikely(ret < 0))
SCReturnInt(TM_ECODE_FAILED);
ret = idmef_message_new_alert(idmef, &alert);
if (unlikely(ret < 0))
goto err;
ret = idmef_alert_new_classification(alert, &class);
if (unlikely(ret < 0))
goto err;
if (pa->s->msg) {
ret = idmef_classification_new_text(class, &str);
if (unlikely(ret < 0))
goto err;
prelude_string_set_ref(str, pa->s->msg);
}
ret = EventToImpact(pa, p, alert);
if (unlikely(ret < 0))
goto err;
ret = EventToReference(pa, p, class);
if (unlikely(ret < 0))
goto err;
ret = EventToSourceTarget(p, alert);
if (unlikely(ret < 0))
goto err;
ret = PacketToData(p, pa, alert, apn->ctx);
if (unlikely(ret < 0))
goto err;
if (PKT_IS_TCP(p) && (pa->flags & PACKET_ALERT_FLAG_STATE_MATCH)) {
uint8_t flag;
if (p->flowflags & FLOW_PKT_TOSERVER) {
flag = FLOW_PKT_TOCLIENT;
} else {
flag = FLOW_PKT_TOSERVER;
}
ret = StreamSegmentForEach(p, flag,
PreludePrintStreamSegmentCallback,
(void *)alert);
}
if (unlikely(ret < 0))
goto err;
ret = idmef_alert_new_detect_time(alert, &time);
if (unlikely(ret < 0))
goto err;
idmef_time_set_from_timeval(time, &p->ts);
ret = idmef_time_new_from_gettimeofday(&time);
if (unlikely(ret < 0))
goto err;
idmef_alert_set_create_time(alert, time);
idmef_alert_set_analyzer(alert, idmef_analyzer_ref(apn->analyzer), IDMEF_LIST_PREPEND);
/* finally, send event */
prelude_client_send_idmef(apn->ctx->client, idmef);
idmef_message_destroy(idmef);
SCReturnInt(TM_ECODE_OK);
err:
if (idmef != NULL)
idmef_message_destroy(idmef);
SCReturnInt(TM_ECODE_FAILED);
}
void AlertPreludeRegister (void)
{
OutputRegisterPacketModule(LOGGER_PRELUDE, "AlertPrelude", "alert-prelude",
AlertPreludeInitCtx, AlertPreludeLogger, AlertPreludeCondition,
AlertPreludeThreadInit, AlertPreludeThreadDeinit, NULL);
}
#endif /* PRELUDE */