Skip to content

Commit 5a85b6e

Browse files
authored
Merge pull request #47 from Critical-Infrastructure-Systems-Lab/dev
Preparing version 0.5.0
2 parents 962da66 + 58a3afd commit 5a85b6e

File tree

20 files changed

+154529
-145
lines changed

20 files changed

+154529
-145
lines changed
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
���ʫ���h������ۮ������� ��������(��労����2
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
�2root"_tf_keras_network*�2{"name": "generator", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": false, "class_name": "Functional", "config": {"name": "generator", "trainable": true, "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 39]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "name": "input_1", "inbound_nodes": []}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 19, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 39, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_1", "inbound_nodes": [[["dense", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 19, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_2", "inbound_nodes": [[["dense_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 39, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_3", "inbound_nodes": [[["dense_2", 0, 0, {}]]]}], "input_layers": [["input_1", 0, 0]], "output_layers": [["dense_3", 0, 0]]}, "shared_object_id": 13, "input_spec": [{"class_name": "InputSpec", "config": {"dtype": null, "shape": {"class_name": "__tuple__", "items": [null, 39]}, "ndim": 2, "max_ndim": null, "min_ndim": null, "axes": {}}}], "build_input_shape": {"class_name": "TensorShape", "items": [null, 39]}, "is_graph_network": true, "full_save_spec": {"class_name": "__tuple__", "items": [[{"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 39]}, "float32", "input_1"]}], {}]}, "save_spec": {"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 39]}, "float32", "input_1"]}, "keras_version": "2.13.1", "backend": "tensorflow", "model_config": {"class_name": "Functional", "config": {"name": "generator", "trainable": true, "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 39]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "name": "input_1", "inbound_nodes": [], "shared_object_id": 0}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 19, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 1}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 2}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense", "inbound_nodes": [[["input_1", 0, 0, {}]]], "shared_object_id": 3}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 39, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 4}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 5}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_1", "inbound_nodes": [[["dense", 0, 0, {}]]], "shared_object_id": 6}, {"class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 19, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 7}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 8}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_2", "inbound_nodes": [[["dense_1", 0, 0, {}]]], "shared_object_id": 9}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 39, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 10}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 11}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_3", "inbound_nodes": [[["dense_2", 0, 0, {}]]], "shared_object_id": 12}], "input_layers": [["input_1", 0, 0]], "output_layers": [["dense_3", 0, 0]]}}, "training_config": {"loss": "mean_squared_error", "metrics": null, "weighted_metrics": null, "loss_weights": null, "optimizer_config": {"class_name": "Custom>Adam", "config": {"name": "Adam", "weight_decay": null, "clipnorm": null, "global_clipnorm": null, "clipvalue": null, "use_ema": false, "ema_momentum": 0.99, "ema_overwrite_frequency": null, "jit_compile": false, "is_legacy_optimizer": false, "learning_rate": 6.25000029685907e-05, "beta_1": 0.9, "beta_2": 0.999, "epsilon": 1e-07, "amsgrad": false}}}}2
3+
� root.layer-0"_tf_keras_input_layer*�{"class_name": "InputLayer", "name": "input_1", "dtype": "float32", "sparse": false, "ragged": false, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 39]}, "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 39]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}}2
4+
�root.layer_with_weights-0"_tf_keras_layer*�{"name": "dense", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 19, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 1}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 2}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "inbound_nodes": [[["input_1", 0, 0, {}]]], "shared_object_id": 3, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 39}}, "shared_object_id": 15}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 39]}}2
5+
�root.layer_with_weights-1"_tf_keras_layer*�{"name": "dense_1", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 39, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 4}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 5}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "inbound_nodes": [[["dense", 0, 0, {}]]], "shared_object_id": 6, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 19}}, "shared_object_id": 16}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 19]}}2
6+
�root.layer_with_weights-2"_tf_keras_layer*�{"name": "dense_2", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 19, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 7}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 8}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "inbound_nodes": [[["dense_1", 0, 0, {}]]], "shared_object_id": 9, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 39}}, "shared_object_id": 17}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 39]}}2
7+
�root.layer_with_weights-3"_tf_keras_layer*�{"name": "dense_3", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 39, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotNormal", "config": {"seed": 12345}, "shared_object_id": 10}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 11}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "inbound_nodes": [[["dense_2", 0, 0, {}]]], "shared_object_id": 12, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 19}}, "shared_object_id": 18}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 19]}}2
8+
�\root.keras_api.metrics.0"_tf_keras_metric*�{"class_name": "Mean", "name": "loss", "dtype": "float32", "config": {"name": "loss", "dtype": "float32"}, "shared_object_id": 19}2
Binary file not shown.
Binary file not shown.

dhalsim/network_attacks/black_box_concealment_attack.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
from dhalsim.network_attacks.utilities import launch_arp_poison, restore_arp
1414
from dhalsim.network_attacks.synced_attack import SyncedAttack
15-
from evasion_attacks.Adversarial_Attacks.Black_Box_Attack import adversarial_AE
16-
17-
from tensorflow.keras.models import Model, load_model
15+
#from tensorflow.keras.models import Model, load_model
1816

1917

2018
class Error(Exception):

