2017-06-27 20:29:19 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-06-30 09:04:24 +02:00
|
|
|
import string
|
|
|
|
|
2017-06-29 09:19:36 +02:00
|
|
|
import numpy as np
|
2017-06-30 09:04:24 +02:00
|
|
|
import pandas as pd
|
2017-06-29 09:19:36 +02:00
|
|
|
from tqdm import tqdm
|
2017-06-27 20:29:19 +02:00
|
|
|
|
|
|
|
|
2017-06-30 09:04:24 +02:00
|
|
|
def get_character_dict():
|
|
|
|
return dict((char, idx) for (idx, char) in
|
|
|
|
enumerate(string.ascii_lowercase + string.punctuation))
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_chunks(dataFrame, windowSize=10, overlapping=False,
|
|
|
|
maxLengthInSeconds=300):
|
|
|
|
# print('maxLength: ' + str(maxLengthInSeconds))
|
|
|
|
maxMilliSeconds = maxLengthInSeconds * 1000
|
|
|
|
outDomainLists = []
|
|
|
|
outDFFrames = []
|
|
|
|
if overlapping == False:
|
|
|
|
numBlocks = int(np.ceil(float(len(dataFrame)) / float(windowSize)))
|
|
|
|
userIDs = np.arange(len(dataFrame))
|
|
|
|
for blockID in np.arange(numBlocks):
|
|
|
|
curIDs = userIDs[(blockID * windowSize):((blockID + 1) * windowSize)]
|
|
|
|
# print(curIDs)
|
|
|
|
useData = dataFrame.iloc[curIDs]
|
|
|
|
curDomains = useData['domain']
|
|
|
|
if maxLengthInSeconds != -1:
|
|
|
|
curMinMilliSeconds = np.min(useData['timeStamp']) + maxMilliSeconds
|
|
|
|
underTimeOutIDs = np.where(np.array(useData['timeStamp']) <= curMinMilliSeconds)
|
|
|
|
if len(underTimeOutIDs) != len(curIDs):
|
|
|
|
curIDs = curIDs[underTimeOutIDs]
|
|
|
|
useData = dataFrame.iloc[curIDs]
|
|
|
|
curDomains = useData['domain']
|
|
|
|
outDomainLists.append(list(curDomains))
|
|
|
|
outDFFrames.append(useData)
|
|
|
|
else:
|
|
|
|
numBlocks = len(dataFrame) + 1 - windowSize
|
|
|
|
userIDs = np.arange(len(dataFrame))
|
|
|
|
for blockID in np.arange(numBlocks):
|
|
|
|
curIDs = userIDs[blockID:blockID + windowSize]
|
|
|
|
# print(curIDs)
|
|
|
|
useData = dataFrame.iloc[curIDs]
|
|
|
|
curDomains = useData['domain']
|
|
|
|
if maxLengthInSeconds != -1:
|
|
|
|
curMinMilliSeconds = np.min(useData['timeStamp']) + maxMilliSeconds
|
|
|
|
underTimeOutIDs = np.where(np.array(useData['timeStamp']) <= curMinMilliSeconds)
|
|
|
|
if len(underTimeOutIDs) != len(curIDs):
|
|
|
|
curIDs = curIDs[underTimeOutIDs]
|
|
|
|
useData = dataFrame.iloc[curIDs]
|
|
|
|
curDomains = useData['domain']
|
|
|
|
outDomainLists.append(list(curDomains))
|
|
|
|
outDFFrames.append(useData)
|
|
|
|
return (outDomainLists, outDFFrames)
|
|
|
|
|
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
def get_domain_features(domain, vocab, max_length=40):
|
|
|
|
curFeature = np.zeros([max_length, ])
|
|
|
|
for j in range(np.min([len(domain), max_length])):
|
2017-06-30 09:04:24 +02:00
|
|
|
curCharacter = domain[-j]
|
2017-06-30 10:42:21 +02:00
|
|
|
if curCharacter in vocab:
|
|
|
|
curFeature[j] = vocab[curCharacter]
|
2017-06-30 09:04:24 +02:00
|
|
|
return curFeature
|
|
|
|
|
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
def get_flow_features(flow):
|
2017-06-30 09:04:24 +02:00
|
|
|
useKeys = ['duration', 'bytes_down', 'bytes_up']
|
|
|
|
curFeature = np.zeros([len(useKeys), ])
|
2017-06-30 10:42:21 +02:00
|
|
|
for i, curKey in enumerate(useKeys):
|
2017-06-30 09:04:24 +02:00
|
|
|
try:
|
2017-06-30 10:42:21 +02:00
|
|
|
curFeature[i] = np.log1p(flow[curKey]).astype(float)
|
2017-06-30 09:04:24 +02:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return curFeature
|
|
|
|
|
|
|
|
|
|
|
|
def getCiscoFeatures(curDataLine, urlSIPDict):
|
|
|
|
numCiscoFeatures = 30
|
|
|
|
try:
|
|
|
|
ciscoFeatures = urlSIPDict[str(curDataLine['domain']) + str(curDataLine['server_ip'])]
|
|
|
|
# log transform
|
|
|
|
ciscoFeatures = np.log1p(ciscoFeatures).astype(float)
|
|
|
|
return ciscoFeatures.ravel()
|
|
|
|
except:
|
|
|
|
return np.zeros([numCiscoFeatures, ]).ravel()
|
|
|
|
|
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
def create_dataset_from_flows(user_flow_df, char_dict, maxLen, threshold=3, windowSize=10, use_cisco_features=False):
|
2017-06-30 09:04:24 +02:00
|
|
|
domainLists = []
|
|
|
|
dfLists = []
|
|
|
|
print("get chunks from user data frames")
|
|
|
|
for i, user_flow in enumerate(get_flow_per_user(user_flow_df)):
|
|
|
|
(domainListsTmp, dfListsTmp) = get_user_chunks(user_flow, windowSize=windowSize,
|
2017-06-30 10:42:21 +02:00
|
|
|
overlapping=True, maxLengthInSeconds=-1)
|
2017-06-30 09:04:24 +02:00
|
|
|
domainLists += domainListsTmp
|
|
|
|
dfLists += dfListsTmp
|
|
|
|
if i >= 10:
|
|
|
|
break
|
|
|
|
|
|
|
|
print("create training dataset")
|
|
|
|
return create_dataset_from_lists(
|
2017-06-30 10:42:21 +02:00
|
|
|
domains=domainLists, dfs=dfLists, charachterDict=char_dict,
|
2017-06-30 09:04:24 +02:00
|
|
|
maxLen=maxLen, threshold=threshold,
|
2017-06-30 10:42:21 +02:00
|
|
|
use_cisco_features=use_cisco_features, urlSIPDIct=dict(),
|
2017-06-30 09:04:24 +02:00
|
|
|
windowSize=windowSize)
|
|
|
|
|
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
def create_dataset_from_lists(domains, dfs, charachterDict, maxLen, threshold=3,
|
|
|
|
use_cisco_features=False, urlSIPDIct=dict(),
|
2017-06-30 09:04:24 +02:00
|
|
|
windowSize=10):
|
2017-06-30 10:42:21 +02:00
|
|
|
if 'hits' in dfs[0].keys():
|
2017-06-30 09:04:24 +02:00
|
|
|
hitName = 'hits'
|
2017-06-30 10:42:21 +02:00
|
|
|
elif 'virusTotalHits' in dfs[0].keys():
|
2017-06-30 09:04:24 +02:00
|
|
|
hitName = 'virusTotalHits'
|
|
|
|
numFlowFeatures = 3
|
|
|
|
numCiscoFeatures = 30
|
|
|
|
numFeatures = numFlowFeatures
|
2017-06-30 10:42:21 +02:00
|
|
|
if use_cisco_features:
|
2017-06-30 09:04:24 +02:00
|
|
|
numFeatures += numCiscoFeatures
|
|
|
|
outputFeatures = []
|
|
|
|
label = []
|
|
|
|
hits = []
|
|
|
|
trainNames = []
|
|
|
|
for i in range(windowSize):
|
2017-06-30 10:42:21 +02:00
|
|
|
outputFeatures.append(np.zeros([len(domains), maxLen]))
|
|
|
|
outputFeatures.append(np.zeros([len(domains), numFeatures]))
|
2017-06-30 09:04:24 +02:00
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
for i in tqdm(np.arange(len(domains)), miniters=10):
|
2017-06-30 09:04:24 +02:00
|
|
|
curCounter = 0
|
|
|
|
# print('len domainList: ' + str(len(domainLists[i])))
|
|
|
|
# print('len df: ' + str(len(dfLists[i])))
|
2017-06-30 10:42:21 +02:00
|
|
|
for j in range(np.min([windowSize, len(domains[i])])):
|
|
|
|
outputFeatures[curCounter][i, :] = get_domain_features(domains[i][j], charachterDict, maxLen)
|
2017-06-30 09:04:24 +02:00
|
|
|
curCounter += 1
|
2017-06-30 10:42:21 +02:00
|
|
|
if use_cisco_features:
|
|
|
|
outputFeatures[curCounter][i, 0:numFlowFeatures] = get_flow_features(dfs[i].iloc[j])
|
|
|
|
outputFeatures[curCounter][i, numFlowFeatures:] = get_cisco_features(dfs[i].iloc[j], urlSIPDIct)
|
2017-06-30 09:04:24 +02:00
|
|
|
else:
|
2017-06-30 10:42:21 +02:00
|
|
|
outputFeatures[curCounter][i, :] = get_flow_features(dfs[i].iloc[j])
|
2017-06-30 09:04:24 +02:00
|
|
|
curCounter += 1
|
|
|
|
curLabel = 0.0
|
2017-06-30 10:42:21 +02:00
|
|
|
if np.max(dfs[i][hitName]) >= threshold:
|
2017-06-30 09:04:24 +02:00
|
|
|
curLabel = 1.0
|
2017-06-30 10:42:21 +02:00
|
|
|
elif np.max(dfs[i][hitName]) == -1:
|
2017-06-30 09:04:24 +02:00
|
|
|
curLabel = -1.0
|
2017-06-30 10:42:21 +02:00
|
|
|
elif np.max(dfs[i][hitName]) > 0 and np.max(dfs[i][hitName]) < threshold:
|
2017-06-30 09:04:24 +02:00
|
|
|
curLabel = -2.0
|
|
|
|
label.append(curLabel)
|
2017-06-30 10:42:21 +02:00
|
|
|
hits.append(np.max(dfs[i][hitName]))
|
|
|
|
trainNames.append(np.unique(dfs[i]['user_hash']))
|
2017-06-30 09:04:24 +02:00
|
|
|
return (outputFeatures, np.array(label), np.array(hits), np.array(trainNames))
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_flow_data():
|
2017-06-30 10:42:21 +02:00
|
|
|
df = pd.read_csv("data/rk_data.csv.gz")
|
|
|
|
df.drop("Unnamed: 0", 1, inplace=True)
|
|
|
|
df.set_index(keys=['user_hash'], drop=False, inplace=True)
|
|
|
|
return df
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_flow_per_user(df):
|
|
|
|
users = df['user_hash'].unique().tolist()
|
|
|
|
for user in users:
|
|
|
|
yield df.loc[df.user_hash == user]
|