为什么 "cksum" 在 Scapy 中不计算
Why "cksum" is not compute in 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 属性仍未计算。
我不明白我的错误。
非常感谢您的帮助
不要使用 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
它应该可以解决您的问题
我用 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 属性仍未计算。 我不明白我的错误。
非常感谢您的帮助
不要使用 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
它应该可以解决您的问题