OpenVAS Scanner 23.29.0
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
443 return advisories_list;
444}
445
453static void
455{
456 // Reallocate more memory if the list is full
457 if (advisories_list->count == advisories_list->max_size)
458 {
459 advisories_list->max_size *= 2;
460 advisories_list->advisories =
461 g_realloc_n (advisories_list->advisories, advisories_list->max_size,
462 sizeof (advisory_t));
463 memset (advisories_list->advisories + advisories_list->count, '\0',
464 (advisories_list->max_size - advisories_list->count)
465 * sizeof (advisory_t *));
466 }
467 advisories_list->advisories[advisories_list->count] = advisory;
468 advisories_list->count++;
469}
470
477static advisory_t *
479{
480 advisory_t *adv = NULL;
481 adv = g_malloc0 (sizeof (advisory_t));
482 adv->oid = g_strdup (oid);
483 adv->count = 0;
484 return adv;
485}
486
495static void
497{
498 if (adv->count == 100)
499 {
500 g_warning ("%s: Failed adding new vulnerable package to the advisory %s. "
501 "No more free slots",
502 __func__, adv->oid);
503 return;
504 }
505
506 adv->pkgs[adv->count] = vuln;
507 adv->count++;
508}
509
515static void
517{
518 if (advisory == NULL)
519 return;
520
521 g_free (advisory->oid);
522 for (size_t i = 0; i < advisory->count; i++)
523 {
524 if (advisory->pkgs[i] != NULL)
525 {
526 g_free (advisory->pkgs[i]->pkg_name);
527 g_free (advisory->pkgs[i]->install_version);
528 if (advisory->pkgs[i]->type == RANGE)
529 {
530 g_free (advisory->pkgs[i]->range->start);
531 g_free (advisory->pkgs[i]->range->stop);
532 }
533 else if (advisory->pkgs[i]->type == SINGLE)
534 {
535 g_free (advisory->pkgs[i]->version->version);
536 g_free (advisory->pkgs[i]->version->specifier);
537 }
538 }
539 }
540 advisory = NULL;
541}
542
548void
550{
551 if (advisories == NULL)
552 return;
553
554 for (size_t i = 0; i < advisories->count; i++)
556 advisories = NULL;
557}
558
573static vuln_pkg_t *
574vulnerable_pkg_new (const char *pkg_name, const char *install_version,
575 enum fixed_type type, char *item1, char *item2)
576{
577 vuln_pkg_t *vuln = NULL;
578 version_range_t *range = NULL;
579 fixed_version_t *fixed_ver = NULL;
580
581 vuln = g_malloc0 (sizeof (vuln_pkg_t));
582 vuln->pkg_name = g_strdup (pkg_name);
583 vuln->install_version = g_strdup (install_version);
584 vuln->type = type;
585 if (type == RANGE)
586 {
587 range = g_malloc0 (sizeof (range_t));
588 range->start = g_strdup (item1);
589 range->stop = g_strdup (item2);
590 vuln->range = range;
591 }
592 else
593 {
594 fixed_ver = g_malloc0 (sizeof (fixed_version_t));
595 fixed_ver->version = g_strdup (item1);
596 fixed_ver->specifier = g_strdup (item2);
597 vuln->version = fixed_ver;
598 }
599
600 return vuln;
601}
602
616process_notus_response (const gchar *resp, const size_t len)
617{
618 JsonParser *parser = NULL;
619 JsonReader *reader = NULL;
620 GError *err = NULL;
621
623
624 parser = json_parser_new ();
625 if (!json_parser_load_from_data (parser, resp, len, &err))
626 {
627 g_message ("Error parsing");
628 }
629
630 reader = json_reader_new (json_parser_get_root (parser));
631
632 if (!json_reader_is_object (reader))
633 {
634 g_debug ("It is not an object");
635 goto cleanup_advisories;
636 }
637
638 char **members = json_reader_list_members (reader);
639
640 for (int i = 0; members[i]; i++)
641 {
643
644 if (!json_reader_read_member (reader, members[i]))
645 {
646 g_debug ("No member oid");
647 goto cleanup_advisories;
648 }
649 if (!json_reader_is_array (reader))
650 {
651 g_debug ("Is not an array");
652 goto cleanup_advisories;
653 }
654
655 advisory = advisory_new (g_strdup (members[i]));
656
657 int count_pkgs = json_reader_count_elements (reader);
658 g_debug ("There are %d packages for advisory %s", count_pkgs, members[i]);
659 for (int j = 0; j < count_pkgs; j++)
660 {
661 vuln_pkg_t *pkg = NULL;
662 char *name = NULL;
663 char *installed_version = NULL;
664 char *start = NULL;
665 char *stop = NULL;
666 char *version = NULL;
667 char *specifier = NULL;
668 enum fixed_type type = UNKNOWN;
669
670 json_reader_read_element (reader, j);
671 if (!json_reader_is_object (reader))
672 {
673 g_warning ("%s: Package %d of advisory %s is not an object",
674 __func__, j, members[i]);
676 goto cleanup_advisories;
677 }
678
679 json_reader_read_member (reader, "name");
680 name = g_strdup (json_reader_get_string_value (reader));
681 json_reader_end_member (reader);
682 g_debug ("name: %s", name);
683
684 json_reader_read_member (reader, "installed_version");
685 installed_version = g_strdup (json_reader_get_string_value (reader));
686 json_reader_end_member (reader);
687 g_debug ("installed_version: %s", installed_version);
688
689 json_reader_read_member (reader, "fixed_version");
690 g_debug ("Fixed_version has %d members",
691 json_reader_count_members (reader));
692
693 // Version Range
694 json_reader_read_member (reader, "start");
695 start = g_strdup (json_reader_get_string_value (reader));
696 json_reader_end_member (reader);
697 json_reader_read_member (reader, "end");
698 stop = g_strdup (json_reader_get_string_value (reader));
699 json_reader_end_member (reader);
700 g_debug ("start %s, end: %s", start, stop);
701
702 // version and specifier
703 json_reader_read_member (reader, "version");
704 version = g_strdup (json_reader_get_string_value (reader));
705 json_reader_end_member (reader);
706 json_reader_read_member (reader, "specifier");
707 specifier = g_strdup (json_reader_get_string_value (reader));
708 json_reader_end_member (reader);
709 g_debug ("version %s, specifier: %s", version, specifier);
710
711 // end read fixes version member
712 json_reader_end_member (reader);
713
714 // end package element
715 json_reader_end_element (reader);
716
717 char *item1 = NULL, *item2 = NULL;
718 if (start && stop)
719 {
720 type = RANGE;
721 item1 = start;
722 item2 = stop;
723 }
724 else if (version && specifier)
725 {
726 type = SINGLE;
727 item1 = version;
728 item2 = specifier;
729 }
730 else
731 {
732 g_warning ("%s: Error parsing json element", __func__);
733 g_free (name);
734 g_free (installed_version);
735 g_free (item1);
736 g_free (item2);
738 goto cleanup_advisories;
739 }
740
741 pkg =
742 vulnerable_pkg_new (name, installed_version, type, item1, item2);
743 g_free (name);
744 g_free (installed_version);
745 g_free (item1);
746 g_free (item2);
747
749 }
750 // end advisory
751 json_reader_end_member (reader);
753 }
754
755cleanup_advisories:
756 if (reader)
757 g_object_unref (reader);
758 g_object_unref (parser);
759
760 return advisories;
761}
762
765struct string
766{
767 char *ptr;
768 size_t len;
769};
770
775static void
776init_string (struct string *s)
777{
778 s->len = 0;
779 s->ptr = g_malloc0 (s->len + 1);
780 if (s->ptr == NULL)
781 {
782 g_warning ("%s: Error allocating memory for response", __func__);
783 return;
784 }
785 s->ptr[0] = '\0';
786}
787
793static size_t
794response_callback_fn (void *ptr, size_t size, size_t nmemb, void *struct_string)
795{
796 struct string *s = struct_string;
797 size_t new_len = s->len + size * nmemb;
798 char *ptr_aux = g_realloc (s->ptr, new_len + 1);
799 s->ptr = ptr_aux;
800 if (s->ptr == NULL)
801 {
802 g_warning ("%s: Error allocating memory for response", __func__);
803 return 0; // no memory left
804 }
805 memcpy (s->ptr + s->len, ptr, size * nmemb);
806 s->ptr[new_len] = '\0';
807 s->len = new_len;
808
809 return size * nmemb;
810}
811
822static long
823send_request (notus_info_t notusdata, const char *os, const char *pkg_list,
824 char **response)
825{
826 CURL *curl;
827 GString *url = NULL;
828 long http_code = -1;
829 struct string resp;
830 struct curl_slist *customheader = NULL;
831 char *os_aux;
832 GString *xapikey = NULL;
833
834 if ((curl = curl_easy_init ()) == NULL)
835 {
836 g_warning ("Not possible to initialize curl library");
837 return http_code;
838 }
839
840 url = g_string_new (notusdata->server);
841 g_string_append (url, "/notus/");
842
843 //
844 os_aux = help_tolower (g_strdup (os));
845 for (size_t i = 0; i < strlen (os_aux); i++)
846 {
847 if (os_aux[i] == ' ')
848 os_aux[i] = '_';
849 }
850
851 g_string_append (url, os_aux);
852 g_free (os_aux);
853
854 g_debug ("%s: URL: %s", __func__, url->str);
855 // Set URL
856 if (curl_easy_setopt (curl, CURLOPT_URL, g_strdup (url->str)) != CURLE_OK)
857 {
858 g_warning ("Not possible to set the URL");
859 curl_easy_cleanup (curl);
860 return http_code;
861 }
862 g_string_free (url, TRUE);
863
864 // Accept an insecure connection. Don't verify the server certificate
865 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
866 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
867
868 // Set API KEY
869 if (prefs_get ("x-apikey"))
870 {
871 xapikey = g_string_new ("X-APIKEY: ");
872 g_string_append (xapikey, prefs_get ("x-apikey"));
873 customheader = curl_slist_append (customheader, g_strdup (xapikey->str));
874 g_string_free (xapikey, TRUE);
875 }
876 // SET Content type
877 customheader =
878 curl_slist_append (customheader, "Content-Type: application/json");
879 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, customheader);
880 // Set body
881 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, pkg_list);
882 curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, strlen (pkg_list));
883
884 // Init the struct where the response is stored and set the callback function
885 init_string (&resp);
886 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, response_callback_fn);
887 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &resp);
888
889 int ret = CURLE_OK;
890 if ((ret = curl_easy_perform (curl)) != CURLE_OK)
891 {
892 g_warning ("%s: Error sending request: %d", __func__, ret);
893 curl_easy_cleanup (curl);
894 g_free (resp.ptr);
895 return http_code;
896 }
897
898 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
899
900 curl_easy_cleanup (curl);
901 g_debug ("Server response %s", resp.ptr);
902 *response = g_strdup (resp.ptr);
903 g_free (resp.ptr);
904 // already free()'ed with curl_easy_cleanup().
905
906 return http_code;
907}
908
917char *
918notus_get_response (const char *pkg_list, const char *os)
919{
920 const char *server = NULL;
921 char *json_pkglist;
922 char *response = NULL;
923 notus_info_t notusdata;
924 long ret;
925
926 // Parse the server and get the port, host, schema
927 // and necessary information to build the message
928 server = prefs_get ("openvasd_server");
929 notusdata = init_notus_info (server);
930
931 if (parse_server (&notusdata) < 0)
932 {
933 free_notus_info (notusdata);
934 return NULL;
935 }
936
937 // Convert the package list string into a string containing json
938 // array of packages
939 if ((json_pkglist = make_package_list_as_json_str (pkg_list)) == NULL)
940 {
941 free_notus_info (notusdata);
942 return NULL;
943 }
944
945 ret = send_request (notusdata, os, json_pkglist, &response);
946 if (ret != 200)
947 g_warning ("%ld: Error sending request to openvasd: %s", ret, response);
948
949 free_notus_info (notusdata);
950 g_free (json_pkglist);
951
952 return response;
953}
954
965static int
966call_rs_notus (const char *ip_str, const char *hostname, const char *pkg_list,
967 const char *os)
968{
969 char *body = NULL;
970 advisories_t *advisories = NULL;
971 int res_count = 0;
972 if ((body = notus_get_response (pkg_list, os)) == NULL)
973 return -1;
974
975 advisories = process_notus_response (body, strlen (body));
976
977 // Process the advisories, generate results and store them in the kb
978 for (size_t i = 0; i < advisories->count; i++)
979 {
981 gchar *buffer;
982 GString *result = g_string_new (NULL);
983 for (size_t j = 0; j < advisory->count; j++)
984 {
985 vuln_pkg_t *pkg = advisory->pkgs[j];
986 GString *res = g_string_new (NULL);
987
988 if (pkg->type == RANGE)
989 {
990 g_string_printf (res,
991 "\n"
992 "Vulnerable package: %s\n"
993 "Installed version: %s-%s\n"
994 "Fixed version: < %s-%s\n"
995 "Fixed version: >=%s-%s\n",
996 pkg->pkg_name, pkg->pkg_name,
997 pkg->install_version, pkg->pkg_name,
998 pkg->range->start, pkg->pkg_name,
999 pkg->range->stop);
1000 }
1001 else if (pkg->type == SINGLE)
1002 {
1003 g_string_printf (res,
1004 "\n"
1005 "Vulnerable package: %s\n"
1006 "Installed version: %s-%s\n"
1007 "Fixed version: %2s%s-%s\n",
1008 pkg->pkg_name, pkg->pkg_name,
1009 pkg->install_version, pkg->version->specifier,
1010 pkg->pkg_name, pkg->version->version);
1011 }
1012 else
1013 {
1014 g_warning ("%s: Unknown fixed version type for advisory %s",
1015 __func__, advisory->oid);
1016 g_string_free (result, TRUE);
1018 return -1;
1019 }
1020 g_string_append (result, res->str);
1021 g_string_free (res, TRUE);
1022 }
1023
1024 // type|||IP|||HOSTNAME|||package|||OID|||the result message|||URI
1025 buffer = g_strdup_printf ("%s|||%s|||%s|||%s|||%s|||%s|||%s", "ALARM",
1026 ip_str, hostname ? hostname : " ", "package",
1027 advisory->oid, result->str, "");
1028 g_string_free (result, TRUE);
1029 kb_item_push_str_with_main_kb_check (get_main_kb (), "internal/results",
1030 buffer);
1031 res_count++;
1032 g_free (buffer);
1033 }
1034
1036 return res_count;
1037}
1038
1039#endif // End RSNOTUS
1040
1056int
1057run_table_driven_lsc (const char *scan_id, const char *ip_str,
1058 const char *hostname, const char *package_list,
1059 const char *os_release)
1060{
1061 int err = 0;
1062 if (!os_release || !package_list)
1063 return 0;
1064
1065 if (prefs_get ("openvasd_server"))
1066 {
1067 g_message ("Running Notus for %s via openvasd", ip_str);
1068 err = call_rs_notus (ip_str, hostname, package_list, os_release);
1069
1070 return err;
1071 }
1072 else
1073 {
1074 gchar *json_str;
1075 gchar *topic;
1076 gchar *payload;
1077 gchar *status = NULL;
1078 int topic_len;
1079 int payload_len;
1080
1081 // Subscribe to status topic
1082 err = mqtt_subscribe ("scanner/status");
1083 if (err)
1084 {
1085 g_warning ("%s: Error starting lsc. Unable to subscribe", __func__);
1086 return -1;
1087 }
1088 /* Get the OS release. TODO: have a list with supported OS. */
1089
1091 os_release, package_list);
1092
1093 // Run table driven lsc
1094 if (json_str == NULL)
1095 return -1;
1096
1097 g_message ("Running Notus for %s", ip_str);
1098 err = mqtt_publish ("scanner/package/cmd/notus", json_str);
1099 if (err)
1100 {
1101 g_warning ("%s: Error publishing message for Notus.", __func__);
1102 g_free (json_str);
1103 return -1;
1104 }
1105
1106 g_free (json_str);
1107
1108 // Wait for Notus scanner to start or interrupt
1109 while (!status)
1110 {
1111 err = mqtt_retrieve_message (&topic, &topic_len, &payload,
1112 &payload_len, 60000);
1113 if (err == -1 || err == 1)
1114 {
1115 g_warning ("%s: Unable to retrieve status message from notus. %s",
1116 __func__, err == 1 ? "Timeout after 60 s." : "");
1117 return -1;
1118 }
1119
1120 // Get status if it belongs to corresponding scan and host
1121 // Else wait for next status message
1123 scan_id, ip_str, payload, payload_len);
1124
1125 g_free (topic);
1126 g_free (payload);
1127 }
1128 // If started wait for it to finish or interrupt
1129 if (!g_strcmp0 (status, "running"))
1130 {
1131 g_debug ("%s: table driven LSC with scan id %s successfully started "
1132 "for host %s",
1133 __func__, scan_id, ip_str);
1134 g_free (status);
1135 status = NULL;
1136 while (!status)
1137 {
1138 err = mqtt_retrieve_message (&topic, &topic_len, &payload,
1139 &payload_len, 60000);
1140 if (err == -1)
1141 {
1142 g_warning (
1143 "%s: Unable to retrieve status message from notus.",
1144 __func__);
1145 return -1;
1146 }
1147 if (err == 1)
1148 {
1149 g_warning (
1150 "%s: Unable to retrieve message. Timeout after 60s.",
1151 __func__);
1152 return -1;
1153 }
1154
1156 scan_id, ip_str, payload, payload_len);
1157 g_free (topic);
1158 g_free (payload);
1159 }
1160 }
1161 else
1162 {
1163 g_warning ("%s: Unable to start lsc. Got status: %s", __func__,
1164 status);
1165 g_free (status);
1166 return -1;
1167 }
1168
1169 if (g_strcmp0 (status, "finished"))
1170 {
1171 g_warning (
1172 "%s: table driven lsc with scan id %s did not finish successfully "
1173 "for host %s. Last status was %s",
1174 __func__, scan_id, ip_str, status);
1175 err = -1;
1176 }
1177 else
1178 g_debug ("%s: table driven lsc with scan id %s successfully finished "
1179 "for host %s",
1180 __func__, scan_id, ip_str);
1181 g_free (status);
1182 }
1183 return err;
1184}
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:439
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:75
size_t max_size
Definition table_driven_lsc.h:78
advisory_t ** advisories
Definition table_driven_lsc.h:76
size_t count
Definition table_driven_lsc.h:77
Definition table_driven_lsc.h:63
size_t count
Definition table_driven_lsc.h:67
char * oid
Definition table_driven_lsc.h:64
vuln_pkg_t * pkgs[100]
Definition table_driven_lsc.h:65
char * specifier
Definition table_driven_lsc.h:31
char * version
Definition table_driven_lsc.h:30
Host information, implemented as doubly linked list.
Definition hosts.c:37
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
Define a string struct for storing the response.
Definition table_driven_lsc.c:766
size_t len
Definition table_driven_lsc.c:768
char * ptr
Definition table_driven_lsc.c:767
char * stop
Definition table_driven_lsc.h:40
char * start
Definition table_driven_lsc.h:39
fixed_version_t * version
Definition table_driven_lsc.h:54
enum fixed_type type
Definition table_driven_lsc.h:50
version_range_t * range
Definition table_driven_lsc.h:53
char * pkg_name
Definition table_driven_lsc.h:48
char * install_version
Definition table_driven_lsc.h:49
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 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
char * notus_get_response(const char *pkg_list, const char *os)
Sent the installed package list and OS to notus.
Definition table_driven_lsc.c:918
static advisory_t * advisory_new(char *oid)
Initialize a new advisory.
Definition table_driven_lsc.c:478
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:1057
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:823
static int parse_server(notus_info_t *notusdata)
Parse the server URL.
Definition table_driven_lsc.c:359
static void advisories_add(advisories_t *advisories_list, advisory_t *advisory)
Initialize a new advisories struct with 100 slots.
Definition table_driven_lsc.c:454
advisories_t * process_notus_response(const gchar *resp, const size_t len)
Process a json object which contains advisories and vulnerable packages.
Definition table_driven_lsc.c:616
static advisories_t * advisories_new()
Initialize a new advisories struct with 100 slots.
Definition table_driven_lsc.c:436
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:794
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 advisory.
Definition table_driven_lsc.c:574
static void advisory_free(advisory_t *advisory)
Free()'s an advisory.
Definition table_driven_lsc.c:516
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:966
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
void advisories_free(advisories_t *advisories)
Free()'s an advisories.
Definition table_driven_lsc.c:549
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:776
static void advisory_add_vuln_pkg(advisory_t *adv, vuln_pkg_t *vuln)
Add a new vulnerability to the advisory.
Definition table_driven_lsc.c:496
struct advisories advisories_t
Definition table_driven_lsc.h:80
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 advisory advisory_t
Definition table_driven_lsc.h:70
struct fixed_version fixed_version_t
Definition table_driven_lsc.h:33
struct vulnerable_pkg vuln_pkg_t
Definition table_driven_lsc.h:58
struct version_range version_range_t
Definition table_driven_lsc.h:42