Skip to content

Commit

Permalink
Change: Use function to calculate timestamps
Browse files Browse the repository at this point in the history
The timestamp calculation has been moved to a dedicated function.
The parameter passed corresponds to seconds.
  • Loading branch information
4D617274696E authored and bjoernricks committed Nov 13, 2023
1 parent 3ab8729 commit ab195f7
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions scripts/generate-random-reports.gmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@
local_date_time = datetime.now()


def calculate_timestamp(delta: int = 0) -> str:
return (
(local_date_time + timedelta(seconds=delta))
.astimezone()
.replace(microsecond=0)
.isoformat()
)


def generate_ports(n_ports):
protocol = ["/tcp", "/udp"]
return [str(randrange(0, 65536)) + choice(protocol) for i in range(n_ports)]
Expand Down Expand Up @@ -166,16 +175,9 @@ def generate_result_elem(
e.SubElement(own, "name").text = generate_id()

elem = e.Element("modification_time")
e.SubElement(result_elem, "modification_time").text = (
local_date_time.astimezone().replace(microsecond=0).isoformat()
)
e.SubElement(result_elem, "modification_time").text = calculate_timestamp(0)
e.SubElement(result_elem, "comment").text = ""
e.SubElement(result_elem, "creation_time").text = (
(local_date_time + timedelta(seconds=-20))
.astimezone()
.replace(microsecond=0)
.isoformat()
)
e.SubElement(result_elem, "creation_time").text = calculate_timestamp(-20)

host_elem = e.Element("host")
host_elem.text = host_ip
Expand Down Expand Up @@ -259,18 +261,8 @@ def generate_host_elem(
e.SubElement(host_elem, "ip").text = host_ip
e.SubElement(host_elem, "asset", {"asset_id": host_asset}).text = ""

e.SubElement(host_elem, "start").text = (
(local_date_time + timedelta(minutes=-15))
.astimezone()
.replace(microsecond=0)
.isoformat()
)
e.SubElement(host_elem, "end").text = (
(local_date_time + timedelta(seconds=-30))
.astimezone()
.replace(microsecond=0)
.isoformat()
)
e.SubElement(host_elem, "start").text = calculate_timestamp(-1000)
e.SubElement(host_elem, "end").text = calculate_timestamp(-30)

app = choice(list(data["apps"]))
os = choice(list(data["oss"]))
Expand Down Expand Up @@ -357,8 +349,7 @@ def generate_reports(task, n_reports, n_results, with_gauss, **kwargs):


def generate_target():
timestamp = local_date_time.astimezone().replace(microsecond=0).isoformat()
target_name = f"Random_Report_Generation_Target_{timestamp}"
target_name = f"Random_Report_Generation_Target_{calculate_timestamp(0)}"
target_comment = (
f"Generated by Random Report Generation Script Version {__version__}."
)
Expand All @@ -374,19 +365,19 @@ def generate_target():


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}"
filter_string=f"Random_Report_Generation_Target_{calculate_timestamp(0)}"
).xpath("//@id")[0]

for i in range(n_tasks):
index = f"{{0:0>{len(str(n_tasks))}}}"
task_name = f"Random_Report_Generation_{task_type.capitalize()}_Task_{timestamp}_{index.format(i + 1)}"
task_name = (
f"Random_Report_Generation_{task_type.capitalize()}_Task_{calculate_timestamp(0)}_{index.format(i + 1)}"
)
task_comment = (
f"Generated by Random Report Generation Script Version {__version__}."
)
Expand Down

0 comments on commit ab195f7

Please sign in to comment.