-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (69 loc) · 2.45 KB
/
main.py
File metadata and controls
91 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import random
import json
import glob
import os
import sys
# find the number of file
list_of_files = glob.glob('./backup/*.json')
if len(list_of_files) == 0:
list_of_files.append("/backup/data.000.json")
latest_file = max(list_of_files, key=os.path.basename)
number = int(latest_file.split('/')[2].split('.')[1])
# const variables
VULNERABILITIES_PATH = "./docs/vulnerabilities.txt"
ATTACKS_PATH = "./docs/attacks.txt"
OUTPUT_PATH = f"./backup/data.{number+1:03d}.json"
BATCHES = int(sys.argv[1])
MAX_RAND = int(sys.argv[2])
# base lists
vulnerabilities = []
attacks = []
# read input documents
with open(VULNERABILITIES_PATH, 'r', encoding='UTF-8') as file:
while line := file.readline():
vulnerabilities.append(line.rstrip())
with open(ATTACKS_PATH, 'r', encoding='UTF-8') as file:
while line := file.readline():
attacks.append(line.rstrip())
print(f'[INFO] loaded {len(attacks)} attacks, {len(vulnerabilities)} vulnerabilities into system.')
if __name__ == "__main__":
# generate output data list
outputs = []
print(f'Output file is "{OUTPUT_PATH}"\n\n')
try:
# creating 100 attacks
for i in range(0, BATCHES):
# get random vulnerabilities
v_tmp = random.randint(1, MAX_RAND)
v_list = random.sample(vulnerabilities, k=v_tmp)
print()
print(f'batch {i+1} out of {BATCHES}')
print("=======")
print()
print(" | ".join(v_list))
print()
print("=======")
print()
# select attacks
for index, item in enumerate(attacks):
if index == len(attacks) - 1:
print(f'[{index}] {item}')
else:
print(f'[{index}] {item}', end=", ")
print()
print()
selected = input('> select attacks: ').split(',')
# generate output
outputs.append({
'vulnerabilities': v_list,
'attacks': [attacks[int(j)] for j in selected]
})
print()
# save output
with open(OUTPUT_PATH, 'w') as file:
file.write(json.dumps(outputs, indent=4))
except KeyboardInterrupt:
print('terminated ...')
# save output
with open(OUTPUT_PATH, 'w') as file:
file.write(json.dumps(outputs, indent=4))