|
| 1 | +import glob |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | +import yaml |
| 5 | +import random |
| 6 | +import utils |
| 7 | + |
| 8 | +# load config |
| 9 | +with open('config.yaml') as stream: |
| 10 | + config = yaml.safe_load(stream) |
| 11 | + |
| 12 | +windowSize = config['windowSize'] |
| 13 | +randomPolicyCreation = config['randomPolicyCreation'] |
| 14 | +randomNumberOfPolicyRules = config['randomNumberOfPolicyRules'] |
| 15 | +minNumberOfPolicyRules = config['minNumberOfPolicyRules'] |
| 16 | +maxNumberOfPolicyRules = config['maxNumberOfPolicyRules'] |
| 17 | +exactNumberOfPolicyRules = config['exactNumberOfPolicyRules'] |
| 18 | +completePolicyCreation = config['completePolicyCreation'] |
| 19 | +expertPolicyCreation = config['expertPolicyCreation'] |
| 20 | +aggregateFunctions = config['aggregateFunctions'] # avg, min, max |
| 21 | + |
| 22 | +AGGREGATEFUNCTION = aggregateFunctions[0] |
| 23 | +SEED = config['seed'] |
| 24 | +POLICYCOLUMNS = utils.POLICYCOLUMNS |
| 25 | + |
| 26 | +# set seed |
| 27 | +random.seed(SEED) |
| 28 | +print(random.random()) |
| 29 | + |
| 30 | + |
| 31 | +def createPolicy(): |
| 32 | + |
| 33 | + # load the csv with windowSize |
| 34 | + filenames = [file for file in glob.glob( |
| 35 | + './*.csv') if 'policy({}).csv'.format(windowSize) in file] |
| 36 | + csvPolicy = pd.read_csv(filenames[0], header=None) |
| 37 | + |
| 38 | + # postprocess: set header and group by malwaretype |
| 39 | + csvPolicy.columns = POLICYCOLUMNS |
| 40 | + # print(malwareGroup.get_group('httpbackdoor')) # DEBUG |
| 41 | + malwareGroup = csvPolicy.groupby(['malware']) |
| 42 | + |
| 43 | + # policy creation |
| 44 | + policy = pd.DataFrame() |
| 45 | + |
| 46 | + # random policy creation |
| 47 | + # iterate over all malware groups and add some (random or defined) rules (row) for each malware type |
| 48 | + if randomPolicyCreation == True: |
| 49 | + method = 'random' |
| 50 | + if randomNumberOfPolicyRules == True: |
| 51 | + method += '({}-{})'.format(minNumberOfPolicyRules, |
| 52 | + maxNumberOfPolicyRules) |
| 53 | + else: |
| 54 | + method += '({})'.format(exactNumberOfPolicyRules) |
| 55 | + |
| 56 | + for malware in malwareGroup: |
| 57 | + # malware is a tuple: (name, df) |
| 58 | + rows = malware[1].shape[0] # number of rows for that malware type |
| 59 | + |
| 60 | + # random number of rules |
| 61 | + if randomNumberOfPolicyRules == True: |
| 62 | + |
| 63 | + # define random number between min/max number of policy rules |
| 64 | + random.seed(SEED) |
| 65 | + nRules = random.choice( |
| 66 | + [minNumberOfPolicyRules, maxNumberOfPolicyRules]) |
| 67 | + |
| 68 | + # defined number of rules |
| 69 | + else: |
| 70 | + nRules = exactNumberOfPolicyRules |
| 71 | + |
| 72 | + # make sure we don't have more rules than rows |
| 73 | + while(rows < nRules): |
| 74 | + nRules -= 1 |
| 75 | + |
| 76 | + # print(malware[1].sample(n = nRules)) # DEBGUG |
| 77 | + # add defined rules to policy |
| 78 | + # todo check what happens if nRules > n when set |
| 79 | + policy = policy.append( |
| 80 | + malware[1].sample(n=nRules, random_state=SEED)) |
| 81 | + policy = policy.drop_duplicates(subset=['metric']) |
| 82 | + |
| 83 | + # complete policy creation |
| 84 | + # iterate over all malware groups and all rules (row) for each malware type |
| 85 | + elif completePolicyCreation == True: |
| 86 | + method = 'complete' |
| 87 | + policy = csvPolicy |
| 88 | + |
| 89 | + # expert policy creation |
| 90 | + elif expertPolicyCreation == True: |
| 91 | + method = 'expert' |
| 92 | + pass |
| 93 | + # to be done |
| 94 | + |
| 95 | + # postprocessing |
| 96 | + # remove aggregate function string for all rows |
| 97 | + policy['metric'] = policy['metric'].str.replace( |
| 98 | + '-{}'.format(AGGREGATEFUNCTION), '') |
| 99 | + policyName = 'policy({})-{}-{}'.format(windowSize, |
| 100 | + AGGREGATEFUNCTION, method) |
| 101 | + policy.to_csv('{}.csv'.format(policyName), index=False) |
| 102 | + |
| 103 | + return policy |
| 104 | + |
| 105 | + |
| 106 | +def malwareDistribution(policy): |
| 107 | + # init |
| 108 | + CNCMALWARE = utils.CNC |
| 109 | + RKMALWALRE = utils.ROOTKIT |
| 110 | + RWMALWARE = utils.RANSOMWARE |
| 111 | + MALWARECATEGORIES = utils.MALWARECATEGORIES |
| 112 | + |
| 113 | + conditions = [ |
| 114 | + (policy['malware'].isin(CNCMALWARE)), |
| 115 | + (policy['malware'].isin(RKMALWALRE)), |
| 116 | + (policy['malware'].isin(RWMALWARE)) |
| 117 | + ] |
| 118 | + # classify each malware by type and add type column |
| 119 | + policy['malwaretype'] = np.select(conditions, MALWARECATEGORIES) |
| 120 | + |
| 121 | + # count different malware types and create a dict |
| 122 | + malwareTypes = policy['malwaretype'].value_counts().index.tolist() |
| 123 | + malwareOccurrences = policy['malwaretype'].value_counts().values.tolist() |
| 124 | + malwareTypeOcc = {malwareTypes[i]: malwareOccurrences[i] |
| 125 | + for i in range(len(malwareTypes))} |
| 126 | + |
| 127 | + # count total occurences of all malware types |
| 128 | + totalOccurences = sum(malwareOccurrences) |
| 129 | + ''' |
| 130 | + malwareTypeOcc: { |
| 131 | + 'Rootkit': 3 |
| 132 | + 'CnC': 7 |
| 133 | + 'Ransomware: 3 |
| 134 | + } |
| 135 | + totalOccurences: 13 |
| 136 | + array([0.53846154, 0.23076923, 0.23076923]) |
| 137 | + ''' |
| 138 | + |
| 139 | + return [malwareTypeOcc, totalOccurences, np.divide(malwareOccurrences, totalOccurences)] |
0 commit comments