/
wm_vuln_detector.c
executable file
·8643 lines (7465 loc) · 319 KB
/
wm_vuln_detector.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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Wazuh Module to analyze system vulnerabilities
* Copyright (C) 2015, Wazuh Inc.
* January 4, 2018.
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation.
*/
#include "cJSON.h"
#include "defs.h"
#include "os_err.h"
#include "wazuhdb_op.h"
#ifndef WIN32
#include "../wmodules.h"
#include "wm_vuln_detector_db.h"
#include "wm_vuln_detector_evr.h"
#include "addagent/manage_agents.h"
#include "wazuh_db/helpers/wdb_global_helpers.h"
#include "wazuh_db/helpers/wdb_agents_helpers.h"
#include <netinet/tcp.h>
#include <openssl/ssl.h>
#include <os_net/os_net.h>
#ifdef WAZUH_UNIT_TESTING
// Remove STATIC qualifier from tests
#define STATIC
#else
#define STATIC static
#endif
#if defined(__MACH__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#define SOL_TCP 6
#endif
STATIC void *wm_vuldet_main(wm_vuldet_t * vuldet);
STATIC void wm_vuldet_destroy(wm_vuldet_t * vuldet);
STATIC int wm_vuldet_run_update(update_node **updates);
STATIC char *wm_vuldet_oval_xml_preparser(char *path, vu_feed dist);
STATIC int wm_vuldet_index_feed(update_node *update);
STATIC int wm_vuldet_fetch_feed(update_node *update, int8_t *need_update);
/**
* @brief Traverse the list of CVEs within the RHSA, and insert
* each one of them into the global linked list of vulnerabilities.
* @param vul_it Vulnerability to analise.
*
*/
STATIC void wm_vuldet_oval_traverse_rhsa(vulnerability *vul_it);
/**
* @brief Append a new CVE to the RHSA list of a given vulnerability.
* @param vul_it Vulnerability to analise.
* @param cve New CVE.
*
*/
STATIC void wm_vuldet_oval_append_rhsa(vulnerability *vul_it, char *cve);
/**
* @brief Copy all the items within the RHSA into a new vulnerability's RHSA list.
* @param old Old Vulnerability being analised.
* @param new New Vulnerability being analised.
*
*/
STATIC void wm_vuldet_oval_copy_rhsa(vulnerability *old, vulnerability *new);
STATIC int wm_vuldet_oval_xml_parser(OS_XML *xml, XML_NODE node, wm_vuldet_db *parsed_oval, update_node *update, vu_logic condition);
STATIC int wm_vuldet_json_parser(char *json_path, wm_vuldet_db *uparsed_vulnerabilities, update_node *update);
/**
* @brief Add a new vulnerability to the linked list.
* @param ctrl_block Parsed oval data strucure.
*
*/
STATIC void wm_vuldet_add_oval_vulnerability(wm_vuldet_db *ctrl_block);
STATIC void wm_vuldet_add_vulnerability_info(wm_vuldet_db *ctrl_block);
/**
* @brief Clean the CVEs linked list data structure.
* @param ctrl_block Parsed oval data strucure.
*
*/
STATIC void wm_vuldet_clean_vulnerability_info(wm_vuldet_db *ctrl_block);
STATIC references *wm_vuldet_extract_advisories(cJSON *advisories);
STATIC int wm_vuldet_check_db();
/**
* @brief Insert the CVEs information, fetched from the feeds, into VULNERABILITIES_INFO table.
* @param parsed_oval The feed's information.
* @param db SQLite3 Database pointer.
* @param stmt SQL query statement pointer.
* @return 0 if success.
*/
STATIC int wm_vuldet_insert_cve_info(wm_vuldet_db *parsed_oval, sqlite3 *db, sqlite3_stmt *stmt);
STATIC int wm_vuldet_insert(wm_vuldet_db *parsed_oval, update_node *update);
STATIC int wm_vuldet_remove_target_table(sqlite3 *db, char *TABLE, const char *target);
/**
* @brief Set and validate the required URL for the feed.
* Currently Debian, Ubuntu and RedHat are supported.
* @param update Update structure of the feed being analyzed.
* @return False if an unexpected feed is analyzed; True othewise.
*/
STATIC bool wm_vuldet_set_feed_update_url(update_node *update);
/**
* @brief Get the Debian status feed to check the status of the vulnerabilities.
* @param update Update structure of the feed being analyzed.
* @return Parsed JSON on success, NULL otherwise.
*/
STATIC cJSON *wm_vuldet_get_debian_status_feed(update_node *update);
/**
* @brief Insert the Debian vulnerable packages into the DB.
* @param db Pointer to the CVE DB.
* @param target Debian OS target.
* @param update Update structure of the feed being analyzed.
* @return 0 on success, -1 otherwise.
*/
STATIC int wm_vuldet_index_debian(sqlite3 *db, const char *target, update_node *update);
/**
* @brief For an specific agent, copy all installed packages' information from SYS_PROGRAM to the AGENTS table.
* @param agent Pointer to an agent node.
* @param db The agent sqlite Data Base.
* @param scan_ctx Context of the scan in progress
* @param request The request type that was used. Full or partial.
* @return 0 on success, -1 otherwise.
*/
STATIC int wm_vuldet_collect_agent_software(scan_agent *agent, sqlite3 *db, scan_ctx_t* scan_ctx);
/**
* @brief Search for known vulnerabilities (NVD and OVAL feeds), and report all found CVEs.
* @param agent Pointer to an agent node.
* @param flags Flag list for Windows agents
* @param scan_ctx Context of the scan in progress
* @return 0 on success, -1 otherwise.
*/
STATIC int wm_vuldet_find_agent_vulnerabilities(sqlite3 *db, scan_agent *agent, wm_vuldet_flags *flags, scan_ctx_t* scan_ctx);
/**
* @brief Eliminate and report every obsolete vulnerability.
* @param scan_ctx Context of the scan in progress
* @return 0 on success, -1 otherwise.
*/
STATIC int wm_vuldet_find_obsolete_vulnerabilities(scan_ctx_t* scan_ctx);
/**
* @brief Traverse the agents linked list, gather the installed packages and search
* for known vulnerabilities.
* @param vuldet The vulnerability detector main data structure.
* @return 0 on success, -1 otherwise.
*/
STATIC int wm_vuldet_check_agent_vulnerabilities(wm_vuldet_t *vuldet);
/**
* @brief Discard any installed Linux kernel package which is not running.
* @param agent Agent being analyzed.
* @param name Name of the package.
* @param version Version of the package.
* @param arch Architecture of the package.
* @return 0 on success, -1 otherwise.
*/
STATIC int wm_vuldet_discard_kernel_package(scan_agent *agent, const char *name, const char *version, const char *arch);
/**
* @brief Discard false positives in CVEs with multiple entries when they differ in their vulnerability diagnosis.
* @param db The agent's sqlite database.
* @param agents_it The software struct from the agent being analyzed.
* @param cve_id The CVE being analyzed.
* @param pkg_version The version from the package being analyzed.
* @param pkg_name The name from the package being analyzed.
* @param vertype The version comparisor type.
* @return The return code from the version comparisor (VU_NOT_VULNERABLE, VU_NOT_FIXED, VU_ERROR_CMP, ...).
*/
STATIC int wm_vuldet_oval_discard_mismatching_cve_entries(sqlite3 *db, scan_agent *agents_it, char *cve_id, char *pkg_version, char *pkg_name, version_type vertype);
STATIC int wm_vuldet_create_file(const char *path, const char *source);
STATIC int wm_vuldet_check_update_period(update_node *upd);
STATIC int wm_vuldet_update_feed(update_node *upd, int8_t *updated);
STATIC int wm_vuldet_sync_feed(update_node *upd, int8_t *need_update);
STATIC int wm_vuldet_check_feed(update_node *upd, int8_t *updated);
STATIC vu_feed wm_vuldet_set_allowed_feed(const char *os_name, const char *os_version, update_node **updates, vu_feed *agent_dist);
/**
* @brief Gets the last update time of a specific feed from the metadata database.
*
* @param feed The feed from which to obtain the information.
* @return -1 on error, a valid time otherwise.
*/
time_t wm_vuldet_get_last_feed_update(vu_feed feed);
/**
* @brief Initialize the main structure(linked list) of all Syscollector agents
* for later analysis.
*
* @param vuldet The vulnearibility detector main data structure.
* @return 0 on success, -1 otherwise.
*/
STATIC int wm_vuldet_collect_agents_to_scan(wm_vuldet_t *vuldet);
/**
* @brief Compare two packages structures (src_name, bin_name, version and arch).
*
* @return 0 if equal, -1 otherwise.
*/
STATIC int wm_vuldet_compare_pkg(cve_vuln_pkg *Pkg1, cve_vuln_pkg *Pkg2);
STATIC cJSON *wm_vuldet_dump(const wm_vuldet_t *vuldet);
STATIC char *wm_vuldet_build_url(char *pattern, char *value);
STATIC void wm_vuldet_adapt_title(char *title, char *cve);
STATIC int wm_vuldet_fetch_oval(update_node *update, char *repo);
STATIC int wm_vuldet_oval_process(update_node *update, char *path, wm_vuldet_db *parsed_vulnerabilities);
STATIC int wm_vuldet_json_rh_parser(cJSON *json_feed, wm_vuldet_db *parsed_vulnerabilities);
STATIC int wm_vuldet_json_arch_parser(cJSON *json_feed, wm_vuldet_db *parsed_vulnerabilities);
STATIC int wm_vuldet_db_empty(sqlite3 *db, vu_feed version);
STATIC int wm_vuldet_nvd_empty();
STATIC int wm_vuldet_generate_os_cpe(sqlite3 *db, scan_agent *agent, int *min_cpe_index, cpe_list **node_list);
STATIC int wm_vuldet_generate_os_and_kernel_package(sqlite3 *db, scan_agent *agent);
STATIC int wm_vuldet_generate_win_cpe(scan_agent *agent);
STATIC vu_feed wm_vuldet_decode_win_os(char *os_raw);
STATIC void wm_vuldet_free_scan_agent(scan_agent *agent);
STATIC int wm_vuldet_insert_agent_data(sqlite3 *db,
scan_agent *agent,
int *cpe_index,
const char *vendor,
const char *product,
const char *source,
const char *version,
const char *src_version,
const char *arch,
const char *reference,
const char *type,
cpe *ag_cpe,
cpe_list **node_list);
/**
* @brief Checks whether the MSU table is populated or not.
* @param db CVE Database with MSU information.
* @return true on success, false otherwise.
*/
STATIC bool wm_vuldet_check_enabled_msu(sqlite3 *db);
/**
* @brief Downloads a feed (either MSU or ALAS) from Wazuh's own repository.
* @param update Update structure of the feed being analyzed.
* @param repo For MSU feeds, it's by default set to NULL; however, if the user specifies another
* URL (tag <url>), this one will be used.
* For ALAS, it contains the already fixed URL to the feed's repository.
* @return 0 if it was successfully downloaded, VU_INV_FEED if it failed,
* and VU_NOT_NEED_UPDATE if it's not required.
*/
STATIC int wm_vuldet_fetch_wazuh_repo(update_node *update, char *repo);
/**
* @brief Checks whether a feed from Wazuh's repository (either ALAS or MSU) must be updated or not.
* This is done by checking the metadata.
* @param update Update structure of the feed being analyzed.
* @return True if it must be updated, false otherwise.
*/
STATIC bool wm_vuldet_check_feed_metadata(update_node *update);
/**
* @brief Downloads the correspondent .meta file for ALAS and MSU feeds.
* @param update Update structure of the feed being analyzed.
* @return Structure that holds all the feed's metadata. NULL otherwise.
*/
STATIC feed_metadata *wm_vuldet_fetch_feed_metadata(update_node *update);
/**
* @brief Parse the .meta file and get the feed's metadata.
* @param path Path of the .meta file.
* @return Structure that holds all the feed's metadata. NULL otherwise.
*/
STATIC feed_metadata *wm_vuldet_parse_feed_metadata(char *path);
/**
* @brief Insert or update the feed's entry in the METADATA table.
* @param msu Structure that holds all the feed's metadata.
* @param update Update structure of the feed being analyzed.
* @return 0 if success, -1 otherwise.
*/
STATIC int wm_insert_feed_metadata(feed_metadata *metadata_feed, update_node *update);
STATIC int wm_vuldet_fetch_redhat(update_node *update);
STATIC int wm_vuldet_fetch_arch(update_node *update, char * repo);
STATIC int wm_vuldet_fetch_wazuh_cpe(update_node *update);
STATIC int wm_vuldet_json_wcpe_parser(cJSON *json_feed, wm_vuldet_db *parsed_vulnerabilities);
STATIC int wm_vuldet_json_dic_parser(cJSON *json_feed, vu_cpe_dic *wcpes);
STATIC int wm_vuldet_fill_dic_section(cJSON *json_feed, vu_cpe_dic_section **wcpes);
STATIC unsigned int wm_vuldet_fill_action_mask(cJSON *json_feed);
STATIC int wm_vuldet_json_msu_parser(cJSON *json_feed, wm_vuldet_db *parsed_vulnerabilities);
STATIC int wm_vuldet_insert_cpe_dic(sqlite3 *db, vu_cpe_dic *w_cpes);
STATIC int wm_vuldet_insert_cpe_dic_array(sqlite3 *db, vu_query query, int id, vu_cpe_dic_section *section);
STATIC vu_cpe_dic_node *wm_vuldet_free_dic_node(vu_cpe_dic_node *dic_node);
STATIC int wm_vuldet_insert_MSU(sqlite3 *db, vu_msu_entries *msu);
STATIC vu_msu_vul_entry *wm_vuldet_free_msu_vul_node(vu_msu_vul_entry *node);
STATIC vu_msu_dep_entry *wm_vuldet_free_msu_dep_node(vu_msu_dep_entry *node);
STATIC int wm_vuldet_build_unix_os_release(scan_agent *agent,
const char* os_major,
const char* os_minor,
const char* os_patch);
/**
* @brief Gets the hotfixes from the agent database in wazuh-db and
* inserts this data in the cve.db database.
*
* @param db Pointer to the cve.db database.
* @param agent_id Id of the agent to request the hotfixes.
* @return OS_SUCCESS if success, OS_INVALID otherwise.
*/
STATIC int wm_vuldet_request_hotfixes(sqlite3 *db, const char *agent_id);
STATIC void wm_vuldet_reset_tables(sqlite3 *db);
STATIC int wm_vuldet_index_json(wm_vuldet_db *parsed_vulnerabilities, update_node *update, char *path, char multi_path);
STATIC int wm_vuldet_index_nvd(sqlite3 *db, update_node *upd, nvd_vulnerability *nvd_it);
STATIC int wm_vuldet_clean_rh(sqlite3 *db);
STATIC int wm_vuldet_clean_wcpe(sqlite3 *db);
STATIC void wm_vuldet_get_package_os(const char *version, const char **os_major, char **os_minor);
STATIC void wm_vuldet_set_subversion(char *version, char **os_minor);
STATIC cJSON *wm_vuldet_json_fread(char *json_file);
STATIC cJSON *wm_vuldet_get_cvss(const char *scoring_vector);
STATIC int wm_vuldet_get_term_condition(char *i_term, char **term, char **comp_field, char **condition);
STATIC void wm_vuldet_run_scan(wm_vuldet_t *vuldet);
STATIC void wm_vuldet_run_sleep(wm_vuldet_t *vuldet);
STATIC void wm_vuldet_init(wm_vuldet_t *vuldet);
STATIC void wm_vuldet_update_last_scan(scan_ctx_t* scan_ctx);
STATIC char *wm_vuldet_normalize_date(char **date);
STATIC int wm_vulndet_insert_msu_vul_entry(sqlite3 *db, vu_msu_vul_entry *vul);
STATIC void wm_vuldet_json_msu_parser_deps(cJSON *json_feed, vu_msu_dep_entry **msu);
STATIC void wm_vuldet_json_msu_parser_vuln(cJSON *json_feed, vu_msu_vul_entry **msu);
STATIC int wm_vulndet_insert_msu_dep_entry(sqlite3 *db, vu_msu_dep_entry *dep);
STATIC void wm_vuln_check_msu_type(vu_msu_vul_entry *msu, cJSON *patchs);
STATIC int wm_vuldet_json_alas_parser(cJSON *json_feed, wm_vuldet_db *parsed_vulnerabilities);
STATIC int wm_vuldet_insert_ALAS(sqlite3 *db, vu_alas_vuln *alas, const char *target);
STATIC vu_alas_pkg *wm_vuldet_packages_parser(char *pkg);
STATIC int wm_vuldet_get_arch_id(sqlite3 * db, const char *cveid, const char *target, const char *pkg_name, const char *pkg_version, int *id);
STATIC void wm_vuldet_free_alas_pkg(vu_alas_pkg **alas_pkg);
STATIC void wm_vuldet_free_alas_node(vu_alas_vuln *node);
STATIC void wm_vuldet_free_alas(vu_alas_vuln *alas_it);
/**
* @brief Updates LAST_FULL_SCAN or LAST_PARTIAL_SCAN from VULN_METADATA with the current time.
* The item to be updated depends on the scan type of scan_ctx.
*
* @param scan_ctx Context of the scan in progress.
*/
void wm_vuldet_update_last_scan(scan_ctx_t* scan_ctx);
/**
* @brief Gets LAST_FULL_SCAN and LAST_PARTIAL_SCAN from VULN_METADATA.
* It returns the value of the specified item.
*
* @param scan_ctx Context of the scan in progress.
* @param item JSON item name to be returned.
* @return A positive value if item exists.
* Zero (0) if item doesn't exists.
* A negative value (-1) if error.
*/
time_t wm_vuldet_get_last_scan(scan_ctx_t* scan_ctx, const char* item);
/**
* @brief Gets LAST_FULL_SCAN from VULN_METADATA.
*
* @param scan_ctx Context of the scan in progress.
* @return A positive value if LAST_FULL_SCAN exists.
* Zero (0) if LAST_FULL_SCAN doesn't exists.
* A negative value (-1) if error.
*/
time_t wm_vuldet_get_last_full_scan(scan_ctx_t* scan_ctx);
/**
* @brief Gets LAST_PARTIAL_SCAN from VULN_METADATA.
*
* @param scan_ctx Context of the scan in progress.
* @return A positive value if LAST_PARTIAL_SCAN exists.
* Zero (0) if LAST_PARTIAL_SCAN doesn't exists.
* A negative value (-1) if error.
*/
time_t wm_vuldet_get_last_partial_scan(scan_ctx_t* scan_ctx);
/**
* @brief Checks if the feed(s) for an agent changed since last scan.
*
* @param agent The scaned agent data structure.
* @param last_scan The time of the last scan.
*
* @return TRUE if any of the feeds of the agent has been modified after the last scan.
* FALSE if all the feeds of the agent hasn't been modified since the last scan.
*/
STATIC bool wm_vuldet_feed_changed(update_node** feeds, scan_agent* agent, time_t last_scan);
/**
* @brief Compares the package's vendor with the official vendors.
*
* @param vendor the package's vendor
* @return 0 if the vendor matches with an official vendor or 1 if it is a external vendor.
*/
STATIC int wm_vuldet_compare_vendors(char * vendor);
/**
* @brief Truncate the minor target for CentOS packages.
*
* @param revision a pointer to the package revision
*/
STATIC void wm_vuldel_truncate_revision(char * revision);
time_t curr_time;
int wdb_vuldet_sock = -1;
int *vu_queue;
// Define time to sleep between messages sent
int usec;
const wm_context WM_VULNDETECTOR_CONTEXT = {
"vulnerability-detector",
(wm_routine)wm_vuldet_main,
(wm_routine)(void *)wm_vuldet_destroy,
(cJSON * (*)(const void *))wm_vuldet_dump,
NULL,
NULL
};
const char *version_null = "0:0";
const char *vu_os_platform_rolling_distros[] = {
"arch"
};
const char *vu_vendor_list[] = {
"canonical",
"debian",
"redhat",
"archlinux",
"linux",
"amazon"
};
const char *vu_os_product_name_list[] = {
"ubuntu_linux",
"debian_linux",
"enterprise_linux",
"arch_linux",
"linux_kernel",
"linux",
"linux_1",
"linux_2"
};
const char *vu_vendor_list_ubuntu_debian[] = {
"canonical",
"ubuntu",
"debian"
};
const char *vu_vendor_list_redhat[] = {
"Red Hat, Inc.",
"CentOS"
};
const char *vu_vendor_list_amazon[] = {
"Amazon Linux",
"Amazon.com",
"Amazon AWS"
};
const char *vu_vendor_list_arch[] = {
"Arch Linux"
};
const char *vu_package_dist[] = {
".el8",
".el7",
".el6",
".el5"
};
const char *vu_feed_tag[] = {
"UBUNTU",
"CANONICAL",
"DEBIAN",
"REDHAT",
"JREDHAT",
"CENTOS",
"ALAS",
"ARCH",
"WIN",
// Ubuntu versions
"TRUSTY",
"XENIAL",
"BIONIC",
"FOCAL",
"JAMMY",
// Debian versions
"STRETCH",
"BUSTER",
"BULLSEYE",
// RedHat versions
"RHEL5",
"RHEL6",
"RHEL7",
"RHEL8",
// NVD
"NVD",
"CPED",
// Amazon versions
"Amazon-Linux",
"Amazon-Linux-2",
// WINDOWS
"WS2003",
"WS2003R2",
"WXP",
"WVISTA",
"W7",
"W8",
"W81",
"W10",
"W11",
"WS2008",
"WS2008R2",
"WS2012",
"WS2012R2",
"WS2016",
"WS2019",
"WS2022",
// OTHER
"CPEW",
"MSU",
"MAC",
"UNKNOWN"
};
const char *vu_feed_ext[] = {
"Ubuntu",
"Canonical",
"Debian",
"Red Hat Enterprise Linux",
"JSON Red Hat Enterprise Linux",
"CentOS",
"Amazon Linux",
"Arch Linux",
"Microsoft Windows",
// Ubuntu versions
"Ubuntu Trusty",
"Ubuntu Xenial",
"Ubuntu Bionic",
"Ubuntu Focal",
"Ubuntu Jammy",
// Debian versions
"Debian Stretch",
"Debian Buster",
"Debian Bullseye",
// RedHat versions
"Red Hat Enterprise Linux 5",
"Red Hat Enterprise Linux 6",
"Red Hat Enterprise Linux 7",
"Red Hat Enterprise Linux 8",
// NVD
"National Vulnerability Database",
"Common Platform Enumeration Dictionary",
// Amazon versions
"Amazon Linux 1",
"Amazon Linux 2",
// WINDOWS
"Windows Server 2003",
"Windows Server 2003 R2",
"Windows XP",
"Windows Vista",
"Windows 7",
"Windows 8",
"Windows 8.1",
"Windows 10",
"Windows 11",
"Windows Server 2008",
"Windows Server 2008 R2",
"Windows Server 2012",
"Windows Server 2012 R2",
"Windows Server 2016",
"Windows Server 2019",
"Windows Server 2022",
// OTHER
"Wazuh CPE dictionary",
"Microsoft Security Update",
"darwin",
"Unknown OS"
};
const char *vu_package_comp[] = {
"less than",
"less than or equal",
"greater than",
"greater than or equal",
"equals",
"not equal",
"exists"
};
const char *vu_severities[] = {
"Low",
"Medium",
"Moderate",
"Unknown",
"High",
"Important",
"Critical",
"None",
"Negligible",
"Untriaged"
};
const char *vu_cpe_dic_option[] = {
"replace_vendor",
"replace_product",
"replace_vendor_if_matches",
"replace_product_if_matches",
"set_version_if_matches",
"replace_sw_edition_if_product_matches",
"replace_msu_name_if_version_matches",
"ignore",
"check_hotfix",
"replace_msu_name",
"set_version_if_product_matches",
"set_targethw_if_product_matches",
"set_version_only_if_product_matches",
"set_targethw_only_if_product_matches"
};
vu_feed wm_vuldet_set_allowed_feed(const char *os_name, const char *os_version, update_node **updates, vu_feed *agent_dist) {
vu_feed retval = FEED_UNKNOWN;
int i;
for (i = 0; i < OS_SUPP_SIZE; i++) {
if (updates[i]) {
if (wm_vuldet_is_single_provider(updates[i]->dist_ref)) {
if (updates[i]->allowed_os_name) {
int j;
char *allowed_os;
char *allowed_ver;
for (allowed_os = *updates[i]->allowed_os_name, allowed_ver = *updates[i]->allowed_os_ver, j = 0; allowed_os; ++j) {
if (strcasestr(os_name, allowed_os) && strcasestr(os_version, allowed_ver)) {
retval = updates[i]->dist_tag_ref;
*agent_dist = updates[i]->dist_ref;
i = OS_SUPP_SIZE;
break;
}
allowed_os = updates[i]->allowed_os_name[j];
allowed_ver = updates[i]->allowed_os_ver[j];
}
}
}
}
}
return retval;
}
int wm_vuldet_check_update_period(update_node *upd) {
return upd && (!upd->last_sync || (upd->interval != WM_VULNDETECTOR_ONLY_ONE_UPD && (upd->last_sync + (time_t) upd->interval) < time(NULL)));
}
int wm_vuldet_sync_feed(update_node *upd, int8_t *need_update) {
return wm_vuldet_fetch_feed(upd, need_update) == OS_INVALID || (*need_update && wm_vuldet_index_feed(upd));
}
int wm_vuldet_create_file(const char *path, const char *source) {
const char *ROOT = "root";
const char *sql;
const char *tail;
sqlite3 *db;
sqlite3_stmt *stmt = NULL;
int result;
uid_t uid;
gid_t gid;
// Open the database file, or create it
if (sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) {
mterror(WM_VULNDETECTOR_LOGTAG, VU_CREATE_DB_ERROR);
return wm_vuldet_sql_error(db, stmt);
}
// Load the tables schema
for (sql = source; sql && *sql; sql = tail) {
if (wm_vuldet_prepare(db, sql, -1, &stmt, &tail) != SQLITE_OK) {
mterror(WM_VULNDETECTOR_LOGTAG, VU_CREATE_DB_ERROR);
return wm_vuldet_sql_error(db, stmt);
}
result = wm_vuldet_step(stmt);
switch (result) {
case SQLITE_MISUSE:
case SQLITE_ROW:
case SQLITE_DONE:
break;
default:
return wm_vuldet_sql_error(db, stmt);
}
wdb_finalize(stmt);
}
sqlite3_close_v2(db);
uid = Privsep_GetUser(ROOT);
gid = Privsep_GetGroup(GROUPGLOBAL);
if (uid == (uid_t) - 1 || gid == (gid_t) - 1) {
mterror(WM_VULNDETECTOR_LOGTAG, USER_ERROR, ROOT, GROUPGLOBAL, strerror(errno), errno);
return OS_INVALID;
}
if (chown(path, uid, gid) < 0) {
mterror(WM_VULNDETECTOR_LOGTAG, CHOWN_ERROR, path, errno, strerror(errno));
return OS_INVALID;
}
if (chmod(path, 0660) < 0) {
mterror(WM_VULNDETECTOR_LOGTAG, CHMOD_ERROR, path, errno, strerror(errno));
return OS_INVALID;
}
return 0;
}
int wm_vuldet_prepare(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **stmt, const char **pzTail) {
int attempts;
int result;
for (attempts = 0; (result = sqlite3_prepare_v2(db, zSql, nByte, stmt, pzTail)) == SQLITE_BUSY; attempts++) {
if (attempts == MAX_SQL_ATTEMPTS) {
mterror(WM_VULNDETECTOR_LOGTAG, VU_MAX_ACC_EXC);
return OS_INVALID;
}
}
return result;
}
int wm_vuldet_step(sqlite3_stmt *stmt) {
int attempts;
int result;
for (attempts = 0; (result = sqlite3_step(stmt)) == SQLITE_BUSY; attempts++) {
if (attempts == MAX_SQL_ATTEMPTS) {
mterror(WM_VULNDETECTOR_LOGTAG, VU_MAX_ACC_EXC);
return OS_INVALID;
}
}
return result;
}
/*
* This function should be taken as the version comparison entry point
*/
int wm_checks_package_vulnerability(char *version_a, const char *operation, const char *version_b, version_type vertype) {
int size;
int epoch, c_epoch;
char version_cl[KEY_SIZE];
char cversion_cl[KEY_SIZE];
char *version_it, *release_it;
char *cversion_it, *crelease_it;
struct pkg_version *package_version;
struct pkg_version *package_feed_version;
enum pkg_relation feed_condition;
int8_t ret = 0;
if (version_b && strcmp(version_b, version_null)) {
// Copy the original values
if (size = snprintf(version_cl, KEY_SIZE, "%s", version_a), size >= KEY_SIZE) {
return OS_INVALID;
}
if (size = snprintf(cversion_cl, KEY_SIZE, "%s", version_b), size >= KEY_SIZE) {
return OS_INVALID;
}
/*
* Check EPOCH:
* If the epoch is not present in the version and we are comparing NVD versions
* we assign -1 to indicate that it shouldn't be compared.
* For Amazon Linux packages we ignore the epoch since the ALAS feed doesn't
* provide it.
*/
if (version_it = strchr(version_cl, ':'), version_it) {
*(version_it++) = '\0';
epoch = (VER_TYPE_RPM_ALAS != vertype) ? strtol(version_cl, NULL, 10) : -1;
} else {
version_it = version_cl;
epoch = (VER_TYPE_NVD != vertype) ? 0 : -1;
}
if (cversion_it = strchr(cversion_cl, ':'), cversion_it) {
*(cversion_it++) = '\0';
c_epoch = strtol(cversion_cl, NULL, 10);
} else {
cversion_it = cversion_cl;
c_epoch = (VER_TYPE_NVD != vertype) ? 0 : -1;
}
// Separate the version from the revision
if (release_it = strchr(version_it, '-'), release_it) {
if (*(release_it++) = '\0', *release_it == '\0') {
release_it = NULL;
}
}
if (crelease_it = strchr(cversion_it, '-'), crelease_it) {
if (*(crelease_it++) = '\0', *crelease_it == '\0') {
crelease_it = NULL;
}
}
// When evaluating CentOS package, the minor target part cannot be compared so it is truncated
if (vertype == VER_TYPE_RPM_CENTOS) {
wm_vuldel_truncate_revision(release_it);
wm_vuldel_truncate_revision(crelease_it);
}
if (vertype == VER_TYPE_DEB) {
// This exception is caused by https://github.com/wazuh/wazuh/issues/11363
if (strstr(cversion_it, " only") != NULL) {
return VU_ERROR_CMP;
}
}
os_calloc(1, sizeof(struct pkg_version), package_version);
os_calloc(1, sizeof(struct pkg_version), package_feed_version);
package_version->epoch = epoch;
package_version->version = version_it;
package_version->revision = release_it ? release_it : "0";
package_feed_version->epoch = c_epoch;
package_feed_version->version = cversion_it;
package_feed_version->revision = crelease_it ? crelease_it : "0";
if (!strcmp(operation, vu_package_comp[VU_COMP_L])) {
feed_condition = PKG_RELATION_LT;
} else if (!strcmp(operation, vu_package_comp[VU_COMP_LE])) {
feed_condition = PKG_RELATION_LE;
} else if (!strcmp(operation, vu_package_comp[VU_COMP_G])) {
feed_condition = PKG_RELATION_GT;
} else if (!strcmp(operation, vu_package_comp[VU_COMP_GE])) {
feed_condition = PKG_RELATION_GE;
} else if (!strcmp(operation, vu_package_comp[VU_COMP_EQ])) {
feed_condition = PKG_RELATION_EQ;
} else {
ret = VU_ERROR_CMP;
goto clean;
}
// This sanity is included since you cannot always expect a revision to come,
// since it is a data provided by the agent.
if ((vertype == VER_TYPE_RPM_CENTOS || vertype == VER_TYPE_RPM_ALAS || vertype == VER_TYPE_RPM) && (NULL == package_version->version)) {
ret = VU_ERROR_CMP;
goto clean;
}
if(pkg_version_relate(package_version, feed_condition, package_feed_version, vertype)) {
ret = VU_VULNERABLE;
} else {
ret = VU_NOT_VULNERABLE;
}
clean:
os_free(package_version);
os_free(package_feed_version);
return ret;
}
return VU_NOT_FIXED;
}
char *wm_vuldet_build_url(char *pattern, char *value) {
size_t size;
char *retval;
os_calloc(VUL_BUILD_REF_MAX + 1, sizeof(char), retval);
size = snprintf(retval, VUL_BUILD_REF_MAX, pattern, value);
os_realloc(retval, size + 1, retval);
return retval;
}
int wm_vuldet_compare_pkg(cve_vuln_pkg *Pkg1, cve_vuln_pkg *Pkg2) {
if (!(!Pkg1->vendor && !Pkg2->vendor) &&
!(Pkg1->vendor && Pkg2->vendor &&
!strcmp(Pkg1->vendor, Pkg2->vendor))) { // Vendor
return -1;
}
if (!(!Pkg1->src_name && !Pkg2->src_name) &&
!(Pkg1->src_name && Pkg2->src_name &&
!strcmp(Pkg1->src_name, Pkg2->src_name))) { // Source name
return -1;
}
if (!(!Pkg1->bin_name && !Pkg2->bin_name) &&
!(Pkg1->bin_name && Pkg2->bin_name &&
!strcmp(Pkg1->bin_name, Pkg2->bin_name))) { // Binary name
return -1;
}
if (!(!Pkg1->version && !Pkg2->version) &&
!(Pkg1->version && Pkg2->version &&
!strcmp(Pkg1->version, Pkg2->version))) { // Version
return -1;
}
/* If only the architecture differs and one of them is 'noarch',
* label them as equals too. */
if (!(!Pkg1->arch && !Pkg2->arch) && // Architecture
(Pkg1->arch && Pkg2->arch && strcmp("noarch", Pkg1->arch) &&
strcmp("noarch", Pkg2->arch) && strcmp(Pkg1->arch, Pkg2->arch))) {
return -1;
}
if (Pkg1->nvd_cond && Pkg2->nvd_cond &&
Pkg1->nvd_cond->id != Pkg2->nvd_cond->id) { // ID from table NVD_CVE_MATCH
return -1;
}
return 0;
}
int wm_vuldet_add_cve_node(cve_vuln_pkg *newPkg, const char *cve, OSHash *cve_table) {
int status = 0;
int found = 0;
cve_vuln_pkg *lastPkg = NULL;
cve_vuln_pkg *tmpPkg = NULL;
if (status = OSHash_Add(cve_table, cve, (void *)newPkg), !status) {
return -1;
}
// If the cve node already exists, add the pkg to the end of the linked list.
if (status == 1) {
if (tmpPkg = (cve_vuln_pkg *)OSHash_Get(cve_table, cve), !tmpPkg) {
return -1;
}
do {
if (!wm_vuldet_compare_pkg(tmpPkg, newPkg)) { // Same package?
if (!tmpPkg->nvd_cond && newPkg->nvd_cond) {
os_calloc(1, sizeof(cve_vuln_cond_NVD), tmpPkg->nvd_cond);
memcpy(tmpPkg->nvd_cond, newPkg->nvd_cond, sizeof(cve_vuln_cond_NVD));
if (newPkg->nvd_cond->start_version) {
os_strdup(newPkg->nvd_cond->start_version, tmpPkg->nvd_cond->start_version);
}
if (newPkg->nvd_cond->end_version) {
os_strdup(newPkg->nvd_cond->end_version, tmpPkg->nvd_cond->end_version);
}
if (newPkg->nvd_cond->operator) {
os_strdup(newPkg->nvd_cond->operator, tmpPkg->nvd_cond->operator);
}
}
if (!tmpPkg->vuln_cond && newPkg->vuln_cond) {
os_calloc(1, sizeof(cve_vuln_cond), tmpPkg->vuln_cond);
memcpy(tmpPkg->vuln_cond, newPkg->vuln_cond, sizeof(cve_vuln_cond));
if (newPkg->vuln_cond->state) {
os_strdup(newPkg->vuln_cond->state, tmpPkg->vuln_cond->state);
}
if (newPkg->vuln_cond->operation) {
os_strdup(newPkg->vuln_cond->operation, tmpPkg->vuln_cond->operation);
}
if (newPkg->vuln_cond->operation_value) {
os_strdup(newPkg->vuln_cond->operation_value, tmpPkg->vuln_cond->operation_value);
}
if (newPkg->vuln_cond->condition) {
os_strdup(newPkg->vuln_cond->condition, tmpPkg->vuln_cond->condition);
}
}
tmpPkg->feed |= newPkg->feed; // Update the feed field with the new source.
found = 1; // Package duplicated
}
lastPkg = tmpPkg; // Save last known entry within the linked list.
tmpPkg = tmpPkg->next;
} while (tmpPkg);
if (!found) {
lastPkg->next = newPkg;
}
}
return found;
}
void wm_vuldet_free_cve_node(void *data){
if (data) {
cve_vuln_pkg *pkg = (cve_vuln_pkg *) data;
cve_vuln_pkg *next = NULL;
do { // Free each package in the linked list.
next = pkg->next;
os_free(pkg->vendor);
os_free(pkg->bin_name);
os_free(pkg->src_name);
os_free(pkg->arch);
os_free(pkg->version);