51学通信论坛2017新版

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1306|回复: 0
打印 上一主题 下一主题

如何让Scapy支持组VXLAN报文

[复制链接]

 成长值: 15619

  • TA的每日心情
    开心
    2022-7-17 17:50
  • 2444

    主题

    2544

    帖子

    7万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    74104
    跳转到指定楼层
    楼主
    发表于 2017-9-17 15:56:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


    作者简介:刘敬一,盛科网络SDN交换机产品线测试主管

    1.下载scapy2.3.2
    Scapy的是一个强大的交互式数据包处理程序(使用python编写)。它能够伪造或者解码大量的网络协议数据包,能够发送、捕捉、匹配请求和回复包等等。戳这里下载Scapy
    2.解压缩
    Shell
    ljyfree@ubuntu:~$ unzip scapy-2.3.2.zip
    3.修改/scapy/all.py
    Shell
    ljyfree@ubuntu:~/scapy-2.3.2/scapy$ pwd
    /home/centec/scapy-2.3.2/scapy
    ljyfree@ubuntu:~/scapy-2.3.2/scapy$ vi all.py
    添加一行
    from contrib.all import *
    4.查看vxlan.py
    此时/scapy/contrib/下,应该已经有人提交过vxlan.py
    Shell

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    ljyfree@ubuntu:~/scapy-2.3.2/scapy$ cd contrib/
    ljyfree@ubuntu:~/scapy-2.3.2/scapy/contrib$
    ljyfree@ubuntu:~/scapy-2.3.2/scapy/contrib$ more vxlan.py
    # RFC 7348 - Virtual eXtensible Local Area Network (VXLAN):
    # A Framework for Overlaying Virtualized Layer 2 Networks over Layer 3 Networks
    # http://tools.ietf.org/html/rfc7348
    # scapy.contrib.description = VXLAN
    # scapy.contrib.status = loads
    from scapy.packet import Packet, bind_layers
    from scapy.layers.l2 import Ether
    from scapy.layers.inet import UDP
    from scapy.fields import FlagsField, XByteField, ThreeBytesField
    _VXLAN_FLAGS = ['R' for i in range(0, 24)] + ['R', 'R', 'R', 'I', 'R', 'R', 'R', 'R', 'R']
    class VXLAN(Packet):
    name = "VXLAN"
    fields_desc = [FlagsField("flags", 0x08000000, 32, _VXLAN_FLAGS),
    ThreeBytesField("vni", 0),
    XByteField("reserved", 0x00)]
    def mysummary(self):
    return self.sprintf("VXLAN (vni=%VXLAN.vni%)")
    bind_layers(UDP, VXLAN, dport=4789)
    bind_layers(VXLAN, Ether)
    ljyfree@ubuntu:~/scapy-2.3.2/scapy/contrib$

    5.在scapy/contrib里面添加all.py
    结合scapy/all.py,可以看到这里的all.py用来加载contrib/下面的各个协议定义,尤其要包含vxlan
    Shell

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    ljyfree@ubuntu:~/scapy-2.3.2/scapy/contrib$ more all.py
    from scapy.config import conf
    from scapy.error import log_loading
    import logging
    log = logging.getLogger("scapy.loading")
    def _import_star(m):
    mod = __import__(m, globals, locals)
    for k,v in mod.__dict__.iteritems:
    globals[k] = v
    _contrib_modules_ = ["bgp","igmp","igmpv3","ldp","mpls","ospf","ripng","rsvp", "vxlan"]
    for _l in _contrib_modules_:
    log_loading.debug("Loading module %s" % _l)
    try:
    _import_star(_l)
    except Exception,e:
    log.warning("can't import module %s: %s" % (_l,e))
    ljyfree@ubuntu:~/scapy-2.3.2/scapy/contrib$

    6.安装
    Shell
    ljyfree@ubuntu:~/scapy-2.3.2$ sudo python setup.py install
    running install
    running build
    running build_py
    running build_scripts
    running install_lib
    ...
    running install_scripts
    copying build/scripts-2.7/UTscapy -> /usr/local/bin
    copying build/scripts-2.7/scapy -> /usr/local/bin
    changing mode of /usr/local/bin/UTscapy to 775
    changing mode of /usr/local/bin/scapy to 775
    running install_data
    creating /usr/local/share/man/man1
    copying doc/scapy.1.gz -> /usr/local/share/man/man1
    running install_egg_info
    Writing /usr/local/lib/python2.7/dist-packages/scapy-2.3.2.egg-info
    ljyfree@ubuntu:~/scapy-2.3.2$
    7.验证
    Shell

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41

    ljyfree@ubuntu:~/scapy-2.3.2$ scapy
    INFO: Can't import python gnuplot wrapper . Won't be able to plot.
    INFO: Can't import PyX. Won't be able to use psdump or pdfdump.
    WARNING: No route found for IPv6 destination :: (no default route?)
    WARNING: can't import module igmp: name 'IP' is not defined
    IGMPv3 is still under development - Nov 2010
    WARNING: can't import module igmpv3: name 'IP' is not defined
    WARNING: can't import module ospf: name 'IP6Field' is not defined
    Welcome to Scapy (2.3.2)
    >>>
    >>>
    >>> p = Ether/IP/UDP/VXLAN
    >>> p.show
    ###[ Ethernet ]###
    dst= ff:ff:ff:ff:ff:ff
    src= 00:00:00:00:00:00
    type= 0x800
    ###[ IP ]###
    version= 4
    ihl= None
    tos= 0x0
    len= None
    id= 1
    flags=
    frag= 0
    ttl= 64
    proto= udp
    chksum= None
    src= 127.0.0.1
    dst= 127.0.0.1
    \options\
    ###[ UDP ]###
    sport= domain
    dport= 4789
    len= None
    chksum= None
    ###[ VXLAN ]###
    flags= I
    vni= 0
    reserved= 0x0
    >>>

    --------------华丽的分割线------------------
    本文系《SDNLAB原创文章奖励计划》投稿文章,该计划旨在鼓励广大从业人员在SDN/NFV/Cloud网络领域创新技术、开源项目、产业动态等方面进行经验和成果的文字传播、分享、交流。有意向投稿的同学请通过官方唯一指定投稿通道进行文章投递,投稿细则请参考《SDNLAB原创文章奖励计划》
    声明:本文转载自网络。版权归原作者所有,如有侵权请联系删除。
    扫描并关注51学通信微信公众号,获取更多精彩通信课程分享。

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|51学通信技术论坛

    GMT+8, 2025-2-1 01:46 , Processed in 0.063932 second(s), 32 queries .

    Powered by Discuz! X3

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表