鱼C论坛

 找回密码
 立即注册
查看: 2584|回复: 2

python网络嗅探问题

[复制链接]
发表于 2017-7-20 18:00:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
首先先上源代码
import struct
import socket
from ctypes import *

host = "192.168.0.109"

# IP头定义
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_uint),
        ("dst",             c_uint),
    ]

    def __new__(self, socket_buffer=None):
        return self.from_buffer_copy(socket_buffer)

    def __init__(self, socket_buffer=None):
        self.protocol_map = {1: "ICMP", 6: "TCP", 17: "UDP"}

        # readable ip address
        # print (struct.pack("L", self.src))
        self.src_address = socket.inet_ntoa(struct.pack("L", self.src))
        self.dst_address = socket.inet_ntoa(struct.pack("L", self.dst))
        #socket.inet_ntoa转换32位打包的IPV4地址为IP地址的标准点号分隔字符串表示
        #pack按照给定的格式(fmt),把数据封装成字符串(实际上是类似于c结构体的字节流)

        """
fmt 格式
FORMAT        C TYPE        PYTHON TYPE        STANDARD SIZE        NOTES
x        pad byte        no value                  
c        char        string of length 1        1         
b        signed char        integer        1        (3)
B        unsigned char        integer        1        (3)
?        _Bool        bool        1        (1)
h        short        integer        2        (3)
H        unsigned short        integer        2        (3)
i        int        integer        4        (3)
I        unsigned int        integer        4        (3)
l        long        integer        4        (3)
L        unsigned long        integer        4        (3)
q        long long        integer        8        (2), (3)
"""
        

        # type of protocol
        try:
            self.protocol = self.protocol_map[self.protocol_num]#解析ip头
        except:
            self.protocol = str(self.protocol_num)

socket_protocol = socket.IPPROTO_IP

sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, 0))
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

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

try:
    while True:
        raw_buffer = sniffer.recvfrom(65565)[0]

        ip_header = IP(raw_buffer[:20])

        print ("Protocol: %s %s -> %s " % (ip_header.protocol, ip_header.src_address, ip_header.dst_address))
except KeyboardInterrupt:
    sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)


这段代码没有错,执行正常.,

只是不太明白
    def __new__(self, socket_buffer=None):
        return self.from_buffer_copy(socket_buffer)
self.from_buffer_copy(socket_buffer)的用法


还有sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON
这两句的用法

望大神能详细指点一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-20 19:07:30 | 显示全部楼层
help(socket)
     |  ioctl(...)
     |      ioctl(cmd, option) -> long
     |      
     |      Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are
     |      SIO_RCVALL:  'option' must be one of the socket.RCVALL_* constants.
     |      SIO_KEEPALIVE_VALS:  'option' is a tuple of (onoff, timeout, interval).
     |  

socket.ioctl(control, option)
平台:        视窗
ioctl()方法是WSAIoctl系统接口的有限接口。有关详细信息,请参阅Win32文档。

在其他平台上,可以使用通用的fcntl.fcntl()和fcntl.ioctl()函数;他们接受一个套接字对象作为他们的第一个参数。


WSAIoctl
https://msdn.microsoft.com/en-us/library/ms741621%28VS.85%29.aspx
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-20 20:09:49 | 显示全部楼层

The WSAIoctl function is used to set or retrieve operating parameters associated with the socket, the transport protocol, or the communications subsystem.

该函数用来设置或者检索与socket相关的参数,传输协议,或者联系子系统?还是不知道该函数有啥用...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-23 14:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表