无法在python中发送原始ip数据包

时间:2014-09-17 16:34:24

标签: python network-programming python-sockets

首先,我正在使用Windows 7,(我读过这可能会导致我的问题),我正在尝试使用python发送带有自定义IP头的RAW IP数据包,但是当我在试图用wireshark捕获它,我设法捕获一个数据包,但数据包看起来像这样,我的Windows内核创建了一个自动IP头并添加了我创建的IP头作为IP负载,现在我正在尝试明白我的代码有什么问题?我如何解决它,以便我能够发送带有自定义IP头的原始IP数据包? 这是我的代码:

import socket
import struct

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)



ipheader = ''

byte1 = int(bin(4)[2:].zfill(4)+bin(5)[2:].zfill(4),2)

ipheader = ipheader + struct.pack('!B',byte1)

byte2 = 0

ipheader = ipheader + struct.pack('!B',byte2)

byte34 = 0

ipheader = ipheader + struct.pack('!H',byte34)

byte56 = 16

ipheader = ipheader + struct.pack('!H',byte56)

byte78 = 0

ipheader = ipheader + struct.pack('!H',byte78)

byte9 = 50 

ipheader = ipheader + struct.pack('!B',byte9)

byte10 = 6

ipheader = ipheader + struct.pack('!B',byte10)

byte1112 = 0 

ipheader = ipheader + struct.pack('!H',byte1112)

byte13 = 10
byte14 = 0
byte15 = 0
byte16 = 1

ipheader = ipheader + struct.pack('!4B',byte13,byte14,byte15,byte16)

byte17 = 8
byte18 = 8
byte19 = 8
byte20 = 8

ipheader = ipheader + struct.pack('!4B',byte17,byte18,byte19,byte20)

s.sendto(ipheader,("8.8.8.8",0))

print len(ipheader)

提前致谢。

0 个答案:

没有答案
相关问题