Attack a whole network.
1249 int max_hosts = 0, max_checks;
1250 const char *hostlist;
1253 int fork_retries = 0;
1257 const gchar *port_range;
1258 int allow_simultaneous_ips;
1265 gboolean test_alive_hosts_only = prefs_get_bool (
"test_alive_hosts_only");
1266 gvm_hosts_t *alive_hosts_list = NULL;
1267 kb_t alive_hosts_kb = NULL;
1268 if (test_alive_hosts_only)
1271 gettimeofday (&then, NULL);
1277 hostlist = prefs_get (
"TARGET");
1278 if (hostlist == NULL)
1284 port_range = prefs_get (
"port_range");
1285 if (validate_port_range (port_range))
1289 main_kb,
"Invalid port list. Ports must be in the range [1-65535]",
1290 NULL, NULL,
"ERRMSG");
1292 g_warning (
"Invalid port list. Ports must be in the range [1-65535]. "
1293 "Scan terminated.");
1300 int plugins_init_error = 0;
1302 prefs_get_bool (
"auto_enable_dependencies"),
1303 &plugins_init_error);
1306 g_message (
"Couldn't initialize the plugin scheduler");
1310 if (plugins_init_error > 0)
1313 "%d errors were found during the plugin scheduling. "
1314 "Some plugins have not been launched.",
1315 plugins_init_error);
1325 hosts = gvm_hosts_new (hostlist);
1329 buffer = g_strdup_printf (
"Invalid target list: %s.", hostlist);
1338 g_warning (
"Invalid target list. Scan terminated.");
1342 unresolved = gvm_hosts_resolve (
hosts);
1345 g_warning (
"Couldn't resolve hostname '%s'", (
char *) unresolved->data);
1346 unresolved = unresolved->next;
1348 g_slist_free_full (unresolved, g_free);
1353 int already_excluded = 0;
1356 #ifdef FEATURE_HOSTS_ALLOWED_ONLY
1360 apply_hosts_allow_deny (
hosts);
1365 sprintf (buf,
"%d", gvm_hosts_count (
hosts) + already_excluded);
1378 g_message (
"Vulnerability scan %s started: Target has %d hosts: "
1379 "%s, with max_hosts = %d and max_checks = %d",
1380 globals->scan_id, gvm_hosts_count (
hosts), hostlist, max_hosts,
1383 if (test_alive_hosts_only)
1386 gboolean ad_finished = FALSE;
1389 struct in6_addr tmpaddr;
1393 err = pthread_create (&tid, NULL, start_alive_detection, (
void *)
hosts);
1396 "%s: pthread_create() returned EAGAIN: Insufficient resources "
1397 "to create thread.",
1400 g_debug (
"%s: started alive detection.", __func__);
1402 for (
host = get_host_from_queue (alive_hosts_kb, &ad_finished);
1404 host = get_host_from_queue (alive_hosts_kb, &ad_finished))
1409 if (gvm_host_get_addr6 (
host, &tmpaddr) == 0)
1414 "%s: Get first host to test from Queue. This host is used for "
1415 "initialising the alive_hosts_list.",
1418 alive_hosts_list = gvm_hosts_new (gvm_host_value_str (
host));
1424 allow_simultaneous_ips = prefs_get_bool (
"allow_simultaneous_ips");
1432 if (!test_alive_hosts_only
1437 gvm_hosts_move_current_host_to_end (
hosts);
1444 rc = kb_new (&arg_host_kb, prefs_get (
"db_address"));
1445 if (rc < 0 && rc != -2)
1459 host_str = gvm_host_value_str (
host);
1463 kb_delete (arg_host_kb);
1470 kb_delete (arg_host_kb);
1478 args.host_kb = arg_host_kb;
1489 g_warning (
"fork() failed - %s. %s won't be tested",
1490 strerror (errno), host_str);
1495 g_debug (
"fork() failed - "
1496 "sleeping %d seconds and trying again...",
1503 if (test_alive_hosts_only)
1505 struct in6_addr tmpaddr;
1506 gvm_host_t *alive_buf;
1511 gboolean ad_finished = FALSE;
1512 for (
host = get_host_from_queue (alive_hosts_kb, &ad_finished);
1514 host = get_host_from_queue (alive_hosts_kb, &ad_finished))
1519 if (
host && !allow_simultaneous_ips
1522 struct in6_addr hostip;
1523 char ip_str[INET6_ADDRSTRLEN];
1526 gvm_host_get_addr6 (
host, &hostip);
1527 addr6_to_str (&hostip, ip_str);
1531 flag_set = finish_signal_on_queue (alive_hosts_kb);
1533 put_host_on_queue (alive_hosts_kb, ip_str);
1534 g_debug (
"Reallocating the host %s at the end of the queue",
1537 gvm_host_free (
host);
1542 g_debug (
"Reallocating finish signal in the host queue");
1543 realloc_finish_signal_on_queue (alive_hosts_kb);
1550 if (
host && gvm_host_get_addr6 (
host, &tmpaddr) == 0)
1554 gvm_host_free (alive_buf);
1559 gvm_hosts_add (alive_hosts_list, gvm_duplicate_host (
host));
1561 g_debug (
"%s: got NULL host, stop/finish scan", __func__);
1574 killpg (getpid (), SIGUSR1);
1576 g_debug (
"Test complete");
1580 files = globals->files_translation;
1582 g_hash_table_destroy (files);
1586 if (test_alive_hosts_only)
1591 kb_lnk_reset (alive_hosts_kb);
1592 g_debug (
"%s: free alive detection data ", __func__);
1595 g_debug (
"%s: waiting for alive detection thread to be finished...",
1600 g_debug (
"%s: pthread_join() returned EDEADLK.", __func__);
1602 g_debug (
"%s: pthread_join() returned EINVAL.", __func__);
1604 g_debug (
"%s: pthread_join() returned ESRCH.", __func__);
1605 if (retval == PTHREAD_CANCELED)
1606 g_debug (
"%s: pthread_join() returned PTHREAD_CANCELED.", __func__);
1610 g_debug (
"%s: Finished waiting for alive detection thread.", __func__);
1615 gettimeofday (&now, NULL);
1616 if (test_alive_hosts_only)
1617 g_message (
"Vulnerability scan %s finished in %ld seconds: "
1618 "%d alive hosts of %d",
1619 globals->scan_id, now.tv_sec - then.tv_sec,
1620 gvm_hosts_count (alive_hosts_list), gvm_hosts_count (
hosts));
1622 g_message (
"Vulnerability scan %s finished in %ld seconds: %d hosts",
1623 globals->scan_id, now.tv_sec - then.tv_sec,
1624 gvm_hosts_count (
hosts));
1626 gvm_hosts_free (
hosts);
1627 if (alive_hosts_list)
1628 gvm_hosts_free (alive_hosts_list);