Python 使用 ctypes 的数据包嗅探器在复制套接字缓冲区时崩溃

Python packet sniffer using ctypes crashes when copying socket buffer

我正在尝试捕获 IP 数据包的前 20 个字节(完整数据包减去选项),填充 structctype 成员,并将我想要的信息打印到屏幕(协议、源和目标地址)。我的IPclass如下:

import socket
import os
import struct
from ctypes import *

# host to listen on
host = "192.168.0.187"
# our IP header
class IP(Structure):
        _fields_ = [
            ("ihl", c_ubyte, 4),
            ("version", c_ubyte, 4),
            ("tos", c_ubyte),
            ("len", c_ushort),
            ("id", c_ushort),
            ("offset", c_ushort),
            ("ttl", c_ubyte),
            ("protocol_num", c_ubyte),
            ("sum", c_ushort),
            ("src", c_ulong),
            ("dst", c_ulong)
        ]
    def __new__(self, socket_buffer=None):
        return self.from_buffer_copy(socket_buffer)
    def __init__(self, socket_buffer=None):
        # map protocol constants to their names
        self.protocol_map = {1:"ICMP", 6:"TCP", 17:"UDP"}
        # human readable IP addresses
        self.src_address = socket.inet_ntoa(struct.pack("<L",self.src))
        self.dst_address = socket.inet_ntoa(struct.pack("<L",self.dst))
        # human readable protocol
        try:
            self.protocol = self.protocol_map[self.protocol_num]
        except:
            self.protocol = str(self.protocol_num)

现在,我创建套接字,将其绑定到主机,然后循环获取数据包:

# create socket and bind to public interface (os dependent)
if os.name == "nt":
    socket_protocol = socket.IPPROTO_IP
else:
    socket_protocol = socket.IPPROTO_ICMP

# create raw socket
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, 0))
# include header information
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

if os.name == "nt":
    sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
    
try:
    while True:
        # read in a packet
        raw_buffer = sniffer.recvfrom(65565)[0]
        # create an IP header from the first 20 bytes of the buffer
        ip_header = IP(raw_buffer[0:20])
        # print out the protocol that was detected and the hosts
        print "Protocol: %s %s -> %s" % (ip_header.protocol, ip_header.src_¬
                                address, ip_header.dst_address)
# handle CTRL-C
except KeyboardInterrupt:
    # if we're using Windows, turn off promiscuous mode
    if os.name == "nt":
        sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

当 运行 使用 c_ulong 作为 "src""dst" _fields_ struct 成员的数据类型时,出现以下错误(运行在一个终端中 ping 而在另一个终端中 ping nostarch.com):

我假设 c_ulong 的大小可能大于一个字节,因此抛开了我对前 20 个字节的要求(我是 python 的新手)。然后我将 c_ulong 更改为 c_ushort 并再次 运行 :

实际ping路径:

因此,虽然脚本 运行 没有错误,但它切断了 srcdst 地址。

当我告诉它我只想要前 20 个字节时,为什么它要求至少 32 个字节?

(我在 Kali64 VBox VM 中,运行在 Win7 主机上使用 Python 2.7)

感谢任何帮助。

需要验证 IP 的大小,即

print(sizeof(IP))

应该return 20 个字节。由于 ctypes.u_long 在 64 位 linux 的情况下为 8,因此大小将为 32 字节(由于填充而额外增加 4 个字节,由于整数大小而额外增加 8 个字节)。使用 ctypes.u_int 或显式大小如下:

from ctypes import *

class IP(Structure):
    _fields_ = [ ("version", c_uint8, 4),
                 ("ihl", c_uint8, 4),
                 ("tos", c_uint8),
                 ("len", c_uint16),
                 ("id", c_uint16),
                 ("offset", c_uint16),
                 ("ttl", c_uint8),
                 ("protocol_num", c_uint8),
                 ("sum", c_uint16),
                 ("src", c_uint32),
                 ("dst", c_uint32) ]

print(sizeof(IP))