dhalsim/network_attacks/concealment_ae_model.py

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from tensorflow.keras.optimizers import Adam
44
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
55
from sklearn.preprocessing import MinMaxScaler
6+
import joblib
67
from tensorflow.keras.initializers import glorot_normal
78

89
import pandas as pd
@@ -13,6 +14,8 @@
1314
from sklearn.preprocessing import MinMaxScaler
1415
from sklearn.model_selection import train_test_split
1516

17+
import joblib
18+
1619
# This module is based on the implementation by Alessandro Erba, original is found here:
1720
# https://github.com/scy-phy/ICS-Evasion-Attacks/blob/master/Adversarial_Attacks/Black_Box_Attack/adversarial_AE.py
1821

@@ -37,7 +40,10 @@ def preprocess_physical(self, path):
3740

3841
return a_pd
3942

40-
def train_model(self):
43+
def train_model(self, training_path):
44+
45+
self.init_generator(training_path)
46+
4147
# fit of the scaler is done at __init__
4248
ben_data = self.physical_pd
4349
ben_data[self.sensor_cols] = self.attacker_scaler.transform(ben_data[self.sensor_cols])
@@ -54,8 +60,48 @@ def train_model(self):
5460
verbose=2,
5561
validation_data=(x_ben_test, x_ben_test))
5662

63+
# print('Printing data used for training')
64+
ben_data.to_csv('trained_data.csv', index=False)
65+
66+
# Loads the scaler originally used to train the model
67+
def load_scaler(self, scaler_path):
68+
#scaler_path = Path.cwd()/'attacker_scaler.gz'
69+
70+
print('Loading scaler at', str(scaler_path))
71+
self.attacker_scaler = joblib.load(str(scaler_path))
72+
print('Scaler loaded')
73+
74+
# Saves the model and the scaler used to train the model
5775
def save_model(self, filename):
58-
self.generator.save(filename)
76+
print('saving trained model at: ', str(filename))
77+
self.generator.save(str(model_path))
78+
79+
scaler_path = Path.cwd()
80+
print('saved scaler model at: ', filename)
81+
joblib.dump(self.attacker_scaler, 'ctown_attacker_scaler.gz')
82+
83+
def init_generator(self, training_path):
84+
# Load and preprocess training data
85+
training_path = Path(__file__).parent/training_path/'training_data.csv'
86+
# print('Reading training data from: ' + str(training_path))
87+
self.physical_pd = self.preprocess_physical(training_path)
88+
89+
# Adversarial model for concealment
90+
# toDo: Ask about this parameter
91+
hide_layers = 39
92+
self.hide_layers = hide_layers
93+
self.generator_layers = [self.feature_dims,
94+
int(self.hide_layers / 2),
95+
self.hide_layers,
96+
int(self.hide_layers / 2), self.feature_dims]
97+
98+
optimizer = Adam(lr=0.001)
99+
# Build the generator
100+
self.generator = self.build_generator()
101+
self.generator.compile(optimizer=optimizer, loss='mean_squared_error')
102+
103+
self.attacker_scaler = MinMaxScaler()
104+
self.transform_fit_scaler()
59105

60106
def build_generator(self):
61107
input = Input(shape=(self.feature_dims,))
@@ -85,40 +131,18 @@ def fix_sample(self, gen_examples):
85131

86132
def predict(self, received_values_df):
87133
print('Attempting to predict concealment values')
134+
# print('Features received to predict: ' + str(received_values_df.columns))
135+
# print('Features received to train: ' + str(self.sensor_cols))
136+
88137
gen_examples = self.generator.predict(self.attacker_scaler.transform(received_values_df))
89138
gen_examples = self.fix_sample(pd.DataFrame(columns=self.sensor_cols,
90139
data=self.attacker_scaler.inverse_transform(gen_examples)))
91140

92141
return gen_examples
93142

94-
def __init__(self, a_path):
95-
# Load and preprocess training data
96-
training_path = Path(__file__).parent/a_path/'training_data.csv'
97-
print('Reading training data from: ' + str(training_path))
98-
self.physical_pd = self.preprocess_physical(training_path)
99-
100-
# Adversarial model for concealment
101-
# toDo: Ask about this parameter
102-
hide_layers = 39
143+
def __init__(self, features_list):
103144

104-
self.sensor_cols = [col for col in self.physical_pd.columns if
145+
# Initialize model with features list
146+
self.sensor_cols = [col for col in features_list if
105147
col not in ['Unnamed: 0', 'iteration', 'timestamp', 'Attack']]
106-
107148
self.feature_dims = len(self.sensor_cols)
108-
109-
self.hide_layers = hide_layers
110-
self.generator_layers = [self.feature_dims,
111-
int(self.hide_layers / 2),
112-
self.hide_layers,
113-
int(self.hide_layers / 2), self.feature_dims]
114-
115-
optimizer = Adam(lr=0.001)
116-
# Build the generator
117-
self.generator = self.build_generator()
118-
self.generator.compile(optimizer=optimizer, loss='mean_squared_error')
119-
120-
self.attacker_scaler = MinMaxScaler()
121-
self.transform_fit_scaler()
122-
123-
124-

0 commit comments

Comments
 (0)