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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Cybersecurity Attacks
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1IaRXrO_gtRMKaHU7_IPzZJ705GonLuMV
|
8 |
+
"""
|
9 |
+
|
10 |
+
import numpy as np
|
11 |
+
import pandas as pd
|
12 |
+
import plotly.express as px
|
13 |
+
import seaborn as sns
|
14 |
+
import matplotlib.pyplot as plt
|
15 |
+
import re
|
16 |
+
|
17 |
+
pd.options.display.float_format = '{:,.2f}'.format
|
18 |
+
|
19 |
+
df = pd.read_csv("/content/cybersecurity_attacks.csv")
|
20 |
+
|
21 |
+
print(f"There are {df.shape[0]}, row and {df.shape[1]} columns in the dataset")
|
22 |
+
|
23 |
+
df.columns
|
24 |
+
|
25 |
+
df.isna().sum()
|
26 |
+
|
27 |
+
df.duplicated().sum()
|
28 |
+
|
29 |
+
df['Malware Indicators'] = df['Malware Indicators'].apply(lambda x: 'No Detection' if pd.isna(x) else x)
|
30 |
+
|
31 |
+
df['Alerts/Warnings'] = df['Alerts/Warnings'].apply(lambda x: 'yes' if x == 'Alert Triggered' else 'no')
|
32 |
+
|
33 |
+
df['Prowx Information'] = df['Proxy Information'].apply(lambda x: 'No proxy' if pd.isna(x) else x)
|
34 |
+
|
35 |
+
df['Firewall Logs'] = df['Firewall Logs'].apply(lambda x: 'No Data' if pd.isna(x) else x)
|
36 |
+
|
37 |
+
df['IDS/IPS Alerts'] = df['IDS/IPS Alerts'].apply(lambda x: 'No Data' if pd.isna(x) else x)
|
38 |
+
|
39 |
+
df['Source Port'] = df['Source Port'].apply(lambda x: x if 0 <= x <= 65535 else 'Invalid Port')
|
40 |
+
df['Destination Port'] = df['Destination Port'].apply(lambda x: x if 0 <= x <= 65535 else 'Invalid Port')
|
41 |
+
|
42 |
+
df.isna().sum()
|
43 |
+
|
44 |
+
df['Device Information'].value_counts()
|
45 |
+
|
46 |
+
df['Browser'] = df['Device Information'].str.split('/').str[0]
|
47 |
+
|
48 |
+
df['Browser'].unique()
|
49 |
+
|
50 |
+
patterns = [
|
51 |
+
r'Windows',
|
52 |
+
r'Linux',
|
53 |
+
r'Android',
|
54 |
+
r'iPad',
|
55 |
+
r'iPhone',
|
56 |
+
r'Macintosh',
|
57 |
+
]
|
58 |
+
|
59 |
+
def extract_device_or_os(user_agent):
|
60 |
+
for pattern in patterns:
|
61 |
+
match = re.search(pattern, user_agent, re.I)
|
62 |
+
if match:
|
63 |
+
return match.group()
|
64 |
+
|
65 |
+
return 'Unknown'
|
66 |
+
|
67 |
+
df['Device/OS'] = df['Device Information'].apply(extract_device_or_os)
|
68 |
+
|
69 |
+
df = df.drop('Device Information', axis = 1)
|
70 |
+
|
71 |
+
device_counts = df['Device/OS'].value_counts()
|
72 |
+
|
73 |
+
device_counts_df = pd.DataFrame(device_counts).reset_index()
|
74 |
+
device_counts_df.columns = ['Device/OS', 'Count of Attacks']
|
75 |
+
|
76 |
+
top_devices = device_counts_df.head(10)
|
77 |
+
print(top_devices)
|
78 |
+
|
79 |
+
plt.figure(figsize=(10, 6))
|
80 |
+
sns.barplot(x='Count of Attacks', y='Device/OS', hue='Device/OS', data=top_devices, palette='viridis', dodge=False)
|
81 |
+
plt.xlabel('Count of Attacks')
|
82 |
+
plt.ylabel('Device/OS')
|
83 |
+
plt.title('Top 10 Devices/OS Targeted by Attacks')
|
84 |
+
plt.tight_layout()
|
85 |
+
plt.show()
|
86 |
+
|
87 |
+
attack_type_counts = df['Attack Type'].value_counts()
|
88 |
+
attack_type_counts_df = pd.DataFrame(attack_type_counts).reset_index()
|
89 |
+
attack_type_counts_df.columns = ['Attack Type', 'Count of Attacks']
|
90 |
+
top_attack_types = attack_type_counts_df.head(10)
|
91 |
+
print(top_attack_types)
|
92 |
+
|
93 |
+
plt.figure(figsize=(10, 6))
|
94 |
+
sns.barplot(x='Count of Attacks', y='Attack Type', hue='Attack Type', data=top_attack_types, palette='viridis', dodge=False)
|
95 |
+
plt.xlabel('Count of Attacks')
|
96 |
+
plt.ylabel('Attack Type')
|
97 |
+
plt.title('Top 10 Most Common Attack Types')
|
98 |
+
plt.tight_layout()
|
99 |
+
plt.show()
|
100 |
+
|
101 |
+
geo_location_counts = df['Geo-location Data'].value_counts()
|
102 |
+
|
103 |
+
geo_location_counts_df = pd.DataFrame(geo_location_counts).reset_index()
|
104 |
+
geo_location_counts_df.columns = ['Geo-location Data', 'Count of Attacks']
|
105 |
+
top_geo_locations = geo_location_counts_df.head(10)
|
106 |
+
print(top_geo_locations)
|
107 |
+
|
108 |
+
plt.figure(figsize=(10, 6))
|
109 |
+
sns.barplot(x='Count of Attacks', y='Geo-location Data', hue='Geo-location Data', data=top_geo_locations, palette='viridis', dodge=False)
|
110 |
+
plt.xlabel('Count of Attacks')
|
111 |
+
plt.ylabel('Geo-location Data')
|
112 |
+
plt.title('Top 10 Geographic Locations Source of Malicious Traffic')
|
113 |
+
plt.tight_layout()
|
114 |
+
plt.show()
|
115 |
+
|
116 |
+
destination_port_counts = df['Destination Port'].value_counts()
|
117 |
+
destination_port_counts_df = pd.DataFrame(destination_port_counts).reset_index()
|
118 |
+
destination_port_counts_df.columns = ['Destination Port', 'Count of Attacks']
|
119 |
+
|
120 |
+
top_destination_ports = destination_port_counts_df.head(10)
|
121 |
+
print(top_destination_ports)
|
122 |
+
|
123 |
+
plt.figure(figsize=(10, 6))
|
124 |
+
sns.barplot(x='Destination Port', y='Count of Attacks', hue='Count of Attacks', data=top_destination_ports, palette='viridis', dodge=False)
|
125 |
+
plt.xlabel('Destination Port')
|
126 |
+
plt.ylabel('Count of Attacks')
|
127 |
+
plt.title('Top 10 Most Targeted Destination Ports')
|
128 |
+
plt.tight_layout()
|
129 |
+
plt.show()
|
130 |
+
|
131 |
+
severity_mapping = {'Low': 1, 'Medium': 2, 'High': 3}
|
132 |
+
df['Severity Level Numeric'] = df['Severity Level'].map(severity_mapping)
|
133 |
+
|
134 |
+
protocol_severity = df.groupby('Protocol')['Severity Level Numeric'].mean().reset_index()
|
135 |
+
|
136 |
+
protocol_severity = protocol_severity.sort_values(by='Severity Level Numeric', ascending=False)
|
137 |
+
|
138 |
+
top_protocol_severity = protocol_severity.head(10)
|
139 |
+
print(top_protocol_severity)
|
140 |
+
|
141 |
+
plt.figure(figsize=(10, 6))
|
142 |
+
sns.barplot(x='Severity Level Numeric', y='Protocol', hue='Severity Level Numeric', data=top_protocol_severity, palette='viridis', dodge=False)
|
143 |
+
plt.xlabel('Mean Severity Level')
|
144 |
+
plt.ylabel('Protocol')
|
145 |
+
plt.title('Top 10 Protocols by Mean Severity Level')
|
146 |
+
plt.tight_layout()
|
147 |
+
plt.show()
|
148 |
+
|
149 |
+
df['Timestamp'] = pd.to_datetime(df['Timestamp'], errors='coerce')
|
150 |
+
df['Month'] = df['Timestamp'].dt.month
|
151 |
+
month_counts = df['Month'].value_counts()
|
152 |
+
month_counts_df = pd.DataFrame(month_counts).reset_index()
|
153 |
+
month_counts_df.columns = ['Month', 'Count of Attacks']
|
154 |
+
sorted_month_counts = month_counts_df.sort_values(by='Month')
|
155 |
+
print(sorted_month_counts)
|
156 |
+
|
157 |
+
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
|
158 |
+
df['Month'] = df['Timestamp'].dt.month
|
159 |
+
|
160 |
+
attacks_by_month = df.groupby('Month').size().reset_index(name='Attack Count')
|
161 |
+
|
162 |
+
heatmap_data = attacks_by_month.pivot_table(index='Month', columns='Month', values='Attack Count', aggfunc='sum', fill_value=0)
|
163 |
+
plt.figure(figsize=(10, 6))
|
164 |
+
sns.heatmap(heatmap_data, annot=True, fmt='d', cmap='YlOrRd', cbar=True)
|
165 |
+
plt.title('Cybersecurity Attacks Frequency by Month')
|
166 |
+
plt.xlabel('Month')
|
167 |
+
plt.ylabel('Month')
|
168 |
+
plt.tight_layout()
|
169 |
+
plt.show()
|
170 |
+
|
171 |
+
malicious_traffic = df[df['Malware Indicators'] == 'IoCDetected']
|
172 |
+
|
173 |
+
traffic_type_counts = malicious_traffic['Traffic Type'].value_counts()
|
174 |
+
traffic_type_counts_df = pd.DataFrame(traffic_type_counts).reset_index()
|
175 |
+
traffic_type_counts_df.columns = ['Traffic Type', 'Count of Malicious Incidents']
|
176 |
+
|
177 |
+
top_traffic_types = traffic_type_counts_df.head(10)
|
178 |
+
print(top_traffic_types)
|
179 |
+
|
180 |
+
plt.figure(figsize=(10, 6))
|
181 |
+
sns.barplot(x='Count of Malicious Incidents', y='Traffic Type', hue='Count of Malicious Incidents', data=top_traffic_types, palette='viridis', dodge=False)
|
182 |
+
plt.xlabel('Count of Malicious Incidents')
|
183 |
+
plt.ylabel('Traffic Type')
|
184 |
+
plt.title('Top Traffic Types Flagged with "IoC Detected"')
|
185 |
+
plt.tight_layout()
|
186 |
+
plt.show()
|
187 |
+
|
188 |
+
threshold = 75.0
|
189 |
+
|
190 |
+
infiltration_data = df[df['Anomaly Scores'] > threshold]
|
191 |
+
|
192 |
+
vulnerable_traffic_counts = infiltration_data['Traffic Type'].value_counts()
|
193 |
+
|
194 |
+
vulnerable_traffic_df = pd.DataFrame(vulnerable_traffic_counts).reset_index()
|
195 |
+
vulnerable_traffic_df.columns = ['Traffic Type', 'Count of Infiltrations']
|
196 |
+
|
197 |
+
top_vulnerable_traffic = vulnerable_traffic_df.head(10)
|
198 |
+
print(top_vulnerable_traffic)
|
199 |
+
|
200 |
+
plt.figure(figsize=(10, 6))
|
201 |
+
sns.barplot(x='Count of Infiltrations', y='Traffic Type', hue='Count of Infiltrations', data=top_vulnerable_traffic, dodge=False)
|
202 |
+
plt.xlabel('Count of Infiltrations')
|
203 |
+
plt.ylabel('Traffic Type')
|
204 |
+
plt.title('Top Traffic Types Vulnerable to Infiltration (Anomaly Scores)')
|
205 |
+
plt.tight_layout()
|
206 |
+
plt.show()
|
207 |
+
|
208 |
+
threshold = df['Anomaly Scores'].quantile(0.95)
|
209 |
+
print(f"Threshold for Anomaly Scores: {threshold}\n")
|
210 |
+
|
211 |
+
infiltration_data = df[df['Anomaly Scores'] > threshold]
|
212 |
+
|
213 |
+
print(infiltration_data.head())
|
214 |
+
|
215 |
+
vulnerable_traffic_counts = infiltration_data['Traffic Type'].value_counts()
|
216 |
+
vulnerable_traffic_df = pd.DataFrame(vulnerable_traffic_counts).reset_index()
|
217 |
+
vulnerable_traffic_df.columns = ['Traffic Type', 'Count of Infiltrations']
|
218 |
+
|
219 |
+
top_vulnerable_traffic = vulnerable_traffic_df.head(10)
|
220 |
+
print(top_vulnerable_traffic)
|
221 |
+
|
222 |
+
plt.figure(figsize=(10, 6))
|
223 |
+
sns.barplot(x='Count of Infiltrations', y='Traffic Type', hue='Count of Infiltrations', data=top_vulnerable_traffic, palette='viridis', dodge=False)
|
224 |
+
plt.xlabel('Count of Infiltrations')
|
225 |
+
plt.ylabel('Traffic Type')
|
226 |
+
plt.title('Top Traffic Types Vulnerable to Infiltration (High Anomaly Scores)')
|
227 |
+
plt.tight_layout()
|
228 |
+
plt.show()
|
229 |
+
|
230 |
+
cyber_attacks_data = df[df['Action Taken'].str.contains('Blocked', case=False, na=False)]
|
231 |
+
|
232 |
+
vulnerable_devices_os_counts = cyber_attacks_data['Device/OS'].value_counts()
|
233 |
+
vulnerable_devices_os_df = pd.DataFrame(vulnerable_devices_os_counts).reset_index()
|
234 |
+
vulnerable_devices_os_df.columns = ['Device/OS', 'Count of Cyber Attacks']
|
235 |
+
|
236 |
+
top_vulnerable_devices_os = vulnerable_devices_os_df.head(10)
|
237 |
+
print(top_vulnerable_devices_os)
|
238 |
+
|
239 |
+
plt.figure(figsize=(10, 6))
|
240 |
+
sns.barplot(x='Count of Cyber Attacks', y='Device/OS', hue='Count of Cyber Attacks', data=top_vulnerable_devices_os, palette='viridis', dodge=False)
|
241 |
+
plt.xlabel('Count of Cyber Attacks')
|
242 |
+
plt.ylabel('Device/OS')
|
243 |
+
plt.title('Top Devices/OS Vulnerable to Cyber Attacks')
|
244 |
+
plt.tight_layout()
|
245 |
+
plt.show
|