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-07-04 09:18:50 +02:00
|
|
|
chars = dict((char, idx + 1) for (idx, char) in
|
|
|
|
enumerate(string.ascii_lowercase + string.punctuation + string.digits))
|
|
|
|
|
2017-06-27 20:29:19 +02:00
|
|
|
|
2017-06-30 09:04:24 +02:00
|
|
|
def get_character_dict():
|
2017-07-04 09:18:50 +02:00
|
|
|
return chars
|
|
|
|
|
|
|
|
|
|
|
|
def encode_char(c):
|
|
|
|
if c in chars:
|
|
|
|
return chars[c]
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
encode_char = np.vectorize(encode_char)
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_user_chunks(dataFrame, windowSize=10, overlapping=False,
|
|
|
|
maxLengthInSeconds=300):
|
|
|
|
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]
|
|
|
|
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)
|
2017-07-04 09:18:50 +02:00
|
|
|
if len(outDomainLists[-1]) != windowSize:
|
|
|
|
outDomainLists.pop(-1)
|
|
|
|
outDFFrames.pop(-1)
|
2017-06-30 09:04:24 +02:00
|
|
|
return (outDomainLists, outDFFrames)
|
|
|
|
|
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
def get_domain_features(domain, vocab, max_length=40):
|
2017-07-04 09:18:50 +02:00
|
|
|
encoding = np.zeros((max_length,))
|
2017-06-30 10:42:21 +02:00
|
|
|
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:
|
2017-07-04 09:18:50 +02:00
|
|
|
encoding[j] = vocab[curCharacter]
|
|
|
|
return encoding
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
def get_flow_features(flow):
|
2017-06-30 17:19:04 +02:00
|
|
|
keys = ['duration', 'bytes_down', 'bytes_up']
|
|
|
|
features = np.zeros([len(keys), ])
|
|
|
|
for i, key in enumerate(keys):
|
|
|
|
# TODO: does it still works after exceptions occur -- default: zero!
|
|
|
|
# i wonder whether something brokes
|
|
|
|
# if there are exceptions regarding to inconsistent feature length
|
2017-06-30 09:04:24 +02:00
|
|
|
try:
|
2017-06-30 17:19:04 +02:00
|
|
|
features[i] = np.log1p(flow[key]).astype(float)
|
2017-06-30 09:04:24 +02:00
|
|
|
except:
|
|
|
|
pass
|
2017-06-30 17:19:04 +02:00
|
|
|
return features
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
2017-06-30 17:19:04 +02:00
|
|
|
def get_cisco_features(curDataLine, urlSIPDict):
|
2017-06-30 09:04:24 +02:00
|
|
|
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-07-04 09:18:50 +02:00
|
|
|
def create_dataset_from_flows(user_flow_df, char_dict, max_len, window_size=10, use_cisco_features=False):
|
|
|
|
domains = []
|
|
|
|
features = []
|
2017-06-30 09:04:24 +02:00
|
|
|
print("get chunks from user data frames")
|
|
|
|
for i, user_flow in enumerate(get_flow_per_user(user_flow_df)):
|
2017-07-04 09:18:50 +02:00
|
|
|
(domain_windows, feature_windows) = get_user_chunks(user_flow,
|
|
|
|
windowSize=window_size,
|
2017-07-05 19:16:03 +02:00
|
|
|
overlapping=False,
|
2017-07-04 09:18:50 +02:00
|
|
|
maxLengthInSeconds=-1)
|
|
|
|
domains += domain_windows
|
|
|
|
features += feature_windows
|
2017-06-30 17:19:04 +02:00
|
|
|
# TODO: remove later
|
2017-07-05 19:16:03 +02:00
|
|
|
if i >= 50:
|
2017-06-30 09:04:24 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
print("create training dataset")
|
|
|
|
return create_dataset_from_lists(
|
2017-07-04 09:18:50 +02:00
|
|
|
domains=domains, features=features, vocab=char_dict,
|
|
|
|
max_len=max_len,
|
2017-06-30 10:42:21 +02:00
|
|
|
use_cisco_features=use_cisco_features, urlSIPDIct=dict(),
|
2017-07-04 09:18:50 +02:00
|
|
|
window_size=window_size)
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
2017-07-04 09:18:50 +02:00
|
|
|
def create_dataset_from_lists(domains, features, vocab, max_len,
|
2017-06-30 10:42:21 +02:00
|
|
|
use_cisco_features=False, urlSIPDIct=dict(),
|
2017-06-30 17:19:04 +02:00
|
|
|
window_size=10):
|
2017-07-04 09:18:50 +02:00
|
|
|
"""
|
|
|
|
combines domain and feature windows to sequential training data
|
|
|
|
:param domains: list of domain windows
|
|
|
|
:param features: list of feature windows
|
|
|
|
:param vocab:
|
|
|
|
:param max_len:
|
|
|
|
:param use_cisco_features: idk
|
|
|
|
:param urlSIPDIct: idk
|
|
|
|
:param window_size: size of the flow window
|
|
|
|
:return:
|
|
|
|
"""
|
2017-06-30 17:42:18 +02:00
|
|
|
# TODO: check for hits vs vth consistency
|
2017-07-04 09:18:50 +02:00
|
|
|
# if 'hits' in dfs[0].keys():
|
|
|
|
# hits_col = 'hits'
|
|
|
|
# elif 'virusTotalHits' in dfs[0].keys():
|
|
|
|
# hits_col = 'virusTotalHits'
|
|
|
|
hits_col = "virusTotalHits"
|
|
|
|
|
2017-06-30 09:04:24 +02:00
|
|
|
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
|
2017-07-04 09:18:50 +02:00
|
|
|
sample_size = len(domains)
|
2017-06-30 09:04:24 +02:00
|
|
|
hits = []
|
2017-06-30 17:19:04 +02:00
|
|
|
names = []
|
|
|
|
servers = []
|
|
|
|
trusted_hits = []
|
2017-07-04 09:18:50 +02:00
|
|
|
|
|
|
|
domain_features = np.zeros((sample_size, window_size, max_len))
|
|
|
|
flow_features = np.zeros((sample_size, window_size, numFeatures))
|
|
|
|
|
|
|
|
for i in tqdm(np.arange(sample_size), miniters=10):
|
|
|
|
for j in range(window_size):
|
|
|
|
domain_features[i, j] = get_domain_features(domains[i][j], vocab, max_len)
|
|
|
|
flow_features[i, j] = get_flow_features(features[i].iloc[j])
|
|
|
|
# TODO: cisco features?
|
|
|
|
|
|
|
|
hits.append(np.max(features[i][hits_col]))
|
|
|
|
names.append(np.unique(features[i]['user_hash']))
|
|
|
|
servers.append(np.max(features[i]['serverLabel']))
|
|
|
|
trusted_hits.append(np.max(features[i]['trustedHits']))
|
2017-07-05 18:37:29 +02:00
|
|
|
return (domain_features, flow_features,
|
|
|
|
np.array(hits), np.array(names), np.array(servers), np.array(trusted_hits))
|
2017-06-30 17:19:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def discretize_label(values, threshold):
|
|
|
|
maxVal = np.max(values)
|
|
|
|
if maxVal >= threshold:
|
|
|
|
return 1.0
|
|
|
|
elif maxVal == -1:
|
|
|
|
return -1.0
|
|
|
|
elif 0 < maxVal < threshold:
|
|
|
|
return -2.0
|
|
|
|
else:
|
|
|
|
return 0.0
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
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]
|