Attack a whole network. return 0 on successes, -1 if there was a critical error.
1170{
1171 int max_hosts = 0, max_checks;
1172 const char *hostlist;
1175 int fork_retries = 0;
1178 const gchar *port_range;
1179 int allow_simultaneous_ips;
1181 GSList *unresolved;
1182 char buf[96];
1183 int error = 0;
1184
1186
1187 gboolean test_alive_hosts_only = prefs_get_bool ("test_alive_hosts_only");
1188 gvm_hosts_t *alive_hosts_list = NULL;
1189 kb_t alive_hosts_kb = NULL;
1190 if (test_alive_hosts_only)
1192
1193 gettimeofday (&then, NULL);
1194
1196 {
1197 error = -1;
1198 return error;
1199 }
1200
1201#ifdef FEATURE_HOST_DISCOVERY_IPV6
1202 alive_test_t alive_test;
1203 const char *target_aux = prefs_get ("TARGET");
1204 char *host_found = "";
1205
1206 get_alive_test_methods (&alive_test);
1207 if (alive_test == 32)
1208 {
1209 int print_results = 0;
1210 run_cli_for_ipv6_network (target_aux, &host_found, print_results);
1211 hostlist = host_found;
1212
1213 prefs_set("ALIVE_TEST", "8");
1214 }
1215 else
1216#endif
1217 {
1218 hostlist = prefs_get ("TARGET");
1219 }
1220
1221 if (hostlist == NULL)
1222 {
1223 error = -1;
1224 return error;
1225 }
1226
1227
1228 port_range = prefs_get ("port_range");
1229 if (validate_port_range (port_range))
1230 {
1233 main_kb,
"Invalid port list. Ports must be in the range [1-65535]",
1234 NULL, NULL, "ERRMSG");
1236 g_warning ("Invalid port list. Ports must be in the range [1-65535]. "
1237 "Scan terminated.");
1239
1240 error = -1;
1241 return error;
1242 }
1243
1244
1245 int plugins_init_error = 0;
1247 prefs_get_bool ("auto_enable_dependencies"),
1248 &plugins_init_error);
1249 if (!sched)
1250 {
1251 g_message ("Couldn't initialize the plugin scheduler");
1252
1253 error = -1;
1254 return error;
1255 }
1256
1257 if (plugins_init_error > 0)
1258 {
1259 sprintf (buf,
1260 "%d errors were found during the plugin scheduling. "
1261 "Some plugins have not been launched.",
1262 plugins_init_error);
1263
1267 }
1268
1271
1272 hosts = gvm_hosts_new (hostlist);
1274 {
1275 char *buffer;
1276 buffer = g_strdup_printf ("Invalid target list: %s.", hostlist);
1279 g_free (buffer);
1280
1281
1283 "HOSTS_COUNT");
1285 g_warning ("Invalid target list. Scan terminated.");
1286
1287 error = -1;
1288 goto stop;
1289 }
1290
1291 unresolved = gvm_hosts_resolve (
hosts);
1292 while (unresolved)
1293 {
1294 g_warning ("Couldn't resolve hostname '%s'", (char *) unresolved->data);
1295 unresolved = unresolved->next;
1296 }
1297 g_slist_free_full (unresolved, g_free);
1298
1299
1301
1302 int already_excluded = 0;
1304
1305#ifdef FEATURE_HOSTS_ALLOWED_ONLY
1306
1307
1308
1309 apply_hosts_allow_deny (
hosts);
1310#endif
1311
1312
1314
1315
1316
1317 sprintf (buf, "%d", exc + already_excluded);
1321
1322
1323
1324 sprintf (buf,
"%d", gvm_hosts_count (
hosts));
1328
1331 goto stop;
1332
1334
1335 g_message ("Vulnerability scan %s started: Target has %d hosts: "
1336 "%s, with max_hosts = %d and max_checks = %d",
1337 globals->
scan_id, gvm_hosts_count (
hosts), hostlist, max_hosts,
1338 max_checks);
1339
1340 if (test_alive_hosts_only)
1341 {
1342
1343 gboolean ad_finished = FALSE;
1344 int err;
1345 pthread_t tid;
1346 struct in6_addr tmpaddr;
1347
1348
1350 err = pthread_create (&tid, NULL, start_alive_detection, (
void *)
hosts);
1351 if (err == EAGAIN)
1352 g_warning (
1353 "%s: pthread_create() returned EAGAIN: Insufficient resources "
1354 "to create thread.",
1355 __func__);
1357 g_debug ("%s: started alive detection.", __func__);
1358
1359 for (
host = get_host_from_queue (alive_hosts_kb, &ad_finished);
1361 host = get_host_from_queue (alive_hosts_kb, &ad_finished))
1362 {
1364 }
1365
1366 if (gvm_host_get_addr6 (
host, &tmpaddr) == 0)
1369 {
1370 g_debug (
1371 "%s: Get first host to test from Queue. This host is used for "
1372 "initialising the alive_hosts_list.",
1373 __func__);
1374 }
1375 alive_hosts_list = gvm_hosts_new (gvm_host_value_str (
host));
1376 }
1377
1378 if (prefs_get ("report_scripts"))
1379 {
1380 char *path = g_strdup_printf (
1381 "%s/%s-stats.json", prefs_get (
"report_scripts"), globals->
scan_id);
1383 g_free (path);
1384 }
1385
1386
1387
1388 allow_simultaneous_ips = prefs_get_bool ("allow_simultaneous_ips");
1391 {
1394 char *host_str;
1395
1396 if (!test_alive_hosts_only
1398 {
1399 sleep (1);
1400
1401 gvm_hosts_move_current_host_to_end (
hosts);
1403 continue;
1404 }
1405
1406 do
1407 {
1408 rc = kb_new (&arg_host_kb, prefs_get ("db_address"));
1409 if (rc < 0 && rc != -2)
1410 {
1412 goto stop;
1413 }
1414 else if (rc == -2)
1415 {
1417 continue;
1418 }
1419 break;
1420 }
1421 while (1);
1422
1423 host_str = gvm_host_value_str (
host);
1426 {
1427 kb_delete (arg_host_kb);
1428 g_free (host_str);
1429 goto stop;
1430 }
1431
1433 {
1434 kb_delete (arg_host_kb);
1435 g_free (host_str);
1436 continue;
1437 }
1438
1442 args.host_kb = arg_host_kb;
1443
1444 forkagain:
1446
1448 {
1449 fork_retries++;
1451 {
1452
1453 g_warning ("fork() failed - %s. %s won't be tested",
1454 strerror (errno), host_str);
1455 g_free (host_str);
1456 goto stop;
1457 }
1458
1459 g_debug ("fork() failed - "
1460 "sleeping %d seconds and trying again...",
1461 fork_retries);
1463 goto forkagain;
1464 }
1466
1467 if (test_alive_hosts_only)
1468 {
1469 struct in6_addr tmpaddr;
1470 gvm_host_t *alive_buf;
1471
1472 while (1)
1473 {
1474
1475 gboolean ad_finished = FALSE;
1476 for (
host = get_host_from_queue (alive_hosts_kb, &ad_finished);
1478 host = get_host_from_queue (alive_hosts_kb, &ad_finished))
1479 {
1481 }
1482
1483 if (
host && !allow_simultaneous_ips
1485 {
1486 struct in6_addr hostip;
1487 char ip_str[INET6_ADDRSTRLEN];
1488 int flag_set;
1489
1490 gvm_host_get_addr6 (
host, &hostip);
1491 addr6_to_str (&hostip, ip_str);
1492
1493
1494
1495 flag_set = finish_signal_on_queue (alive_hosts_kb);
1496
1497 put_host_on_queue (alive_hosts_kb, ip_str);
1498 g_debug ("Reallocating the host %s at the end of the queue",
1499 ip_str);
1500
1501 gvm_host_free (
host);
1503
1504 if (flag_set)
1505 {
1506 g_debug ("Reallocating finish signal in the host queue");
1507 realloc_finish_signal_on_queue (alive_hosts_kb);
1508 }
1509 }
1510 else
1511 break;
1512 }
1513
1514 if (
host && gvm_host_get_addr6 (
host, &tmpaddr) == 0)
1515 {
1518 gvm_host_free (alive_buf);
1519 alive_buf = NULL;
1520 }
1521
1523 gvm_hosts_add (alive_hosts_list, gvm_duplicate_host (
host));
1524 else
1525 g_debug ("%s: got NULL host, stop/finish scan", __func__);
1526 }
1527 else
1528 {
1530 }
1531 g_free (host_str);
1532 }
1533
1534
1535
1538 killpg (getpid (), SIGUSR1);
1539
1540 g_debug ("Test complete");
1541
1542stop:
1543
1544 if (test_alive_hosts_only)
1545 {
1546 int err;
1547 void *retval;
1548
1549 kb_lnk_reset (alive_hosts_kb);
1550 g_debug ("%s: free alive detection data ", __func__);
1551
1552
1553 g_debug ("%s: waiting for alive detection thread to be finished...",
1554 __func__);
1555
1557 if (err == EDEADLK)
1558 g_debug ("%s: pthread_join() returned EDEADLK.", __func__);
1559 if (err == EINVAL)
1560 g_debug ("%s: pthread_join() returned EINVAL.", __func__);
1561 if (err == ESRCH)
1562 g_debug ("%s: pthread_join() returned ESRCH.", __func__);
1563 if (retval == PTHREAD_CANCELED)
1564 g_debug ("%s: pthread_join() returned PTHREAD_CANCELED.", __func__);
1565
1566 if (err == 0)
1568 g_debug ("%s: Finished waiting for alive detection thread.", __func__);
1569 }
1570
1572
1573 gettimeofday (&now, NULL);
1574 if (test_alive_hosts_only)
1575 {
1576 g_message ("Vulnerability scan %s finished in %ld seconds: "
1577 "%d alive hosts of %d",
1578 globals->
scan_id, now.tv_sec - then.tv_sec,
1579 gvm_hosts_count (alive_hosts_list), gvm_hosts_count (
hosts));
1580 }
1581 else
1582 g_message ("Vulnerability scan %s finished in %ld seconds: %d hosts",
1583 globals->
scan_id, now.tv_sec - then.tv_sec,
1584 gvm_hosts_count (
hosts));
1585
1586 if (prefs_get ("report_scripts"))
1587 {
1588 char *buff =
1589 g_strdup_printf ("},\"scan_time\": {\"start\": %ld, \"stop\": %ld}}",
1590 then.tv_sec, now.tv_sec);
1591 char *path = g_strdup_printf (
1592 "%s/%s-stats.json", prefs_get (
"report_scripts"), globals->
scan_id);
1593
1595
1596 g_free (buff);
1597 g_free (path);
1598 }
1599
1600 gvm_hosts_free (
hosts);
1601 if (alive_hosts_list)
1602 gvm_hosts_free (alive_hosts_list);
1603
1605
1606 return error;
1607}
static int scan_is_stopped(void)
static void attack_start(struct ipc_context *ipcc, struct attack_start_args *args)
Set up some data and jump into attack_host().
static int apply_hosts_reverse_lookup_preferences(gvm_hosts_t *hosts)
static int apply_hosts_excluded(gvm_hosts_t *hosts)
static void handle_scan_stop_signal()
static void set_alive_detection_tid(pthread_t tid)
static void fork_sleep(int n)
static void check_deprecated_prefs(void)
Check if any deprecated prefs are in pref table and print warning.
static int connect_main_kb(kb_t *main_kb)
Connect to the main kb. Must be released with kb_lnk_reset() after use.
static void report_kb_failure(int errcode)
static int check_kb_access(void)
static void apply_hosts_preferences_ordering(gvm_hosts_t *hosts)
#define INVALID_TARGET_LIST
static void message_to_client(kb_t kb, const char *msg, const char *ip_str, const char *port, const char *type)
static gboolean ad_thread_joined(gboolean joined)
Set and get if alive detection thread was already joined by main thread.
static pthread_t get_alive_detection_tid()
static void set_scan_status(char *status)
Set scan status. This helps ospd-openvas to identify if a scan crashed or finished cleanly.
static struct host * hosts
int host_is_currently_scanned(gvm_host_t *host_to_check)
Returns 1 if the host is being scanned. 0 otherwise.
int hosts_set_pid(char *name, pid_t pid)
int hosts_read(void)
Returns -1 if client asked to stop all tests or connection was lost or error. 0 otherwise.
int hosts_init(int max_hosts)
int hosts_new(char *name, kb_t kb, kb_t main_kb)
void(* ipc_process_func)(struct ipc_context *, void *)
static struct timeval timeval(unsigned long val)
void plugins_scheduler_free(plugins_scheduler_t sched)
plugins_scheduler_t plugins_scheduler_init(const char *plugins_list, int autoload, int *error)
struct plugins_scheduler * plugins_scheduler_t
pid_t create_ipc_process(ipc_process_func func, void *args)
initializes a communication channels and calls a function with a new process
void(*)(int) openvas_signal(int signum, void(*handler)(int))
struct scan_globals * globals
plugins_scheduler_t sched
Host information, implemented as doubly linked list.
void write_script_stats(const char *buf, const char *path, int mode)
Writes scripts stats into a file.
int get_max_hosts_number(void)
int get_max_checks_number(void)