# Long Duration Flow detector **Repository Path**: lambyte/long-duration-flow-detector ## Basic Information - **Project Name**: Long Duration Flow detector - **Description**: Simple Long Duration Flow detector for offline pcap file. 简单的长流检测器。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # LDF-detector:简单的 pcap 文件长持续时间流检测器 ## 简介 ### 什么是长持续时间流 流是指网络中具有相同源 IP 地址、源端口、目的 IP 地址、目的端口和协议五元组的一段时间内数据包的集合。长持续时间流(Long Duration Flow,LDF),顾名思义,是指网络中持续时间很长的流。LDF 检测是恶意流量分析、QoS 优化等工作的基础工作之一。 ### LDF-detector 可以做什么 LDF-detector 可以从 pcap 文件中获取 TCP 长流的: - 源 IP 地址 src_IP - 源端口 src_port - 目的 IP 地址 dst_IP - 目的端口 dst_port - 流开始时间 start_time - 流结束时间 end_time - 流包含的数据包数目 ## 环境与依赖 ### 环境 Ubuntu 20.04 ### 依赖 - [PcapPlusPlus: PcapPlusPlus is a multiplatform C++ library for capturing, parsing and crafting of network packets.](https://github.com/seladb/PcapPlusPlus) - [cmdline: A Command Line Parser](https://github.com/tanakh/cmdline)(已在目录中) ## 下载与编译 ### Clone 本仓库 ``` git clone https://gitee.com/lambyte/long-duration-flow-detector ``` ### 编译 ``` make ``` 之后,可以在工程目录中看见名为 `LDF-detector` 的可执行文件。 ## 快速上手 ### 查看使用方法 ``` ./LDF-detector -? ``` > usage: ./LDF-detector [options] ... > options: > -i, --input_file input pcap file path (string [=./input.pcap]) > -o, --output_file output file path. This file contains Long Duration Flows' information (string [=./output.txt]) > -t, --LDF_threshold the minimum duration of a long duration flow (second) (long [=300]) > -x, --max_interval the maximum interval time (in second) between two consecutive packets of a same network traffic flow (long [=30]) > -c, --check_interval the frequency to update the flow map. This argument is supposed to less than "max_interval" (long [=1]) > -s, --slience Avoid verbose. > -p, --print print first n-th packets' information (int [=0]) > -?, --help print this message ### 一些简单的例子 从 `/path/to/your/file.pcap` 中检测两个包之间间隔不大于 30 秒,持续时间大于 300 秒的长流,将结果输出到 `./result/LDF_300_30.txt`: ``` ./LDF-detector -i /path/to/your/file.pcap -o ./result/LDF_300_30.txt ``` 从 `/path/to/your/file.pcap` 中检测两个包之间间隔不大于 60 秒,持续时间大于 1000 秒的长流,将结果输出到 `./result/LDF_1000_60.txt`,并减少屏幕输出: ``` ./LDF-detector -i /path/to/your/file.pcap -o ./result/LDF_1000_60.txt -x 60 -t 1000 -s ``` 查看 `/path/to/your/file.pcap` 中前 5 个数据包的信息: ``` ./LDF-detector -i /path/to/your/file.pcap -p 5 ``` ### 生成数据解读 倘若生成的一条数据记录为: > 3748135988 42705 985691779 80 1597538319 1597538623 304 672 各个字段分别对应了 src_IP, src_port, dst_IP, dst_port, start_time, end_time, packet_num 代表IP地址为 223.104.4.52(3748135988)的主机 42705 端口从北京时间 2020-08-16 08:38:39(1597538319 )到北京时间2020-08-16 08:43:43(1597538623),向 IP 地址为 58.192.118.131(985691779)的主机的 80 端口发送了 672 个数据包,持续时间为 304 秒。 ## 程序处理流程 ### 流程图 ![](./imgs/LDFDetector流程图.jpg) | 符号 | 含义 | | --------- | ------------------------------------------------- | | map | 存储流信息的数据结构 | | $I_{j}$ | 流 $j$ 的最后一个数据包到达的时间与当前时间的间隔 | | $I_{max}$ | 长流中两个数据包之间的最大间隔时长 | 其中,遍历更新 map 的具体操作为 1. 计算流的最后一个数据包到达时间到当前时间的间隔 $I_{i} = t_{end} - t_{currrent}$ 2. 若时间间隔小于长流中两个数据包之间的最大间隔时长,即 $I < I_{max}$ 则跳过 3. 若时间间隔大于长流中两个数据包之间的最大间隔时长,即 $I > I_{max}$ ,判断此流是否为长流,若是则上报 map_LDF,然后在 map 删去此流记录,否则直接在 map 删去此流记录 ## 提示 在默认的 `-c` 参数(check_interval = 1)的情况下检出长流持续时间误差小于 2 秒。