我正在使用pylibnet构造和发送UDP数据包。我以此方式构造的UDP数据包似乎都具有无效的校验和。例:
# python
Python 2.4.3 (#1, Sep 3 2009, 15:37:12)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import libnet
>>> from libnet.constants import *
>>>
>>> net = libnet.context(RAW4, 'venet0:0')
>>> ip = net.name2addr4('www.stackoverflow.com', RESOLVE)
>>> data = 'This is my payload.'
>>> udptag = net.build_udp(sp=54321, dp=54321, payload=data)
>>> packetlen = IPV4_H + UDP_H + len(data)
>>> iptag = net.autobuild_ipv4(len=packetlen, prot=IPPROTO_UDP, dst=ip)
>>>
>>> net.write()
在发送主机上捕获上述数据包将显示无效的校验和:
# tcpdump -i venet0:0 -n -v -v port 54321
tcpdump: WARNING: arptype 65535 not supported by libpcap - falling back to cooked socket
tcpdump: listening on venet0:0, link-type LINUX_SLL (Linux cooked), capture size 96 bytes
08:16:10.303719 IP (tos 0x0, ttl 64, id 1, offset 0, flags [none], proto: UDP (17), length: 47) 192.168.55.10.54321 > 69.59.196.211.54321: [bad udp cksum 50c3!] UDP, length 0
我在这里做错什么了吗?
最佳答案
它与tcpdump错误或校验和卸载无关。 Libnet也会在用户模式(FYI)中计算校验和。问题与您未指定UDP标头的长度有关。这不是在pylibnet或libnet中自动计算出来的,因此您必须暂时指定它。下面是您的代码的更正的版本。我将对pylibnet应用补丁,以自动检测rc6中的标头长度。敬请关注http://sourceforge.net/projects/pylibnet进行更新。我将推动解决此问题的新版本。顺便说一句,如果您有错误或功能要求,请随时通过sourceforge的pylibnet页面与我联系。我喜欢收到使用我的软件的开发人员的来信:)
import libnet
from libnet.constants import *
net = libnet.context(RAW4, 'venet0:0')
ip = net.name2addr4('www.stackoverflow.com', RESOLVE)
data = 'This is my payload.'
udptag = net.build_udp(len=UDP_H+len(data), sp=54321, dp=54321, payload=data)
packetlen = IPV4_H + UDP_H + len(data)
iptag = net.autobuild_ipv4(len=packetlen, prot=IPPROTO_UDP, dst=ip)
net.write()