Linux网络抓包技巧

举报
Leo Lee 发表于 2020/01/20 19:17:58 2020/01/20
【摘要】 openvswitch(ovs)抓包脚本#!/bin/bashon(){ if [ ! -z "`ip link | grep -w ${dport}`" -o ! -z "`ovs-vsctl find interface name=${dport}`" ];then echo "port ${port} capture is already on." e...

openvswitch(ovs)抓包脚本

#!/bin/bash
on(){
    if [ ! -z "`ip link | grep -w ${dport}`" -o ! -z "`ovs-vsctl find interface name=${dport}`" ];then
        echo "port ${port} capture is already on."
        exit 1
    fi
    in_port=`ovs-vsctl get interface ${port} ofport`
    if [ -z "${in_port}" ];then
        echo "bridge ${br} doesn't have port ${port}."
        exit 1
    fi
    if [ -z "`ip netns | grep -w ovs-capture`" ];then
        ip netns add ovs-capture
    fi
    ovs-vsctl add-port ${br} ${dport} -- set interface ${dport} type=internal
    out_port=`ovs-vsctl get interface ${dport} ofport`
    ip link set ${dport} netns ovs-capture
    ip netns exec ovs-capture ip link set lo up
    ip netns exec ovs-capture ip link set ${dport} up
    ovs-ofctl add-flow ${br} "table=0,cookie=${magic},priority=${priority},reg7=0x0,in_port=${in_port},actions=output:${out_port},set_field:${magic}->reg7,resubmit(,0)"
    echo "Starting capture. Enter ctrl+c interrupt."
    sleep 1
    ip netns exec ovs-capture tcpdump -s 0 -i ${dport} -nne
    echo "capture command: ip netns exec ovs-capture tcpdump -s 0 -i ${dport} -nne"
}
off(){
    in_port=`ovs-vsctl get interface ${port} ofport`
    if [ -z "${in_port}" ];then
        echo "bridge ${br} doesn't have port ${port}."
        exit 1
    fi
    ovs-ofctl del-flows ${br} "table=0,cookie=${magic}/-1,in_port=${in_port}"
    ovs-vsctl del-port ${dport}
}
if [ "$1" = "history" ];then
    cat .ovs-capture.history
    exit
fi
if [ "$1" = "show" ];then
    ip netns exec ovs-capture ip a
    exit 0
fi
if [ $# -ne 3 ];then
    echo "usage: `basename $0`
  <bridge> <port> on/off   switch on/off capture. 
  show                     display all capture ports.
  history                  display history."
  exit 1
fi
br=$1
port=$2
dport=${port:0:11}-cap
magic=0x62370be
priority=65535
$3 && echo "ovs capture is $3 for ${port}"
echo "`date +"%Y-%m-%d %H:%M:%S"` `basename $0` $*" >> .ovs-capture.history

tcpdump高级用法 - 抓取指定vxlan的流量

# 抓取vni 299976的所有流量
tcpdump -s 0 -i eth0 'port 4789 and udp[8:2] = 0x0800 & 0x0800 and udp[11:4] = 299976 & 0x00FFFFFF'
# arp流量
tcpdump -s 0 -i eth0 'port 4789 and udp[8:2] = 0x0800 & 0x0800 and udp[11:4] = 299976 & 0x00FFFFFF and udp[28:2] = 0x0806 & 0x0806'
# ip流量
tcpdump -s 0 -i eth0 'port 4789 and udp[8:2] = 0x0800 & 0x0800 and udp[11:4] = 299976 & 0x00FFFFFF and udp[28:2] = 0x0800 & 0x0800'
# 抓取icmp流量
tcpdump -s 0 -i eth0 'port 4789 and udp[8:2] = 0x0800 & 0x0800 and udp[11:4] = 299976 & 0x00FFFFFF and udp[28:2] = 0x0800 & 0x0800 and udp[39:1] = 0x01 & 0x01'
# 过滤ip源地址 172.16.7.223(0xac1007df)
tcpdump -s 0 -i eth0 'port 4789 and udp[8:2] = 0x0800 & 0x0800 and udp[11:4] = 299976 & 0x00FFFFFF and udp[28:2] = 0x0800 & 0x0800 and udp[42:4] = 0xac1007df'
# 过滤ip目的地址 172.16.7.219(0xac1007db)
tcpdump -s 0 -i eth0 'port 4789 and udp[8:2] = 0x0800 & 0x0800 and udp[11:4] = 299976 & 0x00FFFFFF and udp[28:2] = 0x0800 & 0x0800 and udp[46:4] = 0xac1007db'

udp[28:2] Ethernet协议号(https://zh.wikipedia.org/wiki/%E4%BB%A5%E5%A4%AA%E7%B1%BB%E5%9E%8B )
udp[39:1] IPv4协议号(https://zh.wikipedia.org/wiki/IP%E5%8D%8F%E8%AE%AE%E5%8F%B7%E5%88%97%E8%A1%A8 )
udp[42:4] 源IPv4地址
udp[46:4] 目的IPv4地址
对于有vlan tag的流量,IP层及其之上的offset需要+4

replay.sh

内核态录制回放虚机出口报文脚本

#!/bin/bash
paramters=("$@")
if [ 0 -eq $# ];then
    echo "$0 <portid> [filters...]"
    exit
fi
port_id=$1
tcpdump -i tap${port_id:0:11} -ne ${paramters[@]:1} -xxx -c 1 > a.pcap
packet=`cat a.pcap | grep -v '>' | awk -F: '{print $2}'`; echo $packet
#查询端口的of号
ofid=`ovs-vsctl get interface tap${port_id:0:11} ofport`
# 回放
ovs-appctl ofproto/trace ply${port_id:0:11} in_port=${ofid} "$packet"

 ovs手动trace

# ping报文
ovs-appctl ofproto/trace ply6741a9ba-de "icmp,in_port=1,vlan_tci=0x0000,dl_src=fa:16:3e:bb:a5:8d,dl_dst=fa:16:3e:13:9c:44,nw_src=192.168.2.204,nw_dst=192.168.2.231,nw_tos=0,nw_ecn=0,nw_ttl=64,icmp_type=8,icmp_code=0"
# arp报文
ovs-appctl ofproto/trace plyxxxx "arp,in_port=1,vlan_tci=0x0000,dl_src=fa:16:3e:bb:a5:8d,dl_dst=ff:ff:ff:ff:ff:ff,arp_spa=192.168.2.204,arp_tpa=192.168.2.231,arp_op=1,arp_sha=fa:16:3e:bb:a5:8d,arp_tha=00:00:00:00:00:00"



【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。