CybersecurityAttacks / cybersecurity_attacks.py
antitheft159's picture
Update cybersecurity_attacks.py
37c27e5 verified
Raw
History Blame Contribute Delete
8.57 kB
import numpy as np
import pandas as pd
import plotly.express as px
import seaborn as sns
import matplotlib.pyplot as plt
import re
pd.options.display.float_format = '{:,.2f}'.format
df = pd.read_csv("/content/cybersecurity_attacks.csv")
print(f"There are {df.shape[0]}, row and {df.shape[1]} columns in the dataset")
df.columns
df.isna().sum()
df.duplicated().sum()
df['Malware Indicators'] = df['Malware Indicators'].apply(lambda x: 'No Detection' if pd.isna(x) else x)
df['Alerts/Warnings'] = df['Alerts/Warnings'].apply(lambda x: 'yes' if x == 'Alert Triggered' else 'no')
df['Prowx Information'] = df['Proxy Information'].apply(lambda x: 'No proxy' if pd.isna(x) else x)
df['Firewall Logs'] = df['Firewall Logs'].apply(lambda x: 'No Data' if pd.isna(x) else x)
df['IDS/IPS Alerts'] = df['IDS/IPS Alerts'].apply(lambda x: 'No Data' if pd.isna(x) else x)
df['Source Port'] = df['Source Port'].apply(lambda x: x if 0 <= x <= 65535 else 'Invalid Port')
df['Destination Port'] = df['Destination Port'].apply(lambda x: x if 0 <= x <= 65535 else 'Invalid Port')
df.isna().sum()
df['Device Information'].value_counts()
df['Browser'] = df['Device Information'].str.split('/').str[0]
df['Browser'].unique()
patterns = [
r'Windows',
r'Linux',
r'Android',
r'iPad',
r'iPhone',
r'Macintosh',
]
def extract_device_or_os(user_agent):
for pattern in patterns:
match = re.search(pattern, user_agent, re.I)
if match:
return match.group()
return 'Unknown'
df['Device/OS'] = df['Device Information'].apply(extract_device_or_os)
df = df.drop('Device Information', axis = 1)
device_counts = df['Device/OS'].value_counts()
device_counts_df = pd.DataFrame(device_counts).reset_index()
device_counts_df.columns = ['Device/OS', 'Count of Attacks']
top_devices = device_counts_df.head(10)
print(top_devices)
plt.figure(figsize=(10, 6))
sns.barplot(x='Count of Attacks', y='Device/OS', hue='Device/OS', data=top_devices, palette='viridis', dodge=False)
plt.xlabel('Count of Attacks')
plt.ylabel('Device/OS')
plt.title('Top 10 Devices/OS Targeted by Attacks')
plt.tight_layout()
plt.show()
attack_type_counts = df['Attack Type'].value_counts()
attack_type_counts_df = pd.DataFrame(attack_type_counts).reset_index()
attack_type_counts_df.columns = ['Attack Type', 'Count of Attacks']
top_attack_types = attack_type_counts_df.head(10)
print(top_attack_types)
plt.figure(figsize=(10, 6))
sns.barplot(x='Count of Attacks', y='Attack Type', hue='Attack Type', data=top_attack_types, palette='viridis', dodge=False)
plt.xlabel('Count of Attacks')
plt.ylabel('Attack Type')
plt.title('Top 10 Most Common Attack Types')
plt.tight_layout()
plt.show()
geo_location_counts = df['Geo-location Data'].value_counts()
geo_location_counts_df = pd.DataFrame(geo_location_counts).reset_index()
geo_location_counts_df.columns = ['Geo-location Data', 'Count of Attacks']
top_geo_locations = geo_location_counts_df.head(10)
print(top_geo_locations)
plt.figure(figsize=(10, 6))
sns.barplot(x='Count of Attacks', y='Geo-location Data', hue='Geo-location Data', data=top_geo_locations, palette='viridis', dodge=False)
plt.xlabel('Count of Attacks')
plt.ylabel('Geo-location Data')
plt.title('Top 10 Geographic Locations Source of Malicious Traffic')
plt.tight_layout()
plt.show()
destination_port_counts = df['Destination Port'].value_counts()
destination_port_counts_df = pd.DataFrame(destination_port_counts).reset_index()
destination_port_counts_df.columns = ['Destination Port', 'Count of Attacks']
top_destination_ports = destination_port_counts_df.head(10)
print(top_destination_ports)
plt.figure(figsize=(10, 6))
sns.barplot(x='Destination Port', y='Count of Attacks', hue='Count of Attacks', data=top_destination_ports, palette='viridis', dodge=False)
plt.xlabel('Destination Port')
plt.ylabel('Count of Attacks')
plt.title('Top 10 Most Targeted Destination Ports')
plt.tight_layout()
plt.show()
severity_mapping = {'Low': 1, 'Medium': 2, 'High': 3}
df['Severity Level Numeric'] = df['Severity Level'].map(severity_mapping)
protocol_severity = df.groupby('Protocol')['Severity Level Numeric'].mean().reset_index()
protocol_severity = protocol_severity.sort_values(by='Severity Level Numeric', ascending=False)
top_protocol_severity = protocol_severity.head(10)
print(top_protocol_severity)
plt.figure(figsize=(10, 6))
sns.barplot(x='Severity Level Numeric', y='Protocol', hue='Severity Level Numeric', data=top_protocol_severity, palette='viridis', dodge=False)
plt.xlabel('Mean Severity Level')
plt.ylabel('Protocol')
plt.title('Top 10 Protocols by Mean Severity Level')
plt.tight_layout()
plt.show()
df['Timestamp'] = pd.to_datetime(df['Timestamp'], errors='coerce')
df['Month'] = df['Timestamp'].dt.month
month_counts = df['Month'].value_counts()
month_counts_df = pd.DataFrame(month_counts).reset_index()
month_counts_df.columns = ['Month', 'Count of Attacks']
sorted_month_counts = month_counts_df.sort_values(by='Month')
print(sorted_month_counts)
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df['Month'] = df['Timestamp'].dt.month
attacks_by_month = df.groupby('Month').size().reset_index(name='Attack Count')
heatmap_data = attacks_by_month.pivot_table(index='Month', columns='Month', values='Attack Count', aggfunc='sum', fill_value=0)
plt.figure(figsize=(10, 6))
sns.heatmap(heatmap_data, annot=True, fmt='d', cmap='YlOrRd', cbar=True)
plt.title('Cybersecurity Attacks Frequency by Month')
plt.xlabel('Month')
plt.ylabel('Month')
plt.tight_layout()
plt.show()
malicious_traffic = df[df['Malware Indicators'] == 'IoCDetected']
traffic_type_counts = malicious_traffic['Traffic Type'].value_counts()
traffic_type_counts_df = pd.DataFrame(traffic_type_counts).reset_index()
traffic_type_counts_df.columns = ['Traffic Type', 'Count of Malicious Incidents']
top_traffic_types = traffic_type_counts_df.head(10)
print(top_traffic_types)
plt.figure(figsize=(10, 6))
sns.barplot(x='Count of Malicious Incidents', y='Traffic Type', hue='Count of Malicious Incidents', data=top_traffic_types, palette='viridis', dodge=False)
plt.xlabel('Count of Malicious Incidents')
plt.ylabel('Traffic Type')
plt.title('Top Traffic Types Flagged with "IoC Detected"')
plt.tight_layout()
plt.show()
threshold = 75.0
infiltration_data = df[df['Anomaly Scores'] > threshold]
vulnerable_traffic_counts = infiltration_data['Traffic Type'].value_counts()
vulnerable_traffic_df = pd.DataFrame(vulnerable_traffic_counts).reset_index()
vulnerable_traffic_df.columns = ['Traffic Type', 'Count of Infiltrations']
top_vulnerable_traffic = vulnerable_traffic_df.head(10)
print(top_vulnerable_traffic)
plt.figure(figsize=(10, 6))
sns.barplot(x='Count of Infiltrations', y='Traffic Type', hue='Count of Infiltrations', data=top_vulnerable_traffic, dodge=False)
plt.xlabel('Count of Infiltrations')
plt.ylabel('Traffic Type')
plt.title('Top Traffic Types Vulnerable to Infiltration (Anomaly Scores)')
plt.tight_layout()
plt.show()
threshold = df['Anomaly Scores'].quantile(0.95)
print(f"Threshold for Anomaly Scores: {threshold}\n")
infiltration_data = df[df['Anomaly Scores'] > threshold]
print(infiltration_data.head())
vulnerable_traffic_counts = infiltration_data['Traffic Type'].value_counts()
vulnerable_traffic_df = pd.DataFrame(vulnerable_traffic_counts).reset_index()
vulnerable_traffic_df.columns = ['Traffic Type', 'Count of Infiltrations']
top_vulnerable_traffic = vulnerable_traffic_df.head(10)
print(top_vulnerable_traffic)
plt.figure(figsize=(10, 6))
sns.barplot(x='Count of Infiltrations', y='Traffic Type', hue='Count of Infiltrations', data=top_vulnerable_traffic, palette='viridis', dodge=False)
plt.xlabel('Count of Infiltrations')
plt.ylabel('Traffic Type')
plt.title('Top Traffic Types Vulnerable to Infiltration (High Anomaly Scores)')
plt.tight_layout()
plt.show()
cyber_attacks_data = df[df['Action Taken'].str.contains('Blocked', case=False, na=False)]
vulnerable_devices_os_counts = cyber_attacks_data['Device/OS'].value_counts()
vulnerable_devices_os_df = pd.DataFrame(vulnerable_devices_os_counts).reset_index()
vulnerable_devices_os_df.columns = ['Device/OS', 'Count of Cyber Attacks']
top_vulnerable_devices_os = vulnerable_devices_os_df.head(10)
print(top_vulnerable_devices_os)
plt.figure(figsize=(10, 6))
sns.barplot(x='Count of Cyber Attacks', y='Device/OS', hue='Count of Cyber Attacks', data=top_vulnerable_devices_os, palette='viridis', dodge=False)
plt.xlabel('Count of Cyber Attacks')
plt.ylabel('Device/OS')
plt.title('Top Devices/OS Vulnerable to Cyber Attacks')
plt.tight_layout()
plt.show