-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
365 lines (301 loc) · 9.72 KB
/
util.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import configparser
from loguru import logger as logging
import time
import json
import yaml
from pprint import pprint, pformat
import pandas as pd
from pathlib import Path
from datetime import datetime
import sys, os
from easydict import EasyDict
from colorama import init, Fore, Back, Style
import time
import random
from copy import deepcopy
from rich.console import Console
import wandb
import torch
import numpy as np
curr_path = os.path.dirname(__file__)
parent_path = os.path.dirname(curr_path)
sys.path.append(parent_path) # add current terminal path to sys.path
sys.path.append(curr_path) # add current terminal path to sys.path
console = Console()
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
class Configure:
conf = configparser.ConfigParser()
try:
result = conf.read(Path(__file__).parent / "config.ini")
except Exception as e:
logging.error("config file not found" + e)
@classmethod
def get(cls, label, name):
return cls.conf.get(label, name)
@classmethod
def read_configure_value(cls, sections=[]):
config_dict = {}
for section in cls.conf.sections():
if not sections or section in sections:
config_dict[section] = {}
# 遍历section中的每个option
for option in cls.conf.options(section):
# 将option和对应的值添加到子字典中
config_dict[section][option] = cls.conf.get(
section, option)
return config_dict
@classmethod
def getBool(cls, label, name):
bl = cls.conf.get(label, name)
if bl.lower() == "true" or bl == "1":
return True
elif bl.lower() == "false" or bl == "0":
return False
else:
raise ValueError("Bool value must be true/1/True/false/False/0")
@classmethod
def set(cls, label, name, value):
cls.conf.set(label, name, str(value))
cls.conf.write(open("config.ini", "w"))
class Metric:
Train_Metric = EasyDict({"all": [], "best": {}})
"""
Training signals
"""
EarlyTerminate = "early_terminate"
Finished = "finished"
Failed = "failed"
Success = "success"
def __init__(self) -> None:
pass
class UTIL:
"""
Running Mode:
"""
isDebug = True if sys.gettrace() else False
Manual = 0
# Train_Manual = 1
Train_Simulate = 1
# Train_Auto = 2
Train_Real = 2
# Real_Attack = 3
Eval_Real = 3
# Evaluation = 4
Eval_Simulate = 4
today = datetime.now().strftime("%b%d")
current_time = datetime.now().strftime("%b%d_%H-%M-%S")
lport = random.randint(10000, 20000)
lport_list = []
Running_title = ""
project_name = Path(__file__).parent.stem
project_path = Path(__file__).parent
scenario_path = project_path / "scenarios"
trained_agent_path = project_path / Configure.get("Train",
"trained_agent_path")
log_path = project_path / "log"
running_record_path = project_path / "running_record"
password = ""
neighbor_discovery = True
def __init__(self) -> None:
pass
@classmethod
def mode_name(cls, mode):
if mode == 0:
return "Manual Mode"
if mode == 1:
return "Simulated Training Mode"
if mode == 2:
return "Real Training Mode"
if mode == 3:
return "Simulated Evaluation Mode"
if mode == 4:
return "Real Evaluation Mode"
@classmethod
def show_banner(cls):
banner = """
___ .______ .______ __ __
/ \ | _ \ | _ \ | | | |
/ ^ \ | |_) | | |_) | | | | |
/ /_\ \ | ___/ | / | | | |
/ _____ \ | | | |\ \----.| | | `----.
/__/ \__\ | _| | _| `._____||__| |_______|
"""
print(banner)
cls.show_credit()
time.sleep(2)
# flag_log
@classmethod
def show_credit(cls):
credit = """
+ -- --=[ APRIL\t: Autonomous Penetesting based on ReInforcement Learning ]=-- -- +
+ -- --=[ Author\t: NUDT-HFBOT Team ]=-- -- +
+ -- --=[ Website\t: https://github.com/Joe-zsc/GAP ]=-- -- +
"""
print(credit)
@classmethod
def line_break(cls, length=60, symbol="-"):
line_break = symbol * length
logging.info(line_break)
def write_csv_DictList(file: Path, data: list):
variables = list(data[0].keys())
pd_data = pd.DataFrame([[i[j] for j in variables] for i in data],
columns=variables)
pd_data.to_csv(file, mode="w", index=False)
return pd_data
@classmethod
def smooth_data(cls, data: list, weight: float = 0.9):
smoothed_data = []
last = data[0]
smoothed = []
for point in data:
smoothed_val = last * weight + (1 - weight) * point
smoothed_data.append(smoothed_val)
last = smoothed_val
return smoothed_data
@classmethod
def write_to_csv(cls, data: list, save_path: str):
"""
data: [dict1,dict2,...]
"""
import csv
assert len(data) > 0, "the input data is empty"
f = open(save_path, "a", encoding="utf8", newline="")
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
for line in data:
writer.writerow(line)
@classmethod
def save_json(cls, path, data):
with open(path, "w", encoding="utf-8") as f:
f.write(json.dumps(data, ensure_ascii=False, indent=4))
@classmethod
def read_yaml(cls, path):
with open(path, "r", encoding="utf-8") as f:
result = yaml.load(f.read(), Loader=yaml.FullLoader)
return result
@classmethod
def set_logger(cls, print_lever="INFO", logfile_level="DEBUG"):
log_file = cls.project_path / "log" / f"{Path(__file__).parent.stem}.log"
logging.remove()
logging.add(sys.stderr,
level=print_lever,
backtrace=True,
diagnose=True)
logging.add(
log_file,
level=logfile_level,
encoding="UTF-8",
rotation="1 day",
colorize=True,
backtrace=True,
diagnose=True,
format=
"{time:YYYY-MM-DD HH:mm:ss} - {level} - {file} - {line} - {message}",
)
logging.level("NOTE", no=35, color="<cyan>",icon='✨')
@classmethod
def set_wandb_url(cls):
try:
os.environ["WANDB_BASE_URL"] = Configure.get(
"wandb", "WANDB_BASE_UR")
wandb.login(key=Configure.get("wandb", "API_Key"))
except:
os.environ["WANDB_BASE_URL"] = "https://api.wandb.ai"
def split_num_l(num_lst):
"""merge successive num, sort lst(ascending or descending): 'as' or 'des'
eg: [1, 3,4,5,6, 9,10] -> [[1], [3, 4, 5, 6], [9, 10]]
"""
num_lst_tmp = [int(n) for n in num_lst]
sort_lst = sorted(num_lst_tmp) # ascending
len_lst = len(sort_lst)
i = 0
split_lst = []
tmp_lst = [sort_lst[i]]
while True:
if i + 1 == len_lst:
break
next_n = sort_lst[i + 1]
if sort_lst[i] + 1 == next_n:
tmp_lst.append(next_n)
else:
split_lst.append(tmp_lst)
tmp_lst = [next_n]
i += 1
split_lst.append(tmp_lst)
return split_lst
def Merge_str_lst(num_lst):
"""[[1], [3, 4, 5, 6], [9, 10]] -> ['1', '3~6', '9~10']"""
if not num_lst:
return []
mylst = split_num_l(num_lst)
mg_l = []
for num_l in mylst:
if len(num_l) == 1:
mg_l.append(str(num_l[0]))
else:
mg_l.append(str(num_l[0]) + "~" + str(num_l[-1]))
return mg_l
class color:
PURPLE = "\033[95m"
CYAN = "\033[96m"
DARKCYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
GREY = "\033[1;30m"
END = "\033[0m"
init(autoreset=True)
@classmethod
def print(cls, s, c=GREEN, end="\n"):
print(c + s + cls.END, end=end)
@classmethod
def color_str(cls, s, c=GREEN):
s = pformat(s)
return c + s + cls.END
# 前景色:红色 背景色:默认
@classmethod
def red(cls, s):
return Fore.RED + s + Fore.RESET
# 前景色:绿色 背景色:默认
@classmethod
def green(cls, s):
return Fore.GREEN + s + Fore.RESET
# 前景色:黄色 背景色:默认
@classmethod
def yellow(cls, s):
return Fore.YELLOW + s + Fore.RESET
# 前景色:蓝色 背景色:默认
@classmethod
def blue(cls, s):
return Fore.BLUE + s + Fore.RESET
# 前景色:洋红色 背景色:默认
@classmethod
def magenta(cls, s):
return Fore.MAGENTA + s + Fore.RESET
# 前景色:青色 背景色:默认
@classmethod
def cyan(cls, s):
return Fore.CYAN + s + Fore.RESET
# 前景色:白色 背景色:默认
@classmethod
def white(cls, s):
return Fore.WHITE + s + Fore.RESET
# 前景色:黑色 背景色:默认
@classmethod
def black(cls, s):
return Fore.BLACK
# 前景色:白色 背景色:绿色
@classmethod
def white_green(cls, s):
return Fore.WHITE + Back.GREEN + s
@classmethod
def dave(cls, s):
return Style.BRIGHT + Fore.GREEN + s
Configure.read_configure_value(sections=["Embedding", "Support", "Exploit"])