-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing anomaly detection with tensorflow
- Loading branch information
Matthias Niedermaier
committed
Dec 3, 2024
1 parent
08a7881
commit 8e07bc5
Showing
7 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import pyshark | ||
import pandas as pd | ||
import numpy as np | ||
import tensorflow as tf | ||
from sklearn.model_selection import train_test_split | ||
from tensorflow.keras.models import Sequential | ||
from tensorflow.keras.layers import Dense, Dropout | ||
from tensorflow.keras.optimizers import Adam | ||
|
||
|
||
def parse_modbus_layer_fields(pcap_file): | ||
""" | ||
Parse a PCAP file and extract all fields dynamically from the Modbus layer. | ||
""" | ||
print(f"Parsing Modbus TCP traffic from PCAP file: {pcap_file}") | ||
capture = pyshark.FileCapture(pcap_file, display_filter="modbus") | ||
traffic_data = [] | ||
|
||
for packet in capture: | ||
if 'MODBUS' in packet: | ||
modbus_layer = packet['MODBUS'] | ||
packet_data = {} | ||
# Iterate through all field names in the Modbus layer | ||
for field in modbus_layer.field_names: | ||
try: | ||
packet_data[field] = getattr(modbus_layer, field) | ||
except AttributeError: | ||
packet_data[field] = None # If field is not available | ||
traffic_data.append(packet_data) | ||
|
||
capture.close() | ||
return pd.DataFrame(traffic_data) | ||
|
||
|
||
def preprocess_data(data): | ||
""" | ||
Preprocess Modbus TCP data for TensorFlow. | ||
""" | ||
# Fill missing values and convert non-numeric fields to numeric | ||
data = data.fillna(0) | ||
for col in data.columns: | ||
try: | ||
data[col] = pd.to_numeric(data[col], errors='coerce').fillna(0) | ||
except ValueError: | ||
pass | ||
|
||
# Normalize the data | ||
normalized_data = (data - data.min()) / (data.max() - data.min()) | ||
return normalized_data | ||
|
||
|
||
def build_and_train_model(data): | ||
""" | ||
Build and train a TensorFlow model on the Modbus TCP dataset. | ||
""" | ||
# Add a label column (0 = non-malicious, for this dataset) | ||
data['label'] = 0 | ||
|
||
# Split features and labels | ||
X = data.drop("label", axis=1).values | ||
y = data["label"].values | ||
|
||
# Split dataset into training and testing sets | ||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | ||
|
||
# Build the TensorFlow model | ||
model = Sequential([ | ||
Dense(64, input_dim=X_train.shape[1], activation='relu'), | ||
Dropout(0.2), | ||
Dense(32, activation='relu'), | ||
Dropout(0.2), | ||
Dense(16, activation='relu'), | ||
Dense(1, activation='sigmoid') # Binary classification | ||
]) | ||
|
||
model.compile(optimizer=Adam(learning_rate=0.001), | ||
loss='binary_crossentropy', | ||
metrics=['accuracy']) | ||
|
||
# Train the model | ||
model.fit(X_train, y_train, validation_data=(X_test, y_test), | ||
epochs=50, batch_size=32, verbose=2) | ||
|
||
# Evaluate the model | ||
loss, accuracy = model.evaluate(X_test, y_test, verbose=0) | ||
print(f"Test Loss: {loss:.4f}, Test Accuracy: {accuracy:.4f}") | ||
|
||
# Save the trained model | ||
model.save("modbus_tf_model.keras") | ||
print("TensorFlow model saved as modbus_tf_model.keras") | ||
|
||
|
||
if __name__ == "__main__": | ||
# Replace with your actual PCAP file path | ||
pcap_file = "modbus_traffic_regular.pcap" | ||
|
||
# Step 1: Extract Modbus TCP traffic | ||
modbus_data = parse_modbus_layer_fields(pcap_file) | ||
|
||
if not modbus_data.empty: | ||
print("Extracted Modbus Data:") | ||
print(modbus_data.head()) | ||
|
||
# Step 2: Preprocess the data | ||
preprocessed_data = preprocess_data(modbus_data) | ||
|
||
# Step 3: Build and train the TensorFlow model | ||
build_and_train_model(preprocessed_data) | ||
else: | ||
print("No Modbus TCP traffic found in the PCAP file.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters