OpenVAS Scanner 23.35.1
table_driven_lsc.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "table_driven_lsc.h"
12
13#include "base/networking.h"
14#include "kb_cache.h"
15#include "plugutils.h"
16
17#include <ctype.h> // for tolower()
18#include <curl/curl.h>
19#include <gnutls/gnutls.h>
20#include <gvm/base/prefs.h>
21#include <gvm/util/mqtt.h> // for mqtt_reset
22#include <gvm/util/uuidutils.h> // for gvm_uuid_make
23#include <json-glib/json-glib.h>
24#include <stddef.h>
25
26#undef G_LOG_DOMAIN
30#define G_LOG_DOMAIN "lib misc"
31
35static int lsc_flag = 0;
36
39void
41{
42 lsc_flag = 1;
43}
44
47int
49{
50 return lsc_flag;
51}
52
64static JsonBuilder *
65add_packages_str_to_list (JsonBuilder *builder, const gchar *packages)
66{
67 gchar **package_list = NULL;
68
69 json_builder_set_member_name (builder, "package_list");
70 json_builder_begin_array (builder);
71
72 package_list = g_strsplit (packages, "\n", 0);
73 if (package_list && package_list[0])
74 {
75 int i;
76 for (i = 0; package_list[i]; i++)
77 json_builder_add_string_value (builder, package_list[i]);
78 }
79
80 json_builder_end_array (builder);
81 g_strfreev (package_list);
82
83 return builder;
84}
85
101static gchar *
102make_table_driven_lsc_info_json_str (const char *scan_id, const char *ip_str,
103 const char *hostname,
104 const char *os_release,
105 const char *package_list)
106{
107 JsonBuilder *builder;
108 JsonGenerator *gen;
109 JsonNode *root;
110 gchar *json_str;
111
112 /* Build the message in json format to be published. */
113 builder = json_builder_new ();
114
115 json_builder_begin_object (builder);
116
117 json_builder_set_member_name (builder, "message_id");
118 builder = json_builder_add_string_value (builder, gvm_uuid_make ());
119
120 json_builder_set_member_name (builder, "group_id");
121 builder = json_builder_add_string_value (builder, gvm_uuid_make ());
122
123 json_builder_set_member_name (builder, "message_type");
124 builder = json_builder_add_string_value (builder, "scan.start");
125
126 json_builder_set_member_name (builder, "created");
127 builder = json_builder_add_int_value (builder, time (NULL));
128
129 json_builder_set_member_name (builder, "scan_id");
130 builder = json_builder_add_string_value (builder, scan_id);
131
132 json_builder_set_member_name (builder, "host_ip");
133 json_builder_add_string_value (builder, ip_str);
134
135 json_builder_set_member_name (builder, "host_name");
136 json_builder_add_string_value (builder, hostname);
137
138 json_builder_set_member_name (builder, "os_release");
139 json_builder_add_string_value (builder, os_release);
140
141 add_packages_str_to_list (builder, package_list);
142
143 json_builder_end_object (builder);
144
145 gen = json_generator_new ();
146 root = json_builder_get_root (builder);
147 json_generator_set_root (gen, root);
148 json_str = json_generator_to_data (gen, NULL);
149
150 json_node_free (root);
151 g_object_unref (gen);
152 g_object_unref (builder);
153
154 if (json_str == NULL)
155 g_warning ("%s: Error while creating JSON.", __func__);
156
157 return json_str;
158}
159
174static gchar *
176 const char *host_ip, const char *json,
177 int len)
178{
179 JsonParser *parser;
180 JsonReader *reader = NULL;
181
182 GError *err = NULL;
183 gchar *ret = NULL;
184
185 parser = json_parser_new ();
186 if (!json_parser_load_from_data (parser, json, len, &err))
187 {
188 goto cleanup;
189 }
190
191 reader = json_reader_new (json_parser_get_root (parser));
192
193 // Check for Scan ID
194 if (!json_reader_read_member (reader, "scan_id"))
195 {
196 goto cleanup;
197 }
198 if (g_strcmp0 (json_reader_get_string_value (reader), scan_id))
199 {
200 goto cleanup;
201 }
202 json_reader_end_member (reader);
203
204 // Check Host IP
205 if (!json_reader_read_member (reader, "host_ip"))
206 {
207 goto cleanup;
208 }
209 if (g_strcmp0 (json_reader_get_string_value (reader), host_ip))
210 {
211 goto cleanup;
212 }
213 json_reader_end_member (reader);
214
215 // Check Status
216 if (!json_reader_read_member (reader, "status"))
217 {
218 goto cleanup;
219 }
220 ret = g_strdup (json_reader_get_string_value (reader));
221
222 json_reader_end_member (reader);
223
224cleanup:
225 if (reader)
226 g_object_unref (reader);
227 g_object_unref (parser);
228 if (err != NULL)
229 {
230 g_warning ("%s: Unable to parse json. Reason: %s", __func__,
231 err->message);
232 }
233 return ret;
234}
235
236#define RSNOTUS
237#ifdef RSNOTUS
242{
243 char *server; // original openvasd server URL
244 char *schema; // schema is http or https
245 char *host; // server hostname
246 char *alpn; // Application layer protocol negotiation: http/1.0, http/1.1, h2
247 char *http_version; // same version as in application layer
248 int port; // server port
249 int tls; // 0: TLS encapsulation disable. Otherwise enable
250};
251
252typedef struct notus_info *notus_info_t;
253
260static notus_info_t
262{
263 notus_info_t notusdata;
264 notusdata = g_malloc0 (sizeof (struct notus_info));
265 if (!notusdata)
266 return NULL;
267 notusdata->server = g_strdup (server);
268 return notusdata;
269}
270
275static void
277{
278 if (notusdata)
279 {
280 g_free (notusdata->server);
281 g_free (notusdata->schema);
282 g_free (notusdata->host);
283 g_free (notusdata->alpn);
284 g_free (notusdata->http_version);
285 }
286}
287
294static char *
296{
297 for (char *p = s; *p; p++)
298 *p = tolower (*p);
299 return s;
300}
301
311static gchar *
312make_package_list_as_json_str (const char *packages)
313{
314 JsonBuilder *builder;
315 JsonGenerator *gen;
316 JsonNode *root;
317 gchar *json_str = NULL;
318 gchar **package_list = NULL;
319 builder = json_builder_new ();
320
321 json_builder_begin_array (builder);
322
323 package_list = g_strsplit (packages, "\n", 0);
324 if (package_list && package_list[0])
325 {
326 int i;
327 for (i = 0; package_list[i]; i++)
328 json_builder_add_string_value (builder, package_list[i]);
329 }
330
331 json_builder_end_array (builder);
332 g_strfreev (package_list);
333
334 gen = json_generator_new ();
335 root = json_builder_get_root (builder);
336 json_generator_set_root (gen, root);
337 json_str = json_generator_to_data (gen, NULL);
338
339 json_node_free (root);
340 g_object_unref (gen);
341 g_object_unref (builder);
342
343 if (json_str == NULL)
344 g_warning ("%s: Error while creating JSON.", __func__);
345
346 return json_str;
347}
348
358static int
360{
361 CURLU *h = curl_url ();
362 char *schema = NULL;
363 char *host = NULL;
364 char *port = NULL;
365
366 if (!notusdata)
367 return -1;
368
369 if (curl_url_set (h, CURLUPART_URL, (*notusdata)->server, 0) > 0)
370 {
371 g_warning ("%s: Error parsing URL %s", __func__, (*notusdata)->server);
372 return -1;
373 }
374
375 curl_url_get (h, CURLUPART_SCHEME, &schema, 0);
376 curl_url_get (h, CURLUPART_HOST, &host, 0);
377 curl_url_get (h, CURLUPART_PORT, &port, 0);
378
379 if (!schema || !host)
380 {
381 g_warning ("%s: Invalid URL %s. It must be in format: "
382 "schema://host:port. E.g. http://localhost:8080",
383 __func__, (*notusdata)->server);
384 curl_url_cleanup (h);
385 curl_free (schema);
386 curl_free (host);
387 curl_free (port);
388 return -1;
389 }
390
391 (*notusdata)->host = g_strdup (host);
392 if (port)
393 (*notusdata)->port = atoi (port);
394 else if (g_strcmp0 (schema, "https"))
395 (*notusdata)->port = 443;
396 else
397 (*notusdata)->port = 80;
398
399 (*notusdata)->schema = g_strdup (help_tolower (schema));
400 if (g_strrstr ((*notusdata)->schema, "https"))
401 {
402 (*notusdata)->tls = 1;
403 (*notusdata)->http_version = g_strdup ("2");
404 (*notusdata)->alpn = g_strdup ("h2");
405 }
406 else if (g_strrstr ((*notusdata)->schema, "http"))
407 {
408 (*notusdata)->tls = 0;
409 (*notusdata)->http_version = g_strdup ("1.1");
410 (*notusdata)->alpn = g_strdup ("http/1.1");
411 }
412 else
413 {
414 g_warning ("%s: Invalid openvasd server schema", (*notusdata)->server);
415 curl_url_cleanup (h);
416 curl_free (schema);
417 curl_free (host);
418 curl_free (port);
419 return -1;
420 }
421
422 curl_url_cleanup (h);
423 curl_free (schema);
424 curl_free (host);
425 curl_free (port);
426
427 return 0;
428}
429
435static advisories_t *
437{
438 advisories_t *advisories_list = g_malloc0 (sizeof (advisories_t));
439 advisories_list->max_size = 100;
440 advisories_list->advisories =
441 g_malloc0_n (advisories_list->max_size, sizeof (advisory_t));
442 advisories_list->type = NOTUS;
443
444 return advisories_list;
445}
446
452static advisories_t *
454{
455 advisories_t *advisories_list = g_malloc0 (sizeof (advisories_t));
456 advisories_list->max_size = 100;
457 advisories_list->skiron_advisory =
458 g_malloc0_n (advisories_list->max_size, sizeof (skiron_advisory_t));
459 advisories_list->type = SKIRON;
460
461 return advisories_list;
462}
463
471static void
473{
474 // Reallocate more memory if the list is full
475 if (advisories_list->count == advisories_list->max_size)
476 {
477 advisories_list->max_size *= 2;
478 advisories_list->advisories =
479 g_realloc_n (advisories_list->advisories, advisories_list->max_size,
480 sizeof (advisory_t));
481 memset (advisories_list->advisories + advisories_list->count, '\0',
482 (advisories_list->max_size - advisories_list->count)
483 * sizeof (advisory_t *));
484 }
485 advisories_list->advisories[advisories_list->count] = notus_advisory;
486 advisories_list->count++;
487}
488
495static advisory_t *
497{
498 advisory_t *adv = NULL;
499 adv = g_malloc0 (sizeof (advisory_t));
500 adv->oid = g_strdup (oid);
501 adv->count = 0;
502 return adv;
503}
504
505static skiron_advisory_t *
506skiron_advisory_new (char *oid, char *message)
507{
508 skiron_advisory_t *adv = NULL;
509 adv = g_malloc0 (sizeof (skiron_advisory_t));
510 adv->oid = g_strdup (oid);
511 adv->message = g_strdup (message);
512 return adv;
513}
514
523static void
525{
526 if (adv->count == 100)
527 {
528 g_warning (
529 "%s: Failed adding new vulnerable package to the notus_advisory %s. "
530 "No more free slots",
531 __func__, adv->oid);
532 return;
533 }
534
535 adv->pkgs[adv->count] = vuln;
536 adv->count++;
537}
538
544static void
546{
547 if (notus_advisory == NULL)
548 return;
549
550 g_free (notus_advisory->oid);
551 for (size_t i = 0; i < notus_advisory->count; i++)
552 {
553 if (notus_advisory->pkgs[i] != NULL)
554 {
555 g_free (notus_advisory->pkgs[i]->pkg_name);
556 g_free (notus_advisory->pkgs[i]->install_version);
557 if (notus_advisory->pkgs[i]->type == RANGE)
558 {
559 g_free (notus_advisory->pkgs[i]->range->start);
560 g_free (notus_advisory->pkgs[i]->range->stop);
561 }
562 else if (notus_advisory->pkgs[i]->type == SINGLE)
563 {
564 g_free (notus_advisory->pkgs[i]->version->version);
565 g_free (notus_advisory->pkgs[i]->version->specifier);
566 }
567 }
568 }
569 notus_advisory = NULL;
570}
571
577void
579{
580 if (advisories == NULL)
581 return;
582
583 for (size_t i = 0; i < advisories->count; i++)
585 advisories = NULL;
586}
587
602static vuln_pkg_t *
603vulnerable_pkg_new (const char *pkg_name, const char *install_version,
604 enum fixed_type type, char *item1, char *item2)
605{
606 vuln_pkg_t *vuln = NULL;
607 version_range_t *range = NULL;
608 fixed_version_t *fixed_ver = NULL;
609
610 vuln = g_malloc0 (sizeof (vuln_pkg_t));
611 vuln->pkg_name = g_strdup (pkg_name);
612 vuln->install_version = g_strdup (install_version);
613 vuln->type = type;
614 if (type == RANGE)
615 {
616 range = g_malloc0 (sizeof (range_t));
617 range->start = g_strdup (item1);
618 range->stop = g_strdup (item2);
619 vuln->range = range;
620 }
621 else
622 {
623 fixed_ver = g_malloc0 (sizeof (fixed_version_t));
624 fixed_ver->version = g_strdup (item1);
625 fixed_ver->specifier = g_strdup (item2);
626 vuln->version = fixed_ver;
627 }
628
629 return vuln;
630}
631
632static advisories_t *
633lsc_process_response_notus (JsonReader *reader)
634{
637 char **members = json_reader_list_members (reader);
638
639 if (!members || !members[0])
640 {
641 g_debug ("No members found");
642 return NULL;
643 }
644
645 for (int i = 0; members[i]; i++)
646 {
648
649 if (!json_reader_read_member (reader, members[i]))
650 {
651 g_debug ("No member oid");
652 return NULL;
653 }
654 if (!json_reader_is_array (reader))
655 {
656 g_debug ("Is not an array");
657 return NULL;
658 }
659
660 notus_advisory = advisory_new (g_strdup (members[i]));
661
662 int count_pkgs = json_reader_count_elements (reader);
663 g_debug ("There are %d packages for notus_advisory %s", count_pkgs,
664 members[i]);
665 for (int j = 0; j < count_pkgs; j++)
666 {
667 vuln_pkg_t *pkg = NULL;
668 char *name = NULL;
669 char *installed_version = NULL;
670 char *start = NULL;
671 char *stop = NULL;
672 char *version = NULL;
673 char *specifier = NULL;
674 enum fixed_type type = UNKNOWN;
675
676 json_reader_read_element (reader, j);
677 if (!json_reader_is_object (reader))
678 {
679 g_warning ("%s: Package %d of notus_advisory %s is not an object",
680 __func__, j, members[i]);
682 return NULL;
683 }
684
685 json_reader_read_member (reader, "name");
686 name = g_strdup (json_reader_get_string_value (reader));
687 json_reader_end_member (reader);
688 g_debug ("name: %s", name);
689
690 json_reader_read_member (reader, "installed_version");
691 installed_version = g_strdup (json_reader_get_string_value (reader));
692 json_reader_end_member (reader);
693 g_debug ("installed_version: %s", installed_version);
694
695 json_reader_read_member (reader, "fixed_version");
696 g_debug ("Fixed_version has %d members",
697 json_reader_count_members (reader));
698
699 // Version Range
700 json_reader_read_member (reader, "start");
701 start = g_strdup (json_reader_get_string_value (reader));
702 json_reader_end_member (reader);
703 json_reader_read_member (reader, "end");
704 stop = g_strdup (json_reader_get_string_value (reader));
705 json_reader_end_member (reader);
706 g_debug ("start %s, end: %s", start, stop);
707
708 // version and specifier
709 json_reader_read_member (reader, "version");
710 version = g_strdup (json_reader_get_string_value (reader));
711 json_reader_end_member (reader);
712 json_reader_read_member (reader, "specifier");
713 specifier = g_strdup (json_reader_get_string_value (reader));
714 json_reader_end_member (reader);
715 g_debug ("version %s, specifier: %s", version, specifier);
716
717 // end read fixes version member
718 json_reader_end_member (reader);
719
720 // end package element
721 json_reader_end_element (reader);
722
723 char *item1 = NULL, *item2 = NULL;
724 if (start && stop)
725 {
726 type = RANGE;
727 item1 = start;
728 item2 = stop;
729 }
730 else if (version && specifier)
731 {
732 type = SINGLE;
733 item1 = version;
734 item2 = specifier;
735 }
736 else
737 {
738 g_warning ("%s: Error parsing json element", __func__);
739 g_free (name);
740 g_free (installed_version);
741 g_free (item1);
742 g_free (item2);
744 return NULL;
745 }
746
747 pkg =
748 vulnerable_pkg_new (name, installed_version, type, item1, item2);
749 g_free (name);
750 g_free (installed_version);
751 g_free (item1);
752 g_free (item2);
753
755 }
756 // end notus_advisory
757 json_reader_end_member (reader);
759 }
760 return advisories;
761}
762
763static advisories_t *
764lsc_process_response_skiron (JsonReader *reader)
765{
767
768 for (int i = 0; json_reader_read_element (reader, i); i++)
769 {
771 char *oid = NULL;
772 char *message = NULL;
773
774 json_reader_read_member (reader, "oid");
775 oid = (char *) json_reader_get_string_value (reader);
776 json_reader_end_member (reader);
777
778 json_reader_read_member (reader, "message");
779 message = (char *) json_reader_get_string_value (reader);
780 json_reader_end_member (reader);
781
783
785
786 // end element
787 json_reader_end_element (reader);
788 }
789
790 return advisories;
791}
792
806lsc_process_response (const gchar *resp, const size_t len)
807{
808 JsonParser *parser = NULL;
809 JsonReader *reader = NULL;
810 GError *err = NULL;
811
812 advisories_t *advisories = NULL;
813
814 parser = json_parser_new ();
815 if (!json_parser_load_from_data (parser, resp, len, &err))
816 {
817 g_message ("Error parsing");
818 }
819
820 reader = json_reader_new (json_parser_get_root (parser));
821
822 if (json_reader_is_object (reader))
823 {
825 }
826 else if (json_reader_is_array (reader))
827 {
829 }
830 else
831 {
832 g_debug ("Unknown JSON response format");
833 }
834
835 if (reader)
836 g_object_unref (reader);
837 g_object_unref (parser);
838
839 return advisories;
840}
841
844struct string
845{
846 char *ptr;
847 size_t len;
848};
849
854static void
855init_string (struct string *s)
856{
857 s->len = 0;
858 s->ptr = g_malloc0 (s->len + 1);
859 if (s->ptr == NULL)
860 {
861 g_warning ("%s: Error allocating memory for response", __func__);
862 return;
863 }
864 s->ptr[0] = '\0';
865}
866
872static size_t
873response_callback_fn (void *ptr, size_t size, size_t nmemb, void *struct_string)
874{
875 struct string *s = struct_string;
876 size_t new_len = s->len + size * nmemb;
877 char *ptr_aux = g_realloc (s->ptr, new_len + 1);
878 s->ptr = ptr_aux;
879 if (s->ptr == NULL)
880 {
881 g_warning ("%s: Error allocating memory for response", __func__);
882 return 0; // no memory left
883 }
884 memcpy (s->ptr + s->len, ptr, size * nmemb);
885 s->ptr[new_len] = '\0';
886 s->len = new_len;
887
888 return size * nmemb;
889}
890
901static long
902send_request (notus_info_t notusdata, const char *os, const char *pkg_list,
903 char **response)
904{
905 CURL *curl;
906 GString *url = NULL;
907 long http_code = -1;
908 struct string resp;
909 struct curl_slist *customheader = NULL;
910 char *os_aux;
911 GString *xapikey = NULL;
912
913 if ((curl = curl_easy_init ()) == NULL)
914 {
915 g_warning ("Not possible to initialize curl library");
916 return http_code;
917 }
918
919 url = g_string_new (notusdata->server);
920 g_string_append (url, "/notus/");
921
922 //
923 os_aux = help_tolower (g_strdup (os));
924 for (size_t i = 0; i < strlen (os_aux); i++)
925 {
926 if (os_aux[i] == ' ')
927 os_aux[i] = '_';
928 }
929
930 g_string_append (url, os_aux);
931 g_free (os_aux);
932
933 g_debug ("%s: URL: %s", __func__, url->str);
934 // Set URL
935 if (curl_easy_setopt (curl, CURLOPT_URL, g_strdup (url->str)) != CURLE_OK)
936 {
937 g_warning ("Not possible to set the URL");
938 curl_easy_cleanup (curl);
939 return http_code;
940 }
941 g_string_free (url, TRUE);
942
943 // Accept an insecure connection. Don't verify the server certificate
944 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
945 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
946
947 // Set API KEY
948 if (prefs_get ("x-apikey"))
949 {
950 xapikey = g_string_new ("X-APIKEY: ");
951 g_string_append (xapikey, prefs_get ("x-apikey"));
952 customheader = curl_slist_append (customheader, g_strdup (xapikey->str));
953 g_string_free (xapikey, TRUE);
954 }
955 // SET Content type
956 customheader =
957 curl_slist_append (customheader, "Content-Type: application/json");
958 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, customheader);
959 // Set body
960 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, pkg_list);
961 curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, strlen (pkg_list));
962
963 // Init the struct where the response is stored and set the callback function
964 init_string (&resp);
965 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, response_callback_fn);
966 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &resp);
967
968 int ret = CURLE_OK;
969 if ((ret = curl_easy_perform (curl)) != CURLE_OK)
970 {
971 g_warning ("%s: Error sending request: %d", __func__, ret);
972 curl_easy_cleanup (curl);
973 g_free (resp.ptr);
974 return http_code;
975 }
976
977 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
978
979 curl_easy_cleanup (curl);
980 g_debug ("Server response %s", resp.ptr);
981 *response = g_strdup (resp.ptr);
982 g_free (resp.ptr);
983 // already free()'ed with curl_easy_cleanup().
984
985 return http_code;
986}
987
996char *
997lsc_get_response (const char *pkg_list, const char *os)
998{
999 const char *server = NULL;
1000 char *json_pkglist;
1001 char *response = NULL;
1002 notus_info_t notusdata;
1003 long ret;
1004
1005 // Parse the server and get the port, host, schema
1006 // and necessary information to build the message
1007 server = prefs_get ("openvasd_server");
1008 notusdata = init_notus_info (server);
1009
1010 if (parse_server (&notusdata) < 0)
1011 {
1012 free_notus_info (notusdata);
1013 return NULL;
1014 }
1015
1016 // Convert the package list string into a string containing json
1017 // array of packages
1018 if ((json_pkglist = make_package_list_as_json_str (pkg_list)) == NULL)
1019 {
1020 free_notus_info (notusdata);
1021 return NULL;
1022 }
1023
1024 ret = send_request (notusdata, os, json_pkglist, &response);
1025 if (ret != 200)
1026 g_warning ("%ld: Error sending request to openvasd: %s", ret, response);
1027
1028 free_notus_info (notusdata);
1029 g_free (json_pkglist);
1030
1031 return response;
1032}
1033
1044static int
1045call_rs_notus (const char *ip_str, const char *hostname, const char *pkg_list,
1046 const char *os)
1047{
1048 char *body = NULL;
1049 advisories_t *advisories = NULL;
1050 int res_count = 0;
1051 if ((body = lsc_get_response (pkg_list, os)) == NULL)
1052 return -1;
1053
1054 advisories = lsc_process_response (body, strlen (body));
1055
1056 // Process the advisories, generate results and store them in the kb
1057 for (size_t i = 0; i < advisories->count; i++)
1058 {
1060 gchar *buffer;
1061 GString *result = g_string_new (NULL);
1062 for (size_t j = 0; j < notus_advisory->count; j++)
1063 {
1064 vuln_pkg_t *pkg = notus_advisory->pkgs[j];
1065 GString *res = g_string_new (NULL);
1066
1067 if (pkg->type == RANGE)
1068 {
1069 g_string_printf (res,
1070 "\n"
1071 "Vulnerable package: %s\n"
1072 "Installed version: %s-%s\n"
1073 "Fixed version: < %s-%s\n"
1074 "Fixed version: >=%s-%s\n",
1075 pkg->pkg_name, pkg->pkg_name,
1076 pkg->install_version, pkg->pkg_name,
1077 pkg->range->start, pkg->pkg_name,
1078 pkg->range->stop);
1079 }
1080 else if (pkg->type == SINGLE)
1081 {
1082 g_string_printf (res,
1083 "\n"
1084 "Vulnerable package: %s\n"
1085 "Installed version: %s-%s\n"
1086 "Fixed version: %2s%s-%s\n",
1087 pkg->pkg_name, pkg->pkg_name,
1088 pkg->install_version, pkg->version->specifier,
1089 pkg->pkg_name, pkg->version->version);
1090 }
1091 else
1092 {
1093 g_warning ("%s: Unknown fixed version type for notus_advisory %s",
1094 __func__, notus_advisory->oid);
1095 g_string_free (result, TRUE);
1097 return -1;
1098 }
1099 g_string_append (result, res->str);
1100 g_string_free (res, TRUE);
1101 }
1102
1103 // type|||IP|||HOSTNAME|||package|||OID|||the result message|||URI
1104 buffer = g_strdup_printf ("%s|||%s|||%s|||%s|||%s|||%s|||%s", "ALARM",
1105 ip_str, hostname ? hostname : " ", "package",
1106 notus_advisory->oid, result->str, "");
1107 g_string_free (result, TRUE);
1108 kb_item_push_str_with_main_kb_check (get_main_kb (), "internal/results",
1109 buffer);
1110 res_count++;
1111 g_free (buffer);
1112 }
1113
1115 return res_count;
1116}
1117
1118#endif // End RSNOTUS
1119
1135int
1136run_table_driven_lsc (const char *scan_id, const char *ip_str,
1137 const char *hostname, const char *package_list,
1138 const char *os_release)
1139{
1140 int err = 0;
1141 if (!os_release || !package_list)
1142 return 0;
1143
1144 if (prefs_get ("openvasd_server"))
1145 {
1146 g_message ("Running Notus for %s via openvasd", ip_str);
1147 err = call_rs_notus (ip_str, hostname, package_list, os_release);
1148
1149 return err;
1150 }
1151 else
1152 {
1153 gchar *json_str;
1154 gchar *topic;
1155 gchar *payload;
1156 gchar *status = NULL;
1157 int topic_len;
1158 int payload_len;
1159
1160 // Subscribe to status topic
1161 err = mqtt_subscribe ("scanner/status");
1162 if (err)
1163 {
1164 g_warning ("%s: Error starting lsc. Unable to subscribe", __func__);
1165 return -1;
1166 }
1167 /* Get the OS release. TODO: have a list with supported OS. */
1168
1170 os_release, package_list);
1171
1172 // Run table driven lsc
1173 if (json_str == NULL)
1174 return -1;
1175
1176 g_message ("Running Notus for %s", ip_str);
1177 err = mqtt_publish ("scanner/package/cmd/notus", json_str);
1178 if (err)
1179 {
1180 g_warning ("%s: Error publishing message for Notus.", __func__);
1181 g_free (json_str);
1182 return -1;
1183 }
1184
1185 g_free (json_str);
1186
1187 // Wait for Notus scanner to start or interrupt
1188 while (!status)
1189 {
1190 err = mqtt_retrieve_message (&topic, &topic_len, &payload,
1191 &payload_len, 60000);
1192 if (err == -1 || err == 1)
1193 {
1194 g_warning ("%s: Unable to retrieve status message from notus. %s",
1195 __func__, err == 1 ? "Timeout after 60 s." : "");
1196 return -1;
1197 }
1198
1199 // Get status if it belongs to corresponding scan and host
1200 // Else wait for next status message
1202 scan_id, ip_str, payload, payload_len);
1203
1204 g_free (topic);
1205 g_free (payload);
1206 }
1207 // If started wait for it to finish or interrupt
1208 if (!g_strcmp0 (status, "running"))
1209 {
1210 g_debug ("%s: table driven LSC with scan id %s successfully started "
1211 "for host %s",
1212 __func__, scan_id, ip_str);
1213 g_free (status);
1214 status = NULL;
1215 while (!status)
1216 {
1217 err = mqtt_retrieve_message (&topic, &topic_len, &payload,
1218 &payload_len, 60000);
1219 if (err == -1)
1220 {
1221 g_warning (
1222 "%s: Unable to retrieve status message from notus.",
1223 __func__);
1224 return -1;
1225 }
1226 if (err == 1)
1227 {
1228 g_warning (
1229 "%s: Unable to retrieve message. Timeout after 60s.",
1230 __func__);
1231 return -1;
1232 }
1233
1235 scan_id, ip_str, payload, payload_len);
1236 g_free (topic);
1237 g_free (payload);
1238 }
1239 }
1240 else
1241 {
1242 g_warning ("%s: Unable to start lsc. Got status: %s", __func__,
1243 status);
1244 g_free (status);
1245 return -1;
1246 }
1247
1248 if (g_strcmp0 (status, "finished"))
1249 {
1250 g_warning (
1251 "%s: table driven lsc with scan id %s did not finish successfully "
1252 "for host %s. Last status was %s",
1253 __func__, scan_id, ip_str, status);
1254 err = -1;
1255 }
1256 else
1257 g_debug ("%s: table driven lsc with scan id %s successfully finished "
1258 "for host %s",
1259 __func__, scan_id, ip_str);
1260 g_free (status);
1261 }
1262 return err;
1263}
kb_t get_main_kb(void)
gets the main_kb. @description returns the previously set main_kb; when asserts are enabled it will a...
Definition kb_cache.c:41
Header file to cache main_kb.
const char * oid
Definition nasl_builtin_find_service.c:51
u_char * payload
Definition nasl_frame_forgery.c:1
const char * name
Definition nasl_init.c:440
uint8_t len
Definition nasl_packet_forgery.c:1
const char * hostname
Definition pluginlaunch.c:68
int kb_item_push_str_with_main_kb_check(kb_t kb, const char *name, const char *value)
Check if the current kb corresponds to the original scanid, if it matches it kb_item_push_str....
Definition plugutils.c:533
Header file for module plugutils.
const char * scan_id
Definition scan_id.c:10
Definition table_driven_lsc.h:90
size_t max_size
Definition table_driven_lsc.h:98
advisory_type_t type
Definition table_driven_lsc.h:96
advisory_t ** advisories
Definition table_driven_lsc.h:93
skiron_advisory_t ** skiron_advisory
Definition table_driven_lsc.h:94
size_t count
Definition table_driven_lsc.h:97
char * specifier
Definition table_driven_lsc.h:39
char * version
Definition table_driven_lsc.h:38
Host information, implemented as doubly linked list.
Definition hosts.c:37
Definition table_driven_lsc.h:71
vuln_pkg_t * pkgs[100]
Definition table_driven_lsc.h:73
char * oid
Definition table_driven_lsc.h:72
size_t count
Definition table_driven_lsc.h:75
Struct to hold necessary information to call and run notus.
Definition table_driven_lsc.c:242
int tls
Definition table_driven_lsc.c:249
int port
Definition table_driven_lsc.c:248
char * host
Definition table_driven_lsc.c:245
char * alpn
Definition table_driven_lsc.c:246
char * schema
Definition table_driven_lsc.c:244
char * server
Definition table_driven_lsc.c:243
char * http_version
Definition table_driven_lsc.c:247
Definition table_driven_lsc.h:80
char * oid
Definition table_driven_lsc.h:81
char * message
Definition table_driven_lsc.h:82
Define a string struct for storing the response.
Definition table_driven_lsc.c:845
size_t len
Definition table_driven_lsc.c:847
char * ptr
Definition table_driven_lsc.c:846
char * stop
Definition table_driven_lsc.h:48
char * start
Definition table_driven_lsc.h:47
fixed_version_t * version
Definition table_driven_lsc.h:62
enum fixed_type type
Definition table_driven_lsc.h:58
version_range_t * range
Definition table_driven_lsc.h:61
char * pkg_name
Definition table_driven_lsc.h:56
char * install_version
Definition table_driven_lsc.h:57
static gchar * make_table_driven_lsc_info_json_str(const char *scan_id, const char *ip_str, const char *hostname, const char *os_release, const char *package_list)
Build a json object with data necessary to start a table drive LSC.
Definition table_driven_lsc.c:102
static void advisory_free(advisory_t *notus_advisory)
Free()'s an notus_advisory.
Definition table_driven_lsc.c:545
static JsonBuilder * add_packages_str_to_list(JsonBuilder *builder, const gchar *packages)
Split the package list string and creates a json array.
Definition table_driven_lsc.c:65
static advisory_t * advisory_new(char *oid)
Initialize a new notus_advisory.
Definition table_driven_lsc.c:496
static notus_info_t init_notus_info(const char *server)
Initialize a notus info struct and stores the server URL.
Definition table_driven_lsc.c:261
static void free_notus_info(notus_info_t notusdata)
Free notus info structure.
Definition table_driven_lsc.c:276
int run_table_driven_lsc(const char *scan_id, const char *ip_str, const char *hostname, const char *package_list, const char *os_release)
Publish the necessary data to start a Table driven LSC scan.
Definition table_driven_lsc.c:1136
static long send_request(notus_info_t notusdata, const char *os, const char *pkg_list, char **response)
Send a request to the server.
Definition table_driven_lsc.c:902
char * lsc_get_response(const char *pkg_list, const char *os)
Sent the installed package list and OS to notus.
Definition table_driven_lsc.c:997
static int parse_server(notus_info_t *notusdata)
Parse the server URL.
Definition table_driven_lsc.c:359
static advisories_t * advisories_new_notus()
Initialize a new advisories struct with 100 slots.
Definition table_driven_lsc.c:436
static void advisories_add(advisories_t *advisories_list, advisory_t *notus_advisory)
Initialize a new advisories struct with 100 slots.
Definition table_driven_lsc.c:472
static advisories_t * advisories_new_skiron()
Initialize a new advisories struct with 100 slots.
Definition table_driven_lsc.c:453
static gchar * make_package_list_as_json_str(const char *packages)
Build a json array from the package list to start a table drive LSC.
Definition table_driven_lsc.c:312
int lsc_has_run(void)
Get lsc_flag value.
Definition table_driven_lsc.c:48
static gchar * get_status_of_table_driven_lsc_from_json(const char *scan_id, const char *host_ip, const char *json, int len)
Get the status of table driven lsc from json object.
Definition table_driven_lsc.c:175
static size_t response_callback_fn(void *ptr, size_t size, size_t nmemb, void *struct_string)
Call back function to stored the response.
Definition table_driven_lsc.c:873
static vuln_pkg_t * vulnerable_pkg_new(const char *pkg_name, const char *install_version, enum fixed_type type, char *item1, char *item2)
Creates a new Vulnerable packages which belongs to an notus_advisory.
Definition table_driven_lsc.c:603
static int call_rs_notus(const char *ip_str, const char *hostname, const char *pkg_list, const char *os)
Call notus and stores the results.
Definition table_driven_lsc.c:1045
static advisories_t * lsc_process_response_skiron(JsonReader *reader)
Definition table_driven_lsc.c:764
static char * help_tolower(char *s)
helper function to lower case
Definition table_driven_lsc.c:295
struct notus_info * notus_info_t
Definition table_driven_lsc.c:252
void set_lsc_flag(void)
Set lsc_flag to 1.
Definition table_driven_lsc.c:40
static skiron_advisory_t * skiron_advisory_new(char *oid, char *message)
Definition table_driven_lsc.c:506
void advisories_free(advisories_t *advisories)
Free()'s an advisories.
Definition table_driven_lsc.c:578
static int lsc_flag
LSC ran or didn't 0 didn't run. 1 ran.
Definition table_driven_lsc.c:35
static void init_string(struct string *s)
Initialize the string struct to hold the response.
Definition table_driven_lsc.c:855
static void advisory_add_vuln_pkg(advisory_t *adv, vuln_pkg_t *vuln)
Add a new vulnerability to the notus_advisory.
Definition table_driven_lsc.c:524
advisories_t * lsc_process_response(const gchar *resp, const size_t len)
Process a json object which contains advisories and vulnerable packages.
Definition table_driven_lsc.c:806
static advisories_t * lsc_process_response_notus(JsonReader *reader)
Definition table_driven_lsc.c:633
struct advisories advisories_t
Definition table_driven_lsc.h:100
fixed_type
Fixed version format.
Definition table_driven_lsc.h:20
@ SINGLE
Definition table_driven_lsc.h:23
@ RANGE
Definition table_driven_lsc.h:22
@ UNKNOWN
Definition table_driven_lsc.h:21
struct notus_advisory advisory_t
Definition table_driven_lsc.h:77
struct fixed_version fixed_version_t
Definition table_driven_lsc.h:41
struct skiron_advisory skiron_advisory_t
Definition table_driven_lsc.h:85
struct vulnerable_pkg vuln_pkg_t
Definition table_driven_lsc.h:66
struct version_range version_range_t
Definition table_driven_lsc.h:50
@ NOTUS
Definition table_driven_lsc.h:28
@ SKIRON
Definition table_driven_lsc.h:29