| 51 | |
| 52 | |
| 53 | class createDataSet: |
| 54 | # EXTRACT_RATIO = 0.2 每个家族抽取样本的比例 ,0.2表示按照8:2划分训练集和验证集 |
| 55 | |
| 56 | def __init__(self, origin_path, goal_path): |
| 57 | self.origin_path = origin_path |
| 58 | self.goal_path = goal_path |
| 59 | |
| 60 | def checkFileisExist(self): |
| 61 | try: |
| 62 | createBlankFile(self.origin_path, self.goal_path).createNewFiles() |
| 63 | Flag = True |
| 64 | except: |
| 65 | print('something wrong in checking File is exist') |
| 66 | Flag = False |
| 67 | return Flag |
| 68 | |
| 69 | def get_floder_name(self, index): |
| 70 | if 0 <= index <= 6: |
| 71 | name = 'Normal' |
| 72 | elif index == 7: |
| 73 | name = 'Infilt' |
| 74 | elif index == 8: |
| 75 | name = 'HttpDoS' |
| 76 | elif index == 9: |
| 77 | name = 'DDoS' |
| 78 | elif index == 10: |
| 79 | name = 'BFSSH' |
| 80 | else: |
| 81 | print('index:', index, ' is error') |
| 82 | raise ValueError |
| 83 | return name |
| 84 | |
| 85 | def copyFile(self): |
| 86 | list = readFilenameList(self.origin_path) |
| 87 | # print(list) |
| 88 | flag = self.checkFileisExist() |
| 89 | if flag == False: return 'copyFile Failed' |
| 90 | index = 0 |
| 91 | for sub_folder, num2pick in dict_num.items(): |
| 92 | print(sub_folder, num2pick) |
| 93 | class_path = os.path.join(self.origin_path, sub_folder) |
| 94 | sample_list = random.sample(os.listdir(class_path), num2pick) |
| 95 | sample_train = sample_list[:int(num2pick * 0.8)] |
| 96 | sample_validation = sample_list[int(num2pick * 0.8):] |
| 97 | |
| 98 | goal_sub_path = self.get_floder_name(index) |
| 99 | for name in sample_train: |
| 100 | shutil.copy(self.origin_path + '\\' + sub_folder + '\\' + name, |
| 101 | self.goal_path + '\\train' + '\\' + goal_sub_path + '\\' + f'{goal_sub_path}_{hash(name)}.pcap') |
| 102 | for name in sample_validation: |
| 103 | shutil.copy(self.origin_path + '\\' + sub_folder + '\\' + name, |
| 104 | self.goal_path + '\\validation' + '\\' + goal_sub_path + '\\' + f'{goal_sub_path}_{hash(name)}.pcap') |
| 105 | index += 1 |
| 106 | return 'moveFile Succeed!' |
| 107 | |
| 108 | # file_abs_path = self.origin_path + "\\" + class_list |
| 109 | # pcap_list = readFilenameList(file_abs_path) |
| 110 | # picknumber = dict_num[malFamily] |