hbugs001 发表于 2025-12-31 00:06:15

实时流量洞察包过滤规则

<h1>1 实时流量洞察</h1>
<p><strong>注意! 1、该功能目前仅支持节点对象之间互联接口</strong>∗∗出/OUT∗∗**出/OUT**<strong>∗</strong>∗<strong>出</strong>/<strong>O</strong>U<strong>T</strong>∗**∗**<strong>方向的流量,并且不支持网络对象(例如cloud 0-9,nat等类型,network对象没有接口)</strong></p>
<p><strong>2、如果创建了包含错误表达式的过滤器,需要删除过滤器重新添加才行,直接修改表达式无效</strong></p>
<h2>1.1 介绍</h2>
<p>该功能可以直接高亮显示出流量经过的链路路径,相对于在接口抓包而言更加直观。</p>
<ul>
<li>使用BPF PCAP捕获报文(<strong>支持捕获过滤器表达式</strong>,<strong>不支持wireshark显示过滤器表达式</strong>,不支持linux shell 过滤条件);需要通过<strong>BPF PCAP filters来问AI想要捕获报文的过滤表达式</strong></li>
<li>支持自定义不同过滤器颜色;</li>
<li>支持统计链路两端节点出方向的数据包数量;</li>
</ul>
<table>
<thead>
<tr>
<th>特性</th>
<th><strong>捕获过滤器 (Capture Filter)</strong></th>
<th><strong>显示过滤器 (Display Filter)</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>工作阶段</strong></td>
<td><strong>抓包时</strong>(数据进入前)</td>
<td><strong>分析时</strong>(数据已捕获后)</td>
</tr>
<tr>
<td><strong>处理位置</strong></td>
<td>内核层/驱动层 (libpcap/BPF)</td>
<td>应用层 (Wireshark)</td>
</tr>
<tr>
<td><strong>语法引擎</strong></td>
<td>**BPF (Berkeley Packet Filter)**语法</td>
<td><strong>Wireshark 专用语法</strong></td>
</tr>
<tr>
<td><strong>执行速度</strong></td>
<td><strong>极快</strong>(内核过滤)</td>
<td>较慢(应用层解析后过滤)</td>
</tr>
<tr>
<td><strong>资源影响</strong></td>
<td><strong>减少内存/CPU使用</strong>(丢弃无关数据)</td>
<td>不影响已保存文件大小</td>
</tr>
<tr>
<td><strong>灵活性</strong></td>
<td><strong>有限</strong>(只能<strong>基于头部信息</strong>)</td>
<td><strong>强大</strong>(<strong>可解析所有字段</strong>)</td>
</tr>
<tr>
<td><strong>可用字段</strong></td>
<td>主要基于<strong>协议头部固定偏移</strong></td>
<td>所有<strong>解析出的协议字段</strong></td>
</tr>
<tr>
<td><strong>修改结果</strong></td>
<td><strong>永久影响</strong>(未捕获的数据永远丢失)</td>
<td><strong>临时影响</strong>(可随时更改)</td>
</tr>
</tbody>
</table>
<p><a href="https://www.wolai.com/8U9Xx89esUgZKonu3L6GoK"><strong>捕获/显示过滤器区别详情右键新标签页打开</strong></a></p>
<table>
<thead>
<tr>
<th>过滤需求</th>
<th><strong>捕获过滤器 (BPF)</strong></th>
<th><strong>显示过滤器 (Wireshark)</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>HTTP 流量</td>
<td><code>tcp port 80</code></td>
<td><code>http</code>或 <code>tcp.port == 80</code></td>
</tr>
<tr>
<td>来自 192.168.1.1</td>
<td><code>src host 192.168.1.1</code></td>
<td><code>ip.src == 192.168.1.1</code></td>
</tr>
<tr>
<td>ARP 请求</td>
<td><code>arp = 1</code></td>
<td><code>arp.opcode == 1</code></td>
</tr>
<tr>
<td>TCP SYN 包</td>
<td><code>tcp &amp; 2 != 0</code></td>
<td><code>tcp.flags.syn == 1</code></td>
</tr>
<tr>
<td>数据包长度 &gt; 1000</td>
<td><code>greater 1000</code></td>
<td><code>frame.len &gt; 1000</code></td>
</tr>
<tr>
<td>DNS 响应</td>
<td>(无法区分)</td>
<td><code>dns.flags.response == 1</code></td>
</tr>
<tr>
<td>HTTP 404 错误</td>
<td>(无法实现)</td>
<td><code>http.response.code == 404</code></td>
</tr>
</tbody>
</table>
<h2>1.2 使用方法</h2>
<h3>1.2.1 进入实验后,在侧边栏点击流量过滤器</h3>
<p><img src="data/attachment/forum/202512/31/000553e2ptl3caqy3hqzwl.png" alt="" /></p>
<h3>1.2.2 新建过滤表达式</h3>
<p><img src="data/attachment/forum/202512/31/000553ztlyretprnm6wep6.png" alt="" /></p>
<h3>1.2.3 填写表达式</h3>
<p><img src="data/attachment/forum/202512/31/000553lrrl7v7j747mla3a.png" alt="" /></p>
<h3>1.2.4 效果演示</h3>
<p>下面视频为使用流量过滤器在链路上显示icmp流量的演示(过滤表达式:icmp,显示颜色:红色,显示时间:500ms)</p>
<h1>2 逻辑<strong>运算符</strong></h1>
<ul>
<li>and:所有的条件都需要满足</li>
<li>or:只要有一个条件满足就可以</li>
<li>not:取反</li>
</ul>
<blockquote>
<p>支持 <code>&gt; &lt; = &gt;= &lt;= &amp;&amp; || ! </code></p>
</blockquote>
<h2>2.1 协议筛选</h2>
<ul>
<li>icmp:<code>icmp</code></li>
<li>tcp:<code>tcp</code></li>
<li>udp:<code>udp</code></li>
<li>http:<code>port 80</code></li>
<li>dns:<code>port 53</code></li>
</ul>
<h1>3 二层协议</h1>
<h2>3.1 以太网帧格式</h2>
<p><a href="https://www.wolai.com/qWQNMAp3AmtuKmj2ewpKgq">https://www.wolai.com/qWQNMAp3AmtuKmj2ewpKgq</a></p>
<h2>3.2 802.1Q VLAN</h2>
<p><a href="https://www.wolai.com/wt6rEFtqwF8rg8yiopnfEN">https://www.wolai.com/wt6rEFtqwF8rg8yiopnfEN</a></p>
<h2>3.3 802.1ad (Q-IN-Q)</h2>
<p><a href="https://www.wolai.com/cT1DrYqbMCDC2yiEyXpvrn">https://www.wolai.com/cT1DrYqbMCDC2yiEyXpvrn</a></p>
<h2>3.4 PVLAN</h2>
<p><a href="https://www.wolai.com/mLD9W1b94HtB4Nj4YkgAQh">https://www.wolai.com/mLD9W1b94HtB4Nj4YkgAQh</a></p>
<h2>3.5 LACP</h2>
<p><a href="https://www.wolai.com/w3J83APT18pX6g8LGfLYKh">https://www.wolai.com/w3J83APT18pX6g8LGfLYKh</a></p>
<h2>3.6 PAgP</h2>
<p><a href="https://www.wolai.com/jpK2gZVuyVJfRzmfMcLYRi">https://www.wolai.com/jpK2gZVuyVJfRzmfMcLYRi</a></p>
<h2>3.7 华为Eth-Trunk</h2>
<p><a href="https://www.wolai.com/7qGTeq8bK6yFQZCbTY4dL6">https://www.wolai.com/7qGTeq8bK6yFQZCbTY4dL6</a></p>
<h2>3.8 STP</h2>
<p><a href="https://www.wolai.com/mVf61ZDi3mNuMziGF1SUvC">https://www.wolai.com/mVf61ZDi3mNuMziGF1SUvC</a></p>
<h2>3.9 RSTP</h2>
<p><a href="https://www.wolai.com/6EcBrCa1k9EFejxneRBZTB">https://www.wolai.com/6EcBrCa1k9EFejxneRBZTB</a></p>
<h2>3.10 MSTP</h2>
<p><a href="https://www.wolai.com/bLsX23CVAHFdMJjVAKdXG">https://www.wolai.com/bLsX23CVAHFdMJjVAKdXG</a></p>
<h2>3.11 L2TP</h2>
<p><a href="https://www.wolai.com/ngyFSdwwyEDcb5g93MRkW">https://www.wolai.com/ngyFSdwwyEDcb5g93MRkW</a></p>
<h2>3.12 思科PVST/PVST+/Rapid-PVST+</h2>
<p><a href="https://www.wolai.com/vw829W6YYHgRGViqYax3Bt">https://www.wolai.com/vw829W6YYHgRGViqYax3Bt</a></p>
<h2>3.13 PPPOE</h2>
<p><a href="https://www.wolai.com/i23dUQ2b8bNr5a1XyGi6sJ">https://www.wolai.com/i23dUQ2b8bNr5a1XyGi6sJ</a></p>
<h2>3.14 DOT1X</h2>
<p><a href="https://www.wolai.com/bWpzB7AN6TvJW7XKF9zRcH">https://www.wolai.com/bWpzB7AN6TvJW7XKF9zRcH</a></p>
<h2>3.15 LLDP</h2>
<p><a href="https://www.wolai.com/nyovvgVoHdzRLYaGRKQpbE">https://www.wolai.com/nyovvgVoHdzRLYaGRKQpbE</a></p>
<h2>3.16 CDP</h2>
<p><a href="https://www.wolai.com/jMq2gnLubteuFmr3fYRers">https://www.wolai.com/jMq2gnLubteuFmr3fYRers</a></p>
<h2>3.17 MPLS</h2>
<p><a href="https://www.wolai.com/sAZYt3f68dM6LjEoyayW8Y">https://www.wolai.com/sAZYt3f68dM6LjEoyayW8Y</a></p>
<h2>3.18 SRV6二层应用</h2>
<p><a href="https://www.wolai.com/pPoU9KWNcBZLsXveDQoQyL">https://www.wolai.com/pPoU9KWNcBZLsXveDQoQyL</a></p>
<h2>3.19 M-LAG</h2>
<p><a href="https://www.wolai.com/hNCk2S7QoZKkqPUTQkqjuG">https://www.wolai.com/hNCk2S7QoZKkqPUTQkqjuG</a></p>
<h1>4 三层协议</h1>
<h2>4.1 IPv4</h2>
<h2>4.2 IPV6</h2>
<h2>4.3 NAT</h2>
<h2>4.4 ARP</h2>
<h2>4.5 RARP</h2>
<h2>4.6 Proxy ARP</h2>
<h2>4.7 GARP</h2>
<h2>4.8 NDP(IPv6)</h2>
<h2>4.9 路由协议</h2>
<h2>4.10 隧道协议</h2>
<h2>4.11 QOS</h2>
<h2>4.12 ICMP/ICMPV6</h2>
<h2>4.13 VRRP/HSRP/GLBP</h2>
<h2>4.14 BFD</h2>
<h2>4.15 DHCP/DHCPv6</h2>
<h1>5 四层协议</h1>
<h2>5.1 TCP</h2>
<h2>5.2 UDP</h2>
<h2>5.3 TLS/SSL</h2>
<h2>5.4 DTLS</h2>
<h1>6 应用层协议</h1>
<h2>6.1 HTTP</h2>
<table>
<thead>
<tr>
<th><strong>表达式语法</strong></th>
<th>筛选内容</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>tcp port 80</code></td>
<td>http协议</td>
<td></td>
</tr>
<tr>
<td><code>80 and tcp[((tcp&amp;0xf0)&gt;&gt;2)+8:4] = 0x20XXYYZZ</code></td>
<td>http 200响应</td>
<td></td>
</tr>
<tr>
<td><code>tcp port 80 and tcp[((tcp&amp;0xf0)&gt;&gt;2)+8:4] = 0x20343034</code></td>
<td>http 404响应</td>
<td></td>
</tr>
<tr>
<td><code>tcp port 80 and tcp[((tcp&amp;0xf0)&gt;&gt;2)+8:4] = 0x20353032</code></td>
<td>http 502 响应</td>
<td></td>
</tr>
<tr>
<td><code>tcp port 80 and tcp[((tcp&amp;0xf0)&gt;&gt;2):4] = 0x47455420</code></td>
<td>http GET请求</td>
<td></td>
</tr>
<tr>
<td><code>tcp port 80 and tcp[((tcp&amp;0xf0)&gt;&gt;2):4] = 0x504f5354</code></td>
<td>http POST请求</td>
<td></td>
</tr>
</tbody>
</table>
<p>更多内容请参考:</p>
<p><a href="https://www.wolai.com/8R78KkgziNE1nrXuevyhYr">https://www.wolai.com/8R78KkgziNE1nrXuevyhYr</a></p>
<hr />
<h1>7 ICMP</h1>
<p><a href="/wTLaNCjfiN7PSavcZCL53s"><strong>右键新标签页打开查看协议详情</strong></a></p>
<ul>
<li>请求包筛选:<code>icmp == 8</code></li>
<li>应答包筛选:<code>icmp == 0</code></li>
</ul>
<h1>8 思科私有二层协议</h1>
<p><a href="https://www.wolai.com/iiVZBh6C1g9hMERmwkJaME">**右键新标签页打开查看协议详情 **</a></p>
<table>
<thead>
<tr>
<th>协议 (Protocol)</th>
<th>协议全称</th>
<th>SNAP PID</th>
<th>捕获过滤器语法 (Capture Filter Syntax)</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>CDP</strong></td>
<td>Cisco Discovery Protocol</td>
<td><code>0x2000</code></td>
<td><code>ether host 01:00:0c:cc:cc:cc and ether == 0x2000</code></td>
<td></td>
</tr>
<tr>
<td><strong>VTP</strong></td>
<td>VLAN Trunking Protocol</td>
<td><code>0x2003</code></td>
<td><code>ether host 01:00:0c:cc:cc:cc and ether == 0x2003</code></td>
<td></td>
</tr>
<tr>
<td><strong>DTP</strong></td>
<td>Dynamic Trunking Protocol</td>
<td><code>0x2004</code></td>
<td><code>ether host 01:00:0c:cc:cc:cc and ether == 0x2004</code></td>
<td></td>
</tr>
<tr>
<td><strong>PAgP</strong></td>
<td>Port Aggregation Protocol</td>
<td><code>0x0104</code></td>
<td><code>ether host 01:00:0c:cc:cc:cc and ether == 0x0104</code></td>
<td></td>
</tr>
<tr>
<td><strong>UDLD</strong></td>
<td>Unidirectional Link Detection</td>
<td><code>0x0111</code></td>
<td><code>ether host 01:00:0c:cc:cc:cc and ether == 0x0111</code></td>
<td></td>
</tr>
<tr>
<td><strong>ISL</strong></td>
<td>Inter-Switch Link</td>
<td></td>
<td><code>ether dst 01:00:0c:00:00:00</code></td>
<td></td>
</tr>
</tbody>
</table>
<h1>9 HOST</h1>
<table>
<thead>
<tr>
<th><strong>表达式语法</strong></th>
<th><strong>使用条件</strong></th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>dst host &lt;主机&gt;</code></td>
<td>数据包的目的IP是 <code>&lt;主机&gt;</code>,它可以是IP地址或主机名</td>
<td></td>
</tr>
<tr>
<td><code>src host &lt;主机&gt;</code></td>
<td>数据包的源IP是 <code>&lt;主机&gt;</code></td>
<td></td>
</tr>
<tr>
<td><code>host &lt;主机&gt;</code></td>
<td>数据包的源或目的IP是 <code>&lt;主机&gt;</code></td>
<td></td>
</tr>
</tbody>
</table>
<p>以上任何主机表达式前均可加上关键字 <code>ip</code>、<code>arp</code>、<code>rarp</code> 或 <code>ip6</code>,例如 <code>ip host &lt;主机&gt;</code>,这等同于 <code>ether proto \ip and host &lt;主机&gt;</code>。如果 <code>&lt;主机&gt;</code> 是一个对应多个 IP 地址的名称,则会检查每个地址是否匹配。</p>
<h1>10 DHCP</h1>
<table>
<thead>
<tr>
<th><strong>类型</strong></th>
<th><strong>表达式语法</strong></th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td>dhcp discover</td>
<td><code>udp = 0x63350101</code></td>
<td></td>
</tr>
<tr>
<td>dhcp offer</td>
<td><code>udp = 0x63350102</code></td>
<td></td>
</tr>
<tr>
<td>dhcp request</td>
<td><code>udp = 0x63350103</code></td>
<td></td>
</tr>
<tr>
<td>dhcp ack</td>
<td><code>udp = 0x63350105</code></td>
<td></td>
</tr>
</tbody>
</table>
<h1>11 网络与掩码(Net &amp; Mask)</h1>
<table>
<thead>
<tr>
<th><strong>语法</strong></th>
<th><strong>条件</strong></th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>dst net &lt;网络&gt;</code></td>
<td>数据包的 IPv4/v6 目的地址的网络号为 <code>&lt;网络&gt;</code></td>
<td></td>
</tr>
<tr>
<td><code>src net &lt;网络&gt;</code></td>
<td>数据包的 IPv4/v6 源地址的网络号为 <code>&lt;网络&gt;</code></td>
<td></td>
</tr>
<tr>
<td><code>net &lt;网络&gt;</code></td>
<td>数据包的 IPv4/v6 源地址或目的地址的网络号为 <code>&lt;网络&gt;</code></td>
<td></td>
</tr>
<tr>
<td><code>net &lt;网络&gt;mask &lt;掩码&gt;</code></td>
<td>IPv4 地址匹配指定 <code>&lt;网络&gt;</code>和 <code>&lt;掩码&gt;</code>。可以用 <code>src</code>或 <code>dst</code>修饰。注意此语法对 IPv6 网络无效</td>
<td></td>
</tr>
<tr>
<td><code>net &lt;网络&gt;/&lt;长度&gt;</code></td>
<td>IPv4/v6 地址匹配 <code>&lt;网络&gt;</code>及其 <code>&lt;长度&gt;</code>位宽的掩码。可以用 <code>src</code>或 <code>dst</code>修饰</td>
<td></td>
</tr>
</tbody>
</table>
<p><code>&lt;网络&gt;</code> 可以来自配置文件(如 <code>/etc/networks</code> 等)中的名称,也可以是一个网络号。</p>
<p>IPv4 网络号可以按照下表网络号写,则会自动对应指定掩码</p>
<table>
<thead>
<tr>
<th>网络号</th>
<th>掩码</th>
</tr>
</thead>
<tbody>
<tr>
<td>192.168.1.1</td>
<td>255.255.255.255</td>
</tr>
<tr>
<td>192.168.1</td>
<td>255.255.255.0</td>
</tr>
<tr>
<td>192.168</td>
<td>255.255.0.0</td>
</tr>
<tr>
<td>192</td>
<td>255.0.0.0</td>
</tr>
</tbody>
</table>
<p>对于 IPv6 地址,网络只能通过使用网络号和掩码长度来定义。例如,过滤器 <code>net fe80:1234:5678:9abc:0000:0000:0000:0000/64</code>将包含所有 IPv6 地址在以下范围内的流量:<code>fe80:1234:5678:9abc:0000:0000:0000:0000</code> - <code>fe80:1234:5678:9abc:ffff:ffff:ffff:ffff</code>。</p>
<h1>12 PORT</h1>
<table>
<thead>
<tr>
<th><strong>语法</strong></th>
<th><strong>条件</strong></th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>dst port &lt;端口&gt;</code></td>
<td>数据包的目的端口值为 <code>&lt;端口&gt;</code></td>
<td></td>
</tr>
<tr>
<td><code>src port &lt;端口&gt;</code></td>
<td>数据包的源端口值为 <code>&lt;端口&gt;</code></td>
<td></td>
</tr>
<tr>
<td><code>port &lt;端口&gt;</code></td>
<td>数据包的源端口或目的端口值为 <code>&lt;端口&gt;</code></td>
<td></td>
</tr>
<tr>
<td><code>dst portrange &lt;端口1&gt;-&lt;端口2&gt;</code></td>
<td>数据包的目的端口值在 <code>&lt;端口1&gt;</code>到 <code>&lt;端口2&gt;</code>之间</td>
<td></td>
</tr>
<tr>
<td><code>src portrange &lt;端口1&gt;-&lt;端口2&gt;</code></td>
<td>数据包的源端口值在 <code>&lt;端口1&gt;</code>到 <code>&lt;端口2&gt;</code>之间</td>
<td></td>
</tr>
<tr>
<td><code>portrange &lt;端口1&gt;-&lt;端口2&gt;</code></td>
<td>数据包的源端口或目的端口值在 <code>&lt;端口1&gt;</code>到 <code>&lt;端口2&gt;</code>之间</td>
<td></td>
</tr>
</tbody>
</table>
<p>当数据包是 IPv4/IPv6 TCP、IPv4/IPv6 UDP 或 IPv4/IPv6 SCTP(在某些系统中)且具有指定的目的端口值时,条件为真。</p>
<p><code>&lt;端口&gt;</code> 可以是一个数字,也可以是 <code>/etc/services</code> 文件中使用的名称。</p>
<p>如果使用名称,则会同时检查端口号和协议,如果使用数字或含义不明确的名称,则只检查端口号(例如,<code>dst port 513</code> 将匹配 TCP/login 流量和 UDP/who 流量;<code>port domain</code> 将匹配 TCP 53和 UDP 53 流量)。</p>
<p>以上任何端口或端口范围表达式前均可加上关键字 <code>tcp</code> 或 <code>udp</code>。例如,<code>tcp src port &lt;端口&gt;</code> 仅匹配源端口为 <code>&lt;端口&gt;</code> 的 TCP 数据包。</p>
<h1>13 应用层协议</h1>
<h2>13.1 HTTP</h2>
<hr />
<h2>13.2 <strong>BPF应用层协议过滤Cookbook (EVE-NG Pro 专用)</strong></h2>
<h3>13.2.1 <strong>核心原理复习</strong></h3>
<ul>
<li><strong>端口过滤是基础</strong>:先用端口号把范围缩小,例如 <code>tcp port 80</code>。</li>
<li><strong>TCP载荷定位是关键</strong>:由于TCP头长度可变,我们用 <code>tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4]</code> 这段“咒语”来动态定位TCP载荷的起始位置,然后从中读取数据。</li>
<li><strong>UDP载荷定位很简单</strong>:UDP头固定是8字节,所以载荷从第8个字节开始(<code>udp</code>)。</li>
</ul>
<hr />
<h3>13.2.2 <strong>案例大全</strong></h3>
<h4>13.2.2.1 <strong>Web &amp; 核心服务</strong></h4>
<table>
<thead>
<tr>
<th>协议</th>
<th>过滤目标</th>
<th>BPF 捕获过滤器语法</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>HTTP</strong></td>
<td>GET 请求</td>
<td><code>tcp port 80 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x47455420)</code></td>
<td></td>
</tr>
<tr>
<td><strong>HTTP</strong></td>
<td>POST 请求</td>
<td><code>tcp port 80 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x504f5354)</code></td>
<td></td>
</tr>
<tr>
<td><strong>DNS</strong></td>
<td>DNS 查询 (Query)</td>
<td><code>udp port 53 and (udp &amp; 0x80) = 0</code></td>
<td></td>
</tr>
<tr>
<td><strong>DNS</strong></td>
<td>DNS 响应 (Response)</td>
<td><code>udp port 53 and (udp &amp; 0x80) != 0</code></td>
<td></td>
</tr>
</tbody>
</table>
<h4>13.2.2.2 <strong>邮件服务</strong></h4>
<table>
<thead>
<tr>
<th>协议</th>
<th>过滤目标</th>
<th>BPF 捕获过滤器语法</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>SMTP</strong></td>
<td><code>HELO</code>/<code>EHLO</code>握手</td>
<td><code>tcp port 25 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x48454c4f or tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x45484c4f)</code></td>
<td></td>
</tr>
<tr>
<td><strong>POP3</strong></td>
<td><code>USER</code>用户名命令</td>
<td><code>tcp port 110 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x55534552)</code></td>
<td></td>
</tr>
<tr>
<td><strong>IMAP</strong></td>
<td><code>LOGIN</code>登录命令</td>
<td><code>tcp port 143 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x4c4f47494e)</code></td>
<td></td>
</tr>
</tbody>
</table>
<h4>13.2.2.3 <strong>文件传输与共享</strong></h4>
<table>
<thead>
<tr>
<th>协议</th>
<th>过滤目标</th>
<th>BPF 捕获过滤器语法</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>FTP</strong></td>
<td><code>USER</code>用户名命令</td>
<td><code>tcp port 21 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x55534552)</code></td>
<td></td>
</tr>
<tr>
<td><strong>FTP</strong></td>
<td><code>PASS</code>密码命令</td>
<td><code>tcp port 21 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x50415353)</code></td>
<td></td>
</tr>
<tr>
<td><strong>SMB/CIFS</strong></td>
<td>SMB 协议头</td>
<td><code>tcp port 445 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0xff534d42)</code></td>
<td></td>
</tr>
</tbody>
</table>
<h4>13.2.2.4 <strong>远程管理</strong></h4>
<table>
<thead>
<tr>
<th>协议</th>
<th>过滤目标</th>
<th>BPF 捕获过滤器语法</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>SSH</strong></td>
<td>初始版本协商 (<code>SSH-</code>)</td>
<td><code>tcp port 22 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x5353482d)</code></td>
<td></td>
</tr>
<tr>
<td><strong>Telnet</strong></td>
<td>包含 <code>login:</code>提示</td>
<td><code>tcp port 23 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x6c6f6769)</code></td>
<td></td>
</tr>
<tr>
<td><strong>RDP</strong></td>
<td>初始连接请求</td>
<td><code>tcp port 3389 and tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x03000013</code></td>
<td></td>
</tr>
</tbody>
</table>
<h4>13.2.2.5 <strong>目录与时间服务</strong></h4>
<table>
<thead>
<tr>
<th>协议</th>
<th>过滤目标</th>
<th>BPF 捕获过滤器语法</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>NTP</strong></td>
<td>客户端请求 (Mode 3)</td>
<td><code>udp port 123 and (udp &amp; 0x07) = 3</code></td>
<td></td>
</tr>
<tr>
<td><strong>LDAP</strong></td>
<td>任何LDAP消息 (ASN.1 SEQUENCE)</td>
<td><code>tcp port 389 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):1] = 0x30)</code></td>
<td></td>
</tr>
</tbody>
</table>
<h4>13.2.2.6 <strong>VoIP &amp; 流媒体</strong></h4>
<table>
<thead>
<tr>
<th>协议</th>
<th>过滤目标</th>
<th>BPF 捕获过滤器语法</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>SIP</strong></td>
<td><code>INVITE</code>呼叫请求</td>
<td><code>udp port 5060 and (udp = 0x494e5649)</code></td>
<td></td>
</tr>
<tr>
<td><strong>RTP</strong></td>
<td>识别RTP报文 (Version 2)</td>
<td><code>udp and (udp &amp; 0xc0) = 0x80</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<table>
<thead>
<tr>
<th>协议 (Protocol)</th>
<th>过滤目标 (Filtering Goal)</th>
<th>BPF 捕获过滤器语法 (Capture Filter Syntax)</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>HTTP</strong></td>
<td>过滤所有<strong>GET</strong>请求</td>
<td><code>tcp port 80 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x47455420)</code></td>
<td></td>
</tr>
<tr>
<td><strong>HTTP</strong></td>
<td>过滤所有<strong>POST</strong>请求</td>
<td><code>tcp port 80 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x504f5354)</code></td>
<td></td>
</tr>
<tr>
<td><strong>DNS</strong></td>
<td>只过滤<strong>查询</strong>报文 (Query)</td>
<td><code>udp port 53 and (udp &amp; 0x80) = 0</code></td>
<td></td>
</tr>
<tr>
<td><strong>DNS</strong></td>
<td>只过滤<strong>响应</strong>报文 (Response)</td>
<td><code>udp port 53 and (udp &amp; 0x80) != 0</code></td>
<td></td>
</tr>
<tr>
<td><strong>NTP</strong></td>
<td>只过滤客户端<strong>请求</strong></td>
<td><code>udp port 123 and (udp &amp; 0x07) = 3</code></td>
<td></td>
</tr>
<tr>
<td><strong>NTP</strong></td>
<td>只过滤服务器<strong>响应</strong></td>
<td><code>udp port 123 and (udp &amp; 0x07) = 4</code></td>
<td></td>
</tr>
<tr>
<td><strong>SSH</strong></td>
<td>过滤初始<strong>协议版本协商</strong>报文</td>
<td><code>tcp port 22 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x5353482d)</code></td>
<td></td>
</tr>
<tr>
<td><strong>FTP</strong></td>
<td>过滤<strong>USER</strong>用户名命令</td>
<td><code>tcp port 21 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x55534552)</code></td>
<td></td>
</tr>
<tr>
<td><strong>FTP</strong></td>
<td>过滤<strong>PASS</strong>密码命令</td>
<td><code>tcp port 21 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x50415353)</code></td>
<td></td>
</tr>
<tr>
<td><strong>DHCP</strong></td>
<td>过滤<strong>DHCPDISCOVER</strong>报文</td>
<td><code>udp port 67 and udp = 0x350101</code></td>
<td></td>
</tr>
<tr>
<td><strong>DHCP</strong></td>
<td>过滤<strong>DHCPOFFER</strong>报文</td>
<td><code>udp port 68 and udp = 0x350102</code></td>
<td></td>
</tr>
<tr>
<td><strong>DHCP</strong></td>
<td>过滤<strong>DHCPREQUEST</strong>报文</td>
<td><code>udp port 67 and udp = 0x350103</code></td>
<td></td>
</tr>
<tr>
<td><strong>DHCP</strong></td>
<td>过滤<strong>DHCPACK</strong>报文</td>
<td><code>udp port 68 and udp = 0x350105</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h3>13.2.3 <strong>重要说明与注意事项</strong></h3>
<ol>
<li><strong>十六进制转换</strong>:上面用到的 <code>0x...</code> 值都是目标字符串的ASCII十六进制表示。比如 <code>GET </code> (注意后面有个空格) 对应的就是 <code>47 45 54 20</code>。</li>
<li><strong>加密流量无效</strong>:对于像HTTPS, SSH(协商后)这类加密的协议,你用这种方法是看不到任何明文内容的,顶多能抓个协议握手。</li>
<li><strong>脆弱性</strong>:这种基于固定偏移量和内容的过滤方法非常脆弱。只要协议有一点点变化,或者报文里多了个选项,就可能导致过滤失效。</li>
<li><strong>DHCP偏移量</strong>:上面DHCP的例子 <code>udp</code> 是基于标准DHCP报文结构计算的(UDP载荷的第240字节开始是DHCP选项)。如果你遇到的环境有特殊封装,这个偏移量可能需要调整(就像我们之前讨论的那样)。</li>
</ol>
<h1>14 路由协议</h1>
<h2>14.1 OSPF</h2>
<h3>14.1.1 <strong>OSPFv2 报文类型过滤 (IPv4)</strong></h3>
<table>
<thead>
<tr>
<th>标题 (Title)</th>
<th>颜色 (Color)</th>
<th>过滤表达式 (Filter Expression)</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>OSPFv2 - ALL</strong></td>
<td>(自定义)</td>
<td><code>ip proto 89</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv2 - Hello</td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 1</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv2 - DBD</td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 2</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv2 - LSR</td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 3</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv2 - LSU</td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 4</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv2 - LSAck</td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 5</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv2 - Multicast</td>
<td>(自定义)</td>
<td><code>ip host 224.0.0.5 or ip host 224.0.0.6</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h3>14.1.2 <strong>OSPFv3 报文类型过滤 (IPv6)</strong></h3>
<p>OSPFv3是跑在IPv6上的,IPv6头部是固定的40字节,所以偏移量也很好算。</p>
<table>
<thead>
<tr>
<th>标题 (Title)</th>
<th>颜色 (Color)</th>
<th>过滤表达式 (Filter Expression)</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>OSPFv3 - ALL</strong></td>
<td>(自定义)</td>
<td><code>ip6 proto 89</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv3 - Hello</td>
<td>(自定义)</td>
<td><code>ip6 proto 89 and ip6 == 1</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv3 - DBD</td>
<td>(自定义)</td>
<td><code>ip6 proto 89 and ip6 == 2</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv3 - LSR</td>
<td>(自定义)</td>
<td><code>ip6 proto 89 and ip6 == 3</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv3 - LSU</td>
<td>(自定义)</td>
<td><code>ip6 proto 89 and ip6 == 4</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv3 - LSAck</td>
<td>(自定义)</td>
<td><code>ip6 proto 89 and ip6 == 5</code></td>
<td></td>
</tr>
<tr>
<td>OSPFv3 - Multicast</td>
<td>(自定义)</td>
<td><code>ip6 host ff02::5 or ip6 host ff02::6</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h3>14.1.3 <strong>进阶:OSPF LSA 类型过滤</strong></h3>
<p>如果你想看得更细,比如只想看特定类型的LSA(LSA是装在LSU报文里的),也可以做到。这对于分析复杂的路由问题特别有用。</p>
<p>(LSA Type的偏移量计算:IP头20字节 + OSPF头24字节 + LSU头4字节 + LSA头内部偏移3字节 = 51)</p>
<table>
<thead>
<tr>
<th>标题 (Title)</th>
<th>颜色 (Color)</th>
<th>过滤表达式 (Filter Expression)</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>LSA - Type 1 (Router)</strong></td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 4 and ip == 1</code></td>
<td></td>
</tr>
<tr>
<td><strong>LSA - Type 2 (Network)</strong></td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 4 and ip == 2</code></td>
<td></td>
</tr>
<tr>
<td><strong>LSA - Type 3 (Summary)</strong></td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 4 and ip == 3</code></td>
<td></td>
</tr>
<tr>
<td><strong>LSA - Type 5 (External)</strong></td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 4 and ip == 5</code></td>
<td></td>
</tr>
<tr>
<td><strong>LSA - Type 7 (NSSA)</strong></td>
<td>(自定义)</td>
<td><code>ip proto 89 and ip == 4 and ip == 7</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<hr />
<h1>15 举例</h1>
<h2>15.1 筛选访问指定1.1.1.1 8080端口的流量</h2>
<p>这里不指定源或目的ip而是直接使用host关键字,这样才能抓取完整会话流量。</p>
<pre><code class="language-Bash">tcp port 8080 and host 1.1.1.1
</code></pre>
<h3>15.1.1 过滤访问223.5.5.5 DNS的流量</h3>
<pre><code class="language-Bash">host 223.5.5.5 and port 53
</code></pre>
<h1>16 BPF过滤能力清单</h1>
<hr />
<h2>16.1 <strong>BPF 直接过滤能力清单 (分层详解)</strong></h2>
<h3>16.1.1 <strong>第二层:数据链路层 (Layer 2)</strong></h3>
<p>这是BPF的起点,它对以太网帧头了如指掌。</p>
<table>
<thead>
<tr>
<th>协议/字段</th>
<th>BPF 捕获过滤器原语/语法</th>
<th>举例</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>以太网 MAC 地址</strong></td>
<td><code>ether src</code>,<code>ether dst</code>,<code>ether host</code></td>
<td><code>ether host 00:11:22:33:44:55</code></td>
<td></td>
</tr>
<tr>
<td><strong>以太网 协议类型</strong></td>
<td><code>ether proto &lt;protocol&gt;</code></td>
<td><code>ether proto arp</code>(或 <code>arp</code>)</td>
<td></td>
</tr>
<tr>
<td><strong>VLAN (802.1Q)</strong></td>
<td><code>vlan </code></td>
<td><code>vlan 100</code>(匹配单层VLAN 100)</td>
<td></td>
</tr>
<tr>
<td><strong>QinQ (802.1ad)</strong></td>
<td><code>vlan and vlan </code></td>
<td><code>vlan 100 and vlan 20</code>(匹配外100内20)</td>
<td></td>
</tr>
<tr>
<td><strong>ARP/RARP</strong></td>
<td><code>arp</code>,<code>rarp</code></td>
<td><code>arp and host 192.168.1.1</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h4>16.1.1.1 <strong>第三层:网络层 (Layer 3)</strong></h4>
<p>这是BPF最强大的主场,几乎IP头里的所有东西都能被直接或间接地过滤。</p>
<table>
<thead>
<tr>
<th>协议/字段</th>
<th>BPF 捕获过滤器原语/语法</th>
<th>举例</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>IP 地址</strong></td>
<td><code>src</code>,<code>dst</code>,<code>host</code>,<code>net</code></td>
<td><code>host 8.8.8.8</code>或 <code>net 192.168.0.0/16</code></td>
<td></td>
</tr>
<tr>
<td><strong>IP 协议号</strong></td>
<td><code>proto &lt;protocol&gt;</code>或 <code>ip proto &lt;protocol&gt;</code></td>
<td><code>ip proto 89</code>(OSPF),<code>ip proto 88</code>(EIGRP)</td>
<td></td>
</tr>
<tr>
<td><strong>ICMP</strong></td>
<td><code>icmp</code>,<code>icmp6</code></td>
<td><code>icmp</code>(匹配所有ICMPv4)</td>
<td></td>
</tr>
<tr>
<td><strong>IGMP</strong></td>
<td><code>igmp</code></td>
<td><code>igmp</code>(匹配所有IGMP)</td>
<td></td>
</tr>
<tr>
<td><strong>IP 广播/组播</strong></td>
<td><code>broadcast</code>,<code>multicast</code></td>
<td><code>ip broadcast</code>(匹配所有IP广播)</td>
<td></td>
</tr>
<tr>
<td><strong>IP头长度/TTL等</strong></td>
<td><code>ip</code>(字节偏移量)</td>
<td><code>ip &lt; 10</code>(过滤TTL小于10的包)</td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h4>16.1.1.2 <strong>第四层:传输层 (Layer 4)</strong></h4>
<p>BPF对TCP和UDP的头部也非常熟悉。</p>
<table>
<thead>
<tr>
<th>协议/字段</th>
<th>BPF 捕获过滤器原语/语法</th>
<th>举例</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>TCP/UDP 端口</strong></td>
<td><code>src port</code>,<code>dst port</code>,<code>port</code>,<code>portrange</code></td>
<td><code>tcp port 80</code>或 <code>udp portrange 1000-2000</code></td>
<td></td>
</tr>
<tr>
<td><strong>TCP 标志位</strong></td>
<td><code>tcp</code>(位掩码)</td>
<td><code>tcp &amp; tcp-syn != 0</code>(过滤SYN包)</td>
<td></td>
</tr>
<tr>
<td><strong>TCP 标志位组合</strong></td>
<td><code>tcp</code>(位掩码)</td>
<td>`tcp &amp; (tcp-syn</td>
<td>tcp-ack) = (tcp-syn</td>
</tr>
</tbody>
</table>
<hr />
<h3>16.1.2 <strong>核心原理:为什么二三四层这么容易?</strong></h3>
<p>答案很简单:<strong>因为这些层的协议头部格式是固定的、标准化的。</strong></p>
<ul>
<li>BPF引擎就像一个流水线上的质检员,它手里有一张“图纸”(协议规范),清楚地知道一个以太网帧的第0-5字节是目的MAC,第6-11字节是源MAC,第12-13字节是协议类型...</li>
<li>对于IP头、TCP头、UDP头也是同理。每个字段(如IP地址、端口号、协议号)在头部里的<strong>位置(偏移量)都是固定的</strong>。</li>
<li>所以,BPF可以以极高的效率,直接去“摸”特定位置的字节,然后跟你给的条件进行比较。这就是它为什么快,为什么天生就适合干这个。</li>
</ul>
<p>而我们之前讨论的应用层协议(HTTP、DNS等),它们的内容位于TCP/UDP的**载荷(Payload)**里,这个载荷的结构是多变的、长度不定的,所以BPF这个“质检员”就很难直接找到它想要的东西,必须用各种“黑魔法”去猜位置。</p>
<hr />
<p><strong>总结一下:</strong></p>
<p>基本上,只要一个协议的信息是明确地、固定地写在<strong>二、三、四层头部</strong>里的,BPF就能用它内置的“原语”(比如 <code>host</code>, <code>port</code>, <code>vlan</code>)或者简单的字节偏移量直接过滤。这覆盖了绝大多数网络基础协议。</p>
<h1>17 BPF Pcap 过滤常见报文</h1>
<h2>17.1 一、二层(数据链路层)过滤条件</h2>
<table>
<thead>
<tr>
<th>过滤场景</th>
<th>BPF过滤条件</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td>所有ARP包</td>
<td><code>arp</code></td>
<td></td>
</tr>
<tr>
<td>仅ARP请求</td>
<td><code>arp and arp == 1</code></td>
<td></td>
</tr>
<tr>
<td>仅ARP响应</td>
<td><code>arp and arp == 2</code></td>
<td></td>
</tr>
<tr>
<td>源MAC为指定值</td>
<td><code>ether src 00:11:22:33:44:55</code></td>
<td></td>
</tr>
<tr>
<td>目的MAC为指定值</td>
<td><code>ether dst 00:11:22:33:44:55</code></td>
<td></td>
</tr>
<tr>
<td>以太网广播包</td>
<td><code>ether dst ff:ff:ff:ff:ff:ff</code></td>
<td></td>
</tr>
<tr>
<td>所有以太网多播包</td>
<td><code>ether &amp; 1 = 1</code></td>
<td></td>
</tr>
<tr>
<td>VLAN ID为100的包</td>
<td><code>vlan 100</code></td>
<td></td>
</tr>
<tr>
<td>双层VLAN(外层100、内层200)</td>
<td><code>vlan 100 and vlan 200</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h2>17.2 二、三层(网络层)过滤条件</h2>
<table>
<thead>
<tr>
<th>过滤场景</th>
<th>BPF过滤条件</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td>所有IPv4包</td>
<td><code>ip</code></td>
<td></td>
</tr>
<tr>
<td>所有IPv6包</td>
<td><code>ip6</code></td>
<td></td>
</tr>
<tr>
<td>源IP为指定地址</td>
<td><code>src host 192.168.1.1</code></td>
<td></td>
</tr>
<tr>
<td>目的IP为指定地址</td>
<td><code>dst host 192.168.1.1</code></td>
<td></td>
</tr>
<tr>
<td>源/目的IP为指定地址</td>
<td><code>host 192.168.1.1</code></td>
<td></td>
</tr>
<tr>
<td>源IP属于指定网段</td>
<td><code>src net 192.168.1.0/24</code></td>
<td></td>
</tr>
<tr>
<td>源/目的IP属于指定网段</td>
<td><code>net 192.168.1.0/24</code></td>
<td></td>
</tr>
<tr>
<td>所有ICMP包(IPv4)</td>
<td><code>icmp</code></td>
<td></td>
</tr>
<tr>
<td>仅ICMP ping请求</td>
<td><code>icmp and icmp == 8</code></td>
<td></td>
</tr>
<tr>
<td>仅ICMP ping响应</td>
<td><code>icmp and icmp == 0</code></td>
<td></td>
</tr>
<tr>
<td>所有ICMPv6包</td>
<td><code>icmp6</code></td>
<td></td>
</tr>
<tr>
<td>所有IP协议中的TCP包</td>
<td><code>ip proto 6</code>(等价于 <code>tcp</code>)</td>
<td></td>
</tr>
<tr>
<td>所有IP协议中的UDP包</td>
<td><code>ip proto 17</code>(等价于 <code>udp</code>)</td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h2>17.3 三、四层(传输层)过滤条件</h2>
<table>
<thead>
<tr>
<th>过滤场景</th>
<th>BPF过滤条件</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td>所有TCP包</td>
<td><code>tcp</code></td>
<td></td>
</tr>
<tr>
<td>源TCP端口为80</td>
<td><code>tcp src port 80</code></td>
<td></td>
</tr>
<tr>
<td>目的TCP端口为80</td>
<td><code>tcp dst port 80</code></td>
<td></td>
</tr>
<tr>
<td>源/目的TCP端口为80</td>
<td><code>tcp port 80</code></td>
<td></td>
</tr>
<tr>
<td>TCP端口范围1-1024</td>
<td><code>tcp portrange 1-1024</code></td>
<td></td>
</tr>
<tr>
<td>仅TCP SYN包</td>
<td><code>tcp &amp; tcp-syn != 0</code></td>
<td></td>
</tr>
<tr>
<td>仅TCP SYN+ACK包</td>
<td><code>tcp &amp; (tcp-syntcp-ack) == (tcp-syntcp-ack)</code></td>
<td></td>
</tr>
<tr>
<td>仅TCP FIN包</td>
<td><code>tcp &amp; tcp-fin != 0</code></td>
<td></td>
</tr>
<tr>
<td>仅TCP RST包</td>
<td><code>tcp &amp; tcp-rst != 0</code></td>
<td></td>
</tr>
<tr>
<td>所有UDP包</td>
<td><code>udp</code></td>
<td></td>
</tr>
<tr>
<td>源UDP端口为53</td>
<td><code>udp src port 53</code></td>
<td></td>
</tr>
<tr>
<td>目的UDP端口为53</td>
<td><code>udp dst port 53</code></td>
<td></td>
</tr>
<tr>
<td>源/目的UDP端口为53</td>
<td><code>udp port 53</code></td>
<td></td>
</tr>
<tr>
<td>UDP数据长度&gt;100字节</td>
<td><code>udp and udp &gt; 100</code></td>
<td></td>
</tr>
<tr>
<td>所有SCTP包</td>
<td><code>sctp</code></td>
<td></td>
</tr>
<tr>
<td>SCTP端口为3868</td>
<td><code>sctp port 3868</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<h2>17.4 四、多层组合过滤条件(实战常用)</h2>
<table>
<thead>
<tr>
<th>过滤场景</th>
<th>BPF过滤条件</th>
<th>验证</th>
</tr>
</thead>
<tbody>
<tr>
<td>192.168.1.0/24网段的HTTP流量</td>
<td><code>ip net 192.168.1.0/24 and tcp port 80</code></td>
<td></td>
</tr>
<tr>
<td>非回环的ICMP ping请求</td>
<td><code>icmp and icmp == 8 and not host 127.0.0.1</code></td>
<td></td>
</tr>
<tr>
<td>VLAN 100中目的IP为10.0.0.1的DNS包</td>
<td><code>vlan 100 and ip dst 10.0.0.1 and udp port 53</code></td>
<td></td>
</tr>
<tr>
<td>排除SSH(22端口)的TCP流量</td>
<td><code>tcp and not tcp port 22</code></td>
<td></td>
</tr>
<tr>
<td>源IP为10.0.0.5且目的TCP端口为443</td>
<td><code>src host 10.0.0.5 and tcp dst port 443</code></td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<hr />
<h3>17.4.1 <strong>到底用哪种表达式合适?</strong></h3>
<h4>17.4.1.1 <strong>第一优先级:使用高级原语 (The High-Level Way)</strong></h4>
<p>这是最简单、最可读、也最高效的选择。只要你想过滤的东西,BPF有现成的关键字,就无脑用它。</p>
<ul>
<li><strong>适用场景:</strong> 过滤绝大多数二、三、四层协议。</li>
<li><strong>举例:</strong>
<ul>
<li><code>host 192.168.1.1</code></li>
<li><code>net 192.168.0.0/16</code></li>
<li><code>tcp port 80</code></li>
<li><code>udp port 53</code></li>
<li><code>arp</code></li>
<li><code>vlan 100</code></li>
<li><code>ip proto 89</code> (OSPF)</li>
</ul>
</li>
</ul>
<h4>17.4.1.2 <strong>第二优先级:使用字节偏移量“黑魔法” (The Low-Level Way)</strong></h4>
<p>只有当高级原语<strong>不好用</strong>或<strong>不存在</strong>时,才退而求其次,使用这种“手术刀”式的玩法。</p>
<ul>
<li><strong>适用场景:</strong>
<ol>
<li>过滤那些因为封装特殊导致高级原语失效的协议(<strong>典型案例:CDP, VTP, DTP等思科私有协议</strong>)。</li>
<li>过滤应用层协议的特定内容(比如HTTP方法、DNS查询类型等)。</li>
</ol>
</li>
<li><strong>举例:</strong>
<ul>
<li><strong>过滤CDP:</strong><code>ether host 01:00:0c:cc:cc:cc and ether == 0x2000</code></li>
<li><strong>过滤HTTP POST:</strong><code>tcp port 80 and (tcp[((tcp &amp; 0xf0) &gt;&gt; 2):4] = 0x504f5354)</code></li>
</ul>
</li>
</ul>
<hr />
<h3>17.4.2 <strong>你的Cookbook终极版</strong></h3>
<table>
<thead>
<tr>
<th>协议/流量类型</th>
<th>推荐的EVE-NG Pro过滤表达式</th>
<th>理由</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>IP/TCP/UDP</strong></td>
<td><code>host</code>,<code>net</code>,<code>port</code>,<code>proto</code></td>
<td>使用最高效、最可读的标准原语。</td>
<td></td>
</tr>
<tr>
<td><strong>ARP/VLAN</strong></td>
<td><code>arp</code>,<code>vlan</code></td>
<td>使用标准原语。</td>
<td></td>
</tr>
<tr>
<td><strong>OSPF/EIGRP</strong></td>
<td><code>ip proto 89</code>,<code>ip proto 88</code></td>
<td>使用协议号原语,一网打尽。</td>
<td></td>
</tr>
<tr>
<td><strong>CDP/VTP/DTP等</strong></td>
<td><code>ether host ... and ether == ...</code></td>
<td>标准原语 <code>cdp</code>不可靠,必须用MAC+字节偏移的组合拳。</td>
<td></td>
</tr>
<tr>
<td><strong>HTTP/DNS等应用层</strong></td>
<td><code>tcp/udp port ... and (tcp/udp == ...)</code></td>
<td>没有应用层原语,只能靠字节偏移的“黑魔法”去猜。</td>
<td></td>
</tr>
</tbody>
</table>
<hr />
<hr />
<h3>17.4.3 <strong>BPF/PCAP 常用原语大全 (The Greatest Hits)</strong></h3>
<table>
<thead>
<tr>
<th>类别 (Category)</th>
<th>原语/关键字 (Primitive/Keyword)</th>
<th>功能说明 (Function)</th>
<th>详细举例 (Examples)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>主机/网络 (Host/Network)</strong></td>
<td><code>host</code>,<code>net</code>,<code>src</code>,<code>dst</code></td>
<td>用于过滤IPv4或IPv6地址和网络。</td>
<td><code>host 8.8.8.8</code>(源或目是8.8.8.8)<code>net 192.168.1.0/24</code>(在192.168.1.0/24网段内)<code>src host 10.0.0.1</code>(源是10.0.0.1)<code>dst net 172.16.0.0 mask 255.240.0.0</code>(目标是172.16.0.0/12)</td>
</tr>
<tr>
<td><strong>端口 (Port)</strong></td>
<td><code>port</code>,<code>portrange</code>,<code>src port</code>,<code>dst port</code></td>
<td>用于过滤TCP或UDP端口。</td>
<td><code>port 80</code>(源或目端口是80)<code>src port 1025</code>(源端口是1025)<code>dst portrange 20-21</code>(目的端口在20到21之间)</td>
</tr>
<tr>
<td><strong>协议 (Protocol)</strong></td>
<td><code>proto</code>,<code>ip</code>,<code>ip6</code>,<code>arp</code>,<code>rarp</code>,<code>tcp</code>,<code>udp</code>,<code>icmp</code>,<code>icmp6</code></td>
<td>用于按协议类型过滤。</td>
<td><code>ip proto 89</code>(OSPFv2)<code>tcp</code>(等同于 <code>ip proto 6</code>)<code>udp</code>(等同于 <code>ip proto 17</code>)<code>arp</code>(过滤所有ARP报文)</td>
</tr>
<tr>
<td><strong>数据链路层 (Data Link)</strong></td>
<td><code>ether host</code>,<code>ether src</code>,<code>ether dst</code>,<code>vlan</code></td>
<td>用于过滤MAC地址和VLAN标签。</td>
<td><code>ether host 00:11:22:aa:bb:cc</code>(源或目MAC是...)<code>ether dst ff:ff:ff:ff:ff:ff</code>(以太网广播)<code>vlan 100</code>(匹配VLAN 100)</td>
</tr>
<tr>
<td><strong>广播/组播 (Broadcast/Multicast)</strong></td>
<td><code>broadcast</code>,<code>multicast</code></td>
<td>用于过滤广播或组播地址。</td>
<td><code>ip broadcast</code>(IP广播, x.x.x.255)<code>ip multicast</code>(IP组播, 224.0.0.0/4)<code>ether broadcast</code>(以太网广播, ff:ff:ff:ff:ff:ff)</td>
</tr>
</tbody>
</table>
<hr />
<h1>18 适用场景分析</h1>
<hr />
<h2>18.1 <strong>适用场景 (Where it Shines ✨)</strong></h2>
<p>这个功能的核心价值在于<strong>实时、直观地可视化数据包的路径和类型</strong>。所以,它最擅长处理那些能被BPF语法<strong>清晰、无歧义地定义</strong>的流量。</p>
<h3>18.1.1 <strong>1. 路由与路径验证 (它的核心价值)</strong></h3>
<ul>
<li><strong>场景举例:</strong> 你配置了复杂的策略路由(PBR)或负载均衡,想知道从PC1到服务器的流量到底走了ISP A还是ISP B。</li>
<li><strong>怎么用:</strong> 创建一个过滤器 <code>host &lt;PC1_IP&gt; and host &lt;Server_IP&gt;</code>。然后从PC1去ping服务器,看彩色的线条出现在哪条物理链路上。这比在每个路由器上敲 <code>traceroute</code>要直观一万倍。</li>
</ul>
<h4>18.1.1.1 <strong>2. 协议学习与教学</strong></h4>
<ul>
<li><strong>场景举例:</strong> 一个新手想搞明白OSPF邻居关系是怎么建立的。</li>
<li><strong>怎么用:</strong> 分别创建Hello, DBD, LSR, LSU, LSAck的过滤器,并设置成不同颜色。然后启动两台路由器,你就能在拓扑图上亲眼看到五颜六色的报文按照“Down -&gt; Init -&gt; 2-Way -&gt; ExStart -&gt; Exchange -&gt; Loading -&gt; Full”的顺序依次亮起,瞬间就懂了。
<ul>
<li><strong>Hello包过滤器:</strong><code>ip proto 89 and ip == 1</code></li>
<li><strong>DBD包过滤器:</strong><code>ip proto 89 and ip == 2</code></li>
</ul>
</li>
</ul>
<h4>18.1.1.2 <strong>3. ACL与防火墙策略验证</strong></h4>
<ul>
<li><strong>场景举例:</strong> 你刚在防火墙上写了一条规则,禁止内网 <code>192.168.1.0/24</code>网段访问外部的Telnet服务。</li>
<li><strong>怎么用:</strong> 创建一个过滤器 <code>net 192.168.1.0/24 and tcp port 23</code>。然后从内网PC尝试telnet外部服务器。如果彩色线条在防火墙那里就“断头”了,说明策略生效;如果线条穿过了防火墙,说明你的规则写错了。</li>
</ul>
<h4>18.1.1.3 <strong>4. 二层协议分析</strong></h4>
<ul>
<li>
<p><strong>场景举例:</strong> 你想知道网络里哪台交换机在发送VTP更新,或者哪些端口在跑CDP/LLDP。</p>
</li>
<li>
<p><strong>怎么用:</strong> 直接用我们今天推导出的精确过滤器。</p>
<ul>
<li><strong>VTP过滤器:</strong><code>ether host 01:00:0c:cc:cc:cc and ether == 0x2003</code></li>
<li><strong>CDP过滤器:</strong><code>ether host 01:00:0c:cc:cc:cc and ether == 0x2000</code></li>
</ul>
<p>你就能看到VTP报文是从哪个交换机发出来的,CDP邻居关系是在哪些链路上传递的。</p>
</li>
</ul>
<hr />
<h3>18.1.2 <strong>不适用场景 (Where it's Blind
页: [1]
查看完整版本: 实时流量洞察包过滤规则