14#include "../misc/kb_cache.h"
15#include "../misc/network.h"
16#include "../misc/nvt_categories.h"
17#include "../misc/plugutils.h"
18#include "../misc/vendorversion.h"
25#include <gnutls/gnutls.h>
27#include <gvm/base/hosts.h>
28#include <gvm/base/networking.h>
29#include <gvm/base/nvti.h>
30#include <gvm/base/prefs.h>
31#include <gvm/util/kb.h>
32#include <libssh/libssh.h>
40#define MAP_FAILED ((void *) -1)
47#define G_LOG_DOMAIN "lib nasl"
55 fprintf (stderr,
"[%d] (%d) %s", getpid (), level, text);
56 if (*text && text[strlen (text) - 1] !=
'\n')
69 if (prefs_get_bool (
"test_empty_vhost"))
72 gvm_vhost_new (addr6_as_str (
ip), g_strdup (
"IP-address"));
92 printf (
"%s could not be loaded\n", infos->
name);
96 infos->
oid = g_strdup (nvti_oid (
nvti));
123 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
125 gcry_check_version (NULL);
126 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
127 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
128 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
129 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
143 static gchar *target = NULL;
144 gchar *default_target =
"127.0.0.1";
145 int mode = 0, err = 0, pos;
149 static gboolean display_version = FALSE;
150 static gboolean nasl_debug = FALSE;
151 static gboolean description_only = FALSE;
152 static gboolean both_modes = FALSE;
153 static gboolean parse_only = FALSE;
154 static gboolean do_lint = FALSE;
155 static gchar *trace_file = NULL;
156 static gchar *config_file = NULL;
157 static gchar *source_iface = NULL;
158 static gchar *port_range = NULL;
159 static gboolean with_safe_checks = FALSE;
160 static gboolean signing_mode = FALSE;
161 static gchar *include_dir = NULL;
162 static gchar **nasl_filenames = NULL;
163 static gchar **kb_values = NULL;
164 static int debug_tls = 0;
165 GError *error = NULL;
166 GOptionContext *option_context;
167 static GOptionEntry entries[] = {
168 {
"version",
'V', 0, G_OPTION_ARG_NONE, &display_version,
169 "Display version information", NULL},
170 {
"debug",
'd', 0, G_OPTION_ARG_NONE, &nasl_debug,
171 "Output debug information to stderr.", NULL},
172 {
"description",
'D', 0, G_OPTION_ARG_NONE, &description_only,
173 "Only run the 'description' part of the script", NULL},
174 {
"both",
'B', 0, G_OPTION_ARG_NONE, &both_modes,
175 "Run in description mode before running the script.", NULL},
176 {
"parse",
'p', 0, G_OPTION_ARG_NONE, &parse_only,
177 "Only parse the script, don't execute it", NULL},
178 {
"lint",
'L', 0, G_OPTION_ARG_NONE, &do_lint,
179 "'lint' the script (extended checks)", NULL},
180 {
"target",
't', 0, G_OPTION_ARG_STRING, &target,
181 "Execute the scripts against <target>",
"<target>"},
182 {
"trace",
'T', 0, G_OPTION_ARG_FILENAME, &trace_file,
183 "Log actions to <file> (or '-' for stderr)",
"<file>"},
184 {
"config-file",
'c', 0, G_OPTION_ARG_FILENAME, &config_file,
185 "Configuration file",
"<filename>"},
186 {
"source-iface",
'e', 0, G_OPTION_ARG_STRING, &source_iface,
187 "Source network interface for established connections.",
"<iface_name>"},
188 {
"safe",
's', 0, G_OPTION_ARG_NONE, &with_safe_checks,
189 "Specifies that the script should be run with 'safe checks' enabled",
191 {
"disable-signing",
'X', 0, G_OPTION_ARG_NONE, &signing_mode,
192 "Run the script with disabled signature verification", NULL},
193 {
"include-dir",
'i', 0, G_OPTION_ARG_STRING, &include_dir,
194 "Search for includes in <dir>",
"<dir>"},
195 {
"debug-tls", 0, 0, G_OPTION_ARG_INT, &debug_tls,
196 "Enable TLS debugging at <level>",
"<level>"},
197 {
"kb",
'k', 0, G_OPTION_ARG_STRING_ARRAY, &kb_values,
198 "Set KB key to value. Can be used multiple times",
"<key=value>"},
199 {
"port-range",
'r', 0, G_OPTION_ARG_STRING, &port_range,
200 "Set the <port-range> used by nasl scripts. ",
"<port-range>"},
201 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &nasl_filenames,
202 "Absolute path to one or more nasl scripts",
"NASL_FILE..."},
203 {NULL, 0, 0, 0, NULL, NULL, NULL}};
206 g_option_context_new (
"- standalone NASL interpreter for OpenVAS");
207 g_option_context_add_main_entries (option_context, entries, NULL);
208 if (!g_option_context_parse (option_context, &argc, &argv, &error))
210 g_print (
"%s\n\n", error->message);
213 g_option_context_free (option_context);
223 printf (
"gnutls %s\n", gnutls_check_version (NULL));
224 printf (
"libssh %s\n", ssh_version (0));
225 printf (
"gpgme %s\n", gpgme_check_version (NULL));
229 printf (
"Copyright (C) 2002 - 2004 Tenable Network Security\n");
230 printf (
"Copyright (C) 2022 Greenbone Networks GmbH\n\n");
238 if (description_only)
246 if (!strcmp (trace_file,
"-"))
250 FILE *fp = fopen (trace_file,
"w");
256 setvbuf (fp, NULL, _IOLBF, BUFSIZ);
265 fprintf (stderr,
"Error. No input file(s) specified !\n");
271 fprintf (stderr,
"** WARNING : packet forgery will not work\n");
272 fprintf (stderr,
"** as NASL is not running as root\n");
274 signal (SIGPIPE, SIG_IGN);
276 if (source_iface && gvm_source_iface_init (source_iface))
278 fprintf (stderr,
"Erroneous network source interface: %s\n",
285 gnutls_global_set_log_level (debug_tls);
289 target = g_strdup (default_target);
291 hosts = gvm_hosts_new (target);
294 fprintf (stderr,
"Erroneous target %s\n", target);
297 unresolved = gvm_hosts_resolve (
hosts);
300 g_warning (
"Couldn't resolve hostname '%s'", (
char *) unresolved->data);
301 unresolved = unresolved->next;
303 g_slist_free_full (unresolved, g_free);
308 if (include_dir != NULL)
313 prefs_config (config_file ? config_file : OPENVAS_CONF);
315 if (prefs_get (
"vendor_version") != NULL)
318 if (port_range != NULL)
320 prefs_set (
"port_range", port_range);
324 if (with_safe_checks)
325 prefs_set (
"safe_checks",
"yes");
334 if (prefs_get_bool (
"expand_vhosts"))
335 gvm_host_add_reverse_lookup (
host);
336 gvm_vhosts_exclude (
host, prefs_get (
"exclude_hosts"));
337 gvm_host_get_addr6 (
host, &ip6);
338 rc = kb_new (&kb, prefs_get (
"db_address") ? prefs_get (
"db_address")
345 for (
int i = 0; nasl_filenames[i] != NULL; i++)
348 if (both_modes || with_safe_checks)
356 else if (with_safe_checks
359 printf (
"%s isn't safe\n", nasl_filenames[i]);
368 gchar **kb_values_aux = kb_values;
369 while (*kb_values_aux)
371 gchar **splits = g_strsplit (*kb_values_aux,
"=", -1);
372 if (splits[2] || !splits[1])
374 fprintf (stderr,
"Erroneous --kb entry %s\n",
378 kb_item_add_str_unique (kb, splits[0], splits[1], 0, pos);
395 gvm_hosts_free (
hosts);
int exec_nasl_script(struct script_infos *script_infos, int mode)
Execute a NASL script.
static struct host * hosts
void set_main_kb(kb_t kb)
sets the shared database between ospd and openvas as a main_kb for further usage. @description this s...
char * nasl_version(void)
static nvti_t * parse_script_infos(struct script_infos *infos)
static struct script_infos * init(struct in6_addr *ip, GSList *vhosts, kb_t kb)
static int nvti_category_is_safe(int category)
Checks that an NVT category is safe.
static void my_gnutls_log_func(int level, const char *text)
int main(int argc, char **argv)
Main of the standalone nasl interpreter.
static void gcrypt_init()
Initialize Gcrypt.
#define NASL_ALWAYS_SIGNED
#define NASL_EXEC_PARSE_ONLY
#define NASL_COMMAND_LINE
int add_nasl_inc_dir(const char *)
Adds the given string as directory for searching for includes.
int openvas_SSL_init()
Initializes SSL support.
Host information, implemented as doubly linked list.
struct scan_globals * globals
void vendor_version_set(const gchar *version)
Set vendor version.