共计 1643 个字符,预计需要花费 5 分钟才能阅读完成。
背景介绍
CICIDS2017 是由加拿大网络安全研究所(CIC)发布的网络入侵检测基准数据集,包含 7 天真实网络流量(包括正常流量和常见攻击如 Brute Force、XSS、DDoS 等)。其价值在于:

- 完整标注了攻击时间窗口和类型
- 同时提供原始 pcap 文件和提取的 CSV 特征
- 模拟了企业级网络环境(包括 HTTP/HTTPS/DNS 等多种协议)
下载指南
- 访问官网:打开浏览器进入CIC 官网
- 选择下载:页面底部找到 ”Download the CICIDS2017 dataset” 部分
- 文件说明:
MachineLearningCSV.zip(已提取特征的 CSV,推荐优先下载)PCAPs.zip(原始流量包,约 50GB)- 解压密码:下载完成后使用官网提供的密码解压(当前为
infected)
数据处理实战
以下 Python 代码演示如何加载 CSV 数据并进行基本分析:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# 加载数据(示例使用 Friday 攻击数据)df = pd.read_csv('MachineLearningCVE/Friday-WorkingHours-Afternoon-DDos.pcap_ISCX.csv')
# 处理缺失值
df.replace([np.inf, -np.inf], np.nan, inplace=True)
df.fillna(0, inplace=True)
# 标签编码(将攻击类型转为数值)le = LabelEncoder()
df['Label'] = le.fit_transform(df['Label'])
# 特征标准化(示例选取 5 个关键特征)from sklearn.preprocessing import StandardScaler
features = ['Flow Duration', 'Total Fwd Packets', 'Total Backward Packets',
'Fwd Packet Length Max', 'Bwd Packet Length Max']
scaler = StandardScaler()
df[features] = scaler.fit_transform(df[features])
print(f"数据集形状: {df.shape}")
print(f"攻击类型分布:\n{df['Label'].value_counts()}")
存储优化方案
针对 50GB+ 的原始数据,推荐以下策略:
- 分层存储:
- 热数据:常用 CSV 特征存入 SSD
-
冷数据:原始 pcap 文件存入机械硬盘
-
数据库选择:
- PostgreSQL(适合结构化查询)
-
MongoDB(适合非结构化流量日志)
-
压缩技巧:
# 使用 pigz 多线程压缩(比 gzip 快 4 倍)sudo apt install pigz tar --use-compress-program=pigz -cvf PCAPs.tar.gz PCAPs/
避坑指南
- 问题 1 :CSV 文件加载内存不足
-
解决方案:使用
dask.dataframe替代 pandas
python
import dask.dataframe as dd
df = dd.read_csv('large_file.csv', blocksize=25e6) # 25MB/ 块 -
问题 2 :pcap 解析速度慢
- 解决方案:使用
scapy的批量读取模式from scapy.utils import PcapReader packets = PcapReader('large.pcap') # 流式读取 for pkt in packets: if pkt.haslayer('IP'): print(pkt['IP'].src)
研究延伸
思考如何利用该数据集改进入侵检测:
1. 尝试结合时序特征(如滑动窗口统计)
2. 对比传统机器学习(如 RandomForest)与深度学习模型(如 LSTM)的效果
3. 探索少样本学习(Few-shot Learning)在新型攻击检测中的应用
完整代码示例已上传 GitHub 仓库(示例链接),欢迎交流讨论。
正文完
发表至: 网络安全
近一天内
