Python icmpv6 客户端

发布于 2024-11-15 10:00:33 字数 1622 浏览 0 评论 0原文

我正在开发一个简单的 icmpv6 客户端,并且校验和有一些问题 这是代码,wireshark显示校验和设置不正确, 回溯显示类似 pcs.FieldBoundsError: 'Value must be Between 0 and 65535' 的错误

import pcs, sys
from socket import *
from pcs.packets.ethernet import *
from pcs.packets.ipv6 import *
from pcs.packets.icmpv6 import *

class packet:
    def __init__(self, src, dst, mac):
        self.src = src
        self.dst = dst
        self.mac = mac

    def construct(self):
        e               = ethernet()
        e.src           = ether_atob("de:de:de:de:de:de")
        e.dst           = ether_atob(self.mac)
        e.type          = ETHERTYPE_IPV6

        ip6               = ipv6()
        ip6.version       = 6
        ip6.traffic_class = 0
        ip6.flow          = 0  
        ip6.length        = 8              # icmpv6 packet length
        ip6.next_header   = IPPROTO_ICMPV6
        ip6.hop           = 255
        ip6.src           = inet_pton(AF_INET6, self.src)
        ip6.dst           = inet_pton(AF_INET6, self.dst)

        icmp6           = icmpv6(ICMP6_ECHO_REQUEST)
        icmp6.code      = 0
        icmp6.id        = 0xf0
        icmp6.seq       = 1
        icmp6.mtu       = 1280
        icmp6.checksum  = 0

        ip6.length      = len(icmp6.getbytes())
        ip6.flow        = len(ip6.getbytes()) + ip6.length

        icmp6.checksum  = icmp6.cksum(ip6)
        pkt = pcs.Chain([e, ip6, icmp6])

        s = pcs.PcapConnector('eth0')
        s.write(pkt.bytes, len(pkt.bytes))

if __name__=='__main__':
    p = packet(sys.argv[1], sys.argv[2], sys.argv[3])
    p.construct()                     

I'm working on a simple icmpv6 client, and i have some problem with checksum
here is the code, wireshark shows that checksum is not set correctly,
the traceback shows an error like pcs.FieldBoundsError: 'Value must be between 0 and 65535'

import pcs, sys
from socket import *
from pcs.packets.ethernet import *
from pcs.packets.ipv6 import *
from pcs.packets.icmpv6 import *

class packet:
    def __init__(self, src, dst, mac):
        self.src = src
        self.dst = dst
        self.mac = mac

    def construct(self):
        e               = ethernet()
        e.src           = ether_atob("de:de:de:de:de:de")
        e.dst           = ether_atob(self.mac)
        e.type          = ETHERTYPE_IPV6

        ip6               = ipv6()
        ip6.version       = 6
        ip6.traffic_class = 0
        ip6.flow          = 0  
        ip6.length        = 8              # icmpv6 packet length
        ip6.next_header   = IPPROTO_ICMPV6
        ip6.hop           = 255
        ip6.src           = inet_pton(AF_INET6, self.src)
        ip6.dst           = inet_pton(AF_INET6, self.dst)

        icmp6           = icmpv6(ICMP6_ECHO_REQUEST)
        icmp6.code      = 0
        icmp6.id        = 0xf0
        icmp6.seq       = 1
        icmp6.mtu       = 1280
        icmp6.checksum  = 0

        ip6.length      = len(icmp6.getbytes())
        ip6.flow        = len(ip6.getbytes()) + ip6.length

        icmp6.checksum  = icmp6.cksum(ip6)
        pkt = pcs.Chain([e, ip6, icmp6])

        s = pcs.PcapConnector('eth0')
        s.write(pkt.bytes, len(pkt.bytes))

if __name__=='__main__':
    p = packet(sys.argv[1], sys.argv[2], sys.argv[3])
    p.construct()                     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2024-11-22 10:00:33

This seems to be related to a bug in python-pcs that causes incorrect checksum calculation. Clipping the errornous bits should work:

cs = icmp6.cksum(ip6) & 0xffff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文