为什么在Scapy中不计算“ cksum”

时间:2018-08-08 10:14:22

标签: scapy

我用Scapy脚本编写了这段代码。 目的是生成ICMP v6请求并获取校验和。

def generate_frame():
    eth = Ether()
    eth.dst = "00:50:56:9E:7B:BB"
    eth.src = "00:50:56:9E:78:AA"
    eth.type = 0x8100
    ip = IPv6()
    ip.src = "2002:c000:0203:0000:0000:0000:0000:00AA"
    ip.dst = "2002:c000:0203:0000:0000:0000:0000:00BB"
    icmp =ICMPv6EchoRequest(seq=0x1, id=0x792)
    icmp.data = "test"
    icmp = ICMPv6EchoRequest(icmp.do_build())
    ip = IPv6(ip.do_build())
    return eth/Dot1Q(vlan=0x185)/ip/icmp

frame = generate_frame()
hexdump(frame)
#frame.pdfdump(layer_shift = 1)
frame.getlayer(ICMPv6EchoRequest).show2()
print "CRC :"+ str(frame['ICMPv6EchoRequest'].cksum)

我得到结果:

0000  00 50 56 9E 7B BB 00 50 56 9E 78 AA 81 00 01 85  .PV.{..PV.x.....
0010  86 DD 60 00 00 00 00 00 3B 40 20 02 C0 00 02 03  ..`.....;@ .....
0020  00 00 00 00 00 00 00 00 00 AA 20 02 C0 00 02 03  .......... .....
0030  00 00 00 00 00 00 00 00 00 BB 80 00 00 00 07 92  ................
0040  00 01 74 65 73 74                                ..test
###[ ICMPv6 Echo Request ]### 
  type      = Echo Request
  code      = 0
  cksum     = 0x0
  id        = 0x792
  seq       = 0x1
  data      = 'test'

CRC :0

但是 cksum 属性仍未评估。 我不明白我的错误。

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

请勿使用do_build功能。这是一个私人功能,不应使用(这是三步构建过程的第二部分)。

此外,没有必要构建每个层。您可以将它们堆叠起来并在最后构建整个数据包。

要构建整个数据包,请在其上调用bytes()。构建数据包将生成校验和。一个不错的选择是调用Ether(bytes(packet))来构建数据包

使用

def generate_frame():    
    eth = Ether()
    eth.dst = "00:50:56:9E:7B:BB"
    eth.src = "00:50:56:9E:78:AA"
    eth.type = 0x8100
    ip = IPv6()
    ip.src = "2002:c000:0203:0000:0000:0000:0000:00AA"
    ip.dst = "2002:c000:0203:0000:0000:0000:0000:00BB"
    icmp =ICMPv6EchoRequest(seq=0x1, id=0x792)
    icmp.data = "test"
    packet = eth/Dot1Q(vlan=0x185)/ip/icmp
    # Build and recalculate the whole packet
    packet = Ether(bytes(packet))
    return packet

它应该解决您的问题

Corrected code result