文章目录
工作原理
# 原理
1、基于netflow采集流量成分(例如5元组之类的),并将其发送到远端 或者发送到本地
2、用 nfcapd 流接收工具,接收netflow流数据,并将其导出到本地(并非抓取,所以不支持远程ip,仅支持本机ip)
3、用 nfdump 流查看工具,分析流量成分
# 涉及的程序,及安装方式
softflowd: 流采集工具,充当netflow导出器,安装文档: https://boke.wsfnk.com/archives/1432.html
# 以下两个命令,都包含在nfdump 这个程序包中,只需安装它即可
nfcapd: 流接收工具,充当netflow接收器
nfdump: 流查看工具,充当netflow分析器
# rhel系列
yum -y install epel-release ; yum -y install nfdump
# debian系列
apt -y install nfdump
第一:采集netflow流量
# 前提(正常情况下不需要执行)
ethtool -K eth0 gro off
ethtool -K eth0 lro off
ethtool -K eth0 gso off
ethtool -K eth0 tso off
ethtool -K eth0 sg off
ethtool -K eth0 rx off
ethtool -K eth0 tx off
# 常用参数介绍
-m 100000 # 指定最大流表大小(默认8192),表示最多跟踪 20000 条活跃 Flow
-s 10 # 采样率(每10个包采样1个)
-v 5 # 指定netflow协议版本,可选值 1|5|9
1: 老旧,不支持 IPv6
5: 常用,但字段少
9: 最强、可扩展、支持 IPv6
-T # Flow 跟踪级别,可选值 full|proto|ip
full: srcip + dstip + port + 协议 + flags(全流跟踪)
proto: 仅按协议聚合
ip: 按srcip + dstIP 聚合,不看port
-6 # 启用 IPv6 Flow 跟踪
-d # 前台运行模式(调试用)
-D # debug 模式,会在前台打印采集的flow
-t # 指定flow超时时间
tcp默认3600s 、 tcp.rst默认120s 、 tcp.fin默认120s
udp默认300s 、 icmp默认300s 、 general默认3600s (指其它协议通用)
maxlife默认7天 (任意 Flow 最长生命周期)
expint默认60s (Flow 导出周期)
# softflowctl用法
softflowctl statistics # 控制运行中的 softflowd(查看统计)
softflowctl expire-all # 刷新配置
# 对 pcap 文件进行流分析(离线模式,将pcap回放成flow发出去)
softflowd -r traffic.pcap -n 127.0.0.1:9995
# softflowctl用法
softflowctl statistics # 控制运行中的 softflowd(查看统计)
softflowctl expire-all # 刷新配置
# 常用组合命令
# 发送到本地
softflowd -i eth0 -n 127.0.0.1:9995 -v 9 -T full -D -t tcp=60 -t tcp.fin=60 -t tcp.rst=60 -t udp=60
softflowd -i eth0 -n 127.0.0.1:9995 -6 -v 9 -m 100000 -T full -t expint=30 -t general=300 -t udp=60 -t tcp=60
softflowd -i eth0 -n 127.0.0.1:9995 -6 -v 9 -m 100000 -s 20 -T full -t expint=30 -t general=300 -t udp=60 -t tcp=60
softflowd -i eth0 -n 127.0.0.1:9995 -6 -v 9 -m 100000 -s 20 -T full -t expint=30 -t general=300 -t udp=60 -t tcp=60 -D
# 发送到远程服务器
softflowd -i eth0 -n 47.122.123.71:9995 -6 -v 9 -m 100000 -s 20 -T full -t expint=30 -t general=300 -t udp=60 -t tcp=60
softflowd -i eth0 -n 47.122.123.71:9995 -6 -v 9 -m 100000 -s 20 -T full -t expint=30 -t general=300 -t udp=60 -t tcp=60 -D
第二:nfcapd 接收并导出数据
# 默认会导出到 /var/log/flows 目录,默认该目录不存在,所以先自行创建,否则会报错
mkdir /var/log/flows
# 常用参数介绍
-t 60 # 文件每60s轮转,就是每60s存一个文件(实测60s是 设备自然时间,非倒计时60s)
-l basdir # 指定输出流量文件的根目录(必须指定)
-n Ident,IP,logdir # 指定流量源,支持多个流(用于多源采集)
-s rate # 采样率,默认 1(即采集所有流量)
-R IP[/port] # 把收到的流量重复发送到指定 IP/端口,最多支持 8 个重复目标
-w:同步文件轮转,默认每 5 分钟轮转一次
-e # 在每个轮转周期结束时过期数据(清理缓存,强制 flush)
-D # 后台运行(守护进程模式)
-E # 打印扩展格式数据,用于调试
-T # 包含扩展标签在记录中
-z # LZO 压缩输出文件
-y # LZ4 压缩输出文件
-x process # 当生成新文件时,执行指定进程(例如触发分析脚本)
# 常用组合命令
nfcapd -l /var/log/flows -b 0.0.0.0 -p 9995 -t 60 -e # 格式简单,推荐
nfcapd -l /var/log/flows -b 0.0.0.0 -p 9995 -t 60 -E # 格式花里胡哨
第三:查看流量
# 指定输出格式
-o "fmt:%ts %td %da %pr %pkt %byt %bps %fl"
-o "fmt:%da %pr %pkt %byt %bps %fl" # 推荐这样用
-o "fmt:%ts %da %pr %byt %fl" # 强烈推荐
# 推荐导出为这种
nfdump -R /var/log/flows -o line > flow.txt
nfdump -R /var/log/flows 'src ip 106.119.154.5 and not dst host 106.119.154.5' -s dstip/bytes -n 200
# -A 高级聚合,聚合多个指标
nfdump -R /var/log/flows 'src ip 106.119.154.5 and not dst host 106.119.154.5' -A dstip,proto -s record/bytes -n 50
nfdump -R /var/log/flows 'src ip 106.119.154.5' -s dstip/bytes -n 20
nfdump -R /var/log/flows 'src ip 106.119.154.5' -s dstip,proto/bytes -n 20
-A dstip,proto -s record/bytes -n 50
-A dstip,proto -s dstip/bytes -n 20
nfdump -R /var/log/flows 'src ip 106.119.154.5 and not dst host 106.119.154.5' -A dstip,proto -s record/bytes -n 12 -o "fmt:%ts %da %pr %pkt %byt %bps %fl"
# 看流量协议成分(TCP|UDP|ICMP)
nfdump -R /var/log/flows 'src ip 106.119.154.5 and not dst host 106.119.154.5' -s proto/bytes -n 8
nfdump -R /var/log/flows 'src ip 106.119.154.5 and not dst net 106.119.154.0/24' -s proto/bytes -n 8
nfdump -R /var/log/flows 'src ip 106.119.154.5' -s proto/bytes -n 8
nfdump -R /var/log/flows 'src ip 106.119.154.5 and not dst host 106.119.154.5' -A dstip,proto -s record/bytes -n 50 -o "fmt:%ts %da %pr %byt %fl"
## 第三,实际组合搭配使用案例
nfdump -R /var/log/flows 'src ip 106.119.154.5 and not dst host 106.119.154.5' -A dstip,proto -s record/bytes -n 50 -o "fmt:%ts %da %pr %byt %fl"
grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}' > flows.txt
++++++++++++++++++++++++++++++++++++
[root@513eb4b96954dca16b0bbdc2 ~]# cat a.sh
#!/bin/bash
INPUT_FILE="flows.txt"
while read -r line; do
# 提取第3列 IP
ip=$(echo "$line" | awk '{print $3}')
# 调用 API 获取归属地信息,并提取 province 和 isp
info=$(curl -s "http://atstm.cc/ip?ip=$ip" \
| jq -r '.data | "\(.province) \(.isp)"')
# 拼接到原始行后面
echo "$line $info"
done < "$INPUT_FILE"
附、常规交换机配置
一、H3C 交换机配置sflow
# 配置 sFlow Agent 的 IP 地址。
# 配置 sFlow Collector 的 IP 地址和端口号。如果没有指定具体的端口号,则缺省值为 6343
# 配置轮询时间间隔为 30 秒。
system-view
sflow agent ip 3.3.3.1
[Switch] sflow collector ip 3.3.3.2
[Switch] sflow interval 30
# 在端口 GigabitEthernet 1/0/1 上开启 sFlow 功能,对出入方向的数据包采样。
# 配置采样频率为 1000。(默认200000个报文抽一个,最小支持1000个抽一个)
[Switch] interface GigabitEthernet 1/0/1
[Switch-GigabitEthernet1/0/1] sflow enable inbound
[Switch-GigabitEthernet1/0/1] sflow enable outbound
[Switch-GigabitEthernet1/0/1] sflow sampling-rate 1000
# 显示 sFlow 的配置信息。
[Switch-GigabitEthernet1/0/1] display sflow
如果文章对你有帮助,欢迎点击上方按钮打赏作者
谢谢你请我吃辣条
暂无评论