• 网络安全攻击数据的多维度可视化分析


    简介

    本研究项目通过应用多种数据处理与可视化技术,对网络安全攻击事件数据集进行了深度分析。首先,利用Pandas库读取并预处理数据,包括检查缺失值、剔除冗余信息以及将时间戳转化为日期时间格式以利于后续时间序列分析。

    研究步骤

    1. 数据分析前准备:加载numpy、pandas、seaborn和matplotlib等科学计算和可视化库,同时使用os模块遍历文件路径,显示数据集所在位置。

    2. 数据初步探索:读取并清洗cybersecurity_attacks.csv数据集,统计各类攻击类型的数量,并可视化攻击类型分布情况,采用柱状图清晰展示了不同攻击类型的频次差异。

    3. 特征分析:针对选定的三个关键特征(Anomaly Scores, Source Port, Packet Length)进行细致探讨,为每种特征分别绘制箱线图,揭示各特征在不同攻击类型下的统计特性。

    4. 网络协议占比分析:统计网络流量中UDP、ICMP和TCP三种主要协议的使用频率,利用饼图直观展现其在网络攻击中的相对重要性。

    5. 时间序列分析:根据攻击发生的时间戳生成年份和月份列,构建透视表并通过热力图展示攻击事件随时间和月份的变化趋势,进一步细化至每周,探究月与周日间攻击活动的周期性规律。

    6. Payload Data内容挖掘:对Payload Data列的内容进行文本分析,将其转换为单一字符串并生成词云,借此揭示攻击载荷中的高频词汇,从而洞察潜在的攻击模式和关键词。

    代码解析及实现

    导入必要的库

    import numpy as np # 线性代数
    import pandas as pd # 数据处理,CSV文件I/O(例如pd.read_csv)
    import seaborn as sns # 数据可视化
    import matplotlib.pyplot as plt # 绘图
    import warnings # 忽略警告
    import calendar # 日历功能
    
    # 输入数据文件位于只读的“../input/”目录中
    # 例如,运行此代码(通过单击运行或按Shift+Enter)将列出输入目录下的所有文件
    
    import os
    for dirname, _, filenames in os.walk('/kaggle/input'):
        for filename in filenames:
            print(os.path.join(dirname, filename))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    data = pd.read_csv('/kaggle/input/cyber-security-attacks/cybersecurity_attacks.csv')
    
    • 1
    data.head()
    
    • 1
    TimestampSource IP AddressDestination IP AddressSource PortDestination PortProtocolPacket LengthPacket TypeTraffic TypePayload Data...Action TakenSeverity LevelUser InformationDevice InformationNetwork SegmentGeo-location DataProxy InformationFirewall LogsIDS/IPS AlertsLog Source
    02023-05-30 06:33:58103.216.15.1284.9.164.2523122517616ICMP503DataHTTPQui natus odio asperiores nam. Optio nobis ius......LoggedLowReyansh DugalMozilla/5.0 (compatible; MSIE 8.0; Windows NT ...Segment AJamshedpur, Sikkim150.9.97.135Log DataNaNServer
    12020-08-26 07:08:3078.199.217.19866.191.137.1541724548166ICMP1174DataHTTPAperiam quos modi officiis veritatis rem. Omni......BlockedLowSumer RanaMozilla/5.0 (compatible; MSIE 8.0; Windows NT ...Segment BBilaspur, NagalandNaNLog DataNaNFirewall
    22022-11-13 08:23:2563.79.210.48198.219.82.171681153600UDP306ControlHTTPPerferendis sapiente vitae soluta. Hic delectu......IgnoredLowHimmat KarpeMozilla/5.0 (compatible; MSIE 9.0; Windows NT ...Segment CBokaro, Rajasthan114.133.48.179Log DataAlert DataFirewall
    32023-07-02 10:38:46163.42.196.10101.228.192.2552001832534UDP385DataHTTPTotam maxime beatae expedita explicabo porro l......BlockedMediumFateh KibeMozilla/5.0 (Macintosh; PPC Mac OS X 10_11_5; ...Segment BJaunpur, RajasthanNaNNaNAlert DataFirewall
    42023-07-16 13:11:0771.166.185.76189.243.174.238613126646TCP1462DataDNSOdit nesciunt dolorem nisi iste iusto. Animi v......BlockedLowDhanush ChadMozilla/5.0 (compatible; MSIE 5.0; Windows NT ...Segment CAnantapur, Tripura149.6.110.119NaNAlert DataFirewall

    5 rows × 25 columns

    data.shape
    
    • 1

    (40000, 25)

    data.info()
    
    • 1

    在这里插入图片描述

    data.columns
    
    • 1

    Index([‘Timestamp’, ‘Source IP Address’, ‘Destination IP Address’,
    ‘Source Port’, ‘Destination Port’, ‘Protocol’, ‘Packet Length’,
    ‘Packet Type’, ‘Traffic Type’, ‘Payload Data’, ‘Malware Indicators’,
    ‘Anomaly Scores’, ‘Alerts/Warnings’, ‘Attack Type’, ‘Attack Signature’,
    ‘Action Taken’, ‘Severity Level’, ‘User Information’,
    ‘Device Information’, ‘Network Segment’, ‘Geo-location Data’,
    ‘Proxy Information’, ‘Firewall Logs’, ‘IDS/IPS Alerts’, ‘Log Source’],
    dtype=‘object’)

    data.isnull().sum()
    
    • 1

    在这里插入图片描述
    无关列去除

    columns_to_drop=['Malware Indicators', 'Alerts/Warnings', 'Proxy Information', 'Firewall Logs', 'IDS/IPS Alerts']
    data=data.drop(columns_to_drop, axis=1)
    
    • 1
    • 2
    data.isnull().sum()
    
    • 1

    在这里插入图片描述

    data.head()
    
    • 1
    TimestampSource IP AddressDestination IP AddressSource PortDestination PortProtocolPacket LengthPacket TypeTraffic TypePayload DataAnomaly ScoresAttack TypeAttack SignatureAction TakenSeverity LevelUser InformationDevice InformationNetwork SegmentGeo-location DataLog Source
    02023-05-30 06:33:58103.216.15.1284.9.164.2523122517616ICMP503DataHTTPQui natus odio asperiores nam. Optio nobis ius...28.67MalwareKnown Pattern BLoggedLowReyansh DugalMozilla/5.0 (compatible; MSIE 8.0; Windows NT ...Segment AJamshedpur, SikkimServer
    12020-08-26 07:08:3078.199.217.19866.191.137.1541724548166ICMP1174DataHTTPAperiam quos modi officiis veritatis rem. Omni...51.50MalwareKnown Pattern ABlockedLowSumer RanaMozilla/5.0 (compatible; MSIE 8.0; Windows NT ...Segment BBilaspur, NagalandFirewall
    22022-11-13 08:23:2563.79.210.48198.219.82.171681153600UDP306ControlHTTPPerferendis sapiente vitae soluta. Hic delectu...87.42DDoSKnown Pattern BIgnoredLowHimmat KarpeMozilla/5.0 (compatible; MSIE 9.0; Windows NT ...Segment CBokaro, RajasthanFirewall
    32023-07-02 10:38:46163.42.196.10101.228.192.2552001832534UDP385DataHTTPTotam maxime beatae expedita explicabo porro l...15.79MalwareKnown Pattern BBlockedMediumFateh KibeMozilla/5.0 (Macintosh; PPC Mac OS X 10_11_5; ...Segment BJaunpur, RajasthanFirewall
    42023-07-16 13:11:0771.166.185.76189.243.174.238613126646TCP1462DataDNSOdit nesciunt dolorem nisi iste iusto. Animi v...0.52DDoSKnown Pattern BBlockedLowDhanush ChadMozilla/5.0 (compatible; MSIE 5.0; Windows NT ...Segment CAnantapur, TripuraFirewall
    data.duplicated().sum()
    
    • 1

    0

    # 在创建DataFrame时,将'Timestamp'列转换为datetime对象
    data['Timestamp'] = pd.to_datetime(data['Timestamp'])
    
    • 1
    • 2

    绘制攻击类型分布的柱状图

    根据给定数据集,绘制攻击类型分布的柱状图。

    # 统计每种攻击类型的数量
    attack_counts = data['Attack Type'].value_counts()
    
    # 设置绘图的颜色方案
    colors = sns.color_palette('viridis', len(attack_counts))
    
    # 创建图表并绘制柱状图
    plt.figure(figsize=(12, 6))
    sns.barplot(x=attack_counts.index, y=attack_counts, palette=colors)
    
    # 为每个柱子添加数据标签
    for i, count in enumerate(attack_counts):
        plt.text(i, count + 0.1, str(count), ha='center', va='bottom', fontsize=10, fontweight='bold')
    
    # 添加x轴和y轴标签以及图表标题
    plt.xlabel('Attack Type', fontsize=14, fontweight='bold')
    plt.ylabel('Count', fontsize=14, fontweight='bold')
    plt.title('Distribution of Attack Types', fontsize=16)
    
    # 旋转x轴标签,以改善可读性
    plt.xticks(rotation=45, ha='right')
    
    # 调整布局并显示图表
    plt.tight_layout()
    plt.show()
    
    • 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

    在这里插入图片描述

    绘制异常分数、源端口和数据包长度的分布箱线图

    绘制异常分数、源端口和数据包长度的分布情况,通过箱线图展示每种特征在不同攻击类型中的分布情况。

    # 定义要绘制的列
    columns = ['Anomaly Scores', 'Source Port', 'Packet Length']
    
    # 遍历列名列表,为每列绘制箱线图
    for col in columns:
        # 创建一个新的画布,设置尺寸为12英寸*6英寸
        plt.figure(figsize=(12, 6))
        # 绘制箱线图,设置不显示异常值(outliers)
        sns.boxplot(data=data, x=col, y='Attack Type', showfliers=False)
        # 设置图表标题
        plt.title(f'Distribution of {col.capitalize()} by Attack Type')
        # 设置x轴和y轴标签
        plt.xlabel(col.capitalize(), fontsize=14, fontweight='bold')
        plt.ylabel('Attack Type', fontsize=14, fontweight='bold')
        # 显示绘制的图表
        plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    利用饼图,展示网络流量协议分布

    # 定义饼图的标签
    labels = ['UDP', 'ICMP', 'TCP']
    # 通过data['Protocol']列的值计数,获取每个类别比例大小
    sizes = data['Protocol'].value_counts()
    # 爆炸效果设置,使第一个类别(UDP)突出
    explode = (0.1, 0, 0)
    # 设置自定义颜色方案
    colors = sns.color_palette('pastel')[0:len(labels)]
    # 设置seaborn的白色格子风格,用于创建具有创意主题的图表
    sns.set(style="whitegrid")
    # 创建饼图
    plt.figure(figsize=(8, 8))
    plt.pie(sizes, labels=labels, colors=colors, explode=explode, autopct='%1.1f%%', startangle=90)
    # 设置等比例属性,确保饼图被绘制为一个圆形
    plt.axis('equal')
    # 设置图表标题
    plt.title('Distribution of Network Traffic Protocols', fontsize=16, fontweight='bold')
    # 显示饼图
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    # 为dataframe的'Timestamp'列创建'Year''Month'两个新列
    # 'Year'列将包含时间戳对应的年份
    # 'Month'列将包含时间戳对应的月份名称
    data['Year'] = data['Timestamp'].dt.year  # 创建'Year'列,包含时间戳的年份
    data['Month'] = data['Timestamp'].dt.month_name()  # 创建'Month'列,包含时间戳的月份名称
    
    • 1
    • 2
    • 3
    • 4
    • 5

    生成一个透视表,以年为行索引,月为列索引,统计每个年份每个月出现的次数。

    """
    
    参数:
    - data: 输入的数据,预期包含'Year', 'Month', 'Timestamp'等列。
    - values: 用于计算透视表的值列,这里使用'Timestamp'列。
    - index: 透视表的行索引,这里使用'Year'列。
    - columns: 透视表的列索引,这里使用'Month'列。
    - aggfunc: 对values列进行聚合的函数,这里使用'count'函数。
    - fill_value: 缺失值的填充值,这里填充为0。
    
    返回值:
    - pivot_table: 生成的透视表,显示了每年每个月的计数。
    """
    # 生成透视表
    pivot_table = pd.pivot_table(data, values='Timestamp', index='Year', columns='Month', aggfunc='count', fill_value=0)
    
    # 显示透视表
    print("Pivot Table - Year vs Month:")
    print(pivot_table)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    分析时间序列,绘制热力图

    # 设置热力图的可视化窗口大小
    plt.figure(figsize=(12, 8))
    # 生成热力图,注释表示数值,格式化为整数,使用"viridis"颜色方案,设置行宽度为0.5
    sns.heatmap(pivot_table, annot=True, fmt="d", cmap="viridis", linewidths=.5)
    # 添加标题和坐标轴标签
    plt.title('Count of Records: Year vs Month')
    plt.xlabel('Month')
    plt.ylabel('Year')
    
    # 显示热力图
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    # 显示透视表
    print("Pivot Table - Year vs Month:")
    print(pivot_table)
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    生成一个关于月份与周几的透视表

    pivot_table_month_weekday = pd.pivot_table(data, values='Timestamp', index='Month', columns='Weekday', aggfunc='count', fill_value=0)
    print("Pivot Table - Month vs Weekday:")
    print(pivot_table_month_weekday)
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    生成一个可视化热力图,展示记录数在月份与周几之间的分布情况。

    # 创建一个新的画布,设置画布的尺寸为12英寸宽,8英寸高
    plt.figure(figsize=(12, 8))
    # 生成热力图,设置显示数值,格式为整数,颜色映射为'YlGnBu',线条宽度为0.5
    sns.heatmap(pivot_table_month_weekday, annot=True, fmt="d", cmap="YlGnBu", linewidths=.5)
    # 设置图表标题,x轴标签和y轴标签
    plt.title('Count of Records: Month vs Weekday')
    plt.xlabel('Weekday')
    plt.ylabel('Month')
    
    # 显示热力图
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    将攻击载荷中的高频词汇生成单词云图

    # 将'Data Payload'列的所有值转换为字符串,并合并为一个单一字符串
    text = ' '.join(data['Payload Data'].astype(str).values)
    
    # 创建绘图子图
    fig, ax = subplots(figsize=(12, 12))
    
    # 生成并显示单词云
    wordcloud = WordCloud(random_state=2023, height=1200, width=1200).generate(text)
    imshow(wordcloud)
    axis('off')  # 关闭坐标轴显示
    
    # 显示单词云图
    plt.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    优化建议

    1. 数据预处理优化:
    • 对于含有大量缺失值的特定列,可以考虑采用更为精细的数据填充策略,如基于统计模型(如均值、中位数、众数填充)或机器学习方法(如KNN插补、多重插补等)进行缺失值填补。
    • 在特征选择阶段,除了基于现有业务理解剔除部分冗余信息外,还可以运用特征重要性评估(如基于随机森林、梯度提升树等模型计算特征重要性),进一步筛选出对攻击类型预测具有较高价值的特征。
    1. 可视化增强与解读:
    • 为柱状图、箱线图和饼图添加图例、图注以及单位信息,使图表更易于理解和解读。
    • 对于时间序列分析,可引入滑动窗口统计或周期性趋势分析,探索攻击事件在季度、季节性周期中的规律性变化,并结合节假日等外部因素研究其潜在关联性。
    • 在词云生成时,增加自定义停用词表过滤常见高频但无实际意义的词汇,并尝试使用TF-IDF或TextRank算法提取更具代表性的关键词。
    1. 深度挖掘与建模:
    • 对Payload Data内容进行深度文本挖掘,例如进行情感分析以判断攻击载荷的威胁程度,或者利用主题模型(如LDA)发现隐藏的主题结构。
    • 构建分类模型(如逻辑回归、支持向量机、神经网络等)来预测攻击类型,通过模型训练和验证,深入了解各特征与攻击类型的内在关系,从而优化防御策略。
    1. 结果解释与报告撰写:
    • 结合可视化结果与统计检验方法(如卡方检验、T检验等),对观察到的模式和趋势提供定量化的显著性分析。
    • 将分析结果整合成详细的研究报告,包括假设检验、数据分析步骤、主要发现及结论,并针对网络安全防护提出具体且有针对性的改进建议。
  • 相关阅读:
    Python机器学习实战-特征重要性分析方法(8):方差分析ANOVA(附源码和实现效果)
    MySQL高级篇_16_MVCC多版本并发控制_尚硅谷_宋红康
    旋转偏心裁切刀切向跟踪及半径补偿
    html网页设计大学生作业成品——公益校园网站设计与实现(HTML+CSS+JavaScript)
    HarmonyOS开发实例:【分布式手写板】
    当添加一个键值对元素时,HashMap发生了什么?
    【Vue】生命周期一文详解
    CJSON解析json字符串示例
    Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
    C++面试知识点
  • 原文地址:https://blog.csdn.net/weixin_43178927/article/details/136662734