2017-06-27 20:29:19 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-07-12 10:25:55 +02:00
|
|
|
import logging
|
2017-06-30 09:04:24 +02:00
|
|
|
import string
|
2017-07-16 09:42:52 +02:00
|
|
|
from multiprocessing import Pool
|
2017-06-30 09:04:24 +02:00
|
|
|
|
2017-07-08 17:46:07 +02:00
|
|
|
import h5py
|
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-07-08 15:04:58 +02:00
|
|
|
from keras.utils import np_utils
|
2017-06-29 09:19:36 +02:00
|
|
|
from tqdm import tqdm
|
2017-06-27 20:29:19 +02:00
|
|
|
|
2017-07-12 10:25:55 +02:00
|
|
|
logger = logging.getLogger('logger')
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-07-11 21:06:58 +02:00
|
|
|
# TODO: refactor
|
2017-07-16 09:42:52 +02:00
|
|
|
def get_user_chunks(user_flow, window=10):
|
|
|
|
# TODO: what is maxLengthInSeconds for?!?
|
|
|
|
# maxMilliSeconds = maxLengthInSeconds * 1000
|
|
|
|
# domains = []
|
|
|
|
# flows = []
|
|
|
|
# if not overlapping:
|
|
|
|
# numBlocks = int(np.ceil(len(user_flow) / window))
|
|
|
|
# userIDs = np.arange(len(user_flow))
|
|
|
|
# for blockID in np.arange(numBlocks):
|
|
|
|
# curIDs = userIDs[(blockID * window):((blockID + 1) * window)]
|
|
|
|
# useData = user_flow.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 = user_flow.iloc[curIDs]
|
|
|
|
# curDomains = useData['domain']
|
|
|
|
# domains.append(list(curDomains))
|
|
|
|
# flows.append(useData)
|
|
|
|
# else:
|
|
|
|
# numBlocks = len(user_flow) + 1 - window
|
|
|
|
# userIDs = np.arange(len(user_flow))
|
|
|
|
# for blockID in np.arange(numBlocks):
|
|
|
|
# curIDs = userIDs[blockID:blockID + window]
|
|
|
|
# useData = user_flow.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 = user_flow.iloc[curIDs]
|
|
|
|
# curDomains = useData['domain']
|
|
|
|
# domains.append(list(curDomains))
|
|
|
|
# flows.append(useData)
|
|
|
|
# if domains and len(domains[-1]) != window:
|
|
|
|
# domains.pop(-1)
|
|
|
|
# flows.pop(-1)
|
|
|
|
# return domains, flows
|
|
|
|
chunk_size = (len(user_flow) // window)
|
|
|
|
last_inrange = chunk_size * window
|
|
|
|
return np.split(user_flow.head(last_inrange), chunk_size) if chunk_size else []
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
2017-07-11 21:06:58 +02:00
|
|
|
def get_domain_features(domain, vocab: dict, max_length=40):
|
2017-07-04 09:18:50 +02:00
|
|
|
encoding = np.zeros((max_length,))
|
2017-07-11 21:06:58 +02:00
|
|
|
for j in range(min(len(domain), max_length)):
|
|
|
|
char = domain[-j] # TODO: why -j -> order reversed for domain url?
|
|
|
|
encoding[j] = vocab.get(char, 0)
|
2017-07-04 09:18:50 +02:00
|
|
|
return encoding
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
2017-07-11 13:46:25 +02:00
|
|
|
def get_all_flow_features(features):
|
2017-07-16 09:42:52 +02:00
|
|
|
flows = np.stack(
|
|
|
|
map(lambda f: f[["duration", "bytes_up", "bytes_down"]], features)
|
2017-07-11 13:46:25 +02:00
|
|
|
)
|
|
|
|
return np.log1p(flows)
|
|
|
|
|
|
|
|
|
2017-07-11 21:06:58 +02:00
|
|
|
def create_dataset_from_flows(user_flow_df, char_dict, max_len, window_size=10):
|
2017-07-12 10:25:55 +02:00
|
|
|
logger.info("get chunks from user data frames")
|
2017-07-16 09:42:52 +02:00
|
|
|
with Pool() as pool:
|
|
|
|
results = []
|
|
|
|
for user_flow in tqdm(get_flow_per_user(user_flow_df), total=len(user_flow_df['user_hash'].unique().tolist())):
|
|
|
|
results.append(pool.apply_async(get_user_chunks, (user_flow, window_size)))
|
|
|
|
windows = [window for res in results for window in res.get()]
|
2017-07-12 10:25:55 +02:00
|
|
|
logger.info("create training dataset")
|
2017-07-16 09:42:52 +02:00
|
|
|
domain_tr, flow_tr, hits_tr, _, server_tr, trusted_hits_tr = create_dataset_from_lists(chunks=windows,
|
2017-07-11 21:06:58 +02:00
|
|
|
vocab=char_dict,
|
2017-07-16 09:42:52 +02:00
|
|
|
max_len=max_len)
|
2017-07-05 21:19:19 +02:00
|
|
|
# make client labels discrete with 4 different values
|
2017-07-06 16:27:47 +02:00
|
|
|
hits_tr = np.apply_along_axis(lambda x: discretize_label(x, 3), 0, np.atleast_2d(hits_tr))
|
2017-07-05 21:19:19 +02:00
|
|
|
# select only 1.0 and 0.0 from training data
|
2017-07-06 16:27:47 +02:00
|
|
|
pos_idx = np.where(np.logical_or(hits_tr == 1.0, trusted_hits_tr >= 1.0))[0]
|
|
|
|
neg_idx = np.where(hits_tr == 0.0)[0]
|
2017-07-05 21:19:19 +02:00
|
|
|
idx = np.concatenate((pos_idx, neg_idx))
|
|
|
|
# choose selected sample to train on
|
|
|
|
domain_tr = domain_tr[idx]
|
|
|
|
flow_tr = flow_tr[idx]
|
2017-07-06 16:27:47 +02:00
|
|
|
client_tr = np.zeros_like(idx, float)
|
|
|
|
client_tr[:pos_idx.shape[-1]] = 1.0
|
|
|
|
server_tr = server_tr[idx]
|
2017-07-05 21:19:19 +02:00
|
|
|
|
2017-07-08 15:04:58 +02:00
|
|
|
client_tr = np_utils.to_categorical(client_tr, 2)
|
|
|
|
server_tr = np_utils.to_categorical(server_tr, 2)
|
|
|
|
|
2017-07-06 16:27:47 +02:00
|
|
|
return domain_tr, flow_tr, client_tr, server_tr
|
2017-07-05 21:19:19 +02:00
|
|
|
|
2017-06-30 09:04:24 +02:00
|
|
|
|
2017-07-09 23:58:08 +02:00
|
|
|
def store_h5dataset(path, domain_tr, flow_tr, client_tr, server_tr):
|
|
|
|
f = h5py.File(path, "w")
|
2017-07-08 17:46:07 +02:00
|
|
|
domain_tr = domain_tr.astype(np.int8)
|
|
|
|
f.create_dataset("domain", data=domain_tr)
|
|
|
|
f.create_dataset("flow", data=flow_tr)
|
|
|
|
server_tr = server_tr.astype(np.bool)
|
|
|
|
client_tr = client_tr.astype(np.bool)
|
|
|
|
f.create_dataset("client", data=client_tr)
|
|
|
|
f.create_dataset("server", data=server_tr)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2017-07-09 23:58:08 +02:00
|
|
|
def load_h5dataset(path):
|
|
|
|
data = h5py.File(path, "r")
|
|
|
|
return data["domain"], data["flow"], data["client"], data["server"]
|
|
|
|
|
|
|
|
|
2017-07-16 09:42:52 +02:00
|
|
|
def create_dataset_from_lists(chunks, vocab, max_len):
|
2017-07-04 09:18:50 +02:00
|
|
|
"""
|
|
|
|
combines domain and feature windows to sequential training data
|
2017-07-16 09:42:52 +02:00
|
|
|
:param chunks: list of flow feature windows
|
2017-07-04 09:18:50 +02:00
|
|
|
:param vocab:
|
|
|
|
:param max_len:
|
|
|
|
:return:
|
|
|
|
"""
|
2017-07-16 09:42:52 +02:00
|
|
|
|
|
|
|
def get_domain_features_reduced(d):
|
|
|
|
return get_domain_features(d[0], vocab, max_len)
|
|
|
|
|
|
|
|
logger.info(" compute domain features")
|
|
|
|
domain_features = []
|
|
|
|
for ds in tqdm(map(lambda f: f.domain, chunks)):
|
|
|
|
assert min(np.atleast_3d(ds).shape) > 0, f"shape of 0 for {ds}"
|
|
|
|
domain_features.append(np.apply_along_axis(get_domain_features_reduced, 2, np.atleast_3d(ds)))
|
|
|
|
domain_features = np.concatenate(domain_features, 0)
|
|
|
|
logger.info(" compute flow features")
|
|
|
|
flow_features = get_all_flow_features(chunks)
|
|
|
|
logger.info(" select hits")
|
|
|
|
hits = np.max(np.stack(map(lambda f: f.virusTotalHits, chunks)), axis=1)
|
|
|
|
logger.info(" select names")
|
|
|
|
names = np.unique(np.stack(map(lambda f: f.user_hash, chunks)), axis=1)
|
|
|
|
logger.info(" select servers")
|
|
|
|
servers = np.max(np.stack(map(lambda f: f.serverLabel, chunks)), axis=1)
|
|
|
|
logger.info(" select trusted hits")
|
|
|
|
trusted_hits = np.max(np.stack(map(lambda f: f.trustedHits, chunks)), axis=1)
|
2017-07-11 21:06:58 +02:00
|
|
|
|
2017-07-05 18:37:29 +02:00
|
|
|
return (domain_features, flow_features,
|
2017-07-11 21:06:58 +02:00
|
|
|
hits, names, servers, trusted_hits)
|
2017-06-30 17:19:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def discretize_label(values, threshold):
|
2017-07-16 09:42:52 +02:00
|
|
|
max_val = np.max(values)
|
|
|
|
if max_val >= threshold:
|
2017-06-30 17:19:04 +02:00
|
|
|
return 1.0
|
2017-07-16 09:42:52 +02:00
|
|
|
elif max_val == -1:
|
2017-06-30 17:19:04 +02:00
|
|
|
return -1.0
|
2017-07-16 09:42:52 +02:00
|
|
|
elif 0 < max_val < threshold:
|
2017-06-30 17:19:04 +02:00
|
|
|
return -2.0
|
|
|
|
else:
|
|
|
|
return 0.0
|
2017-06-30 09:04:24 +02:00
|
|
|
|
|
|
|
|
2017-07-05 21:19:19 +02:00
|
|
|
def get_user_flow_data(csv_file):
|
2017-07-08 17:46:07 +02:00
|
|
|
types = {
|
|
|
|
"duration": int,
|
|
|
|
"bytes_down": int,
|
|
|
|
"bytes_up": int,
|
|
|
|
"domain": object,
|
|
|
|
"timeStamp": float,
|
|
|
|
"server_ip": object,
|
|
|
|
"user_hash": float,
|
|
|
|
"virusTotalHits": int,
|
|
|
|
"serverLabel": int,
|
|
|
|
"trustedHits": int
|
|
|
|
}
|
2017-07-05 21:19:19 +02:00
|
|
|
df = pd.read_csv(csv_file)
|
2017-07-08 17:46:07 +02:00
|
|
|
df = df[list(types.keys())]
|
2017-06-30 10:42:21 +02:00
|
|
|
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:
|
2017-07-16 09:42:52 +02:00
|
|
|
yield df.loc[df.user_hash == user].dropna(axis=0, how="any")
|
2017-07-14 14:58:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def load_or_generate_h5data(h5data, train_data, domain_length, window_size):
|
|
|
|
char_dict = get_character_dict()
|
|
|
|
logger.info(f"check for h5data {h5data}")
|
|
|
|
try:
|
|
|
|
open(h5data, "r")
|
2017-07-16 09:42:52 +02:00
|
|
|
raise FileNotFoundError
|
2017-07-14 14:58:17 +02:00
|
|
|
except FileNotFoundError:
|
|
|
|
logger.info("h5 data not found - load csv file")
|
|
|
|
user_flow_df = get_user_flow_data(train_data)
|
|
|
|
logger.info("create training dataset")
|
|
|
|
domain_tr, flow_tr, client_tr, server_tr = create_dataset_from_flows(user_flow_df, char_dict,
|
|
|
|
max_len=domain_length,
|
|
|
|
window_size=window_size)
|
|
|
|
logger.info("store training dataset as h5 file")
|
|
|
|
store_h5dataset(h5data, domain_tr, flow_tr, client_tr, server_tr)
|
|
|
|
logger.info("load h5 dataset")
|
|
|
|
return load_h5dataset(h5data)
|