From 3ab8729b7c5397a756b55e07c6b73b8407ba29a8 Mon Sep 17 00:00:00 2001 From: Martin Hermann Date: Wed, 8 Nov 2023 11:24:38 +0100 Subject: [PATCH] Add: Support for specifying task type Added the command line argument '--task-type' to specify the type of tasks to generate and add randomly generated reports to and made several complementary changes. --- scripts/generate-random-reports.gmp.py | 117 +++++++++++++++++-------- 1 file changed, 81 insertions(+), 36 deletions(-) mode change 100644 => 100755 scripts/generate-random-reports.gmp.py diff --git a/scripts/generate-random-reports.gmp.py b/scripts/generate-random-reports.gmp.py old mode 100644 new mode 100755 index b4e778f9..a7445c28 --- a/scripts/generate-random-reports.gmp.py +++ b/scripts/generate-random-reports.gmp.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2017-2021 Greenbone AG +# Copyright (C) 2017-2023 Greenbone AG # # SPDX-License-Identifier: GPL-3.0-or-later # @@ -20,20 +20,20 @@ import json import textwrap -import time from argparse import ArgumentParser, Namespace, RawTextHelpFormatter from pathlib import Path from random import choice, gauss, randrange, seed +from datetime import datetime, timedelta from gvm.protocols.gmp import Gmp from lxml import etree as e from gvmtools.helper import generate_id, generate_random_ips, generate_uuid -__version__ = "0.2.0" +__version__ = "0.3.0" HELP_TEXT = f""" - Random Report Generation Script {__version__} (C) 2017-2021 Greenbone AG + Random Report Generation Script {__version__} (C) 2017-2023 Greenbone AG This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -59,6 +59,8 @@ Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.""" +local_date_time = datetime.now() + def generate_ports(n_ports): protocol = ["/tcp", "/udp"] @@ -165,15 +167,14 @@ def generate_result_elem( elem = e.Element("modification_time") e.SubElement(result_elem, "modification_time").text = ( - time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time()))[:-2] - + ":00" - ) # Hell of a Timeformat :D + local_date_time.astimezone().replace(microsecond=0).isoformat() + ) e.SubElement(result_elem, "comment").text = "" e.SubElement(result_elem, "creation_time").text = ( - time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time() - 20))[ - :-2 - ] - + ":00" + (local_date_time + timedelta(seconds=-20)) + .astimezone() + .replace(microsecond=0) + .isoformat() ) host_elem = e.Element("host") @@ -226,9 +227,7 @@ def generate_host_detail_elem( return host_detail_elem -def generate_additional_host_details( - n_details, host_details, *, not_vuln=False -): +def generate_additional_host_details(n_details, host_details, *, not_vuln=False): host_detail_elems = [] for _ in range(n_details): @@ -261,16 +260,16 @@ def generate_host_elem( e.SubElement(host_elem, "asset", {"asset_id": host_asset}).text = "" e.SubElement(host_elem, "start").text = ( - time.strftime( - "%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time() - 1000) - )[:-2] - + ":00" + (local_date_time + timedelta(minutes=-15)) + .astimezone() + .replace(microsecond=0) + .isoformat() ) e.SubElement(host_elem, "end").text = ( - time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time() - 30))[ - :-2 - ] - + ":00" + (local_date_time + timedelta(seconds=-30)) + .astimezone() + .replace(microsecond=0) + .isoformat() ) app = choice(list(data["apps"])) @@ -322,9 +321,7 @@ def generate_host_elem( if n_host_details: host_elem.extend( - generate_additional_host_details( - n_host_details, data["host_details"] - ) + generate_additional_host_details(n_host_details, data["host_details"]) ) dev = n_not_vuln / 10 @@ -359,22 +356,61 @@ def generate_reports(task, n_reports, n_results, with_gauss, **kwargs): return reports -def generate_data(gmp, n_tasks, **kwargs): +def generate_target(): + timestamp = local_date_time.astimezone().replace(microsecond=0).isoformat() + target_name = f"Random_Report_Generation_Target_{timestamp}" + target_comment = ( + f"Generated by Random Report Generation Script Version {__version__}." + ) + target_hosts = ["198.18.0.0/24"] + target_port_list_id = "c7e03b6c-3bbe-11e1-a057-406186ea4fc5" + + gmp.create_target( + name=target_name, + comment=target_comment, + hosts=target_hosts, + port_list_id=target_port_list_id, + ) + + +def generate_data(gmp, n_tasks, task_type, **kwargs): + timestamp = local_date_time.astimezone().replace(microsecond=0).isoformat() + + if task_type == "scan": + scanner_id = "08b69003-5fc2-4037-a479-93b440211c73" + config_id = "085569ce-73ed-11df-83c3-002264764cea" + generate_target() + target_id = gmp.get_targets( + filter_string=f"Random_Report_Generation_Target_{timestamp}" + ).xpath("//@id")[0] + for i in range(n_tasks): index = f"{{0:0>{len(str(n_tasks))}}}" - task_name = f"Task_for_GenReport:_{index.format(i + 1)}" + task_name = f"Random_Report_Generation_{task_type.capitalize()}_Task_{timestamp}_{index.format(i + 1)}" + task_comment = ( + f"Generated by Random Report Generation Script Version {__version__}." + ) + + gmp.create_container_task(name=task_name, comment=task_comment) - gmp.create_container_task(task_name) + task_id = gmp.get_tasks(filter_string=f"name={task_name}").xpath("//@id")[0] - task_id = gmp.get_tasks(filter_string=f"name={task_name}").xpath( - "//@id" - )[0] + if task_type == "scan": + gmp.modify_task(task_id=task_id, alterable=1) reports = generate_reports(task=(task_id, task_name), **kwargs) for report in reports[0:]: gmp.import_report(report, task_id=task_id, in_assets=True) + if task_type == "scan": + gmp.modify_task( + task_id=task_id, + target_id=target_id, + scanner_id=scanner_id, + config_id=config_id, + ) + def main(gmp: Gmp, args: Namespace) -> None: # pylint: disable=undefined-variable, line-too-long @@ -395,9 +431,7 @@ def main(gmp: Gmp, args: Namespace) -> None: ), ) - parser.add_argument( - "-H", action="help", help="Show this help message and exit." - ) + parser.add_argument("-H", action="help", help="Show this help message and exit.") parser.add_argument( "--datafile", @@ -474,6 +508,16 @@ def main(gmp: Gmp, args: Namespace) -> None: "--seed", help="RNG Seed, in case the same data should be generated." ) + parser.add_argument( + "--task-type", + dest="task_type", + type=str, + choices=["container", "scan"], + default="container", + help="Type of Task(s) to store the generated Reports. Can either " + "be 'container' or 'scan', default: 'container'.", + ) + script_args = parser.parse_args(args.script_args) if not script_args.seed: @@ -484,7 +528,7 @@ def main(gmp: Gmp, args: Namespace) -> None: with open(str(script_args.datafile), encoding="utf-8") as file: data = json.load(file) - print("\n Generating randomized data(s)...\n") + print("\n Generating randomized data...\n") generate_data( gmp, @@ -497,9 +541,10 @@ def main(gmp: Gmp, args: Namespace) -> None: data=data, with_gauss=script_args.with_gauss, with_descriptions=script_args.with_descriptions, + task_type=script_args.task_type, ) - print("\n Generation done.\n") + print("\n Generation completed.\n") if __name__ == "__gmp__":