-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
207 lines (181 loc) · 7.4 KB
/
Copy pathapp.py
File metadata and controls
207 lines (181 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Network Anomaly Detection System
This Streamlit app uses a trained machine learning model to detect spyware and spoofing in network traffic data.
Users can either enter feature values manually or upload a CSV file for batch processing.
"""
import streamlit as st
import pandas as pd
import joblib
import matplotlib.pyplot as plt
# Load the saved model
model = joblib.load('best_model.pkl')
# Define categorical feature categories
categorical_categories = {
'protocol_type': ['tcp', 'udp', 'icmp'],
'service': ['http', 'smtp', 'ftp', 'ssh', 'dns', 'other'],
'flag': ['SF', 'S0', 'S1', 'S2', 'S3', 'OTH']
}
# Load feature names (ensure this matches the training data)
feature_names = [
'duration', 'protocol_type', 'service', 'flag', 'src_bytes', 'dst_bytes',
'land', 'wrong_fragment', 'urgent', 'hot', 'num_failed_logins', 'logged_in',
'num_compromised', 'root_shell', 'su_attempted', 'num_root', 'num_file_creations',
'num_shells', 'num_access_files', 'num_outbound_cmds'
]
# Function to preprocess input data
def preprocess_input(data):
# Encode categorical features using predefined categories
for feature, categories in categorical_categories.items():
data[feature] = data[feature].map({cat: idx for idx, cat in enumerate(categories)})
# Skip scaling numerical features
return data
# Function to predict anomalies
def predict_anomaly(features):
df = pd.DataFrame([features], columns=feature_names)
df = preprocess_input(df) # Preprocess input data
prediction = model.predict(df)
return "Spyware/Spoofing Detected" if prediction == 1 else "Normal Traffic"
# Function to plot anomalies
def plot_anomalies(data):
anomalies = data[data['prediction'] == 1]
normal = data[data['prediction'] == 0]
plt.figure(figsize=(10, 5))
plt.scatter(normal.index, normal.iloc[:, 0], color='blue', label="Normal")
plt.scatter(anomalies.index, anomalies.iloc[:, 0], color='red', label="Spyware/Spoofing")
plt.legend()
plt.xlabel("Samples")
plt.ylabel("Feature Value")
st.pyplot(plt)
# Streamlit app
st.set_page_config(page_title="Spyware & Spoofing Detection", page_icon="🛡️", layout="wide")
# Custom CSS for styling
st.markdown(
"""
<style>
.stButton button {
background-color: #4CAF50;
color: white;
font-weight: bold;
border-radius: 5px;
padding: 10px 20px;
}
.stButton button:hover {
background-color: #45a049;
}
.stHeader {
color: #4CAF50;
}
.stSidebar {
background-color: #f0f2f6;
}
</style>
""",
unsafe_allow_html=True
)
# App title and description
st.title("🛡️ Spyware & Spoofing Detection System")
st.markdown("""
Welcome to the Spyware & Spoofing Detection System! This app uses a trained machine learning model to detect spyware and spoofing in network traffic data.
You can either enter feature values manually or upload a CSV file for batch processing.
""")
# Display the required feature set
st.header("📋 Required Features")
st.write("The following **20 features** are required for prediction:")
st.write(feature_names)
# Sidebar for single prediction
st.sidebar.header("🔍 Single Prediction")
features = []
for feature in feature_names:
if feature in categorical_categories: # Categorical features
value = st.sidebar.selectbox(f"Select {feature}", options=categorical_categories[feature])
else: # Numerical features
value = st.sidebar.number_input(f"Enter {feature}", value=0.0)
features.append(value)
# Predict anomaly for single input
if st.sidebar.button("Check for Spyware/Spoofing"):
result = predict_anomaly(features)
if result == "Spyware/Spoofing Detected":
st.sidebar.error(f"Prediction: **{result}** 🚨")
else:
st.sidebar.success(f"Prediction: **{result}** ✅")
# Main section for batch processing
st.header("📂 Batch Processing")
# File uploader
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
# Process uploaded file
if uploaded_file is not None:
try:
data = pd.read_csv(uploaded_file)
missing_features = set(feature_names) - set(data.columns)
extra_features = set(data.columns) - set(feature_names)
if not missing_features:
# Preprocess the uploaded data
data_preprocessed = preprocess_input(data[feature_names])
# Make predictions
predictions = model.predict(data_preprocessed)
data['prediction'] = predictions
st.success("✅ Your CSV file is ready for analysis!")
st.write("Predictions:")
st.write(data)
# Visualize anomalies
st.header("📊 Spyware/Spoofing Visualization")
plot_anomalies(data)
# Explanation for the visualization
st.markdown("""
### Understanding the Visualization
- **Blue Dots**: Represent **normal traffic**.
- **Red Dots**: Represent **spyware/spoofing attacks**.
- The x-axis represents the **sample index** (row number in the dataset).
- The y-axis represents the **value of the first feature** (`duration` in this case).
- The plot helps you visualize the distribution of normal traffic and spyware/spoofing attacks in your dataset.
""")
else:
st.error("❌ Oops! Your CSV file doesn't have all the required features.")
st.write("Here’s what’s missing:")
st.write(list(missing_features))
st.write("Please make sure your CSV file includes all the required features listed above.")
if extra_features:
st.warning("⚠️ Your CSV file has some extra features that aren't needed:")
st.write(list(extra_features))
st.write("You can ignore these extra features, but make sure all the required features are included.")
except Exception as e:
st.error(f"❌ Error reading the CSV file: {e}")
# Generate a sample CSV file for testing
st.header("📥 Generate a Sample CSV File")
if st.button("Download Sample CSV"):
# Create a sample DataFrame with the required 20 features and some extra features
sample_data = {
'duration': [0, 1, 2],
'protocol_type': ['tcp', 'udp', 'icmp'],
'service': ['http', 'smtp', 'ftp'],
'flag': ['SF', 'S0', 'S1'],
'src_bytes': [100, 200, 300],
'dst_bytes': [500, 600, 700],
'land': [0, 0, 0],
'wrong_fragment': [0, 0, 0],
'urgent': [0, 0, 0],
'hot': [0, 0, 0],
'num_failed_logins': [0, 0, 0],
'logged_in': [1, 1, 1],
'num_compromised': [0, 0, 0],
'root_shell': [0, 0, 0],
'su_attempted': [0, 0, 0],
'num_root': [0, 0, 0],
'num_file_creations': [0, 0, 0],
'num_shells': [0, 0, 0],
'num_access_files': [0, 0, 0],
'num_outbound_cmds': [0, 0, 0],
'extra_feature_1': [1, 2, 3], # Extra feature
'extra_feature_2': [4, 5, 6] # Extra feature
}
sample_df = pd.DataFrame(sample_data)
# Save the sample DataFrame to a CSV file
sample_df.to_csv("sample_data.csv", index=False)
# Provide the CSV file for download
with open("sample_data.csv", "rb") as file:
st.download_button(
label="Download Sample CSV",
data=file,
file_name="sample_data.csv",
mime="text/csv"
)