python嗅探器_Python中的包嗅探器

我想在Python 3.5中做一个包嗅探器,它捕获UDP、TCP和ICMP。这是一个简短的例子:import socket

import struct

# the public network interface

HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface

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

s.bind((HOST,0))

# Include IP headers

s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# receive all packages

s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

# receive a package

n=1

while(n<=400):

print(Number , n)

data=s.recvfrom(65565)

packet=data[0]

address= data[1]

header=struct.unpack(!BBHHHBBHBBBBBBBB, packet[:20])

if(header[6]==6): #header[6] is the field of the Protocol

print("Protocol = TCP")

elif(header[6]==17):

print("Protocol = UDP")

elif(header[5]==1):

print("Protocol = ICMP")

n=n+1

问题是它只捕获UDP数据包:(

输出:Number 1 Protocol = UDP Number 2 Protocol = UDP Number 3 Protocol = UDP Number 4 Protocol = UDP Number 5 Protocol = UDP Number 6 Protocol = UDP Number 7

有两种选择:嗅探器只能捕获UDP数据包。

我刚收到UDP数据包。

我认为最合理的答案是我的嗅探器工作不正常,它只是捕获UDP。知道吗?

我想在Python 3.5中做一个包嗅探器,它捕获UDP、TCP和ICMP。这是一个简短的例子:import socket import struct # the public network interface HOST = socket.gethostbyname(socket.gethostname()) # create a raw socket and bind it to the public interface s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP) s.bind((HOST,0)) # Include IP headers s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) # receive all packages s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) # receive a package n=1 while(n<=400): print(Number , n) data=s.recvfrom(65565) packet=data[0] address= data[1] header=struct.unpack(!BBHHHBBHBBBBBBBB, packet[:20]) if(header[6]==6): #header[6] is the field of the Protocol print("Protocol = TCP") elif(header[6]==17): print("Protocol = UDP") elif(header[5]==1): print("Protocol = ICMP") n=n+1 问题是它只捕获UDP数据包:( 输出:Number 1 Protocol = UDP Number 2 Protocol = UDP Number 3 Protocol = UDP Number 4 Protocol = UDP Number 5 Protocol = UDP Number 6 Protocol = UDP Number 7 有两种选择:嗅探器只能捕获UDP数据包。 我刚收到UDP数据包。 我认为最合理的答案是我的嗅探器工作不正常,它只是捕获UDP。知道吗?
经验分享 程序员 微信小程序 职场和发展